public void TestDeserializeWithClearCache()
        {
            // Create a token accessor with keys in it that are NOT in the expected token cache
            var originalAccessor = CreateTokenCacheAccessorWithKeyPrefix("FAKE", 7, 6, 5, 4);
            var s1 = new TokenCacheJsonSerializer(originalAccessor);

            byte[] originalBytes = s1.Serialize(null);

            var differentAccessor = CreateTokenCacheAccessor();
            var s2 = new TokenCacheJsonSerializer(differentAccessor);

            byte[] differentBytes = s2.Serialize(null);

            // Assert that they have different counts of items...
            Assert.AreNotEqual(originalAccessor.GetAllAccessTokens().Count(), differentAccessor.GetAllAccessTokens().Count());
            Assert.AreNotEqual(originalAccessor.GetAllRefreshTokens().Count(), differentAccessor.GetAllRefreshTokens().Count());
            Assert.AreNotEqual(originalAccessor.GetAllIdTokens().Count(), differentAccessor.GetAllIdTokens().Count());
            Assert.AreNotEqual(originalAccessor.GetAllAccounts().Count(), differentAccessor.GetAllAccounts().Count());

            // Now, deserialize differentBytes into originalAccessor with cacheFlush = true
            // This means we should destroy the contents of originalAccessor and replace them with the
            // contents of the different cache

            s1.Deserialize(differentBytes, true);

            AssertAccessorsAreEqual(differentAccessor, originalAccessor);

            string expectedJson = File.ReadAllText(ResourceHelper.GetTestResourceRelativePath("ExpectedTokenCache.json"));

            // serialize again to detect errors that come from deserialization
            byte[] bytes2      = s1.Serialize(null);
            string actualJson2 = new UTF8Encoding().GetString(bytes2);

            Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson2), JObject.Parse(expectedJson)));
        }
        public void TestJsonSerialization()
        {
            string expectedJson = File.ReadAllText(ResourceHelper.GetTestResourceRelativePath("ExpectedTokenCache.json"));
            var    accessor     = CreateTokenCacheAccessor();

            var s1 = new TokenCacheJsonSerializer(accessor);

            byte[] bytes      = s1.Serialize(null);
            string actualJson = new UTF8Encoding().GetString(bytes);

            Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson), JObject.Parse(expectedJson)));

            var otherAccessor = new InMemoryTokenCacheAccessor(Substitute.For <ICoreLogger>());
            var s2            = new TokenCacheJsonSerializer(otherAccessor);

            s2.Deserialize(bytes, false);

            AssertAccessorsAreEqual(accessor, otherAccessor);

            // serialize again to detect errors that come from deserialization
            byte[] bytes2      = s2.Serialize(null);
            string actualJson2 = new UTF8Encoding().GetString(bytes2);

            Assert.IsTrue(JToken.DeepEquals(JObject.Parse(actualJson2), JObject.Parse(expectedJson)));
        }
        public void TestPythonCacheSerializationInterop()
        {
            var    accessor          = new InMemoryTokenCacheAccessor(Substitute.For <ICoreLogger>());
            var    s                 = new TokenCacheJsonSerializer(accessor);
            string pythonBinFilePath = ResourceHelper.GetTestResourceRelativePath("cachecompat_python.bin");

            byte[] bytes = File.ReadAllBytes(pythonBinFilePath);
            s.Deserialize(bytes, false);

            Assert.AreEqual(0, accessor.GetAllAccessTokens().Count());
            Assert.AreEqual(0, accessor.GetAllRefreshTokens().Count());
            Assert.AreEqual(0, accessor.GetAllIdTokens().Count());
            Assert.AreEqual(0, accessor.GetAllAccounts().Count());
        }
        public void TestDeserializeWithNoClearCache()
        {
            // Create a token accessor with keys in it that are NOT in the expected token cache
            var originalAccessor = CreateTokenCacheAccessorWithKeyPrefix("FAKE", 7, 6, 5, 4);
            var s1 = new TokenCacheJsonSerializer(originalAccessor);

            byte[] originalBytes = s1.Serialize(null);

            var differentAccessor = CreateTokenCacheAccessor();
            var s2 = new TokenCacheJsonSerializer(differentAccessor);

            byte[] differentBytes = s2.Serialize(null);

            // Assert that they have different counts of items...

            int originalAccessTokenCount  = originalAccessor.GetAllAccessTokens().Count();
            int originalRefreshTokenCount = originalAccessor.GetAllRefreshTokens().Count();
            int originalIdTokenCount      = originalAccessor.GetAllIdTokens().Count();
            int originalAccountsCount     = originalAccessor.GetAllAccounts().Count();

            Assert.AreNotEqual(originalAccessTokenCount, differentAccessor.GetAllAccessTokens().Count());
            Assert.AreNotEqual(originalRefreshTokenCount, differentAccessor.GetAllRefreshTokens().Count());
            Assert.AreNotEqual(originalIdTokenCount, differentAccessor.GetAllIdTokens().Count());
            Assert.AreNotEqual(originalAccountsCount, differentAccessor.GetAllAccounts().Count());

            // Now, deserialize differentBytes into originalAccessor with cacheFlush = false
            // This means we should merge the contents of originalAccessor and the
            // contents of the different cache

            s1.Deserialize(differentBytes, false);

            Assert.AreEqual(originalAccessor.GetAllAccessTokens().Count(), differentAccessor.GetAllAccessTokens().Count() + originalAccessTokenCount);

            // This is -1 because the PRT FOCI refresh token will not duplicate since it has the same key.
            Assert.AreEqual(originalAccessor.GetAllRefreshTokens().Count(), differentAccessor.GetAllRefreshTokens().Count() + originalRefreshTokenCount - 1);
            Assert.AreEqual(originalAccessor.GetAllIdTokens().Count(), differentAccessor.GetAllIdTokens().Count() + originalIdTokenCount);
            Assert.AreEqual(originalAccessor.GetAllAccounts().Count(), differentAccessor.GetAllAccounts().Count() + originalAccountsCount);
        }