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();
        }
        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 CacheStorageFactoryWindows()
        {
            MsalCacheStorage store = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);

            Assert.IsTrue(store.CacheAccessor is DpApiEncryptedFileAccessor);
            store.VerifyPersistence();

            store = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);
            Assert.IsTrue(store.CacheAccessor is DpApiEncryptedFileAccessor);
        }
        public void CacheStorageFactoryMac()
        {
            MsalCacheStorage store = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);

            Assert.IsTrue(store.CacheAccessor is MacKeychainAccessor);
            store.VerifyPersistence();

            store = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);
            Assert.IsTrue(store.CacheAccessor is MacKeychainAccessor);
        }
        public void VerifyPersistenceThrowsIfDataReadIsEmpty()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = Substitute.For <ICacheAccessor>();

            cacheAccessor.CreateForPersistenceValidation().Returns(cacheAccessor);
            var storage = new MsalCacheStorage(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 stringListener = new TraceStringListener();
            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
        }
        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 MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            cacheAccessor.Read().Throws(exception);

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

            // Assert
            Assert.AreEqual(ex.InnerException, exception);
        }
        public void VerifyPersistenceHappyPath()
        {
            // Arrange
            byte[] dummyData     = Encoding.UTF8.GetBytes(MsalCacheStorage.PersistenceValidationDummyData);
            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(dummyData);

            // Act
            storage.VerifyPersistence();

            // Assert
            Received.InOrder(() => {
                cacheAccessor.CreateForPersistenceValidation();
                cacheAccessor.Write(Arg.Any <byte[]>());
                cacheAccessor.Read();
                cacheAccessor.Clear();
            });
        }