Exemplo n.º 1
0
        public void TestPerformanceWithLargePayloads()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg)
                { return(arg); };

                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "lrpctest")))
                {
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);

                    byte[] bytes = new byte[1 * 1024 * 1024]; //1mb in/out
                    new Random().NextBytes(bytes);

                    Stopwatch timer = new Stopwatch();
                    timer.Start();

                    for (int i = 0; i < 50; i++)
                    {
                        client.Execute(bytes);
                    }

                    timer.Stop();
                    Trace.WriteLine(timer.ElapsedMilliseconds.ToString(), "TestPerformanceWithLargePayloads");
                }
            }
        }
        public void TestUnregisterListener()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                RpcExecuteHandler handler =
                    delegate(IRpcCallInfo client, byte[] arg)
                { return(arg); };
                var endpointBinding = new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "lrpctest");
                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, endpointBinding))
                {
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);

                    server.OnExecute += handler;
                    client.Execute(new byte[0]);

                    server.OnExecute -= handler;
                    try
                    {
                        client.Execute(new byte[0]);
                        Assert.Fail();
                    }
                    catch (RpcException)
                    { }
                }
            }
        }
        public void TestInfiniteRecursiveCalls()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                RpcExecuteHandler handler =
                    delegate(IRpcCallInfo client, byte[] arg)
                {
                    using (ExplicitBytesClient innerClient = new ExplicitBytesClient(iid, new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "lrpctest")))
                    {
                        innerClient.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                        return(innerClient.Execute(new byte[0]));
                    }
                };
                server.OnExecute += handler;
                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "lrpctest")))
                {
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);
                }
            }
        }
        public void TestClientAbandon()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncalrpc, "lrpctest", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg)
                { return(arg); };

                {
                    var endpoingBinding        = new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "lrpctest");
                    ExplicitBytesClient client = new ExplicitBytesClient(iid, endpoingBinding);
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);
                    client = null;
                }

                GC.Collect(0, GCCollectionMode.Forced);
                GC.WaitForPendingFinalizers();

                server.StopListening();
            }
        }
        public static IExplicitBytesClient CreateClient(Binding binding, Guid uuid, string address)
        {
            var bindingInfo = EndpointMapper.WcfToRpc(address);

            bindingInfo.EndPoint = CanonizeEndpoint(bindingInfo);
            var client = new ExplicitBytesClient(uuid, bindingInfo);

            //NOTE: applying any authentication on local IPC greatly slows down start up of many simulatanious service
            bool skipAuthentication = binding.Authentication == RPC_C_AUTHN.RPC_C_AUTHN_NONE && bindingInfo.Protseq == RpcProtseq.ncalrpc;

            if (skipAuthentication)
            {
                client.AuthenticateAsNone();
            }
            else
            {
                client.AuthenticateAs(null, binding.Authentication == RPC_C_AUTHN.RPC_C_AUTHN_NONE
                                                                  ? ExplicitBytesClient.Anonymous
                                                                  : ExplicitBytesClient.Self,
                                      binding.Authentication == RPC_C_AUTHN.RPC_C_AUTHN_NONE
                                                                  ? RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_NONE
                                                                  : RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
                                      binding.Authentication);
            }

            return(client);
        }
Exemplo n.º 6
0
        public void ClientSecurityCallbackCalled()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_ip_tcp, "18080", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo clientInfo, byte[] arg)
                {
                    return(arg);
                };


                var  endpoingBinding = new EndpointBindingInfo(RpcProtseq.ncacn_ip_tcp, "127.0.0.1", "18080");
                bool securityCalled  = false;
                using (var client = new ExplicitBytesClient(iid, endpoingBinding)) {
                    var authCallback = new FunctionPtr <RPC_C_SECURITY_CALLBACK>(x => {
                        securityCalled         = true;
                        IntPtr securityContext = IntPtr.Zero;
                        var getSecurityStatus  = NativeMethods.I_RpcBindingInqSecurityContext(x, out securityContext);
                        Assert.AreEqual(RPC_STATUS.RPC_S_OK, getSecurityStatus);
                        Assert.AreNotEqual(IntPtr.Zero, securityContext);
                    });
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    var setStatus = NativeMethods.RpcBindingSetOption(client.Handle, (uint)RpcBindingOptions.RPC_C_OPT_SECURITY_CALLBACK, authCallback.Handle);
                    Assert.AreEqual(RPC_STATUS.RPC_S_OK, setStatus);
                    client.Execute(new byte[0]);
                }
                Assert.IsTrue(securityCalled);
            }
        }
Exemplo n.º 7
0
        public void TestNestedClientImpersonate()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg)
                {
                    Assert.AreEqual(false, client.IsImpersonating);
                    using (client.Impersonate())
                    {
                        Assert.AreEqual(true, client.IsImpersonating);
                        using (client.Impersonate())
                            Assert.AreEqual(true, client.IsImpersonating);
                        //does not dispose, we are still impersonating
                        Assert.AreEqual(true, client.IsImpersonating);
                    }
                    Assert.AreEqual(false, client.IsImpersonating);
                    return(arg);
                };
                var endpointBinding = new EndpointBindingInfo(RpcProtseq.ncacn_np, null, @"\pipe\testpipename");
                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, endpointBinding))
                {
                    client.AuthenticateAs(ExplicitBytesClient.Self);
                    client.Execute(new byte[0]);
                }
            }
        }
Exemplo n.º 8
0
        public void TestConcurentCreationOfServersAndClients2()
        {
            var callbackWasCalled = false;
            var serverId          = Guid.NewGuid();
            var serverPipe        = "\\pipe\\testserver" + MethodBase.GetCurrentMethod().Name;
            var callbackPipe      = "\\pipe\\testcallback" + MethodBase.GetCurrentMethod().Name;
            var callbackId        = Guid.NewGuid();
            var taskServer        = new Task(() =>
            {
                var server = new ExplicitBytesServer(serverId);
                server.AddProtocol(RpcProtseq.ncacn_np, serverPipe, byte.MaxValue);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                server.StartListening();

                server.OnExecute += (x, y) =>
                {
                    var taskCallback = new Task(() =>
                    {
                        ExplicitBytesClient callbackClient = new ExplicitBytesClient(callbackId, new EndpointBindingInfo(RpcProtseq.ncacn_np, null,
                                                                                                                         callbackPipe));
                        callbackClient.AuthenticateAs(null, ExplicitBytesClient.Self,
                                                      RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_NONE,
                                                      RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                        callbackClient.Execute(new byte[0]);
                    });
                    taskCallback.Start();
                    taskCallback.Wait();
                    return(y);
                };
            });

            taskServer.Start();
            taskServer.Wait();

            var taskClient = new Task(() =>
            {
                var client = new ExplicitBytesClient(serverId, new EndpointBindingInfo(RpcProtseq.ncacn_np, null, serverPipe));
                client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_NONE,
                                      RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                var callbackServer = new ExplicitBytesServer(callbackId);
                callbackServer.AddProtocol(RpcProtseq.ncacn_np, callbackPipe, byte.MaxValue);
                callbackServer.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                callbackServer.OnExecute += (x, y) =>
                {
                    callbackWasCalled = true;
                    return(y);
                };
                client.Execute(new byte[0]);
            });

            taskClient.Start();
            taskClient.Wait();
            Assert.IsTrue(callbackWasCalled);
        }
Exemplo n.º 9
0
        public void TestRandomizedConcurentCreationOfServersAndClients()
        {
            var tasks            = new List <Task>();
            int counter          = 0;
            var servers          = 10;
            int clientsPerServer = 10;

            for (int i = 0; i < servers; i++)
            {
                var task = new Task(() =>
                {
                    Thread.Sleep(_rand.Next(0, 50));
                    Guid iid = Guid.NewGuid();
                    using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
                    {
                        server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename" + iid, clientsPerServer);
                        server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                        server.StartListening();
                        server.OnExecute +=
                            delegate(IRpcCallInfo client, byte[] arg)
                        {
                            System.Threading.Interlocked.Increment(ref counter);
                            return(arg);
                        };
                        var clientTasks = new List <Task>();
                        for (int j = 0; j < clientsPerServer; j++)
                        {
                            var clientTask = new Task(() =>
                            {
                                Thread.Sleep(_rand.Next(0, 50));
                                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, new EndpointBindingInfo(RpcProtseq.ncacn_np, null, @"\pipe\testpipename" + iid)))
                                {
                                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_NONE, RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                                    Assert.AreEqual(123, client.Execute(new byte[1] {
                                        123
                                    })[0]);
                                }
                            });
                            clientTask.Start();
                            clientTasks.Add(clientTask);
                        }
                        Task.WaitAll(clientTasks.ToArray());
                    }
                });
                task.Start();
                tasks.Add(task);
            }
            Task.WaitAll(tasks.ToArray());
            Assert.AreEqual(servers * clientsPerServer, counter);
        }
Exemplo n.º 10
0
        public void TestClientOnAnonymousPipe()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_NONE);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg)
                {
                    Assert.AreEqual(0, arg.Length);
                    Assert.AreEqual(RPC_C_AUTHN.RPC_C_AUTHN_NONE, client.AuthenticationLevel);
                    Assert.AreEqual(RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_NONE, client.ProtectionLevel);
                    Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType);
                    Assert.AreEqual(new byte[0], client.ClientAddress);
                    Assert.AreEqual(0, client.ClientPid.ToInt32());
                    Assert.AreEqual(null, client.ClientPrincipalName);
                    Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetAnonymous().Name, client.ClientUser.Name);
                    Assert.AreEqual(true, client.IsClientLocal);
                    Assert.AreEqual(false, client.IsAuthenticated);
                    Assert.AreEqual(false, client.IsImpersonating);

                    bool failed = false;
                    try { client.Impersonate().Dispose(); }
                    catch (UnauthorizedAccessException) { failed = true; }
                    Assert.AreEqual(true, failed);
                    return(arg);
                };

                var endpointBinding = new EndpointBindingInfo(RpcProtseq.ncacn_np, null, @"\pipe\testpipename");
                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, endpointBinding))
                {
                    client.AuthenticateAs(ExplicitBytesClient.Anonymous);
                    client.Execute(new byte[0]);
                }
            }
        }
Exemplo n.º 11
0
        public void LongName()
        {
            var  address = "\\pipe\\127.0.0.1\\1\\test.test\\testLongNameLongNameLongNameLongNameLongNamed0286a6-0b9b-4db1-8659-b715e5db5b3bd0286a6-0b9b-4db1-8659-b715e5db5b3b";
            Guid iid     = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, address, 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                RpcExecuteHandler handler =
                    delegate(IRpcCallInfo client, byte[] arg)
                {
                    return(arg);
                };
                server.OnExecute += handler;
                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, new EndpointBindingInfo(RpcProtseq.ncacn_np, null, address)))
                {
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);
                }
            }
        }
Exemplo n.º 12
0
        static void ReversePingTest(RpcProtseq protocol, string[] hostNames, string endpoint, RPC_C_AUTHN auth)
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg)
                {
                    Array.Reverse(arg);
                    return(arg);
                };

                server.AddProtocol(protocol, endpoint, 5);
                server.AddAuthentication(auth);
                server.StartListening();

                byte[] input  = Encoding.ASCII.GetBytes("abc");
                byte[] expect = Encoding.ASCII.GetBytes("cba");

                foreach (string hostName in hostNames)
                {
                    using (ExplicitBytesClient client = new ExplicitBytesClient(iid, new EndpointBindingInfo(protocol, hostName, endpoint)))
                    {
                        client.AuthenticateAs(null, auth == RPC_C_AUTHN.RPC_C_AUTHN_NONE
                                                      ? ExplicitBytesClient.Anonymous
                                                      : ExplicitBytesClient.Self,
                                              auth == RPC_C_AUTHN.RPC_C_AUTHN_NONE
                                                      ? RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_NONE
                                                      : RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
                                              auth);

                        Assert.AreEqual(expect, client.Execute(input));
                    }
                }
            }
        }
Exemplo n.º 13
0
        public void TestClientOnNamedPipe()
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(RpcProtseq.ncacn_np, @"\pipe\testpipename", 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg)
                {
                    Assert.AreEqual(0, arg.Length);
                    Assert.AreEqual(RPC_C_AUTHN.RPC_C_AUTHN_WINNT, client.AuthenticationLevel);
                    Assert.AreEqual(RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, client.ProtectionLevel);
                    Assert.AreEqual(RpcProtoseqType.NMP, client.ProtocolType);
                    Assert.AreEqual(new byte[0], client.ClientAddress);
                    Assert.AreEqual(0, client.ClientPid.ToInt32());
                    Assert.AreEqual(String.Empty, client.ClientPrincipalName);
                    Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name, client.ClientUser.Name);
                    Assert.AreEqual(true, client.IsClientLocal);
                    Assert.AreEqual(true, client.IsAuthenticated);
                    Assert.AreEqual(false, client.IsImpersonating);
                    using (client.Impersonate())
                        Assert.AreEqual(true, client.IsImpersonating);
                    Assert.AreEqual(false, client.IsImpersonating);
                    return(arg);
                };

                var endpointBinding = new EndpointBindingInfo(RpcProtseq.ncacn_np, null, @"\pipe\testpipename");
                using (ExplicitBytesClient client = new ExplicitBytesClient(iid, endpointBinding))
                {
                    client.AuthenticateAs(ExplicitBytesClient.Self);
                    client.Execute(new byte[0]);
                }
            }
        }
Exemplo n.º 14
0
        private static void TestPerformanceInternal(RpcProtseq protoseq, string endpointName, int payloadSize)
        {
            Guid iid = Guid.NewGuid();

            using (ExplicitBytesServer server = new ExplicitBytesServer(iid))
            {
                server.AddProtocol(protoseq, endpointName, 5);
                server.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                server.StartListening();
                server.OnExecute +=
                    delegate(IRpcCallInfo client, byte[] arg) { return(arg); };

                using (
                    ExplicitBytesClient client = new ExplicitBytesClient(iid,
                                                                         new EndpointBindingInfo(protoseq, null, endpointName)))
                {
                    client.AuthenticateAs(null, ExplicitBytesClient.Self, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
                                          RPC_C_AUTHN.RPC_C_AUTHN_WINNT);
                    client.Execute(new byte[0]);

                    byte[] bytes = new byte[payloadSize];
                    new Random().NextBytes(bytes);

                    Stopwatch timer = new Stopwatch();
                    timer.Start();

                    for (int i = 0; i < 5000; i++)
                    {
                        client.Execute(bytes);
                    }

                    timer.Stop();
                    Trace.WriteLine(timer.ElapsedMilliseconds.ToString(), endpointName);
                }
            }
        }