public void TestLongLineInIMAPConversation() { // hMailServer is kind and allows up to 1000000 octets. StringBuilder sb = new StringBuilder(); for (int i = 0; i < 100000; i++) { sb.Append("1234567890"); } sb.Append(".com\r\n"); string command = "A03 NOOP " + sb.ToString(); TCPSocket socket = new TCPSocket(); Assert.IsTrue(socket.Connect(143)); socket.Receive(); socket.Send(command); try { string response = socket.Receive(); Assert.IsTrue(response.StartsWith("* BYE")); } catch (System.Net.Sockets.SocketException ex) { Assert.IsTrue(ex.ErrorCode == 10054); } socket.Disconnect(); }
public void TestLongLineInPOP3Conversation() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 400; i++) { sb.Append("1234567890"); } sb.Append(".com"); string command = "NOOP " + sb.ToString(); TCPSocket socket = new TCPSocket(); Assert.IsTrue(socket.Connect(110)); socket.Receive(); socket.Send(command + "\r\n"); string response = socket.Receive(); Assert.IsFalse(response.StartsWith("+OK")); socket.Disconnect(); }
public void TestLongSMTPCommand() { TCPSocket socket = new TCPSocket(); socket.Connect(25); // Build a large string... StringBuilder sb = new StringBuilder(); sb.Append("A01"); for (int i = 0; i < 1000000; i++) { sb.Append("01234567890"); } int length = sb.Length; sb.Append(Environment.NewLine); for (int i = 0; i < 100; i++) { try { socket.Send(sb.ToString()); } catch (Exception) { return; } } socket.Send("\r\n"); string result = socket.Receive(); socket.Disconnect(); }
public void TestSMTPServerAuthPlain() { _settings.AllowSMTPAuthPlain = true; TCPSocket sock = new TCPSocket(); sock.Connect(25); Assert.IsTrue(sock.Receive().StartsWith("220")); sock.Send("EHLO test.com\r\n"); Assert.IsTrue(sock.Receive().StartsWith("250")); sock.Send("AUTH PLAIN\r\n"); Assert.IsTrue(sock.Receive().StartsWith("334")); string str = "\t" + GetUsername() + "\t" + GetPassword(); sock.Send(EncodeBase64(str) + "\r\n"); Assert.IsTrue(sock.Receive().StartsWith("535")); EnsureNoPassword(); }
public void TestLongSMTPDataSessionIncludingNewline() { long memoryUsage = Shared.GetCurrentMemoryUsage(); _application.Settings.MaxMessageSize = 0; TCPSocket socket = new TCPSocket(); socket.Connect(25); socket.Receive(); socket.Send("HELO test.com\r\n"); socket.Receive(); // Build a large string... StringBuilder sb = new StringBuilder(); sb.Append("A01"); for (int i = 0; i < 10000; i++) { sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890\r\n"); } int length = sb.Length; sb.Append(Environment.NewLine); socket.Send("MAIL FROM: [email protected]\r\n"); socket.Receive(); socket.Send("RCPT TO: [email protected]\r\n"); socket.Receive(); socket.Send("DATA\r\n"); socket.Receive(); for (int i = 0; i < 100; i++) { Shared.AssertLowMemoryUsage(memoryUsage + 30); try { socket.Send(sb.ToString()); } catch (Exception) { return; } } socket.Send("\r\n.\r\n"); string result = socket.Receive(); Assert.IsTrue(result.StartsWith("250")); socket.Send("QUIT\r\n"); result = socket.Receive(); socket.Disconnect(); POP3Simulator.AssertMessageCount(_domain.Accounts[0].Address, "test", 1); }
public void TestPOP3DownloadOfLargeMessage() { TestLongSMTPDataSessionIncludingNewline(); POP3Simulator.AssertMessageCount("*****@*****.**", "test", 1); TCPSocket socket = new TCPSocket(); socket.Connect(110); // Receive welcome message. string sData = socket.Receive(); socket.Send("USER " + "*****@*****.**" + "\r\n"); sData = socket.ReadUntil("+OK"); socket.Send("PASS " + "test" + "\r\n"); sData = socket.ReadUntil("+OK"); socket.Send("RETR 1\r\n"); string sRetVal = ""; while (sRetVal.IndexOf("\r\n.\r\n") < 0) { if (sRetVal.IndexOf("-ERR no such message") >= 0) { socket.Disconnect(); Assert.Fail("Nope"); } sRetVal = socket.Receive(); } socket.Send("DELE 1\r\n"); sData = socket.ReadUntil("+OK"); socket.Send("QUIT\r\n"); sData = socket.ReadUntil("+OK"); socket.Disconnect(); }
public void TestSMTPServerAuthLoginUsernameAsThirdParameter() { _settings.AllowSMTPAuthPlain = true; TCPSocket sock = new TCPSocket(); sock.Connect(25); Assert.IsTrue(sock.Receive().StartsWith("220")); sock.Send("EHLO test.com\r\n"); Assert.IsTrue(sock.Receive().StartsWith("250")); string base64EncodedUsername = EncodeBase64(GetUsername()); sock.Send("AUTH LOGIN " + base64EncodedUsername + "\r\n"); Assert.IsTrue(sock.Receive().StartsWith("334")); sock.Send(EncodeBase64(GetPassword()) + "\r\n"); Assert.IsTrue(sock.Receive().StartsWith("535")); EnsureNoPassword(); }
public void TestLongSMTPDataSessionWithoutNewline() { long memoryUsage = Shared.GetCurrentMemoryUsage(); TCPSocket socket = new TCPSocket(); socket.Connect(25); socket.Receive(); socket.Send("HELO test.com\r\n"); socket.Receive(); // Build a large string... StringBuilder sb = new StringBuilder(); sb.Append("A01"); for (int i = 0; i < 10000; i++) { sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890"); } int length = sb.Length; socket.Send("MAIL FROM: [email protected]\r\n"); socket.Receive(); socket.Send("RCPT TO: [email protected]\r\n"); socket.Receive(); socket.Send("DATA\r\n"); socket.Receive(); for (int i = 0; i < 1000; i++) { Shared.AssertLowMemoryUsage(memoryUsage + 30); try { socket.Send(sb.ToString()); } catch (Exception) { return; } } socket.Send("\r\n.\r\n"); string result = socket.Receive(); Assert.IsTrue(result.StartsWith("554 Too long line was received. Transmission aborted.")); socket.Send("QUIT\r\n"); result = socket.Receive(); socket.Disconnect(); }
public void AuthShouldUpdateLastLogonTime() { hMailServer.Account account = SingletonProvider<Utilities>.Instance.AddAccount(_domain, "*****@*****.**", "test"); DateTime lastLogonTimeBefore = Convert.ToDateTime(account.LastLogonTime); System.Threading.Thread.Sleep(1000); TCPSocket sock = new TCPSocket(); sock.Connect(25); Assert.IsTrue(sock.Receive().StartsWith("220")); sock.Send("EHLO test.com\r\n"); Assert.IsTrue(sock.Receive().StartsWith("250")); string base64EncodedUsername = EncodeBase64("*****@*****.**"); sock.Send("AUTH LOGIN " + base64EncodedUsername + "\r\n"); Assert.IsTrue(sock.Receive().StartsWith("334")); sock.Send(EncodeBase64("test") + "\r\n"); Assert.IsTrue(sock.Receive().StartsWith("235")); DateTime lastLogonTimeAfter = Convert.ToDateTime(SingletonProvider<Utilities>.Instance.GetApp().Domains[0].Accounts[0].LastLogonTime); Assert.AreNotEqual(lastLogonTimeBefore, lastLogonTimeAfter); }
private static byte[] SendMessageToSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack, ProtocolType protocol, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate heartbeatCallback, bool isSynchronious, bool useSocketPool) { Socket TCPSocket = null; if (useSocketPool) { TCPSocket = CheckIfSocketAlreadyOpen(source, destination); } HeartbeatInfo info = null; try { if (TCPSocket == null) { if (protocol == ProtocolType.Udp) { TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, protocol); } else if (protocol == ProtocolType.Tcp) { TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocol); } TCPSocket.Bind(source); if (protocol == ProtocolType.Tcp) { TCPSocket.Connect(destination); if (useSocketPool) { info = new HeartbeatInfo(TCPSocket, heartbeatMessage, responseFormat, heartbeatCallback, 10); info.CallbackDelegate += new BytesReceivedDelegate(CheckIfHeartbeatFailed); lock (openSocketsList) { openSocketsList.Add(info); } } } } // lock (TCPSocket) { if (bufferToSend != null) { if (protocol == ProtocolType.Tcp) { TCPSocket.Send(bufferToSend); } else if (protocol == ProtocolType.Udp) { TCPSocket.SendTo(bufferToSend, destination); } } StateObject state = new StateObject(); if (isSynchronious) { TCPSocket.ReceiveTimeout = 5000; TCPSocket.Receive(state.buffer); if (!useSocketPool) { TCPSocket.Shutdown(SocketShutdown.Both); TCPSocket.Close(); } return(state.buffer); } else if (callBack != null) { state.workSocket = TCPSocket; state.callbackDelegate += callBack; if (info != null) { state.callbackDelegate += info.ProcessMessageReceivedFromSocket; } TCPSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } // } return(null); } catch (Exception ex) { LoggingHelper.LogExceptionInApplicationLog(ex.Source, ex, EventLogEntryType.Error); LoggingHelper.WriteExceptionLogEntry(ex.Source, ex); if (TCPSocket != null && TCPSocket.Connected) { TCPSocket.Shutdown(SocketShutdown.Both); TCPSocket.Close(); } } return(null); }
public void TestEHLOKeywords() { hMailServer.Application application = SingletonProvider<Utilities>.Instance.GetApp(); hMailServer.Settings settings = _settings; settings.HostName = "examplify.com"; TCPSocket socket = new TCPSocket(); Assert.IsTrue(socket.Connect(25)); string result = socket.Receive(); socket.Send("EHLO example.com\r\n"); result = socket.Receive(); socket.Disconnect(); Assert.IsTrue(result.Contains("250-" + settings.HostName)); }
private void onReceive() { //接受数据的函数 FuncReceive receive = delegate(byte[] buffer, int len) { if (DATA_MAX_LENGTH < len) { len = DATA_MAX_LENGTH; } int ret = TCPSocket.Receive(buffer, len); return(ret); }; //状态切换函数 System.Action switch_idle = delegate() { status = RecvState.idle; receiveLen = 0; }; System.Action switch_head = delegate() { status = RecvState.head; receiveLen = LENGTH_HEAD; }; System.Action switch_die = delegate() { status = RecvState.die; receiveLen = 0; }; System.Action <int> switch_body = delegate(int len) { status = RecvState.body; receiveLen = len; }; //状态操作函数 System.Action idle_func = delegate() { switch_head(); }; //接收包头 System.Action head_func = delegate() { byte[] buffer = new byte[receiveLen];; int ret = receive(buffer, receiveLen); if (-1 == ret) { switch_die(); return; } if (ret <= 0) { return; } int bodyLen = Function.Byte4ToInt(buffer); if (DATA_MAX_LENGTH < bodyLen) { Debug.LogError("Client.onReceive body data is too long!"); } switch_body(bodyLen); }; //接受数据 System.Action body_func = delegate() { byte[] buffer = new byte[receiveLen]; int ret = receive(buffer, receiveLen); if (-1 == ret) { switch_die(); return; } if (ret <= 0) { return; } if (null != lastBuffer && 0 < lastBuffer.Length) { //需要拼接数据 lastBuffer = Function.MergeArray <byte>(lastBuffer, buffer); } else { lastBuffer = buffer; } if (ret < receiveLen) { //数据没有接收完 switch_body(receiveLen - ret); } else { //数据接收完成 try { receiveData(lastBuffer); } finally { lastBuffer = null; switch_idle(); } } }; System.Action die_func = delegate() { //pass }; //start switch (status) { case RecvState.idle: idle_func(); break; case RecvState.head: head_func(); break; case RecvState.body: body_func(); break; case RecvState.die: head_func(); break; default: idle_func(); break; } }