Skip to content

Commit fecfaf3

Browse files
authored
Merge pull request #288 from microsoftgraph/po/0.7.1-fix
0.7.0 fix
2 parents 612c93b + 9682234 commit fecfaf3

File tree

9 files changed

+337
-105
lines changed

9 files changed

+337
-105
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
namespace Microsoft.Graph.Authentication.Test.TokenCache
22
{
3+
using Microsoft.Graph.PowerShell.Authentication;
34
using Microsoft.Graph.PowerShell.Authentication.TokenCache;
45
using System;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Xunit;
910

10-
public class TokenCacheStorageTests: IDisposable
11+
public class CurrentUserTokenCacheStorageTests: IDisposable
1112
{
12-
private const string TestAppId1 = "test_app_id_1";
13+
private const ContextScope _userContextScope = ContextScope.CurrentUser;
14+
private readonly IAuthContext _testAppContext1 = new AuthContext { ClientId = "test_app_id_1", ContextScope = _userContextScope };
1315

1416
[Fact]
1517
public void ShouldStoreNewTokenToPlatformCache()
@@ -19,15 +21,15 @@ public void ShouldStoreNewTokenToPlatformCache()
1921
byte[] bufferToStore = Encoding.UTF8.GetBytes(strContent);
2022

2123
// Act
22-
TokenCacheStorage.SetToken(TestAppId1, bufferToStore);
24+
TokenCacheStorage.SetToken(_testAppContext1, bufferToStore);
2325

2426
// Assert
25-
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
27+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
2628
Assert.Equal(bufferToStore.Length, storedBuffer.Length);
2729
Assert.Equal(strContent, Encoding.UTF8.GetString(storedBuffer));
2830

2931
// Cleanup
30-
CleanTokenCache(TestAppId1);
32+
CleanTokenCache(_testAppContext1);
3133
}
3234

3335
[Fact]
@@ -37,26 +39,26 @@ public void ShouldStoreMultipleAppTokensInPlatformCache()
3739
string app1StrContent = "random data for app 1.";
3840
byte[] app1BufferToStore = Encoding.UTF8.GetBytes(app1StrContent);
3941

40-
string TestAppId2 = "test_app_id_2";
42+
IAuthContext testAppContext2 = new AuthContext { ClientId = "test_app_id_2", ContextScope = _userContextScope };
4143
string app2StrContent = "random data for app 2 plus more data.";
4244
byte[] app2BufferToStore = Encoding.UTF8.GetBytes(app2StrContent);
4345

4446
// Act
45-
TokenCacheStorage.SetToken(TestAppId1, app1BufferToStore);
46-
TokenCacheStorage.SetToken(TestAppId2, app2BufferToStore);
47+
TokenCacheStorage.SetToken(_testAppContext1, app1BufferToStore);
48+
TokenCacheStorage.SetToken(testAppContext2, app2BufferToStore);
4749

4850
// Assert
49-
byte[] app1StoredBuffer = TokenCacheStorage.GetToken(TestAppId1);
51+
byte[] app1StoredBuffer = TokenCacheStorage.GetToken(_testAppContext1);
5052
Assert.Equal(app1BufferToStore.Length, app1StoredBuffer.Length);
5153
Assert.Equal(app1StrContent, Encoding.UTF8.GetString(app1StoredBuffer));
5254

53-
byte[] app2StoredBuffer = TokenCacheStorage.GetToken(TestAppId2);
55+
byte[] app2StoredBuffer = TokenCacheStorage.GetToken(testAppContext2);
5456
Assert.Equal(app2BufferToStore.Length, app2StoredBuffer.Length);
5557
Assert.Equal(app2StrContent, Encoding.UTF8.GetString(app2StoredBuffer));
5658

5759
// Cleanup
58-
CleanTokenCache(TestAppId1);
59-
CleanTokenCache(TestAppId2);
60+
CleanTokenCache(_testAppContext1);
61+
CleanTokenCache(testAppContext2);
6062
}
6163

6264

@@ -66,31 +68,31 @@ public void ShouldUpdateTokenInPlatformCache()
6668
// Arrange
6769
string originalStrContent = "random data for app.";
6870
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
69-
TokenCacheStorage.SetToken(TestAppId1, originalBuffer);
71+
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);
7072

7173
// Act
7274
string strContentToUpdate = "updated random data for app.";
7375
byte[] updateBuffer = Encoding.UTF8.GetBytes(strContentToUpdate);
74-
TokenCacheStorage.SetToken(TestAppId1, updateBuffer);
76+
TokenCacheStorage.SetToken(_testAppContext1, updateBuffer);
7577

7678
// Assert
77-
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
79+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
7880
Assert.NotEqual(originalBuffer.Length, storedBuffer.Length);
7981
Assert.Equal(updateBuffer.Length, storedBuffer.Length);
8082
Assert.Equal(strContentToUpdate, Encoding.UTF8.GetString(storedBuffer));
8183

8284
// Cleanup
83-
CleanTokenCache(TestAppId1);
85+
CleanTokenCache(_testAppContext1);
8486
}
8587

8688
[Fact]
8789
public void ShouldReturnNoContentWhenPlatformCacheIsEmpty()
8890
{
8991
// Arrange
90-
CleanTokenCache(TestAppId1);
92+
CleanTokenCache(_testAppContext1);
9193

9294
// Act
93-
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
95+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
9496

9597
// Assert
9698
Assert.Empty(storedBuffer);
@@ -102,13 +104,13 @@ public void ShouldDeleteCache()
102104
// Arrange
103105
string originalStrContent = "random data for app.";
104106
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
105-
TokenCacheStorage.SetToken(TestAppId1, originalBuffer);
107+
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);
106108

107109
// Act
108-
TokenCacheStorage.DeleteToken(TestAppId1);
110+
TokenCacheStorage.DeleteToken(_testAppContext1);
109111

110112
// Assert
111-
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
113+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
112114
Assert.Empty(storedBuffer);
113115
}
114116

@@ -124,15 +126,16 @@ public void ShouldMakeParallelCallsToTokenCache()
124126
// Act
125127
Parallel.For(0, executions, (index) => {
126128
byte[] contentBuffer = Encoding.UTF8.GetBytes(index.ToString());
127-
TokenCacheStorage.SetToken($"{index}", contentBuffer);
129+
var testAuthContext = new AuthContext { ClientId = index.ToString(), ContextScope = _userContextScope };
130+
TokenCacheStorage.SetToken(testAuthContext, contentBuffer);
128131

129-
byte[] storedBuffer = TokenCacheStorage.GetToken(index.ToString());
132+
byte[] storedBuffer = TokenCacheStorage.GetToken(testAuthContext);
130133
if (index.ToString() != Encoding.UTF8.GetString(storedBuffer))
131134
{
132135
failed = true;
133136
}
134137

135-
CleanTokenCache(index.ToString());
138+
CleanTokenCache(testAuthContext);
136139
Interlocked.Increment(ref count);
137140
});
138141

@@ -143,12 +146,12 @@ public void ShouldMakeParallelCallsToTokenCache()
143146

144147
public void Dispose()
145148
{
146-
CleanTokenCache(TestAppId1);
149+
CleanTokenCache(_testAppContext1);
147150
}
148151

149-
private void CleanTokenCache(string appId)
152+
private void CleanTokenCache(IAuthContext authContext)
150153
{
151-
TokenCacheStorage.DeleteToken(appId);
154+
TokenCacheStorage.DeleteToken(authContext);
152155
}
153156
}
154157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
namespace Microsoft.Graph.Authentication.Test.TokenCache
2+
{
3+
using Microsoft.Graph.PowerShell.Authentication;
4+
using Microsoft.Graph.PowerShell.Authentication.TokenCache;
5+
using System;
6+
using System.Text;
7+
using System.Threading;
8+
using Xunit;
9+
10+
public class ProcessTokenCacheStorageTests: IDisposable
11+
{
12+
// Defaults to process context scope.
13+
private IAuthContext _testAppContext1;
14+
public ProcessTokenCacheStorageTests()
15+
{
16+
_testAppContext1 = new AuthContext { ClientId = "test_app_id_1" };
17+
GraphSessionInitializer.InitializeSession();
18+
}
19+
20+
[Fact]
21+
public void ShouldStoreNewTokenInProccessCache()
22+
{
23+
// Arrange
24+
string strContent = "random data for app.";
25+
byte[] bufferToStore = Encoding.UTF8.GetBytes(strContent);
26+
27+
// Act
28+
TokenCacheStorage.SetToken(_testAppContext1, bufferToStore);
29+
30+
// Assert
31+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
32+
Assert.Equal(bufferToStore.Length, storedBuffer.Length);
33+
Assert.Equal(strContent, Encoding.UTF8.GetString(storedBuffer));
34+
}
35+
36+
[Fact]
37+
public void ShouldUpdateTokenInProccessCache()
38+
{
39+
// Arrange
40+
string originalStrContent = "random data for app.";
41+
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
42+
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);
43+
44+
// Act
45+
string strContentToUpdate = "updated random data for app.";
46+
byte[] updateBuffer = Encoding.UTF8.GetBytes(strContentToUpdate);
47+
TokenCacheStorage.SetToken(_testAppContext1, updateBuffer);
48+
49+
// Assert
50+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
51+
Assert.NotEqual(originalBuffer.Length, storedBuffer.Length);
52+
Assert.Equal(updateBuffer.Length, storedBuffer.Length);
53+
Assert.Equal(strContentToUpdate, Encoding.UTF8.GetString(storedBuffer));
54+
}
55+
56+
[Fact]
57+
public void ShouldReturnNoContentWhenProccessCacheIsEmpty()
58+
{
59+
// Act
60+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
61+
62+
// Assert
63+
Assert.Empty(storedBuffer);
64+
}
65+
66+
[Fact]
67+
public void ShouldDeleteProccessCache()
68+
{
69+
// Arrange
70+
string originalStrContent = "random data for app.";
71+
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
72+
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);
73+
74+
// Act
75+
TokenCacheStorage.DeleteToken(_testAppContext1);
76+
77+
// Assert
78+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
79+
Assert.Empty(storedBuffer);
80+
}
81+
82+
83+
[Fact]
84+
public void ProccessTokenCacheShouldBeThreadSafe()
85+
{
86+
// Arrange
87+
int executions = 50;
88+
int count = 0;
89+
bool failed = false;
90+
Thread[] threads = new Thread[executions];
91+
92+
// Act
93+
for (int i = 0; i < threads.Length; i++)
94+
{
95+
threads[i] = new Thread(() => {
96+
byte[] contentBuffer = Encoding.UTF8.GetBytes(i.ToString());
97+
TokenCacheStorage.SetToken(_testAppContext1, contentBuffer);
98+
Thread.Sleep(2000);
99+
100+
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
101+
if (i.ToString() != Encoding.UTF8.GetString(storedBuffer))
102+
{
103+
failed = true;
104+
}
105+
106+
Interlocked.Increment(ref count);
107+
});
108+
}
109+
110+
foreach (Thread thread in threads)
111+
{
112+
thread.Start();
113+
}
114+
115+
foreach (Thread thread in threads)
116+
{
117+
thread.Join();
118+
}
119+
120+
// Assert
121+
Assert.Equal(executions, count);
122+
Assert.False(failed, "Unexpected content found.");
123+
}
124+
125+
public void Dispose()
126+
{
127+
CleanTokenCache(_testAppContext1);
128+
}
129+
130+
private void CleanTokenCache(IAuthContext authContext)
131+
{
132+
TokenCacheStorage.DeleteToken(authContext);
133+
GraphSession.Reset();
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)