| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- //
- // SmtpClientTest.cs - NUnit Test Cases for System.Net.Mail.SmtpClient
- //
- // Authors:
- // John Luke ([email protected])
- //
- // (C) 2006 John Luke
- //
- using NUnit.Framework;
- using System;
- using System.IO;
- using System.Net.Mail;
- using System.Net.Mime;
- using System.Threading;
- namespace MonoTests.System.Net.Mail
- {
- [TestFixture]
- public class SmtpClientTest
- {
- SmtpClient _smtp;
- SmtpClient smtp { get { return _smtp ?? (_smtp = new SmtpClient ()); } }
- string tempFolder;
-
- [SetUp]
- public void GetReady ()
- {
- tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
- if (Directory.Exists (tempFolder))
- Directory.Delete (tempFolder, true);
- Directory.CreateDirectory (tempFolder);
- }
- [TearDown]
- public void TearDown ()
- {
- if (Directory.Exists (tempFolder))
- Directory.Delete (tempFolder, true);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Credentials_Default ()
- {
- Assert.IsNull (smtp.Credentials);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void DeliveryMethod ()
- {
- Assert.AreEqual (SmtpDeliveryMethod.Network, smtp.DeliveryMethod, "#1");
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- Assert.AreEqual (SmtpDeliveryMethod.SpecifiedPickupDirectory,
- smtp.DeliveryMethod, "#2");
- smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
- Assert.AreEqual (SmtpDeliveryMethod.PickupDirectoryFromIis,
- smtp.DeliveryMethod, "#3");
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- Assert.AreEqual (SmtpDeliveryMethod.Network,
- smtp.DeliveryMethod, "#4");
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void EnableSsl ()
- {
- Assert.IsFalse (smtp.EnableSsl, "#1");
- smtp.EnableSsl = true;
- Assert.IsTrue (smtp.EnableSsl, "#2");
- smtp.EnableSsl = false;
- Assert.IsFalse (smtp.EnableSsl, "#3");
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Host ()
- {
- smtp.Host = "127.0.0.1";
- Assert.AreEqual ("127.0.0.1", smtp.Host, "#2");
- smtp.Host = "smtp.ximian.com";
- Assert.AreEqual ("smtp.ximian.com", smtp.Host, "#3");
- }
- [Test]
- [Category ("NotWorking")]
- public void Host_Default ()
- {
- Assert.IsNull (smtp.Host);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Host_Value_Null ()
- {
- try {
- smtp.Host = null;
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("value", ex.ParamName, "#5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Host_Value_Empty ()
- {
- try {
- smtp.Host = String.Empty;
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // This property cannot be set to an empty string
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("value", ex.ParamName, "#5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void PickupDirectoryLocation ()
- {
- Assert.IsNull (smtp.PickupDirectoryLocation, "#1");
- smtp.PickupDirectoryLocation = tempFolder;
- Assert.AreSame (tempFolder, smtp.PickupDirectoryLocation, "#2");
- smtp.PickupDirectoryLocation = "shouldnotexist";
- Assert.AreEqual ("shouldnotexist", smtp.PickupDirectoryLocation, "#3");
- smtp.PickupDirectoryLocation = null;
- Assert.IsNull (smtp.PickupDirectoryLocation, "#4");
- smtp.PickupDirectoryLocation = string.Empty;
- Assert.AreEqual (string.Empty, smtp.PickupDirectoryLocation, "#5");
- smtp.PickupDirectoryLocation = "\0";
- Assert.AreEqual ("\0", smtp.PickupDirectoryLocation, "#6");
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Port ()
- {
- Assert.AreEqual (25, smtp.Port, "#1");
- smtp.Port = 1;
- Assert.AreEqual (1, smtp.Port, "#2");
- smtp.Port = int.MaxValue;
- Assert.AreEqual (int.MaxValue, smtp.Port, "#3");
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Port_Value_Invalid ()
- {
- // zero
- try {
- smtp.Port = 0;
- Assert.Fail ("#A1");
- } catch (ArgumentOutOfRangeException ex) {
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.AreEqual ("value", ex.ParamName, "#A5");
- }
- // negative
- try {
- smtp.Port = -1;
- Assert.Fail ("#B1");
- } catch (ArgumentOutOfRangeException ex) {
- Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("value", ex.ParamName, "#B5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_Message_Null ()
- {
- try {
- smtp.Send ((MailMessage) null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.AreEqual ("message", ex.ParamName, "#B5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_Network_Host_Null ()
- {
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (InvalidOperationException ex) {
- // The SMTP host was not specified
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_Network_Host_Whitespace ()
- {
- smtp.Host = " \r\n ";
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (InvalidOperationException ex) {
- // The SMTP host was not specified
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_SpecifiedPickupDirectory ()
- {
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- smtp.PickupDirectoryLocation = tempFolder;
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- string [] files = Directory.GetFiles (tempFolder, "*");
- Assert.AreEqual (1, files.Length, "#1");
- Assert.AreEqual (".eml", Path.GetExtension (files [0]), "#2");
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_DirectoryNotFound ()
- {
- Directory.Delete (tempFolder);
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- smtp.PickupDirectoryLocation = tempFolder;
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (SmtpException ex) {
- // Failure sending email
- Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
- Assert.IsNotNull (ex.InnerException, "#3");
- Assert.AreEqual (typeof (DirectoryNotFoundException), ex.InnerException.GetType (), "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
- // Could not find a part of the path '...'
- DirectoryNotFoundException inner = (DirectoryNotFoundException) ex.InnerException;
- Assert.IsNull (inner.InnerException, "#7");
- Assert.IsNotNull (inner.Message, "#8");
- Assert.IsTrue (inner.Message.IndexOf (tempFolder) != -1, "#9");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Empty ()
- {
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- smtp.PickupDirectoryLocation = string.Empty;
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (SmtpException ex) {
- // Only absolute directories are allowed for
- // pickup directory
- Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_IllegalChars ()
- {
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- smtp.PickupDirectoryLocation = "\0abc";
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (SmtpException ex) {
- // Failure sending email
- Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
- Assert.IsNotNull (ex.InnerException, "#3");
- Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
- // Illegal characters in path
- ArgumentException inner = (ArgumentException) ex.InnerException;
- Assert.IsNull (inner.InnerException, "#7");
- Assert.IsNotNull (inner.Message, "#8");
- Assert.IsNull (inner.ParamName, "#9");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_NotAbsolute ()
- {
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- smtp.PickupDirectoryLocation = "relative";
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (SmtpException ex) {
- // Only absolute directories are allowed for
- // pickup directory
- Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Null ()
- {
- smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
- try {
- smtp.Send ("[email protected]", "[email protected]",
- "introduction", "hello");
- Assert.Fail ("#1");
- } catch (SmtpException ex) {
- // Only absolute directories are allowed for
- // pickup directory
- Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
- }
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Timeout ()
- {
- Assert.AreEqual (100000, smtp.Timeout, "#1");
- smtp.Timeout = 50;
- Assert.AreEqual (50, smtp.Timeout, "#2");
- smtp.Timeout = 0;
- Assert.AreEqual (0, smtp.Timeout, "#3");
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #else
- [ExpectedException (typeof (ArgumentOutOfRangeException))]
- #endif
- public void Timeout_Value_Negative ()
- {
- smtp.Timeout = -1;
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void UseDefaultCredentials_Default ()
- {
- Assert.IsFalse (smtp.UseDefaultCredentials);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Deliver ()
- {
- var server = new SmtpServer ();
- var client = new SmtpClient ("localhost", server.EndPoint.Port);
- var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo\r\n");
- Thread t = new Thread (server.Run);
- t.Start ();
- client.Send (msg);
- t.Join ();
- Assert.AreEqual ("<[email protected]>", server.mail_from);
- Assert.AreEqual ("<[email protected]>", server.rcpt_to);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Deliver_Envelope ()
- {
- var server = new SmtpServer ();
- var client = new SmtpClient ("localhost", server.EndPoint.Port);
- var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo\r\n");
- msg.Sender = new MailAddress ("[email protected]");
- Thread t = new Thread (server.Run);
- t.Start ();
- client.Send (msg);
- t.Join ();
- Assert.AreEqual ("<[email protected]>", server.mail_from);
- Assert.AreEqual ("<[email protected]>", server.rcpt_to);
- }
- [Test]
- #if FEATURE_NO_BSD_SOCKETS
- [ExpectedException (typeof (PlatformNotSupportedException))]
- #endif
- public void Deliver_Async ()
- {
- // SmtpClient uses BackgroundWorker and listens for the RunWorkerCompleted
- // to mark an async task as completed. The problem is that BackgroundWorker uses
- // System.ComponentModel.AsyncOperationManager to get the synchronization
- // context, and in monotouch that returns by default a synchronization
- // context for the main thread. Since tests are also run on the main thread,
- // we'll block the main thread while waiting for the async send to complete,
- // while the async completion is waiting for the main thread to process it.
- // So instead use a SynchronizationContext that uses the threadpool instead
- // of the main thread.
- var existing_context = global::System.ComponentModel.AsyncOperationManager.SynchronizationContext;
- global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = new ThreadPoolSynchronizationContext ();
- try {
- var server = new SmtpServer ();
- var client = new SmtpClient ("localhost", server.EndPoint.Port);
- var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo\r\n");
- Thread t = new Thread (server.Run);
- t.Start ();
- var task = client.SendMailAsync (msg);
- t.Join ();
- Assert.AreEqual ("<[email protected]>", server.mail_from);
- Assert.AreEqual ("<[email protected]>", server.rcpt_to);
- Assert.IsTrue (task.Wait (1000));
- Assert.IsTrue (task.IsCompleted, "task");
- } finally {
- global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = existing_context;
- }
- }
- internal class ThreadPoolSynchronizationContext : SynchronizationContext
- {
- public override void Post (SendOrPostCallback d, object state)
- {
- ThreadPool.QueueUserWorkItem ((v) => d (state));
- }
- public override void Send (SendOrPostCallback d, object state)
- {
- d (state);
- }
- }
- }
- }
|