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 MsalNewStoreNoFile()
        {
            var store = new MsalCacheStorage(s_storageCreationProperties, logger: _logger);

            Assert.IsFalse(store.HasChanged);
            Assert.IsFalse(store.ReadData().Any());
        }
        public void ReadCanThrowExceptions()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();

            cacheAccessor.Read().Throws(new InvalidOperationException());
            var storage = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Act
            byte[] result = storage.ReadData();
            Assert.AreEqual(0, result.Length);

            // Assert
            AssertException.Throws <InvalidOperationException>(
                () => storage.ReadData(ignoreExceptions: false));
        }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        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 CacheStorageReadCanHandleReadingNull()
        {
            // Arrange
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();

            cacheAccessor.Read().Returns((byte[])null);

            var actualLogger = new TraceSourceLogger(_logger);
            var storage      = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Act
            byte[] result = storage.ReadData();

            // Assert
            Assert.AreEqual(0, result.Length);
        }
        public void CacheStorageReadCanHandleExceptionsWhenReading()
        {
            // Arrange
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();
            var exception     = new InvalidOperationException();

            cacheAccessor.Read().Throws(exception);

            var actualLogger = new TraceSourceLogger(_logger);
            var storage      = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Act
            byte[] result = storage.ReadData();

            // Assert
            Assert.AreEqual(0, result.Length);
        }
        /// <summary>
        /// Notification that is triggered before token acquisition.
        /// </summary>
        /// <param name="args">Arguments related to the cache item impacted</param>
        public override void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            MsalCacheStorage cacheStorage = GetMsalCacheStorage();

            args.AssertNotNull(nameof(args));

            try
            {
                cacheLock = new CrossPlatformLock($"{CacheFilePath}.lockfile");

                cacheLock.CreateLockAsync().ConfigureAwait(false);
                args.TokenCache.DeserializeMsalV3(cacheStorage.ReadData());
            }
            catch (Exception)
            {
                cacheStorage.Clear();
                throw;
            }
        }
        public void CacheStorageCanHandleMultipleExceptionsWhenReading()
        {
            // Arrange
            var stringListener = new TraceStringListener();
            var cacheAccessor  = Substitute.For <ICacheAccessor>();
            var exception      = new InvalidOperationException("some error");

            cacheAccessor.Read().Throws(exception);
            cacheAccessor.When((x) => x.Clear()).Do(x => throw exception);
            _logger.Listeners.Add(stringListener);
            var actualLogger = new TraceSourceLogger(_logger);
            var storage      = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Act
            byte[] result = storage.ReadData();

            // Assert
            Assert.AreEqual(0, result.Length);
            Assert.IsTrue(stringListener.CurrentLog.Contains("TestSource Error"));
            Assert.IsTrue(stringListener.CurrentLog.Contains("InvalidOperationException"));
            Assert.IsTrue(stringListener.CurrentLog.Contains("some error"));
        }