public void CreateUserHash_HandleConcurrentRequestProperly()
        {
            HttpContext[] contexts = new []
            {
                HttpContextHelper.GetContextWithIp("192.168.132.17"),
                HttpContextHelper.GetContextWithIp("192.168.132.18"),
                HttpContextHelper.GetContextWithIp("192.168.132.19")
            };

            TestIdentityProvider       provider   = new ("ProviderWrapper", new IpBasedUserIdentityProvider());
            UserHashIdentityMiddleware middleware = GetMiddelware(saltProvider: null, provider);

            Enumerable.Range(0, 100).AsParallel()
            .Select(index =>
            {
                int contextIndex = index % contexts.Length;
                string hash      = middleware.CreateUserHash(contexts[contextIndex]);
                return(contextIndex, hash);
            })
            .GroupBy(hashIndexPair => hashIndexPair.contextIndex)
            .Select(hashesPerContext => hashesPerContext.Distinct().Count())
            .ForAll(uniquHashesPerContext => {
                Assert.AreEqual(1, uniquHashesPerContext, "Hashes for the same context should be identical");
            });
        }
Ejemplo n.º 2
0
        public void TryWriteBytes_ReturnFalseIfNoIpAddress()
        {
            IUserIdentityProvider provider = new IpBasedUserIdentityProvider();

            (HttpContext context, _) = HttpContextHelper.CreateHttpContext();

            bool result = provider.TryWriteBytes(context, new byte[provider.MaxBytesInIdentity].AsSpan(), out int bytesWritten);

            Assert.IsFalse(result);
            Assert.AreEqual(-1, bytesWritten);
        }
Ejemplo n.º 3
0
        public void TryWriteBytes_ChangedBaseOnIpAddress()
        {
            IUserIdentityProvider provider = new IpBasedUserIdentityProvider();

            HttpContext context1 = HttpContextHelper.GetContextWithIp("192.168.0.2");
            HttpContext context2 = HttpContextHelper.GetContextWithIp("127.0.0.2");

            byte[] hash1 = GetIdentity(provider, context1);
            byte[] hash2 = GetIdentity(provider, context2);

            CollectionAssert.AreNotEqual(hash1, hash2);
            CollectionAssert.AreEqual(hash1, GetIdentity(provider, context1));
            CollectionAssert.AreEqual(hash2, GetIdentity(provider, context2));
        }
        public void CreateUserHash_UseSalt()
        {
            Random           random       = new Random();
            TestSaltProvider saltProvider = new TestSaltProvider();

            random.NextBytes(saltProvider.Salt);
            UserHashIdentityMiddleware middleware = GetMiddelware(saltProvider: saltProvider);

            HttpContext context     = HttpContextHelper.GetContextWithIp("192.168.100.1");
            string      initialHash = middleware.CreateUserHash(context);

            random.NextBytes(saltProvider.Salt);
            string changedHash = middleware.CreateUserHash(context);

            Assert.AreNotEqual(changedHash, initialHash);
        }
        public void CreateUserHash_CallProvidersInOrder(bool firstApplicable, bool secondApplicable, bool firstShouldBeCalled, bool secondShouldBeCalled)
        {
            HttpContext                context    = HttpContextHelper.GetContextWithIp("192.168.201.1");
            TestIdentityProvider       mock1      = new ("Provider1", 15) { IsApplicable = firstApplicable };
            TestIdentityProvider       mock2      = new ("Procider2", 10) { IsApplicable = secondApplicable };
            UserHashIdentityMiddleware middleware = GetMiddelware(saltProvider: null, mock1, mock2);

            string hash = middleware.CreateUserHash(context);

            mock1.AssertCallsAndReset(firstShouldBeCalled);
            mock2.AssertCallsAndReset(secondShouldBeCalled);

            if (!firstApplicable && !secondApplicable)
            {
                Assert.AreEqual(string.Empty, hash, "Hash should be empty when all providers not applicable");
            }
        }
        public void InvokeAsync_SetsActivityPropetry()
        {
            HttpContext context1 = HttpContextHelper.GetContextWithIp("192.168.0.4");
            HttpContext context2 = HttpContextHelper.GetContextWithIp("127.0.0.4");

            IMiddleware middleware = GetMiddelware();

            string GetActivityUserHash(HttpContext context)
            {
                Activity activity = new Activity(nameof(InvokeAsync_SetsActivityPropetry)).Start();

                middleware.InvokeAsync(context, c => Task.CompletedTask);
                activity.Stop();
                return(activity.GetUserHash());
            }

            string hash1 = GetActivityUserHash(context1);
            string hash2 = GetActivityUserHash(context2);

            Assert.AreNotEqual(hash1, hash2);
            Assert.AreEqual(hash1, GetActivityUserHash(context1));
            Assert.AreEqual(hash2, GetActivityUserHash(context2));
        }