SmtpClientTest.cs 12 KB

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