public void MsalTestClear() { var store = new MsalCacheStorage(s_storageCreationProperties, logger: _logger); Assert.IsTrue(store.HasChanged); var tempData = store.ReadData(); Assert.IsFalse(store.HasChanged); var store2 = new MsalCacheStorage(s_storageCreationProperties, logger: _logger); Assert.IsNotNull(Exception <ArgumentNullException>(() => store.WriteData(null))); byte[] data = { 2, 2, 3 }; store.WriteData(data); Assert.IsFalse(store.HasChanged); Assert.IsTrue(store2.HasChanged); store2.ReadData(); Enumerable.SequenceEqual(store.ReadData(), data); Assert.IsTrue(File.Exists(CacheFilePath)); store.Clear(); Assert.IsFalse(store.HasChanged); Assert.IsTrue(store2.HasChanged); Assert.IsFalse(store.ReadData().Any()); Assert.IsFalse(store2.ReadData().Any()); Assert.IsFalse(File.Exists(CacheFilePath)); }
public void MsalWriteEmptyData() { var store = new MsalCacheStorage(s_storageCreationProperties, logger: _logger); Assert.ThrowsException <ArgumentNullException>(() => store.WriteData(null)); store.WriteData(new byte[0]); Assert.IsFalse(store.ReadData().Any()); }
public void WriteCanThrowExceptions() { // Arrange var actualLogger = new TraceSourceLogger(_logger); var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>(); cacheAccessor.WhenForAnyArgs(c => c.Write(null)).Throw(new InvalidOperationException()); var storage = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger); // Act storage.WriteData(new byte[0]); // Assert AssertException.Throws <InvalidOperationException>( () => storage.WriteData(new byte[0], ignoreExceptions: false)); }
/// <summary> /// Notification that is triggered after token acquisition. /// </summary> /// <param name="args">Arguments related to the cache item impacted</param> public override void AfterAccessNotification(TokenCacheNotificationArgs args) { MsalCacheStorage cacheStorage = GetMsalCacheStorage(); args.AssertNotNull(nameof(args)); try { if (args.HasStateChanged) { cacheStorage.WriteData(args.TokenCache.SerializeMsalV3()); } } catch (Exception) { cacheStorage.Clear(); throw; } finally { CrossPlatformLock localDispose = cacheLock; cacheLock = null; localDispose?.Dispose(); } }
public void CacheStorageFactory_WithFallback_Linux() { var storageWithKeyRing = new StorageCreationPropertiesBuilder( Path.GetFileName(CacheFilePath), Path.GetDirectoryName(CacheFilePath), "ClientIDGoesHere") .WithMacKeyChain(serviceName: "Microsoft.Developer.IdentityService", accountName: "MSALCache") .WithLinuxKeyring( schemaName: "msal.cache", collection: "default", secretLabel: "MSALCache", attribute1: new KeyValuePair <string, string>("MsalClientID", "Microsoft.Developer.IdentityService"), attribute2: new KeyValuePair <string, string>("MsalClientVersion", "1.0.0.0")) .Build(); // Tests run on machines without Libsecret MsalCacheStorage store = MsalCacheStorage.Create(storageWithKeyRing, logger: _logger); Assert.IsTrue(store.CacheAccessor is LinuxKeyringAccessor); // ADO Linux test agents do not have libsecret installed by default // If you run this test on a Linux box with UI / LibSecret, then this test will fail // because the statement below will not throw. AssertException.Throws <MsalCachePersistenceException>( () => store.VerifyPersistence()); MsalCacheStorage unprotectedStore = MsalCacheStorage.Create(s_storageCreationProperties, _logger); Assert.IsTrue(unprotectedStore.CacheAccessor is FileAccessor); unprotectedStore.VerifyPersistence(); unprotectedStore.WriteData(new byte[] { 2, 3 }); // Unproteced cache file should exist Assert.IsTrue(File.Exists(unprotectedStore.CacheFilePath)); // Mimic another sdk client to check libsecret availability by calling // MsalCacheStorage.VerifyPeristence() -> LinuxKeyringAccessor.CreateForPersistenceValidation() AssertException.Throws <MsalCachePersistenceException>( () => store.VerifyPersistence()); // Verify above call doesn't delete existing cache file Assert.IsTrue(File.Exists(unprotectedStore.CacheFilePath)); }
public void MsalWriteGoodData() { var store = new MsalCacheStorage(s_storageCreationProperties, logger: _logger); Assert.ThrowsException <ArgumentNullException>(() => store.WriteData(null)); byte[] data = { 2, 2, 3 }; byte[] data2 = { 2, 2, 3, 4, 4 }; store.WriteData(data); Assert.IsTrue(Enumerable.SequenceEqual(store.ReadData(), data)); store.WriteData(data); store.WriteData(data2); store.WriteData(data); store.WriteData(data2); Assert.IsTrue(Enumerable.SequenceEqual(store.ReadData(), data2)); }