public void TestOneRecursiveCall() { Guid iid = Guid.NewGuid(); bool wasCalled = false; 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) { if (!wasCalled) { wasCalled = true; 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]); } } return 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 TestClientOnLocalRpc() { 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) { 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.LRPC, client.ProtocolType); Assert.AreEqual(new byte[0], client.ClientAddress); Assert.AreEqual(System.Diagnostics.Process.GetCurrentProcess().Id, client.ClientPid.ToInt32()); Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name, 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.ncalrpc, null, "lrpctest"); using (ExplicitBytesClient client = new ExplicitBytesClient(iid, endpointBinding)) { client.AuthenticateAs(ExplicitBytesClient.Self); client.Execute(new byte[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); } }
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(); } }
private static void hostRpc() { var guid = Marshal.GenerateGuidForType(typeof(api)); _api = new ExplicitBytesServer(guid); _api.AddProtocol(RpcProtseq.ncalrpc, "FastDataServer", 20); _api.OnExecute += api_OnExecute; _api.StartListening(); }
public static IExplicitBytesServer CreateHost(Binding binding, string address, Guid uuid) { var host = new ExplicitBytesServer(uuid); var endpointBinding = EndpointMapper.WcfToRpc(address); string endPoint = CanonizeEndpoint(endpointBinding); host.AddProtocol(binding.ProtocolTransport, endPoint, binding.MaxConnections); host.AddAuthentication(binding.Authentication); return host; }
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); }
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); }
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]); } } }
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]); } } }
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)); } } } }
public void TestServerWithDiblicateUuid() { Guid iid = Guid.NewGuid(); ExplicitBytesServer server1 = new ExplicitBytesServer(iid); try { server1.AddProtocol(RpcProtseq.ncalrpc, "lrpctest1", 5); server1.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT); server1.StartListening(); ExplicitBytesServer server2 = new ExplicitBytesServer(iid); server2.AddProtocol(RpcProtseq.ncalrpc, "lrpctest2", 5); server2.AddAuthentication(RPC_C_AUTHN.RPC_C_AUTHN_WINNT); server2.StartListening(); } finally { server1.Dispose(); } }
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); } } }
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 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]); } } }