public void ShouldClearTypeNameCacheOnConfigChange()
        {
            var microdotSerializationSecurityConfig = new MicrodotSerializationSecurityConfig {
                AssemblyNamesRegexReplacements =
                    new List <MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement>(new []
                {
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("foobar", "buz")
                })
            };

            var serializationConstraints =
                new MicrodotSerializationConstraints(
                    () =>
            {
                return(microdotSerializationSecurityConfig);
            });

            var result = serializationConstraints.TryGetAssemblyNameAndTypeReplacement("bar", "foobar");

            Assert.AreEqual("bar", result.AssemblyName);
            Assert.AreEqual("buz", result.TypeName);

            microdotSerializationSecurityConfig = new MicrodotSerializationSecurityConfig {
                AssemblyNamesRegexReplacements =
                    new List <MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement>(new []
                {
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("foobar", "ding")
                })
            };

            result = serializationConstraints.TryGetAssemblyNameAndTypeReplacement("bar", "foobar");

            Assert.AreEqual("bar", result.AssemblyName);
            Assert.AreEqual("ding", result.TypeName);
        }
        public void TryGetAssemblyNameAndTypeNameReplacementWhenReplacementExistsForFirstResult()
        {
            var microdotSerializationSecurityConfig = new MicrodotSerializationSecurityConfig {
                AssemblyNamesRegexReplacements =
                    new List <MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement>(new []
                {
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("foobar", "buz"),
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("foobar", "bull"),
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("carmel", "gilboa"),
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("carmel", "megido"),
                })
            };

            var serializationConstraints =
                new MicrodotSerializationConstraints(
                    () =>
            {
                return(microdotSerializationSecurityConfig);
            });

            var result = serializationConstraints.TryGetAssemblyNameAndTypeReplacement("carmel", "foobar");

            Assert.AreEqual("gilboa", result.AssemblyName);
            Assert.AreEqual("buz", result.TypeName);

            result = serializationConstraints.TryGetAssemblyAndTypeNameReplacementFromType(
                typeof(string), "carmel", "foobar");

            Assert.AreEqual("gilboa", result.AssemblyName);
            Assert.AreEqual("buz", result.TypeName);
        }
        public void TryGetTypeNameReplacementWhenReplacementDoesNotExists()
        {
            var microdotSerializationSecurityConfig = new MicrodotSerializationSecurityConfig {
                AssemblyNamesRegexReplacements =
                    new List <MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement>(new []
                {
                    new MicrodotSerializationSecurityConfig.AssemblyNameToRegexReplacement("mobar", "buz")
                })
            };

            var serializationConstraints =
                new MicrodotSerializationConstraints(
                    () =>
            {
                return(microdotSerializationSecurityConfig);
            });

            var result = serializationConstraints.TryGetAssemblyNameAndTypeReplacement("bar", "foobar");

            Assert.AreEqual("bar", result.AssemblyName);
            Assert.AreEqual("foobar", result.TypeName);

            result = serializationConstraints.TryGetAssemblyAndTypeNameReplacementFromType(
                typeof(string), "bar", "foobar");

            Assert.AreEqual("bar", result.AssemblyName);
            Assert.AreEqual("foobar", result.TypeName);
        }
        public void ShouldNotThrowIfTypeIsNotExcluded()
        {
            var serializationConstraints =
                new MicrodotSerializationConstraints(
                    () => new MicrodotSerializationSecurityConfig
            {
                DeserializationForbiddenTypes = new[] { "foo" }.ToList()
            });


            Assert.DoesNotThrow(() => serializationConstraints.ThrowIfExcluded("bar"));
        }
Ejemplo n.º 5
0
        public async Task CreateListener_HttpsEnabled_CreationFailsNoCertificateFound()
        {
            var endpointDefinition = Substitute.For <IServiceEndPointDefinition>();

            using (var httpPort = DisposablePort.GetPort())
                using (var httpsPort = DisposablePort.GetPort())
                {
                    endpointDefinition.HttpPort.Returns(ci => httpPort.Port);
                    endpointDefinition.HttpsPort.Returns(ci => httpsPort.Port); // HTTPS enabled by a non-null HTTPS port
                    endpointDefinition.ClientCertificateVerification.Returns(ci =>
                                                                             ClientCertificateVerificationMode.VerifyIdenticalRootCertificate);

                    var certificateLocator = Substitute.For <ICertificateLocator>();
                    certificateLocator.GetCertificate(Arg.Any <string>()).Throws <Exception>();

                    var microdotSerializationConstraints = new MicrodotSerializationConstraints(() =>
                                                                                                new MicrodotSerializationSecurityConfig()
                                                                                                );

                    Assert.Throws <Exception>(() => new HttpServiceListener(Substitute.For <IActivator>(),
                                                                            Substitute.For <IWorker>(),
                                                                            endpointDefinition,
                                                                            certificateLocator,
                                                                            Substitute.For <ILog>(),
                                                                            Enumerable.Empty <ICustomEndpoint>(),
                                                                            Substitute.For <IEnvironment>(),
                                                                            new JsonExceptionSerializer(
                                                                                Substitute.For <IStackTraceEnhancer>(),
                                                                                new JsonExceptionSerializationSettings(
                                                                                    new ExceptionHierarchySerializationBinder(
                                                                                        new MicrodotTypePolicySerializationBinder(
                                                                                            microdotSerializationConstraints
                                                                                            )
                                                                                        )
                                                                                    )
                                                                                ),
                                                                            new ServiceSchema(),
                                                                            () => new LoadShedding(),
                                                                            Substitute.For <IServerRequestPublisher>(),
                                                                            new CurrentApplicationInfo(
                                                                                nameof(HttpServiceListenerTests),
                                                                                Environment.UserName,
                                                                                System.Net.Dns.GetHostName()),
                                                                            () => new MicrodotHostingConfig(),
                                                                            new MicrodotTypePolicySerializationBinder(microdotSerializationConstraints)
                                                                            ));

                    certificateLocator.Received(1).GetCertificate("Service");
                }
        }
        public void ShouldThrowIfTypeIsExcluded()
        {
            var serializationConstraints =
                new MicrodotSerializationConstraints(
                    () => new MicrodotSerializationSecurityConfig
            {
                DeserializationForbiddenTypes = new[] { "foo" }.ToList()
            }
                    );

            Assert.Throws <UnauthorizedAccessException>(() => serializationConstraints.ThrowIfExcluded("foo"));
            Assert.Throws <UnauthorizedAccessException>(() => serializationConstraints.ThrowIfExcluded("barfoobuzz"));
            Assert.Throws <UnauthorizedAccessException>(() => serializationConstraints.ThrowIfExcluded("barfOobuzz"));
        }