public void TestAllow() { if (!m_Server.IsRunning) { m_Server.Start(); } EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_ServerConfig.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { TestConnectionFilter.Allow = true; int oldCount = TestConnectionFilter.ConnectedCount; var signal = new ManualResetEventSlim(false); ThreadPool.QueueUserWorkItem((o) => { var s = o as Socket; s.Connect(serverAddress); signal.Set(); }, socket); Assert.IsTrue(signal.Wait(2000)); Thread.Sleep(100); Assert.AreEqual(oldCount + 1, TestConnectionFilter.ConnectedCount); if (!m_NewSessionConnectedEvent.WaitOne(1000)) { Assert.Fail("New session hasnot been accept on time!"); } } }
public void TestCommandParser() { var server = new TestServer(new TestCommandParser()); server.Setup(m_RootConfig, m_Config, SocketServerFactory.Instance); try { server.Start(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port); using (Socket socket = CreateClientSocket()) { string command = string.Format("Hello World ({0})!", Guid.NewGuid().ToString()); socket.SendTo(m_Encoding.GetBytes("ECHO:" + command + Environment.NewLine), serverAddress); string echoMessage = m_Encoding.GetString(ReceiveMessage(socket, serverAddress).ToArray()); Assert.AreEqual(command, echoMessage); } } catch (Exception e) { throw e; } finally { if (server.IsRunning) { server.Stop(); } } }
public void TestCommands() { if (!m_Server.IsRunning) { m_Server.Start(); } EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_ServerConfig.Port); using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); using (StreamReader reader = new StreamReader(socketStream, m_Encoding, true)) using (StreamWriter writer = new StreamWriter(socketStream, m_Encoding, 1024 * 8)) { reader.ReadLine(); Random rd = new Random(); for (int i = 0; i < 10; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "ADD", x, y); Console.WriteLine(command); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Console.WriteLine(line); Assert.AreEqual(x + y, int.Parse(line)); } for (int i = 0; i < 10; i++) { int x = rd.Next(1, 1000), y = rd.Next(1, 1000); string command = string.Format("{0} {1} {2}", "MULT", x, y); Console.WriteLine(command); writer.WriteLine(command); writer.Flush(); string line = reader.ReadLine(); Console.WriteLine(line); Assert.AreEqual(x * y, int.Parse(line)); } } } }
public void TestTerminatorRequestFilterA() { var appServer = new TestServer(); using (appServer as IDisposable) { var setupResult = appServer.Setup("127.0.0.1", 2012, null, new TerminatorRequestFilterFactory("##", m_Encoding), new ConsoleLogFactory(), null, null); Assert.IsTrue(setupResult); Assert.IsTrue(appServer.Start()); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2012); using (var socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); var socketStream = new NetworkStream(socket); using (var reader = new StreamReader(socketStream, m_Encoding, true)) using (var writer = new StreamWriter(socketStream, m_Encoding, 1024 * 8)) { var welcomeString = reader.ReadLine(); Assert.AreEqual(string.Format(TestSession.WelcomeMessageFormat, appServer.Name), welcomeString); var actions = new Action <StreamWriter, string>[] { (w, r) => SendRequestA(w, r), (w, r) => SendRequestB(w, r), (w, r) => SendRequestC(w, r) }; var rd = new Random(); for (var i = 0; i < 50; i++) { var command = Guid.NewGuid().ToString(); var act = actions[rd.Next(0, 100) % actions.Length]; act(writer, command); var echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(command, echoMessage); } } } } }
public void TestTerminatorRequestFilter() { var appServer = new TestServer(); using (appServer as IDisposable) { var setupResult = appServer.Setup("127.0.0.1", 2012, null, new TerminatorRequestFilterFactory("##", m_Encoding), new ConsoleLogFactory(), null, null); Assert.IsTrue(setupResult); Assert.IsTrue(appServer.Start()); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2012); using (var socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.Connect(serverAddress); var socketStream = new NetworkStream(socket); using (var reader = new StreamReader(socketStream, m_Encoding, true)) using (var writer = new StreamWriter(socketStream, m_Encoding, 1024 * 8)) { var welcomeString = reader.ReadLine(); Assert.AreEqual(string.Format(TestSession.WelcomeMessageFormat, appServer.Name), welcomeString); var chars = new char[] { 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H' }; var rd = new Random(1); var sb = new StringBuilder(); for (var i = 0; i < 100; i++) { sb.Append(chars[rd.Next(0, chars.Length - 1)]); var command = sb.ToString(); writer.Write("ECHO " + command); writer.Write("##"); writer.Flush(); var echoMessage = reader.ReadLine(); Console.WriteLine("C:" + echoMessage); Assert.AreEqual(command, echoMessage); } } } } }
//[Test, Repeat(3)] //public void TestCommandParameterParser() //{ // var server = new TestServer(new TestCommandParser(), new TestCommandParameterParser()); // server.Setup(m_Config); // try // { // server.Start(); // EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port); // using (Socket socket = CreateClientSocket()) // { // string[] arrParam = new string[] { "A1", "A2", "A4", "B2", "A6", "E5" }; // socket.SendTo(m_Encoding.GetBytes("PARA:" + string.Join(",", arrParam) + Environment.NewLine), serverAddress); // List<string> received = new List<string>(); // foreach (var p in arrParam) // { // string r = m_Encoding.GetString(ReceiveMessage(socket, serverAddress).ToArray()); // Console.WriteLine("C: " + r); // received.Add(r); // } // Assert.AreEqual(arrParam, received); // } // } // catch (Exception e) // { // throw e; // } // finally // { // if (server.IsRunning) // server.Stop(); // } //} private bool TestMaxConnectionNumber(int maxConnectionNumber) { var server = new TestServer(); var defaultConfig = DefaultServerConfig; var config = new ServerConfig { Ip = defaultConfig.Ip, LogCommand = defaultConfig.LogCommand, MaxConnectionNumber = maxConnectionNumber, Mode = defaultConfig.Mode, Name = defaultConfig.Name, Port = defaultConfig.Port }; server.Setup(m_RootConfig, config, SocketServerFactory.Instance); List <Socket> sockets = new List <Socket>(); try { server.Start(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port); for (int i = 0; i < maxConnectionNumber; i++) { Socket socket = CreateClientSocket(); socket.SendTo(m_Encoding.GetBytes(Guid.NewGuid().ToString() + Environment.NewLine), serverAddress); Console.WriteLine("C: " + m_Encoding.GetString(ReceiveMessage(socket, serverAddress).ToArray())); sockets.Add(socket); } using (Socket trySocket = CreateClientSocket()) { trySocket.SendTo(m_Encoding.GetBytes(Guid.NewGuid().ToString() + Environment.NewLine), serverAddress); Thread thread = new Thread(new ThreadStart(() => { Console.WriteLine("C: " + m_Encoding.GetString(ReceiveMessage(trySocket, serverAddress).ToArray())); })); thread.Start(); if (thread.Join(500)) { //Assert.Fail("Current connection number: {0}, max connectionnumber: {1}", maxConnectionNumber + 1, maxConnectionNumber); return(false); } else { thread.Abort(); return(true); } } } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); return(false); } finally { server.Stop(); } }
private void StartServer() { m_Server.Start(); Console.WriteLine("The UDP Socket server is started!"); }
private bool TestMaxConnectionNumber(int maxConnectionNumber) { var server = new TestServer(); var defaultConfig = DefaultServerConfig; var config = new ServerConfig { Ip = defaultConfig.Ip, LogCommand = defaultConfig.LogCommand, MaxConnectionNumber = maxConnectionNumber, Mode = defaultConfig.Mode, Name = defaultConfig.Name, Port = defaultConfig.Port, Security = defaultConfig.Security, Certificate = defaultConfig.Certificate }; server.Setup(m_RootConfig, config, SocketServerFactory.Instance); List <Socket> sockets = new List <Socket>(); try { server.Start(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port); for (int i = 0; i < maxConnectionNumber; i++) { Socket socket = CreateClientSocket(); socket.Connect(serverAddress); Stream socketStream = GetSocketStream(socket); StreamReader reader = new StreamReader(socketStream, m_Encoding, true); reader.ReadLine(); sockets.Add(socket); } try { using (Socket trySocket = CreateClientSocket()) { trySocket.Connect(serverAddress); var innerSocketStream = new NetworkStream(trySocket); innerSocketStream.ReadTimeout = 500; using (StreamReader tryReader = new StreamReader(innerSocketStream, m_Encoding, true)) { string welcome = tryReader.ReadLine(); Console.WriteLine(welcome); return(true); } } } catch (Exception) { return(true); } } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); return(false); } finally { server.Stop(); } }
private void TestMaxConnectionNumber(int maxConnectionNumber) { var server = new TestServer(); var defaultConfig = DefaultServerConfig; var config = new ServerConfig { Ip = defaultConfig.Ip, LogCommand = defaultConfig.LogCommand, MaxConnectionNumber = maxConnectionNumber, Mode = defaultConfig.Mode, Name = defaultConfig.Name, Port = defaultConfig.Port }; server.Setup(string.Empty, config, string.Empty); List <Socket> sockets = new List <Socket>(); try { server.Start(); EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port); for (int i = 0; i < maxConnectionNumber; i++) { Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Connect(serverAddress); Stream socketStream = new NetworkStream(socket); StreamReader reader = new StreamReader(socketStream, Encoding.Default, true); reader.ReadLine(); sockets.Add(socket); } using (Socket trySocket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { Console.WriteLine("Start to connect try socket"); trySocket.Connect(serverAddress); var innerSocketStream = new NetworkStream(trySocket); innerSocketStream.ReadTimeout = 500; using (StreamReader tryReader = new StreamReader(innerSocketStream, Encoding.Default, true)) { Assert.Throws <IOException>(delegate { string welcome = tryReader.ReadLine(); Console.WriteLine(welcome); }); } } } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); } finally { sockets.ForEach(s => { s.Shutdown(SocketShutdown.Both); s.Close(); }); server.Stop(); } }