Fix compilation of System.Configuration test suite.
[mono.git] / mcs / class / System / Test / System.Net.Mail / SmtpServer.cs
1 //
2 // SmtpServer.cs - Dummy SMTP server used to test SmtpClient
3 //
4 // Author:
5 //   Raja R Harinath <harinath@hurrynot.org>
6 //
7
8 using System;
9 using System.Diagnostics;
10 using System.IO;
11 using System.Net;
12 using System.Net.Mail;
13 using System.Net.Sockets;
14 using System.Text;
15 using System.Threading;
16
17 namespace MonoTests.System.Net.Mail {
18         public class SmtpServer
19         {
20                 public string mail_from, rcpt_to;
21                 public StringBuilder data;
22
23                 TcpListener server;
24                 public IPEndPoint EndPoint {
25                         get { return (IPEndPoint) server.LocalEndpoint; }
26                 }
27
28                 public SmtpServer ()
29                 {
30                         server = new TcpListener (0);
31                         server.Start (1);
32                 }
33
34                 private static void WriteNS (NetworkStream ns, string s)
35                 {
36                         Trace ("response", s);
37                         byte [] bytes = Encoding.ASCII.GetBytes (s);
38                         ns.Write (bytes, 0, bytes.Length);
39                 }
40
41                 public void Run ()
42                 {
43                         string s;
44                         using (TcpClient client = server.AcceptTcpClient ()) {
45                                 Trace ("connection", EndPoint.Port);
46                                 using (NetworkStream ns = client.GetStream ()) {
47                                         WriteNS (ns, "220 localhost\r\n");
48                                         using (StreamReader r = new StreamReader (ns, Encoding.UTF8)) {
49                                                 while ((s = r.ReadLine ()) != null && Dispatch (ns, r, s))
50                                                         ;
51                                         }
52                                 }
53                         }
54                 }
55
56                 // return false == terminate
57                 public bool Dispatch (NetworkStream ns, StreamReader r, string s)
58                 {
59                         Trace ("command", s);
60                         if (s.Length < 4) {
61                                 WriteNS (ns, "502 Huh\r\n");
62                                 return false;
63                         }
64
65                         bool retval = true;
66                         switch (s.Substring (0, 4)) {
67                         case "HELO":
68                                 break;
69                         case "QUIT":
70                                 WriteNS (ns, "221 Quit\r\n");
71                                 return false;
72                         case "MAIL":
73                                 mail_from = s.Substring (10);
74                                 break;
75                         case "RCPT":
76                                 rcpt_to = s.Substring (8);
77                                 break;
78                         case "DATA":
79                                 WriteNS (ns, "354 Continue\r\n");
80                                 data = new StringBuilder ();
81                                 while ((s = r.ReadLine ()) != null) {
82                                         if (s == ".")
83                                                 break;
84                                         data.AppendLine (s);
85                                 }
86                                 Trace ("end of data", s);
87                                 retval = (s != null);
88                                 break;
89                         default:
90                                 WriteNS (ns, "502 Huh\r\n");
91                                 return true;
92                         }
93
94                         WriteNS (ns, "250 OK\r\n");
95                         return retval;
96                 }
97
98                 [Conditional ("TEST")]
99                 static void Trace (string key, object value)
100                 {
101                         Console.Error.WriteLine ("{0}: {1}", key, value);
102                 }
103
104 #if TEST
105                 static void DoTest (SmtpServer s, SmtpClient c, MailMessage m)
106                 {
107                         Thread t = new Thread (s.Run);
108                         t.Start ();
109                         c.Send (m);
110                         t.Join ();
111
112                         Console.WriteLine ("Message From: {0}", m.From);
113                         Console.WriteLine ("Message Sender: {0}", m.Sender);
114                         Console.WriteLine ("Mail From: {0}", s.mail_from);
115                         Console.WriteLine ("Rcpt To: {0}", s.rcpt_to);
116                         Console.WriteLine ("-------------------------------------");
117                         Console.Write (s.data);
118                         Console.WriteLine ("-------------------------------------");
119                 }
120
121                 static void Main ()
122                 {
123                         var server = new SmtpServer ();
124                         var client = new SmtpClient ("localhost", server.EndPoint.Port);
125                         var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo");
126
127                         DoTest (server, client, msg);
128
129                         msg.Sender = new MailAddress ("baz@example.com");
130
131                         DoTest (server, client, msg);
132                 }
133 #endif
134         }
135 }