Beispiel #1
0
        public static void Main(string[] args)
        {
            Console.Title = "Halibut Server";
            var certificate = new X509Certificate2("HalibutServer.pfx");

            var endPoint = new IPEndPoint(IPAddress.Any, 8433);

            var server = new HalibutServer(endPoint, certificate);
            server.Services.Register<ICalculatorService, CalculatorService>();
            server.Options.ClientCertificateValidator = ValidateClientCertificate;
            server.Start();

            Console.WriteLine("Server listening on port 8433...");

            while (true)
            {
                var line = Console.ReadLine();
                if (string.Equals("cls", line, StringComparison.OrdinalIgnoreCase))
                {
                    Console.Clear();
                    continue;
                }

                if (string.Equals("q", line, StringComparison.OrdinalIgnoreCase))
                    return;

                if (string.Equals("exit", line, StringComparison.OrdinalIgnoreCase))
                    return;

                Console.WriteLine("Unknown command. Enter 'q' to quit.");
            }
        }
Beispiel #2
0
        public void AliceCanSendMessagesToBob()
        {
            using (var bob = new HalibutServer(new IPEndPoint(IPAddress.Any, 8013), Certificates.Bob))
            {
                bob.Services.Register<IEchoService, EchoService>();
                bob.Options.ClientCertificateValidator = v => v.Thumbprint == Certificates.AlicePublicThumbprint ? CertificateValidationResult.Valid : CertificateValidationResult.Rejected;
                bob.Start();

                var alice = new HalibutClient(Certificates.Alice);
                var echo = alice.Create<IEchoService>(new Uri("rpc://localhost:8013"), Certificates.BobPublicThumbprint);

                Assert.That(echo.SayHello("Hi Bob, it's Alice"), Is.EqualTo("Hello!"));
            }
        }
Beispiel #3
0
        public void SupportsDifferentServiceContractMethods()
        {
            using (var bob = new HalibutServer(new IPEndPoint(IPAddress.Any, 8013), Certificates.Bob))
            {
                bob.Services.Register<ISupportedServices, SupportedServices>();
                bob.Options.ClientCertificateValidator = v => v.Thumbprint == Certificates.AlicePublicThumbprint ? CertificateValidationResult.Valid : CertificateValidationResult.Rejected;
                bob.Start();

                var alice = new HalibutClient(Certificates.Alice);
                var echo = alice.Create<ISupportedServices>(new Uri("rpc://localhost:8013"), Certificates.BobPublicThumbprint);

                echo.MethodReturningVoid(12, 14);

                Assert.That(echo.Hello(), Is.EqualTo("Hello"));
                Assert.That(echo.Hello("a"), Is.EqualTo("Hello a"));
                Assert.That(echo.Hello("a", "b"), Is.EqualTo("Hello a b"));
                Assert.That(echo.Hello("a", "b", "c"), Is.EqualTo("Hello a b c"));
                Assert.That(echo.Hello("a", "b", "c", "d"), Is.EqualTo("Hello a b c d"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e"), Is.EqualTo("Hello a b c d e"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e", "f"), Is.EqualTo("Hello a b c d e f"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g"), Is.EqualTo("Hello a b c d e f g"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h"), Is.EqualTo("Hello a b c d e f g h"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i"), Is.EqualTo("Hello a b c d e f g h i"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), Is.EqualTo("Hello a b c d e f g h i j"));
                Assert.That(echo.Hello("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"), Is.EqualTo("Hello a b c d e f g h i j k"));

                Assert.That(echo.Add(1, 2), Is.EqualTo(3));
                Assert.That(echo.Add(1.00, 2.00), Is.EqualTo(3.00));
                Assert.That(echo.Add(1.10M, 2.10M), Is.EqualTo(3.20M));

                Assert.That(echo.Ambiguous("a", "b"), Is.EqualTo("Hello string"));
                Assert.That(echo.Ambiguous("a", new Tuple<string, string>("a", "b")), Is.EqualTo("Hello tuple"));

                var ex = Assert.Throws<JsonRpcException>(() => echo.Ambiguous("a", (string) null));
                Assert.That(ex.Message, Is.StringContaining("Ambiguous"));
            }
        }
Beispiel #4
0
        public void ServerExceptionsAreWrappedAsJsonExceptions()
        {
            using (var bob = new HalibutServer(new IPEndPoint(IPAddress.Any, 8013), Certificates.Bob))
            {
                bob.Services.Register<IEchoService, EchoService>();
                bob.Options.ClientCertificateValidator = v => v.Thumbprint == Certificates.AlicePublicThumbprint ? CertificateValidationResult.Valid : CertificateValidationResult.Rejected;
                bob.Start();

                var alice = new HalibutClient(Certificates.Alice);
                var echo = alice.Create<IEchoService>(new Uri("rpc://localhost:8013"), Certificates.BobPublicThumbprint);

                var jsonex = Assert.Throws<JsonRpcException>(() => echo.Crash());
                Assert.That(jsonex.Message, Is.StringContaining("divide by zero"));
            }
        }
Beispiel #5
0
        public void BobOnlyAcceptsMessagesFromAlice()
        {
            using (var bob = new HalibutServer(new IPEndPoint(IPAddress.Any, 8013), Certificates.Bob))
            {
                bob.Services.Register<IEchoService, EchoService>();
                bob.Options.ClientCertificateValidator = v => v.Thumbprint == Certificates.AlicePublicThumbprint ? CertificateValidationResult.Valid : CertificateValidationResult.Rejected;
                bob.Start();

                var eve = new HalibutClient(Certificates.Eve);
                var echo = eve.Create<IEchoService>(new Uri("rpc://localhost:8013"), Certificates.BobPublicThumbprint);

                var ex = Assert.Throws<JsonRpcException>(() => echo.SayHello("Hi Bob, it's Eve"));
                Assert.That(ex.Message, Is.StringContaining("This can happen when the remote server does not trust the certificate that we provided."));
            }
        }
Beispiel #6
0
        public void AliceOnlySendsMessagesToBob()
        {
            using (var eve = new HalibutServer(new IPEndPoint(IPAddress.Any, 8013), Certificates.Eve))
            {
                eve.Services.Register<IEchoService, EchoService>();
                eve.Options.ClientCertificateValidator = v => v.Thumbprint == Certificates.AlicePublicThumbprint ? CertificateValidationResult.Valid : CertificateValidationResult.Rejected;
                eve.Start();

                var alice = new HalibutClient(Certificates.Alice);
                var echo = alice.Create<IEchoService>(new Uri("rpc://localhost:8013"), Certificates.BobPublicThumbprint);

                var ex = Assert.Throws<JsonRpcException>(() => echo.SayHello("Hi Bob, it's Eve"));
                Assert.That(ex.Message, Is.StringContaining("We aborted the connection because the remote host was not authenticated"));
            }
        }