Skip to content

0.7.0 fix #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
namespace Microsoft.Graph.Authentication.Test.TokenCache
{
using Microsoft.Graph.PowerShell.Authentication;
using Microsoft.Graph.PowerShell.Authentication.TokenCache;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

public class TokenCacheStorageTests: IDisposable
public class CurrentUserTokenCacheStorageTests: IDisposable
{
private const string TestAppId1 = "test_app_id_1";
private const ContextScope _userContextScope = ContextScope.CurrentUser;
private readonly IAuthContext _testAppContext1 = new AuthContext { ClientId = "test_app_id_1", ContextScope = _userContextScope };

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

// Act
TokenCacheStorage.SetToken(TestAppId1, bufferToStore);
TokenCacheStorage.SetToken(_testAppContext1, bufferToStore);

// Assert
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.Equal(bufferToStore.Length, storedBuffer.Length);
Assert.Equal(strContent, Encoding.UTF8.GetString(storedBuffer));

// Cleanup
CleanTokenCache(TestAppId1);
CleanTokenCache(_testAppContext1);
}

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

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

// Act
TokenCacheStorage.SetToken(TestAppId1, app1BufferToStore);
TokenCacheStorage.SetToken(TestAppId2, app2BufferToStore);
TokenCacheStorage.SetToken(_testAppContext1, app1BufferToStore);
TokenCacheStorage.SetToken(testAppContext2, app2BufferToStore);

// Assert
byte[] app1StoredBuffer = TokenCacheStorage.GetToken(TestAppId1);
byte[] app1StoredBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.Equal(app1BufferToStore.Length, app1StoredBuffer.Length);
Assert.Equal(app1StrContent, Encoding.UTF8.GetString(app1StoredBuffer));

byte[] app2StoredBuffer = TokenCacheStorage.GetToken(TestAppId2);
byte[] app2StoredBuffer = TokenCacheStorage.GetToken(testAppContext2);
Assert.Equal(app2BufferToStore.Length, app2StoredBuffer.Length);
Assert.Equal(app2StrContent, Encoding.UTF8.GetString(app2StoredBuffer));

// Cleanup
CleanTokenCache(TestAppId1);
CleanTokenCache(TestAppId2);
CleanTokenCache(_testAppContext1);
CleanTokenCache(testAppContext2);
}


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

// Act
string strContentToUpdate = "updated random data for app.";
byte[] updateBuffer = Encoding.UTF8.GetBytes(strContentToUpdate);
TokenCacheStorage.SetToken(TestAppId1, updateBuffer);
TokenCacheStorage.SetToken(_testAppContext1, updateBuffer);

// Assert
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.NotEqual(originalBuffer.Length, storedBuffer.Length);
Assert.Equal(updateBuffer.Length, storedBuffer.Length);
Assert.Equal(strContentToUpdate, Encoding.UTF8.GetString(storedBuffer));

// Cleanup
CleanTokenCache(TestAppId1);
CleanTokenCache(_testAppContext1);
}

[Fact]
public void ShouldReturnNoContentWhenPlatformCacheIsEmpty()
{
// Arrange
CleanTokenCache(TestAppId1);
CleanTokenCache(_testAppContext1);

// Act
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);

// Assert
Assert.Empty(storedBuffer);
Expand All @@ -102,13 +104,13 @@ public void ShouldDeleteCache()
// Arrange
string originalStrContent = "random data for app.";
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
TokenCacheStorage.SetToken(TestAppId1, originalBuffer);
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);

// Act
TokenCacheStorage.DeleteToken(TestAppId1);
TokenCacheStorage.DeleteToken(_testAppContext1);

// Assert
byte[] storedBuffer = TokenCacheStorage.GetToken(TestAppId1);
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.Empty(storedBuffer);
}

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

byte[] storedBuffer = TokenCacheStorage.GetToken(index.ToString());
byte[] storedBuffer = TokenCacheStorage.GetToken(testAuthContext);
if (index.ToString() != Encoding.UTF8.GetString(storedBuffer))
{
failed = true;
}

CleanTokenCache(index.ToString());
CleanTokenCache(testAuthContext);
Interlocked.Increment(ref count);
});

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

public void Dispose()
{
CleanTokenCache(TestAppId1);
CleanTokenCache(_testAppContext1);
}

private void CleanTokenCache(string appId)
private void CleanTokenCache(IAuthContext authContext)
{
TokenCacheStorage.DeleteToken(appId);
TokenCacheStorage.DeleteToken(authContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
namespace Microsoft.Graph.Authentication.Test.TokenCache
{
using Microsoft.Graph.PowerShell.Authentication;
using Microsoft.Graph.PowerShell.Authentication.TokenCache;
using System;
using System.Text;
using System.Threading;
using Xunit;

public class ProcessTokenCacheStorageTests: IDisposable
{
// Defaults to process context scope.
private IAuthContext _testAppContext1;
public ProcessTokenCacheStorageTests()
{
_testAppContext1 = new AuthContext { ClientId = "test_app_id_1" };
GraphSessionInitializer.InitializeSession();
}

[Fact]
public void ShouldStoreNewTokenInProccessCache()
{
// Arrange
string strContent = "random data for app.";
byte[] bufferToStore = Encoding.UTF8.GetBytes(strContent);

// Act
TokenCacheStorage.SetToken(_testAppContext1, bufferToStore);

// Assert
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.Equal(bufferToStore.Length, storedBuffer.Length);
Assert.Equal(strContent, Encoding.UTF8.GetString(storedBuffer));
}

[Fact]
public void ShouldUpdateTokenInProccessCache()
{
// Arrange
string originalStrContent = "random data for app.";
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);

// Act
string strContentToUpdate = "updated random data for app.";
byte[] updateBuffer = Encoding.UTF8.GetBytes(strContentToUpdate);
TokenCacheStorage.SetToken(_testAppContext1, updateBuffer);

// Assert
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.NotEqual(originalBuffer.Length, storedBuffer.Length);
Assert.Equal(updateBuffer.Length, storedBuffer.Length);
Assert.Equal(strContentToUpdate, Encoding.UTF8.GetString(storedBuffer));
}

[Fact]
public void ShouldReturnNoContentWhenProccessCacheIsEmpty()
{
// Act
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);

// Assert
Assert.Empty(storedBuffer);
}

[Fact]
public void ShouldDeleteProccessCache()
{
// Arrange
string originalStrContent = "random data for app.";
byte[] originalBuffer = Encoding.UTF8.GetBytes(originalStrContent);
TokenCacheStorage.SetToken(_testAppContext1, originalBuffer);

// Act
TokenCacheStorage.DeleteToken(_testAppContext1);

// Assert
byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
Assert.Empty(storedBuffer);
}


[Fact]
public void ProccessTokenCacheShouldBeThreadSafe()
{
// Arrange
int executions = 50;
int count = 0;
bool failed = false;
Thread[] threads = new Thread[executions];

// Act
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(() => {
byte[] contentBuffer = Encoding.UTF8.GetBytes(i.ToString());
TokenCacheStorage.SetToken(_testAppContext1, contentBuffer);
Thread.Sleep(2000);

byte[] storedBuffer = TokenCacheStorage.GetToken(_testAppContext1);
if (i.ToString() != Encoding.UTF8.GetString(storedBuffer))
{
failed = true;
}

Interlocked.Increment(ref count);
});
}

foreach (Thread thread in threads)
{
thread.Start();
}

foreach (Thread thread in threads)
{
thread.Join();
}

// Assert
Assert.Equal(executions, count);
Assert.False(failed, "Unexpected content found.");
}

public void Dispose()
{
CleanTokenCache(_testAppContext1);
}

private void CleanTokenCache(IAuthContext authContext)
{
TokenCacheStorage.DeleteToken(authContext);
GraphSession.Reset();
}
}
}
Loading