SmtpClientTest.cs 11 KB

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