SmtpClientTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. //
  2. // SmtpClientTest.cs - NUnit Test Cases for System.Net.Mail.SmtpClient
  3. //
  4. // Authors:
  5. // John Luke ([email protected])
  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. namespace MonoTests.System.Net.Mail
  16. {
  17. [TestFixture]
  18. public class SmtpClientTest
  19. {
  20. SmtpClient _smtp;
  21. SmtpClient smtp { get { return _smtp ?? (_smtp = new SmtpClient ()); } }
  22. string tempFolder;
  23. [SetUp]
  24. public void GetReady ()
  25. {
  26. tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
  27. if (Directory.Exists (tempFolder))
  28. Directory.Delete (tempFolder, true);
  29. Directory.CreateDirectory (tempFolder);
  30. }
  31. [TearDown]
  32. public void TearDown ()
  33. {
  34. if (Directory.Exists (tempFolder))
  35. Directory.Delete (tempFolder, true);
  36. }
  37. [Test]
  38. #if FEATURE_NO_BSD_SOCKETS
  39. [ExpectedException (typeof (PlatformNotSupportedException))]
  40. #endif
  41. public void Credentials_Default ()
  42. {
  43. Assert.IsNull (smtp.Credentials);
  44. }
  45. [Test]
  46. #if FEATURE_NO_BSD_SOCKETS
  47. [ExpectedException (typeof (PlatformNotSupportedException))]
  48. #endif
  49. public void DeliveryMethod ()
  50. {
  51. Assert.AreEqual (SmtpDeliveryMethod.Network, smtp.DeliveryMethod, "#1");
  52. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  53. Assert.AreEqual (SmtpDeliveryMethod.SpecifiedPickupDirectory,
  54. smtp.DeliveryMethod, "#2");
  55. smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
  56. Assert.AreEqual (SmtpDeliveryMethod.PickupDirectoryFromIis,
  57. smtp.DeliveryMethod, "#3");
  58. smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  59. Assert.AreEqual (SmtpDeliveryMethod.Network,
  60. smtp.DeliveryMethod, "#4");
  61. }
  62. [Test]
  63. #if FEATURE_NO_BSD_SOCKETS
  64. [ExpectedException (typeof (PlatformNotSupportedException))]
  65. #endif
  66. public void EnableSsl ()
  67. {
  68. Assert.IsFalse (smtp.EnableSsl, "#1");
  69. smtp.EnableSsl = true;
  70. Assert.IsTrue (smtp.EnableSsl, "#2");
  71. smtp.EnableSsl = false;
  72. Assert.IsFalse (smtp.EnableSsl, "#3");
  73. }
  74. [Test]
  75. #if FEATURE_NO_BSD_SOCKETS
  76. [ExpectedException (typeof (PlatformNotSupportedException))]
  77. #endif
  78. public void Host ()
  79. {
  80. smtp.Host = "127.0.0.1";
  81. Assert.AreEqual ("127.0.0.1", smtp.Host, "#2");
  82. smtp.Host = "smtp.ximian.com";
  83. Assert.AreEqual ("smtp.ximian.com", smtp.Host, "#3");
  84. }
  85. [Test]
  86. [Category ("NotWorking")]
  87. public void Host_Default ()
  88. {
  89. Assert.IsNull (smtp.Host);
  90. }
  91. [Test]
  92. #if FEATURE_NO_BSD_SOCKETS
  93. [ExpectedException (typeof (PlatformNotSupportedException))]
  94. #endif
  95. public void Host_Value_Null ()
  96. {
  97. try {
  98. smtp.Host = null;
  99. Assert.Fail ("#1");
  100. } catch (ArgumentNullException ex) {
  101. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  102. Assert.IsNull (ex.InnerException, "#3");
  103. Assert.IsNotNull (ex.Message, "#4");
  104. Assert.AreEqual ("value", ex.ParamName, "#5");
  105. }
  106. }
  107. [Test]
  108. #if FEATURE_NO_BSD_SOCKETS
  109. [ExpectedException (typeof (PlatformNotSupportedException))]
  110. #endif
  111. public void Host_Value_Empty ()
  112. {
  113. try {
  114. smtp.Host = String.Empty;
  115. Assert.Fail ("#1");
  116. } catch (ArgumentException ex) {
  117. // This property cannot be set to an empty string
  118. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  119. Assert.IsNull (ex.InnerException, "#3");
  120. Assert.IsNotNull (ex.Message, "#4");
  121. Assert.AreEqual ("value", ex.ParamName, "#5");
  122. }
  123. }
  124. [Test]
  125. #if FEATURE_NO_BSD_SOCKETS
  126. [ExpectedException (typeof (PlatformNotSupportedException))]
  127. #endif
  128. public void PickupDirectoryLocation ()
  129. {
  130. Assert.IsNull (smtp.PickupDirectoryLocation, "#1");
  131. smtp.PickupDirectoryLocation = tempFolder;
  132. Assert.AreSame (tempFolder, smtp.PickupDirectoryLocation, "#2");
  133. smtp.PickupDirectoryLocation = "shouldnotexist";
  134. Assert.AreEqual ("shouldnotexist", smtp.PickupDirectoryLocation, "#3");
  135. smtp.PickupDirectoryLocation = null;
  136. Assert.IsNull (smtp.PickupDirectoryLocation, "#4");
  137. smtp.PickupDirectoryLocation = string.Empty;
  138. Assert.AreEqual (string.Empty, smtp.PickupDirectoryLocation, "#5");
  139. smtp.PickupDirectoryLocation = "\0";
  140. Assert.AreEqual ("\0", smtp.PickupDirectoryLocation, "#6");
  141. }
  142. [Test]
  143. #if FEATURE_NO_BSD_SOCKETS
  144. [ExpectedException (typeof (PlatformNotSupportedException))]
  145. #endif
  146. public void Port ()
  147. {
  148. Assert.AreEqual (25, smtp.Port, "#1");
  149. smtp.Port = 1;
  150. Assert.AreEqual (1, smtp.Port, "#2");
  151. smtp.Port = int.MaxValue;
  152. Assert.AreEqual (int.MaxValue, smtp.Port, "#3");
  153. }
  154. [Test]
  155. #if FEATURE_NO_BSD_SOCKETS
  156. [ExpectedException (typeof (PlatformNotSupportedException))]
  157. #endif
  158. public void Port_Value_Invalid ()
  159. {
  160. // zero
  161. try {
  162. smtp.Port = 0;
  163. Assert.Fail ("#A1");
  164. } catch (ArgumentOutOfRangeException ex) {
  165. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  166. Assert.IsNull (ex.InnerException, "#A3");
  167. Assert.IsNotNull (ex.Message, "#A4");
  168. Assert.AreEqual ("value", ex.ParamName, "#A5");
  169. }
  170. // negative
  171. try {
  172. smtp.Port = -1;
  173. Assert.Fail ("#B1");
  174. } catch (ArgumentOutOfRangeException ex) {
  175. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  176. Assert.IsNull (ex.InnerException, "#B3");
  177. Assert.IsNotNull (ex.Message, "#B4");
  178. Assert.AreEqual ("value", ex.ParamName, "#B5");
  179. }
  180. }
  181. [Test]
  182. #if FEATURE_NO_BSD_SOCKETS
  183. [ExpectedException (typeof (PlatformNotSupportedException))]
  184. #endif
  185. public void Send_Message_Null ()
  186. {
  187. try {
  188. smtp.Send ((MailMessage) null);
  189. Assert.Fail ("#1");
  190. } catch (ArgumentNullException ex) {
  191. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  192. Assert.IsNull (ex.InnerException, "#B3");
  193. Assert.IsNotNull (ex.Message, "#B4");
  194. Assert.AreEqual ("message", ex.ParamName, "#B5");
  195. }
  196. }
  197. [Test]
  198. #if FEATURE_NO_BSD_SOCKETS
  199. [ExpectedException (typeof (PlatformNotSupportedException))]
  200. #endif
  201. public void Send_Network_Host_Null ()
  202. {
  203. try {
  204. smtp.Send ("[email protected]", "[email protected]",
  205. "introduction", "hello");
  206. Assert.Fail ("#1");
  207. } catch (InvalidOperationException ex) {
  208. // The SMTP host was not specified
  209. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  210. Assert.IsNull (ex.InnerException, "#3");
  211. Assert.IsNotNull (ex.Message, "#4");
  212. }
  213. }
  214. [Test]
  215. #if FEATURE_NO_BSD_SOCKETS
  216. [ExpectedException (typeof (PlatformNotSupportedException))]
  217. #endif
  218. public void Send_Network_Host_Whitespace ()
  219. {
  220. smtp.Host = " \r\n ";
  221. try {
  222. smtp.Send ("[email protected]", "[email protected]",
  223. "introduction", "hello");
  224. Assert.Fail ("#1");
  225. } catch (InvalidOperationException ex) {
  226. // The SMTP host was not specified
  227. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  228. Assert.IsNull (ex.InnerException, "#3");
  229. Assert.IsNotNull (ex.Message, "#4");
  230. }
  231. }
  232. [Test]
  233. #if FEATURE_NO_BSD_SOCKETS
  234. [ExpectedException (typeof (PlatformNotSupportedException))]
  235. #endif
  236. public void Send_SpecifiedPickupDirectory ()
  237. {
  238. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  239. smtp.PickupDirectoryLocation = tempFolder;
  240. smtp.Send ("[email protected]", "[email protected]",
  241. "introduction", "hello");
  242. string [] files = Directory.GetFiles (tempFolder, "*");
  243. Assert.AreEqual (1, files.Length, "#1");
  244. Assert.AreEqual (".eml", Path.GetExtension (files [0]), "#2");
  245. }
  246. [Test]
  247. #if FEATURE_NO_BSD_SOCKETS
  248. [ExpectedException (typeof (PlatformNotSupportedException))]
  249. #endif
  250. public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_DirectoryNotFound ()
  251. {
  252. Directory.Delete (tempFolder);
  253. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  254. smtp.PickupDirectoryLocation = tempFolder;
  255. try {
  256. smtp.Send ("[email protected]", "[email protected]",
  257. "introduction", "hello");
  258. Assert.Fail ("#1");
  259. } catch (SmtpException ex) {
  260. // Failure sending email
  261. Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
  262. Assert.IsNotNull (ex.InnerException, "#3");
  263. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.InnerException.GetType (), "#4");
  264. Assert.IsNotNull (ex.Message, "#5");
  265. Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
  266. // Could not find a part of the path '...'
  267. DirectoryNotFoundException inner = (DirectoryNotFoundException) ex.InnerException;
  268. Assert.IsNull (inner.InnerException, "#7");
  269. Assert.IsNotNull (inner.Message, "#8");
  270. Assert.IsTrue (inner.Message.IndexOf (tempFolder) != -1, "#9");
  271. }
  272. }
  273. [Test]
  274. #if FEATURE_NO_BSD_SOCKETS
  275. [ExpectedException (typeof (PlatformNotSupportedException))]
  276. #endif
  277. public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Empty ()
  278. {
  279. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  280. smtp.PickupDirectoryLocation = string.Empty;
  281. try {
  282. smtp.Send ("[email protected]", "[email protected]",
  283. "introduction", "hello");
  284. Assert.Fail ("#1");
  285. } catch (SmtpException ex) {
  286. // Only absolute directories are allowed for
  287. // pickup directory
  288. Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
  289. Assert.IsNull (ex.InnerException, "#3");
  290. Assert.IsNotNull (ex.Message, "#4");
  291. Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
  292. }
  293. }
  294. [Test]
  295. #if FEATURE_NO_BSD_SOCKETS
  296. [ExpectedException (typeof (PlatformNotSupportedException))]
  297. #endif
  298. public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_IllegalChars ()
  299. {
  300. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  301. smtp.PickupDirectoryLocation = "\0abc";
  302. try {
  303. smtp.Send ("[email protected]", "[email protected]",
  304. "introduction", "hello");
  305. Assert.Fail ("#1");
  306. } catch (SmtpException ex) {
  307. // Failure sending email
  308. Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
  309. Assert.IsNotNull (ex.InnerException, "#3");
  310. Assert.AreEqual (typeof (ArgumentException), ex.InnerException.GetType (), "#4");
  311. Assert.IsNotNull (ex.Message, "#5");
  312. Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#6");
  313. // Illegal characters in path
  314. ArgumentException inner = (ArgumentException) ex.InnerException;
  315. Assert.IsNull (inner.InnerException, "#7");
  316. Assert.IsNotNull (inner.Message, "#8");
  317. Assert.IsNull (inner.ParamName, "#9");
  318. }
  319. }
  320. [Test]
  321. #if FEATURE_NO_BSD_SOCKETS
  322. [ExpectedException (typeof (PlatformNotSupportedException))]
  323. #endif
  324. public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_NotAbsolute ()
  325. {
  326. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  327. smtp.PickupDirectoryLocation = "relative";
  328. try {
  329. smtp.Send ("[email protected]", "[email protected]",
  330. "introduction", "hello");
  331. Assert.Fail ("#1");
  332. } catch (SmtpException ex) {
  333. // Only absolute directories are allowed for
  334. // pickup directory
  335. Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
  336. Assert.IsNull (ex.InnerException, "#3");
  337. Assert.IsNotNull (ex.Message, "#4");
  338. Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
  339. }
  340. }
  341. [Test]
  342. #if FEATURE_NO_BSD_SOCKETS
  343. [ExpectedException (typeof (PlatformNotSupportedException))]
  344. #endif
  345. public void Send_SpecifiedPickupDirectory_PickupDirectoryLocation_Null ()
  346. {
  347. smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
  348. try {
  349. smtp.Send ("[email protected]", "[email protected]",
  350. "introduction", "hello");
  351. Assert.Fail ("#1");
  352. } catch (SmtpException ex) {
  353. // Only absolute directories are allowed for
  354. // pickup directory
  355. Assert.AreEqual (typeof (SmtpException), ex.GetType (), "#2");
  356. Assert.IsNull (ex.InnerException, "#3");
  357. Assert.IsNotNull (ex.Message, "#4");
  358. Assert.AreEqual (SmtpStatusCode.GeneralFailure, ex.StatusCode, "#5");
  359. }
  360. }
  361. [Test]
  362. #if FEATURE_NO_BSD_SOCKETS
  363. [ExpectedException (typeof (PlatformNotSupportedException))]
  364. #endif
  365. public void Timeout ()
  366. {
  367. Assert.AreEqual (100000, smtp.Timeout, "#1");
  368. smtp.Timeout = 50;
  369. Assert.AreEqual (50, smtp.Timeout, "#2");
  370. smtp.Timeout = 0;
  371. Assert.AreEqual (0, smtp.Timeout, "#3");
  372. }
  373. [Test]
  374. #if FEATURE_NO_BSD_SOCKETS
  375. [ExpectedException (typeof (PlatformNotSupportedException))]
  376. #else
  377. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  378. #endif
  379. public void Timeout_Value_Negative ()
  380. {
  381. smtp.Timeout = -1;
  382. }
  383. [Test]
  384. #if FEATURE_NO_BSD_SOCKETS
  385. [ExpectedException (typeof (PlatformNotSupportedException))]
  386. #endif
  387. public void UseDefaultCredentials_Default ()
  388. {
  389. Assert.IsFalse (smtp.UseDefaultCredentials);
  390. }
  391. [Test]
  392. #if FEATURE_NO_BSD_SOCKETS
  393. [ExpectedException (typeof (PlatformNotSupportedException))]
  394. #endif
  395. public void Deliver ()
  396. {
  397. var server = new SmtpServer ();
  398. var client = new SmtpClient ("localhost", server.EndPoint.Port);
  399. var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo\r\n");
  400. Thread t = new Thread (server.Run);
  401. t.Start ();
  402. client.Send (msg);
  403. t.Join ();
  404. Assert.AreEqual ("<[email protected]>", server.mail_from);
  405. Assert.AreEqual ("<[email protected]>", server.rcpt_to);
  406. }
  407. [Test]
  408. #if FEATURE_NO_BSD_SOCKETS
  409. [ExpectedException (typeof (PlatformNotSupportedException))]
  410. #endif
  411. public void Deliver_Envelope ()
  412. {
  413. var server = new SmtpServer ();
  414. var client = new SmtpClient ("localhost", server.EndPoint.Port);
  415. var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo\r\n");
  416. msg.Sender = new MailAddress ("[email protected]");
  417. Thread t = new Thread (server.Run);
  418. t.Start ();
  419. client.Send (msg);
  420. t.Join ();
  421. Assert.AreEqual ("<[email protected]>", server.mail_from);
  422. Assert.AreEqual ("<[email protected]>", server.rcpt_to);
  423. }
  424. [Test]
  425. #if FEATURE_NO_BSD_SOCKETS
  426. [ExpectedException (typeof (PlatformNotSupportedException))]
  427. #endif
  428. public void Deliver_Async ()
  429. {
  430. // SmtpClient uses BackgroundWorker and listens for the RunWorkerCompleted
  431. // to mark an async task as completed. The problem is that BackgroundWorker uses
  432. // System.ComponentModel.AsyncOperationManager to get the synchronization
  433. // context, and in monotouch that returns by default a synchronization
  434. // context for the main thread. Since tests are also run on the main thread,
  435. // we'll block the main thread while waiting for the async send to complete,
  436. // while the async completion is waiting for the main thread to process it.
  437. // So instead use a SynchronizationContext that uses the threadpool instead
  438. // of the main thread.
  439. var existing_context = global::System.ComponentModel.AsyncOperationManager.SynchronizationContext;
  440. global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = new ThreadPoolSynchronizationContext ();
  441. try {
  442. var server = new SmtpServer ();
  443. var client = new SmtpClient ("localhost", server.EndPoint.Port);
  444. var msg = new MailMessage ("[email protected]", "[email protected]", "hello", "howdydoo\r\n");
  445. Thread t = new Thread (server.Run);
  446. t.Start ();
  447. var task = client.SendMailAsync (msg);
  448. t.Join ();
  449. Assert.AreEqual ("<[email protected]>", server.mail_from);
  450. Assert.AreEqual ("<[email protected]>", server.rcpt_to);
  451. Assert.IsTrue (task.Wait (1000));
  452. Assert.IsTrue (task.IsCompleted, "task");
  453. } finally {
  454. global::System.ComponentModel.AsyncOperationManager.SynchronizationContext = existing_context;
  455. }
  456. }
  457. internal class ThreadPoolSynchronizationContext : SynchronizationContext
  458. {
  459. public override void Post (SendOrPostCallback d, object state)
  460. {
  461. ThreadPool.QueueUserWorkItem ((v) => d (state));
  462. }
  463. public override void Send (SendOrPostCallback d, object state)
  464. {
  465. d (state);
  466. }
  467. }
  468. }
  469. }