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

            Assert.ThrowsException <ArgumentNullException>(() => store.WriteData(null));

            store.WriteData(new byte[0]);

            Assert.IsFalse(store.ReadData().Any());
        }
        /// <summary>
        /// Gets an aptly configured instance of the <see cref="MsalCacheStorage" /> class.
        /// </summary>
        /// <returns>An aptly configured instance of the <see cref="MsalCacheStorage" /> class.</returns>
        private MsalCacheStorage GetMsalCacheStorage()
        {
            StorageCreationPropertiesBuilder builder = new StorageCreationPropertiesBuilder(Path.GetFileName(CacheFilePath), Path.GetDirectoryName(CacheFilePath), ClientId);

            builder = builder.WithMacKeyChain(serviceName: "Microsoft.Developer.IdentityService", accountName: "MSALCache");
            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"));

            return(MsalCacheStorage.Create(builder.Build(), new TraceSource("Partner Center PowerShell")));
        }
        public void MsalWriteGoodData()
        {
            var store = MsalCacheStorage.Create(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));
        }
        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 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));
        }
        private void CleanTestData()
        {
            var store = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);

            store.Clear();
        }
        public void MsalNewStoreNoFile()
        {
            var store = MsalCacheStorage.Create(s_storageCreationProperties, logger: _logger);

            Assert.IsFalse(store.ReadData().Any());
        }