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());

            store = MsalCacheStorage.Create(s_storageCreationProperties, _logger);
            Assert.IsTrue(store.CacheAccessor is FileAccessor);

            store.VerifyPersistence();
        }
Esempio n. 2
0
        public void UnprotectedOptionMutuallyExclusiveWithOtherOptions()
        {
            var builder = new StorageCreationPropertiesBuilder(
                Path.GetFileName(CacheFilePath),
                Path.GetDirectoryName(CacheFilePath));

            builder = builder.WithMacKeyChain(serviceName: "Microsoft.Developer.IdentityService", accountName: "MSALCache");
            builder.WithUnprotectedFile();

            AssertException.Throws <ArgumentException>(() => builder.Build());


            builder = new StorageCreationPropertiesBuilder(
                Path.GetFileName(CacheFilePath),
                Path.GetDirectoryName(CacheFilePath));

            builder = builder.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"));
            builder.WithUnprotectedFile();
            AssertException.Throws <ArgumentException>(() => builder.Build());

            builder = new StorageCreationPropertiesBuilder(
                Path.GetFileName(CacheFilePath),
                Path.GetDirectoryName(CacheFilePath));
            builder.WithLinuxUnprotectedFile();
            builder.WithUnprotectedFile();

            AssertException.Throws <ArgumentException>(() => builder.Build());
        }
Esempio n. 3
0
        public void ImportExport_ThrowException()
        {
            // Arrange
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();
            var cache         = new MockTokenCache();
            var storage       = new MsalCacheStorage(
                _storageCreationPropertiesBuilder.Build(),
                cacheAccessor,
                new TraceSourceLogger(new TraceSource("ts")));
            var helper = new MsalCacheHelper(cache, storage, _logger);

            byte[] dataToSave = Encoding.UTF8.GetBytes("Hello World 2");
            var    ex         = new InvalidCastException();

            cacheAccessor.Read().Throws(ex);

            // Act
            var actualEx = AssertException.Throws <InvalidCastException>(
                () => helper.LoadUnencryptedTokenCache());

            // Assert
            Assert.AreEqual(ex, actualEx);

            // Arrange
            cacheAccessor.WhenForAnyArgs(c => c.Write(default)).Throw(ex);
Esempio n. 4
0
        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 Storage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Assert
            AssertException.Throws <InvalidOperationException>(
                () => storage.WriteData(new byte[0]));
        }
Esempio n. 5
0
        public void ReadCanThrowExceptions()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();

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

            // Assert
            AssertException.Throws <InvalidOperationException>(
                () => storage.ReadData());
        }
        public void ClearCanThrowExceptions()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();

            cacheAccessor.WhenForAnyArgs(c => c.Clear()).Throw(new InvalidOperationException());
            var storage = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Act
            storage.Clear();

            // Assert
            AssertException.Throws <InvalidOperationException>(
                () => storage.Clear(ignoreExceptions: false));
        }
Esempio n. 7
0
        public void CacheStorageFactory_WithFallback_Linux()
        {
            var storageWithKeyRing = new StorageCreationPropertiesBuilder(
                Path.GetFileName(CacheFilePath),
                Path.GetDirectoryName(CacheFilePath))
                                     .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
            Storage store = Storage.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());

            Storage unprotectedStore = Storage.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(s_storageCreationProperties.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(s_storageCreationProperties.CacheFilePath));

            // Verify that file permissions are set to 600
            FileHelper.AssertChmod600(s_storageCreationProperties.CacheFilePath);
        }
        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));
        }
Esempio n. 9
0
        public void VerifyPersistenceThrowsIfDataReadIsEmpty()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = Substitute.For <ICacheAccessor>();

            cacheAccessor.CreateForPersistenceValidation().Returns(cacheAccessor);
            var storage = new Storage(s_storageCreationProperties, cacheAccessor, actualLogger);


            // Act
            var ex = AssertException.Throws <MsalCachePersistenceException>(
                () => storage.VerifyPersistence());

            // Assert
            Assert.IsNull(ex.InnerException); // no more details available
        }
        public void VerifyPersistenceThrowsIfDataReadIsDiffrentFromDataWritten()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = Substitute.For <ICacheAccessor>();

            cacheAccessor.CreateForPersistenceValidation().Returns(cacheAccessor);
            var storage = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            cacheAccessor.Read().Returns(Encoding.UTF8.GetBytes("other_dummy_data"));

            // Act
            var ex = AssertException.Throws <MsalCachePersistenceException>(
                () => storage.VerifyPersistence());

            // Assert
            Assert.IsNull(ex.InnerException); // no more details available
        }
Esempio n. 11
0
        public void VerifyPersistenceThrowsInnerExceptions()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = Substitute.For <ICacheAccessor>();

            cacheAccessor.CreateForPersistenceValidation().Returns(cacheAccessor);
            var exception = new InvalidOperationException("some error");
            var storage   = new Storage(s_storageCreationProperties, cacheAccessor, actualLogger);

            cacheAccessor.Read().Throws(exception);

            // Act
            var ex = AssertException.Throws <MsalCachePersistenceException>(
                () => storage.VerifyPersistence());

            // Assert
            Assert.AreEqual(ex.InnerException, exception);
        }
        public void MsalTestClear()
        {
            var store    = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);
            var tempData = store.ReadData();

            var store2 = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);

            AssertException.Throws <ArgumentNullException>(() => store.WriteData(null));

            byte[] data = { 2, 2, 3 };
            store.WriteData(data);
            store2.ReadData();

            Assert.IsTrue(Enumerable.SequenceEqual(store.ReadData(), data));
            Assert.IsTrue(File.Exists(CacheFilePath));

            store.Clear();

            Assert.IsFalse(store.ReadData().Any());
            Assert.IsFalse(store2.ReadData().Any());
            Assert.IsFalse(File.Exists(CacheFilePath));
        }