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 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; }
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 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 TestConcurentCreationOfServersAndClients() { 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(() => { 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(() => { 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 Main(string[] args) { var iid = Guid.Parse("FF9B1856-934A-459B-92AF-18AEBD745BC1"); Console.WriteLine("Server id = " + iid); var client = new ExplicitBytesClient(iid, new EndpointBindingInfo(RpcProtseq.ncacn_np, null, "\\pipe\\testnamedpipe" + iid)); Console.WriteLine("Client started"); Console.WriteLine("Doing several requests to server"); for (int i = 0; i < 10; i++) { var resp = client.Execute(Encoding.Unicode.GetBytes("Client request")); Console.WriteLine(Encoding.Unicode.GetString(resp)); } Console.WriteLine("Press any key to send request on which server throws"); Console.ReadKey(); try { client.Execute(Encoding.Unicode.GetBytes("Throw Client request")); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Kill server process now and press any key"); Console.ReadKey(); try { client.Execute(Encoding.Unicode.GetBytes("This will not be delivered")); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
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 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) { } } } }
private static void init_rpc() { var guid = Marshal.GenerateGuidForType(typeof(api)); _api = new ExplicitBytesClient(guid, new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "FastDataServer")); }
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 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]); } } }
public void TestClientCannotConnect() { var endpoingBinding = new EndpointBindingInfo(RpcProtseq.ncalrpc, null, "lrpc-endpoint-doesnt-exist"); using (ExplicitBytesClient client = new ExplicitBytesClient(Guid.NewGuid(), endpoingBinding)) client.Execute(new byte[0]); }
public void TestPropertyProtocol() { var endpoingBinding = new EndpointBindingInfo(RpcProtseq.ncacn_ip_tcp, null, "123"); using (ExplicitBytesClient client = new ExplicitBytesClient(Guid.NewGuid(), endpoingBinding)) Assert.AreEqual(RpcProtseq.ncacn_ip_tcp, client.Protocol); }