Exemple #1
0
        public void PingWithExtensionTest()
        {
            using (RpcServer server = RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                                      .AddProtocol("ncacn_np", @"\pipe\p1"))
            {
                UnitTestRpcInterop.RegisterAllExtensions(server.ExtensionRegistry);
                server.Ping += delegate(RpcPingRequest r)
                {
                    if (r.HasExtension(UnitTestRpcInterop.CustomPingDataIn))
                    {
                        return(RpcPingResponse.CreateBuilder()
                               .SetExtension(UnitTestRpcInterop.CustomPingDataOut,
                                             r.GetExtension(UnitTestRpcInterop.CustomPingDataIn))
                               .Build());
                    }
                    return(RpcPingResponse.DefaultInstance);
                };
                server.StartListening();

                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncacn_np", null, @"\pipe\p1").Authenticate(
                            RpcAuthenticationType.Anonymous))
                {
                    UnitTestRpcInterop.RegisterAllExtensions(client.ExtensionRegistry);

                    RpcPingRequest r = RpcPingRequest.CreateBuilder()
                                       .SetExtension(UnitTestRpcInterop.CustomPingDataIn, "ping-request-data")
                                       .Build();
                    RpcPingResponse response = client.Ping(r);
                    Assert.IsTrue(response.HasExtension(UnitTestRpcInterop.CustomPingDataOut));
                    Assert.AreEqual("ping-request-data", response.GetExtension(UnitTestRpcInterop.CustomPingDataOut));
                }
            }
        }
Exemple #2
0
        public void DemoExampleConnection()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   //allow GSS_NEGOTIATE
                   .AddAuthNegotiate()
                   //LRPC named 'lrpctest'
                   .AddProtocol("ncalrpc", "lrpctest")
                   .AddProtocol("ncacn_ip_tcp", "12345")
                   .AddProtocol("ncacn_np", @"\pipe\p1")
                   //Begin responding
                   .StartListening())
            {
                // Demonstrate a typical client-implemented wrapper that can parse URIs, retry connections, etc.
                using (var client = new SearchService(new ExampleRpcConnection(iid, "lrpc://localhost/lrpctest")))
                    Assert.AreEqual(1, client.Search(SearchRequest.CreateBuilder().AddCriteria("Test1").Build()).ResultsCount);

                using (var client = new SearchService(new ExampleRpcConnection(iid, "rpc://self@localhost:12345")))
                    Assert.AreEqual(1, client.Search(SearchRequest.CreateBuilder().AddCriteria("Test2").Build()).ResultsCount);

                using (var client = new SearchService(new ExampleRpcConnection(iid, "np://self@localhost/pipe/p1")))
                    Assert.AreEqual(1, client.Search(SearchRequest.CreateBuilder().AddCriteria("Test3").Build()).ResultsCount);
            }
        }
Exemple #3
0
 public void MultiPartMessageTest()
 {
     //Notice that both client and server must enable multi-part messages...
     using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
            .AddAuthNegotiate()
            .AddProtocol("ncacn_ip_tcp", "12345")
            .EnableMultiPart()
            .StartListening())
     {
         using (
             SearchService client = new SearchService(RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "::1", "12345")
                                                      .Authenticate(RpcAuthenticationType.Self).
                                                      EnableMultiPart(1000000)))
         {
             // Non-LRPC channels have limitations on message sizes, we use multiple calls to forward large messages
             // and store state on the server in the RpcSession associated with this client.  This is all transparent
             // to the caller, but this 7 meg message will produce several rpc-calls.
             SearchRequest.Builder criteria = SearchRequest.CreateBuilder();
             byte[] bytes = new byte[2500];
             Random r     = new Random();
             for (int i = 0; i < 2500; i++)
             {
                 r.NextBytes(bytes);
                 criteria.AddCriteria(Convert.ToBase64String(bytes));
             }
             SearchResponse results = client.Search(criteria.Build());
             Assert.AreEqual(2500, results.ResultsCount);
         }
     }
 }
Exemple #4
0
        public void DemoRpcOverLrpc()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   //allow GSS_NEGOTIATE
                   .AddAuthNegotiate()
                   //LRPC named 'lrpctest'
                   .AddProtocol("ncalrpc", "lrpctest")
                   //Begin responding
                   .StartListening())
            {
                //Create the rpc client connection and give it to the new SearchService
                using (
                    SearchService client =
                        new SearchService(
                            RpcClient.ConnectRpc(iid, "ncalrpc", null, "lrpctest").Authenticate(
                                RpcAuthenticationType.Self)))
                {
                    //party on!
                    SearchResponse results =
                        client.Search(SearchRequest.CreateBuilder().AddCriteria("Test Criteria").Build());
                    Assert.AreEqual(1, results.ResultsCount);
                    Assert.AreEqual("Test Criteria", results.ResultsList[0].Name);
                    Assert.AreEqual("http://whatever.com", results.ResultsList[0].Url);
                }
            }
        }
Exemple #5
0
        public void DemoClientProxyChain()
        {
            Guid iid1 = Guid.NewGuid();
            Guid iid2 = Guid.NewGuid();

            //forward reuests from iid1 to service iid2:
            using (
                RpcServer.CreateRpc(iid1,
                                    new SearchService.ServerStub(
                                        RpcClient.ConnectRpc(iid2, "ncalrpc", null, @"lrpctest").Authenticate(
                                            RpcAuthenticationType.Self)))
                .AddProtocol("ncalrpc", @"lrpctest")
                .AddAuthNegotiate()
                .StartListening())
                //iid calls the implementation
                using (RpcServer.CreateRpc(iid2, new SearchService.ServerStub(new AuthenticatedSearch()))
                       .AddProtocol("ncalrpc", @"lrpctest")
                       .AddAuthNegotiate()
                       .StartListening())
                {
                    using (
                        SearchService client =
                            new SearchService(
                                RpcClient.ConnectRpc(iid1, "ncalrpc", null, @"lrpctest").Authenticate(
                                    RpcAuthenticationType.Self)))
                        Assert.AreEqual(1,
                                        client.Search(SearchRequest.CreateBuilder().AddCriteria(String.Empty).Build()).
                                        ResultsCount);
                }
        }
 protected override IDisposable StartServer(int responseSize)
 {
     _service = RpcServer.CreateRpc(Iid, CreateStub(responseSize));
     PrepareService(_service, Iid);
     _service.StartListening();
     return(_service);
 }
Exemple #7
0
 public void SimplePingTest()
 {
     using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
            .AddProtocol("ncacn_ip_tcp", "12345")
            .AddAuthNegotiate()
            .StartListening())
     {
         using (
             RpcClient client =
                 RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "127.0.0.1", "12345").Authenticate(
                     RpcAuthenticationType.Self))
         {
             client.Ping();
         }
     }
 }
Exemple #8
0
        public void RoundTripCallContextExtensionsTest()
        {
            using (RpcServer server = RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                                      .AddProtocol("ncacn_np", @"\pipe\p1"))
            {
                UnitTestRpcInterop.RegisterAllExtensions(server.ExtensionRegistry);
                server.Ping += delegate
                {
                    String strValue =
                        RpcCallContext.Current.GetExtension(UnitTestRpcInterop.CustomContextString);
                    UInt64 intValue =
                        RpcCallContext.Current.GetExtension(UnitTestRpcInterop.CustomContextNumber);

                    char[] tmp = strValue.ToCharArray();
                    Array.Reverse(tmp);
                    strValue = new string(tmp);
                    intValue = ~intValue;

                    RpcCallContext.Current = RpcCallContext.Current.ToBuilder()
                                             .SetExtension(UnitTestRpcInterop.CustomContextString, strValue)
                                             .SetExtension(UnitTestRpcInterop.CustomContextNumber, intValue)
                                             .Build();

                    return(RpcPingResponse.DefaultInstance);
                };
                server.StartListening();

                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncacn_np", null, @"\pipe\p1").Authenticate(
                            RpcAuthenticationType.Anonymous))
                {
                    UnitTestRpcInterop.RegisterAllExtensions(client.ExtensionRegistry);

                    client.CallContext = client.CallContext.ToBuilder()
                                         .SetExtension(UnitTestRpcInterop.CustomContextString, "abc")
                                         .SetExtension(UnitTestRpcInterop.CustomContextNumber, 0x70000000FFFFFFFFUL)
                                         .Build();
                    client.Ping();

                    Assert.AreEqual("cba", client.CallContext.GetExtension(UnitTestRpcInterop.CustomContextString));
                    Assert.AreEqual(~0x70000000FFFFFFFFUL,
                                    client.CallContext.GetExtension(UnitTestRpcInterop.CustomContextNumber));
                }
            }
        }
Exemple #9
0
 public void AuthWinNTTest()
 {
     using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
            .AddAuthWinNT()
            .AddProtocol("ncacn_ip_tcp", "12345")
            .StartListening())
     {
         using (
             SearchService client =
                 new SearchService(
                     RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "127.0.0.1", "12345").Authenticate(
                         RpcAuthenticationType.Self)))
         {
             SearchResponse results =
                 client.Search(SearchRequest.CreateBuilder().AddCriteria("Test Criteria").Build());
             Assert.AreEqual(1, results.ResultsCount);
         }
     }
 }
Exemple #10
0
        public void MultiPartMessageCancel()
        {
            //Notice that both client and server must enable multi-part messages...
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   .AddAuthNegotiate()
                   .AddProtocol("ncacn_ip_tcp", "12345")
                   .EnableMultiPart()
                   .StartListening())
            {
                ByteString transaction = ByteString.CopyFrom(Guid.NewGuid().ToByteArray());
                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "::1", "12345").Authenticate(
                            RpcAuthenticationType.Self))
                {
                    RpcMultiPartResponse response = client.CallMethod(".multi",
                                                                      RpcMultiPartRequest.CreateBuilder()
                                                                      .SetTransactionId(transaction)
                                                                      .SetMessageStatus(
                                                                          RpcMultiPartRequest.Types.RpcMessageStatus
                                                                          .CONTINUE)
                                                                      .SetMethodName("Whatever")
                                                                      .SetCurrentPosition(0)
                                                                      .SetBytesSent(1000)
                                                                      .SetTotalBytes(2000)
                                                                      .SetPayloadBytes(
                                                                          ByteString.CopyFrom(new byte[1000]))
                                                                      .Build(),
                                                                      RpcMultiPartResponse.CreateBuilder());

                    Assert.IsTrue(response.Continue);

                    response = client.CallMethod(".multi",
                                                 RpcMultiPartRequest.CreateBuilder()
                                                 .SetTransactionId(transaction)
                                                 .SetMessageStatus(RpcMultiPartRequest.Types.RpcMessageStatus.CANCEL)
                                                 .Build(),
                                                 RpcMultiPartResponse.CreateBuilder());
                    Assert.IsFalse(response.Continue);
                }
            }
        }
Exemple #11
0
        public void DemoCustomAuthorization()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));
            //create the implementation and wrap our autthorization around it
            IRpcDispatch impl = new AuthorizeFilter(new SearchService.Dispatch(new AuthenticatedSearch()));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(impl))
                   .AddProtocol("ncalrpc", @"lrpctest")
                   .StartListening())
            {
                using (
                    SearchService client =
                        new SearchService(
                            RpcClient.ConnectRpc(iid, "ncalrpc", null, @"lrpctest").Authenticate(
                                RpcAuthenticationType.Self)))
                    client.Search(SearchRequest.CreateBuilder().AddCriteria(String.Empty).Build());
            }
        }
Exemple #12
0
        public void DemoRpcOverAnonymousNamedPipe()
        {
            //obtain the interface id for rpc registration
            Guid iid = Marshal.GenerateGuidForType(typeof(ISearchService));

            //Create the server with a stub pointing to our implementation
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AnonymousSearch()))
                   //pipes start with '\pipe\'
                   .AddProtocol("ncacn_np", @"\pipe\p1")
                   //Begin responding
                   .StartListening())
            {
                //using the anonId interface and AnonymousSearch implementation we can call without authentication
                using (
                    SearchService client =
                        new SearchService(
                            RpcClient.ConnectRpc(iid, "ncacn_np", @"\\localhost", @"\pipe\p1").Authenticate(
                                RpcAuthenticationType.Anonymous)))
                    client.Search(SearchRequest.CreateBuilder().Build());
            }
        }
Exemple #13
0
        public void MultiPartMessageFailsWithoutHandler()
        {
            //Notice that both client and server must enable multi-part messages...
            using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                   .AddAuthNegotiate()
                   .AddProtocol("ncacn_ip_tcp", "12345")
//Omit server response:        .EnableMultiPart()
                   .StartListening())
            {
                using (
                    SearchService client = new SearchService(RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "::1", "12345")
                                                             .Authenticate(RpcAuthenticationType.Self).
                                                             EnableMultiPart(1000000)))
                {
                    SearchRequest.Builder criteria = SearchRequest.CreateBuilder();
                    byte[] bytes = new byte[1000000];
                    new Random().NextBytes(bytes);
                    SearchResponse results = client.Search(criteria.AddCriteria(Convert.ToBase64String(bytes)).Build());
                    Assert.AreEqual(2500, results.ResultsCount);
                }
            }
        }
Exemple #14
0
        public void LrpcPerformanceTest()
        {
            Win32RpcServer.VerboseLogging = false;
            try
            {
                using (RpcServer.CreateRpc(iid, new SearchService.ServerStub(new AuthenticatedSearch()))
                       .AddAuthNegotiate()
                       .AddProtocol("ncalrpc", "lrpctest")
                       .StartListening())
                {
                    using (
                        SearchService client =
                            new SearchService(
                                RpcClient.ConnectRpc(iid, "ncalrpc", null, "lrpctest").Authenticate(
                                    RpcAuthenticationType.Self)))
                    {
                        SearchResponse previous = client.Search(SearchRequest.CreateBuilder()
                                                                .AddCriteria("one").AddCriteria("two").AddCriteria(
                                                                    "three").Build());
                        RefineSearchRequest req = RefineSearchRequest.CreateBuilder()
                                                  .AddCriteria("four").SetPreviousResults(previous).Build();

                        Stopwatch w = new Stopwatch();
                        w.Start();
                        for (int i = 0; i < 1000; i++)
                        {
                            client.RefineSearch(req);
                        }
                        w.Stop();
                        Trace.TraceInformation("Lrpc Performance = {0}", w.ElapsedMilliseconds);
                    }
                }
            }
            finally
            {
                Win32RpcServer.VerboseLogging = true;
            }
        }
Exemple #15
0
        public void ClientInformationTest()
        {
            //Create the server with a stub pointing to our implementation
            using (Win32RpcServer server = RpcServer.CreateRpc(iid, new ClientInformationFilter())
                                           .AddAuthWinNT()
                                           .AddProtocol("ncalrpc", "lrpctest")
                                           .AddProtocol("ncacn_ip_tcp", "12345")
                                           .AddProtocol("ncacn_np", @"\pipe\p1"))
            {
                server.StartListening();

                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncalrpc", null, "lrpctest").Authenticate(RpcAuthenticationType.Self))
                    client.CallMethod("ncalrpc", RpcVoid.DefaultInstance, RpcVoid.CreateBuilder());

                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncacn_ip_tcp", "127.0.0.1", "12345").Authenticate(
                            RpcAuthenticationType.Self))
                    client.CallMethod("ncacn_ip_tcp", RpcVoid.DefaultInstance, RpcVoid.CreateBuilder());

                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncacn_np", @"\\localhost", @"\pipe\p1").Authenticate(
                            RpcAuthenticationType.Anonymous))
                    client.CallMethod("ncacn_np-Anonymous", RpcVoid.DefaultInstance, RpcVoid.CreateBuilder());

                server.AddAuthNegotiate(); //winnt authentication not supported over pipes... need to allow nego
                using (
                    RpcClient client =
                        RpcClient.ConnectRpc(iid, "ncacn_np", @"\\localhost", @"\pipe\p1").Authenticate(
                            RpcAuthenticationType.Self))
                    client.CallMethod("ncacn_np", RpcVoid.DefaultInstance, RpcVoid.CreateBuilder());
            }
        }
        static void Main(string[] args)
        {
            args    = new[] { "" };
            args[0] = "listen";

            switch (args[0].ToLower())
            {
            case "listen":
            {
                ////using (RpcServer.CreateRpc(IID, new Impersonation(new MyService.ServerStub(new Implementation())))
                //using (RpcServer.CreateRpc(IID, new Impersonation(new Greeter.ServerStub(new Implementation_Greeter())))
                //    //.AddAuthentication(CSharpTest.Net.RpcLibrary.RpcAuthentication.RPC_C_AUTHN_NONE)
                //    //.AddAuthNegotiate()
                //    .AddProtocol("ncacn_ip_tcp", "50051")
                //    //.AddProtocol("ncacn_np", @"\pipe\Greeter")
                //    ////.AddProtocol("ncalrpc", "MyService")
                //    //.AddProtocol("ncalrpc", "Greeter")
                //    .StartListening())
                //{
                //    Console.WriteLine("Waiting for connections...");
                //    Console.ReadLine();
                //}


                Guid iid = Marshal.GenerateGuidForType(typeof(IGreeter));
                using (RpcServer.CreateRpc(iid, new Greeter.ServerStub(new Anonymous_Greeter()))
                       //.AddAuthNegotiate()
                       .AddAuthentication(CSharpTest.Net.RpcLibrary.RpcAuthentication.RPC_C_AUTHN_NONE)
                       .AddAuthNegotiate()
                       .AddProtocol("ncacn_ip_tcp", "50051")
                       //.AddProtocol("ncalrpc", "Greeter")
                       .StartListening())
                {
                    Console.WriteLine("Waiting for connections...");
                    string name = "123";         // Console.ReadLine();


                    using (Greeter client = new Greeter(RpcClient
                                                        .ConnectRpc(iid, "ncacn_ip_tcp", @"localhost", "50051")
                                                        .Authenticate(RpcAuthenticationType.Self)
                                                        //.Authenticate(RpcAuthenticationType.None)
                                                        ))
                    {
                        HelloReply response = client.SayHello(HelloRequest.CreateBuilder().SetName(name).Build());
                        Console.WriteLine("OK: " + response.Message);
                    }
                    Console.ReadLine();
                }

                //Guid iid = Marshal.GenerateGuidForType(typeof(IBookService));
                //using (RpcServer.CreateRpc(iid, new BookService.ServerStub(new Anonymous_BookService()))
                //    .AddProtocol("ncacn_ip_tcp", "50051")
                //    .AddProtocol("ncalrpc", "BookService")
                //    .StartListening())
                //{
                //    Console.WriteLine("Waiting for connections...");
                //    Console.ReadLine();
                //}


                break;
            }

            case "send-lrpc":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncalrpc", null, "MyService")
                           .Authenticate(RpcAuthenticationType.Self)))
                {
                    MyResponse response = client.Send(
                        MyRequest.CreateBuilder().SetMessage("Hello via LRPC!").Build());
                }
                break;
            }

            case "send-tcp":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncacn_ip_tcp", @"localhost", "8080")
                           .Authenticate(RpcAuthenticationType.Self)))
                {
                    MyResponse response = client.Send(
                        MyRequest.CreateBuilder().SetMessage("Hello via Tcp/Ip!").Build());
                }
                break;
            }

            case "send-np":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncacn_np", @"\\localhost", @"\pipe\MyService")
                           .Authenticate(RpcAuthenticationType.Self)))
                {
                    MyResponse response = client.Send(
                        MyRequest.CreateBuilder().SetMessage("Hello via Named Pipe!").Build());
                }
                break;
            }

            case "send-anon":
            {
                using (MyService client = new MyService(
                           RpcClient.ConnectRpc(IID, "ncacn_np", @"\\localhost", @"\pipe\MyService")
                           .Authenticate(RpcAuthenticationType.Anonymous)))
                {
                    try
                    {
                        MyResponse response = client.Send(
                            MyRequest.CreateBuilder().SetMessage("Hello from Anonymous!").Build());
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                    }
                }
                break;
            }
            }
        }