Add a dummy SmtpServer to talk to our SmtpClient in unit tests
authorRaja R Harinath <harinath@hurrynot.org>
Wed, 4 Aug 2010 02:40:08 +0000 (08:10 +0530)
committerRaja R Harinath <harinath@hurrynot.org>
Wed, 4 Aug 2010 04:27:08 +0000 (09:57 +0530)
* SmtpServer.cs: New.
* SmtpClientTest.cs (Deliver): New.  Use it.

mcs/class/System/System_test.dll.sources
mcs/class/System/Test/System.Net.Mail/SmtpClientTest.cs
mcs/class/System/Test/System.Net.Mail/SmtpServer.cs [new file with mode: 0644]

index c383fe9aa5be382ee25e6717c22af9e86b2b76f8..1d86f9dfb86e3cdd457f6d7baa22c94354a5c628 100644 (file)
@@ -250,6 +250,7 @@ System.Net.Mail/SmtpClientTest.cs
 System.Net.Mail/SmtpExceptionTest.cs
 System.Net.Mail/SmtpPermissionTest.cs
 System.Net.Mail/SmtpPermissionAttributeTest.cs
+System.Net.Mail/SmtpServer.cs
 System.Net.Mime/ContentDispositionTest.cs
 System.Net.Mime/ContentTypeTest.cs
 System.Net.NetworkInformation/PhysicalAddressTest.cs
index e1f7fe013354254c09ff62c4bf1a81ff0b6a57bf..f01686794194b0420b6c0a5b515029a02a906c6b 100644 (file)
@@ -12,6 +12,7 @@ using System;
 using System.IO;
 using System.Net.Mail;
 using System.Net.Mime;
+using System.Threading;
 
 namespace MonoTests.System.Net.Mail
 {
@@ -354,6 +355,22 @@ namespace MonoTests.System.Net.Mail
                {
                        Assert.IsFalse (smtp.UseDefaultCredentials);
                }
+
+               [Test]
+               public void Deliver ()
+               {
+                       var server = new SmtpServer ();
+                       var client = new SmtpClient ("localhost", server.EndPoint.Port);
+                       var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
+
+                       Thread t = new Thread (server.Run);
+                       t.Start ();
+                       client.Send (msg);
+                       t.Join ();
+
+                       Assert.AreEqual ("<foo@example.com>", server.mail_from);
+                       Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
+               }
        }
 }
 #endif
diff --git a/mcs/class/System/Test/System.Net.Mail/SmtpServer.cs b/mcs/class/System/Test/System.Net.Mail/SmtpServer.cs
new file mode 100644 (file)
index 0000000..48f6943
--- /dev/null
@@ -0,0 +1,135 @@
+//
+// SmtpServer.cs - Dummy SMTP server used to test SmtpClient
+//
+// Author:
+//   Raja R Harinath <harinath@hurrynot.org>
+//
+
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Net;
+using System.Net.Mail;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+
+namespace MonoTests.System.Net.Mail {
+       public class SmtpServer
+       {
+               public string mail_from, rcpt_to;
+               public StringBuilder data;
+
+               TcpListener server;
+               public IPEndPoint EndPoint {
+                       get { return (IPEndPoint) server.LocalEndpoint; }
+               }
+
+               public SmtpServer ()
+               {
+                       server = new TcpListener (0);
+                       server.Start (1);
+               }
+
+               private static void WriteNS (NetworkStream ns, string s)
+               {
+                       Trace ("response", s);
+                       byte [] bytes = Encoding.ASCII.GetBytes (s);
+                       ns.Write (bytes, 0, bytes.Length);
+               }
+
+               public void Run ()
+               {
+                       string s;
+                       using (TcpClient client = server.AcceptTcpClient ()) {
+                               Trace ("connection", EndPoint.Port);
+                               using (NetworkStream ns = client.GetStream ()) {
+                                       WriteNS (ns, "220 localhost\r\n");
+                                       using (StreamReader r = new StreamReader (ns, Encoding.UTF8)) {
+                                               while ((s = r.ReadLine ()) != null && Dispatch (ns, r, s))
+                                                       ;
+                                       }
+                               }
+                       }
+               }
+
+               // return false == terminate
+               public bool Dispatch (NetworkStream ns, StreamReader r, string s)
+               {
+                       Trace ("command", s);
+                       if (s.Length < 4) {
+                               WriteNS (ns, "502 Huh\r\n");
+                               return false;
+                       }
+
+                       bool retval = true;
+                       switch (s.Substring (0, 4)) {
+                       case "HELO":
+                               break;
+                       case "QUIT":
+                               WriteNS (ns, "221 Quit\r\n");
+                               return false;
+                       case "MAIL":
+                               mail_from = s.Substring (10);
+                               break;
+                       case "RCPT":
+                               rcpt_to = s.Substring (8);
+                               break;
+                       case "DATA":
+                               WriteNS (ns, "354 Continue\r\n");
+                               data = new StringBuilder ();
+                               while ((s = r.ReadLine ()) != null) {
+                                       if (s == ".")
+                                               break;
+                                       data.AppendLine (s);
+                               }
+                               Trace ("end of data", s);
+                               retval = (s != null);
+                               break;
+                       default:
+                               WriteNS (ns, "502 Huh\r\n");
+                               return true;
+                       }
+
+                       WriteNS (ns, "250 OK\r\n");
+                       return retval;
+               }
+
+               [Conditional ("TEST")]
+               static void Trace (string key, object value)
+               {
+                       Console.Error.WriteLine ("{0}: {1}", key, value);
+               }
+
+#if TEST
+               static void DoTest (SmtpServer s, SmtpClient c, MailMessage m)
+               {
+                       Thread t = new Thread (s.Run);
+                       t.Start ();
+                       c.Send (m);
+                       t.Join ();
+
+                       Console.WriteLine ("Message From: {0}", m.From);
+                       Console.WriteLine ("Message Sender: {0}", m.Sender);
+                       Console.WriteLine ("Mail From: {0}", s.mail_from);
+                       Console.WriteLine ("Rcpt To: {0}", s.rcpt_to);
+                       Console.WriteLine ("-------------------------------------");
+                       Console.Write (s.data);
+                       Console.WriteLine ("-------------------------------------");
+               }
+
+               static void Main ()
+               {
+                       var server = new SmtpServer ();
+                       var client = new SmtpClient ("localhost", server.EndPoint.Port);
+                       var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo");
+
+                       DoTest (server, client, msg);
+
+                       msg.Sender = new MailAddress ("baz@example.com");
+
+                       DoTest (server, client, msg);
+               }
+#endif
+       }
+}