Changed link from GUID to URL
[mono.git] / mcs / class / System / Test / System.Net.Mail / SmtpClientTest.cs
1 //
2 // SmtpClientTest.cs - NUnit Test Cases for System.Net.Mail.SmtpClient
3 //
4 // Authors:
5 //   John Luke (john.luke@gmail.com)
6 //
7 // (C) 2006 John Luke
8 //
9 using NUnit.Framework;
10 using System;
11 using System.IO;
12 using System.Net.Mail;
13 using System.Net.Mime;
14 using System.Threading;
15
16 namespace MonoTests.System.Net.Mail
17 {
18         [TestFixture]
19         public class SmtpClientTest
20         {
21                 SmtpClient smtp;
22                 string tempFolder;
23                 
24                 [SetUp]
25                 public void GetReady ()
26                 {
27                         smtp = new SmtpClient ();
28                         tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
29                         if (Directory.Exists (tempFolder))
30                                 Directory.Delete (tempFolder, true);
31                         Directory.CreateDirectory (tempFolder);
32                 }
33
34                 [TearDown]
35                 public void TearDown ()
36                 {
37                         if (Directory.Exists (tempFolder))
38                                 Directory.Delete (tempFolder, true);
39                 }
40
41                 [Test]
42                 public void Credentials_Default ()
43                 {
44                         Assert.IsNull (smtp.Credentials);
45                 }
46
47                 [Test]
48                 public void DeliveryMethod ()
49                 {
50                         Assert.AreEqual (SmtpDeliveryMethod.Network, smtp.DeliveryMethod, "#1");
51                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
52                         Assert.AreEqual (SmtpDeliveryMethod.SpecifiedPickupDirectory,
53                                 smtp.DeliveryMethod, "#2");
54                         smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
55                         Assert.AreEqual (SmtpDeliveryMethod.PickupDirectoryFromIis,
56                                 smtp.DeliveryMethod, "#3");
57                         smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
58                         Assert.AreEqual (SmtpDeliveryMethod.Network,
59                                 smtp.DeliveryMethod, "#4");
60                 }
61
62                 [Test]
63                 public void EnableSsl ()
64                 {
65                         Assert.IsFalse (smtp.EnableSsl, "#1");
66                         smtp.EnableSsl = true;
67                         Assert.IsTrue (smtp.EnableSsl, "#2");
68                         smtp.EnableSsl = false;
69                         Assert.IsFalse (smtp.EnableSsl, "#3");
70                 }
71
72                 [Test]
73                 public void Host ()
74                 {
75                         smtp.Host = "127.0.0.1";
76                         Assert.AreEqual ("127.0.0.1", smtp.Host, "#2");
77                         smtp.Host = "smtp.ximian.com";
78                         Assert.AreEqual ("smtp.ximian.com", smtp.Host, "#3");
79                 }
80
81                 [Test]
82                 [Category ("NotWorking")]
83                 public void Host_Default ()
84                 {
85                         Assert.IsNull (smtp.Host);
86                 }
87
88                 [Test]
89                 public void Host_Value_Null ()
90                 {
91                         try {
92                                 smtp.Host = null;
93                                 Assert.Fail ("#1");
94                         } catch (ArgumentNullException ex) {
95                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
96                                 Assert.IsNull (ex.InnerException, "#3");
97                                 Assert.IsNotNull (ex.Message, "#4");
98                                 Assert.AreEqual ("value", ex.ParamName, "#5");
99                         }
100                 }
101
102                 [Test]
103                 public void Host_Value_Empty ()
104                 {
105                         try {
106                                 smtp.Host = String.Empty;
107                                 Assert.Fail ("#1");
108                         } catch (ArgumentException ex) {
109                                 // This property cannot be set to an empty string
110                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
111                                 Assert.IsNull (ex.InnerException, "#3");
112                                 Assert.IsNotNull (ex.Message, "#4");
113                                 Assert.AreEqual ("value", ex.ParamName, "#5");
114                         }
115                 }
116
117                 [Test]
118                 public void PickupDirectoryLocation ()
119                 {
120                         Assert.IsNull (smtp.PickupDirectoryLocation, "#1");
121                         smtp.PickupDirectoryLocation = tempFolder;
122                         Assert.AreSame (tempFolder, smtp.PickupDirectoryLocation, "#2");
123                         smtp.PickupDirectoryLocation = "shouldnotexist";
124                         Assert.AreEqual ("shouldnotexist", smtp.PickupDirectoryLocation, "#3");
125                         smtp.PickupDirectoryLocation = null;
126                         Assert.IsNull (smtp.PickupDirectoryLocation, "#4");
127                         smtp.PickupDirectoryLocation = string.Empty;
128                         Assert.AreEqual (string.Empty, smtp.PickupDirectoryLocation, "#5");
129                         smtp.PickupDirectoryLocation = "\0";
130                         Assert.AreEqual ("\0", smtp.PickupDirectoryLocation, "#6");
131                 }
132
133                 [Test]
134                 public void Port ()
135                 {
136                         Assert.AreEqual (25, smtp.Port, "#1");
137                         smtp.Port = 1;
138                         Assert.AreEqual (1, smtp.Port, "#2");
139                         smtp.Port = int.MaxValue;
140                         Assert.AreEqual (int.MaxValue, smtp.Port, "#3");
141                 }
142
143                 [Test]
144                 public void Port_Value_Invalid ()
145                 {
146                         // zero
147                         try {
148                                 smtp.Port = 0;
149                                 Assert.Fail ("#A1");
150                         } catch (ArgumentOutOfRangeException ex) {
151                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
152                                 Assert.IsNull (ex.InnerException, "#A3");
153                                 Assert.IsNotNull (ex.Message, "#A4");
154                                 Assert.AreEqual ("value", ex.ParamName, "#A5");
155                         }
156
157                         // negative
158                         try {
159                                 smtp.Port = -1;
160                                 Assert.Fail ("#B1");
161                         } catch (ArgumentOutOfRangeException ex) {
162                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
163                                 Assert.IsNull (ex.InnerException, "#B3");
164                                 Assert.IsNotNull (ex.Message, "#B4");
165                                 Assert.AreEqual ("value", ex.ParamName, "#B5");
166                         }
167                 }
168
169                 [Test]
170                 public void Send_Message_Null ()
171                 {
172                         try {
173                                 smtp.Send ((MailMessage) null);
174                                 Assert.Fail ("#1");
175                         } catch (ArgumentNullException ex) {
176                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
177                                 Assert.IsNull (ex.InnerException, "#B3");
178                                 Assert.IsNotNull (ex.Message, "#B4");
179                                 Assert.AreEqual ("message", ex.ParamName, "#B5");
180                         }
181                 }
182
183                 [Test]
184                 public void Send_Network_Host_Null ()
185                 {
186                         try {
187                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
188                                         "introduction", "hello");
189                                 Assert.Fail ("#1");
190                         } catch (InvalidOperationException ex) {
191                                 // The SMTP host was not specified
192                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
193                                 Assert.IsNull (ex.InnerException, "#3");
194                                 Assert.IsNotNull (ex.Message, "#4");
195                         }
196                 }
197
198                 [Test]
199                 public void Send_Network_Host_Whitespace ()
200                 {
201                         smtp.Host = " \r\n ";
202                         try {
203                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
204                                         "introduction", "hello");
205                                 Assert.Fail ("#1");
206                         } catch (InvalidOperationException ex) {
207                                 // The SMTP host was not specified
208                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
209                                 Assert.IsNull (ex.InnerException, "#3");
210                                 Assert.IsNotNull (ex.Message, "#4");
211                         }
212                 }
213
214                 [Test]
215                 public void Send_SpecifiedPickupDirectory ()
216                 {
217                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
218                         smtp.PickupDirectoryLocation = tempFolder;
219                         smtp.Send ("mono@novell.com", "everyone@novell.com",
220                                 "introduction", "hello");
221
222                         string [] files = Directory.GetFiles (tempFolder, "*");
223                         Assert.AreEqual (1, files.Length, "#1");
224                         Assert.AreEqual (".eml", Path.GetExtension (files [0]), "#2");
225                 }
226
227                 [Test]
228                 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_DirectoryNotFound ()
229                 {
230                         Directory.Delete (tempFolder);
231
232                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
233                         smtp.PickupDirectoryLocation = tempFolder;
234                         try {
235                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
236                                         "introduction", "hello");
237                                 Assert.Fail ("#1");
238                         } catch (SmtpException ex) {
239                                 // Failure sending email
240                                 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
241                                 Assert.IsNotNull (ex.InnerException, "#3");
242                                 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.InnerException.GetType (), "#4");
243                                 Assert.IsNotNull (ex.Message, "#5");
244                                 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
245
246                                 // Could not find a part of the path '...'
247                                 DirectoryNotFoundException inner = (DirectoryNotFoundException) ex.InnerException;
248                                 Assert.IsNull (inner.InnerException, "#7");
249                                 Assert.IsNotNull (inner.Message, "#8");
250                                 Assert.IsTrue (inner.Message.IndexOf (tempFolder) != -1, "#9");
251                         }
252                 }
253
254                 [Test]
255                 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Empty ()
256                 {
257                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
258                         smtp.PickupDirectoryLocation = string.Empty;
259                         try {
260                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
261                                         "introduction", "hello");
262                                 Assert.Fail ("#1");
263                         } catch (SmtpException ex) {
264                                 // Only absolute directories are allowed for
265                                 // pickup directory
266                                 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
267                                 Assert.IsNull (ex.InnerException, "#3");
268                                 Assert.IsNotNull (ex.Message, "#4");
269                                 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
270                         }
271                 }
272
273                 [Test]
274                 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_IllegalChars ()
275                 {
276                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
277                         smtp.PickupDirectoryLocation = "\0abc";
278                         try {
279                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
280                                         "introduction", "hello");
281                                 Assert.Fail ("#1");
282                         } catch (SmtpException ex) {
283                                 // Failure sending email
284                                 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
285                                 Assert.IsNotNull (ex.InnerException, "#3");
286                                 Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#4");
287                                 Assert.IsNotNull (ex.Message, "#5");
288                                 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
289
290                                 // Illegal characters in path
291                                 ArgumentException inner = (ArgumentException) ex.InnerException;
292                                 Assert.IsNull (inner.InnerException, "#7");
293                                 Assert.IsNotNull (inner.Message, "#8");
294                                 Assert.IsNull (inner.ParamName, "#9");
295                         }
296                 }
297
298                 [Test]
299                 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_NotAbsolute ()
300                 {
301                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
302                         smtp.PickupDirectoryLocation = "relative";
303                         try {
304                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
305                                         "introduction", "hello");
306                                 Assert.Fail ("#1");
307                         } catch (SmtpException ex) {
308                                 // Only absolute directories are allowed for
309                                 // pickup directory
310                                 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
311                                 Assert.IsNull (ex.InnerException, "#3");
312                                 Assert.IsNotNull (ex.Message, "#4");
313                                 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
314                         }
315                 }
316
317                 [Test]
318                 public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Null ()
319                 {
320                         smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
321                         try {
322                                 smtp.Send ("mono@novell.com", "everyone@novell.com",
323                                         "introduction", "hello");
324                                 Assert.Fail ("#1");
325                         } catch (SmtpException ex) {
326                                 // Only absolute directories are allowed for
327                                 // pickup directory
328                                 Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
329                                 Assert.IsNull (ex.InnerException, "#3");
330                                 Assert.IsNotNull (ex.Message, "#4");
331                                 Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
332                         }
333                 }
334
335                 [Test]
336                 public void Timeout ()
337                 {
338                         Assert.AreEqual (100000, smtp.Timeout, "#1");
339                         smtp.Timeout = 50;
340                         Assert.AreEqual (50, smtp.Timeout, "#2");
341                         smtp.Timeout = 0;
342                         Assert.AreEqual (0, smtp.Timeout, "#3");
343                 }
344
345                 [Test]
346                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
347                 public void Timeout_Value_Negative ()
348                 {
349                         smtp.Timeout = -1;
350                 }
351
352                 [Test]
353                 public void UseDefaultCredentials_Default ()
354                 {
355                         Assert.IsFalse (smtp.UseDefaultCredentials);
356                 }
357
358                 [Test]
359                 public void Deliver ()
360                 {
361                         var server = new SmtpServer ();
362                         var client = new SmtpClient ("localhost", server.EndPoint.Port);
363                         var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
364
365                         Thread t = new Thread (server.Run);
366                         t.Start ();
367                         client.Send (msg);
368                         t.Join ();
369
370                         Assert.AreEqual ("<foo@example.com>", server.mail_from);
371                         Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
372                 }
373
374                 [Test]
375                 public void Deliver_Envelope ()
376                 {
377                         var server = new SmtpServer ();
378                         var client = new SmtpClient ("localhost", server.EndPoint.Port);
379                         var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
380
381                         msg.Sender = new MailAddress ("baz@example.com");
382
383                         Thread t = new Thread (server.Run);
384                         t.Start ();
385                         client.Send (msg);
386                         t.Join ();
387
388                         Assert.AreEqual ("<baz@example.com>", server.mail_from);
389                         Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
390                 }
391
392                 [Test]
393                 public void Deliver_Async ()
394                 {
395                         var server = new SmtpServer ();
396                         var client = new SmtpClient ("localhost", server.EndPoint.Port);
397                         var msg = new MailMessage ("foo@example.com", "bar@example.com", "hello", "howdydoo\r\n");
398
399                         Thread t = new Thread (server.Run);
400                         t.Start ();
401                         var task = client.SendMailAsync (msg);
402                         t.Join ();
403
404                         Assert.AreEqual ("<foo@example.com>", server.mail_from);
405                         Assert.AreEqual ("<bar@example.com>", server.rcpt_to);
406
407                         Assert.IsTrue (task.Wait (1000));
408                         Assert.IsTrue (task.IsCompleted, "task");
409                 }
410
411         }
412 }