SmtpMailCas.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // SmtpMailCas.cs - CAS unit tests for System.Web.Mail.SmtpMail
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using System.Web;
  36. using System.Web.Mail;
  37. namespace MonoCasTests.System.Web.Mail {
  38. [TestFixture]
  39. [Category ("CAS")]
  40. public class SmtpMailCas : AspNetHostingMinimal {
  41. private MailMessage msg;
  42. [TestFixtureSetUp]
  43. public void FixtureSetUp ()
  44. {
  45. string fname = Path.GetTempFileName ();
  46. using (FileStream fs = File.OpenWrite (fname)) {
  47. fs.WriteByte (0);
  48. fs.Close ();
  49. }
  50. msg = new MailMessage ();
  51. msg.Attachments.Add (new MailAttachment (fname));
  52. msg.Bcc = "[email protected]";
  53. msg.Body = "Hola!";
  54. msg.BodyEncoding = Encoding.ASCII;
  55. msg.Cc = "[email protected]";
  56. msg.Fields["mono"] = "monkey";
  57. msg.From = "[email protected]";
  58. msg.Headers["monkey"] = "mono";
  59. msg.Priority = MailPriority.High;
  60. msg.Subject = "Monkey business";
  61. msg.To = "[email protected]";
  62. msg.UrlContentBase = "http://www.mono-project.com";
  63. msg.UrlContentLocation = "http://www.go-mono.com";
  64. // ensure the static ctor is called at full trust
  65. Assert.IsNotNull (SmtpMail.SmtpServer);
  66. }
  67. [Test]
  68. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  69. public void SmtpServer_Deny_Unrestricted ()
  70. {
  71. // get/set
  72. SmtpMail.SmtpServer = "localhost";
  73. Assert.AreEqual ("localhost", SmtpMail.SmtpServer, "SmtpServer");
  74. }
  75. [Test]
  76. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  77. [ExpectedException (typeof (SecurityException))]
  78. public void SendMessage_Deny_Medium ()
  79. {
  80. SmtpMail.Send (msg);
  81. }
  82. [Test]
  83. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  84. public void SendMessage_PermitOnly_Medium ()
  85. {
  86. try {
  87. SmtpMail.Send (msg);
  88. }
  89. catch (Exception e) {
  90. // HttpException: can't sent mail
  91. // TypeInitializationException: missing stuff in machine.config
  92. // ConfigurationException: missing stuff in machine.config
  93. if (e is SecurityException)
  94. throw e; // that's the one we're looking for
  95. }
  96. }
  97. [Test]
  98. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  99. [ExpectedException (typeof (SecurityException))]
  100. public void Send_Deny_Medium ()
  101. {
  102. SmtpMail.Send (msg.From, msg.To, msg.Subject, msg.Body);
  103. }
  104. [Test]
  105. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  106. public void Send_PermitOnly_Medium ()
  107. {
  108. try {
  109. SmtpMail.Send (msg.From, msg.To, msg.Subject, msg.Body);
  110. }
  111. catch (Exception e) {
  112. // HttpException: can't sent mail
  113. // TypeInitializationException: missing stuff in machine.config
  114. // ConfigurationException: missing stuff in machine.config
  115. if (e is SecurityException)
  116. throw e; // that's the one we're looking for
  117. }
  118. }
  119. // LinkDemand tests
  120. public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
  121. {
  122. // in this case testing with a (private) ctor isn't very conveniant
  123. // and the LinkDemand promotion (to Demand) will still occurs on other stuff
  124. // and (finally) we know that the SmtpServer properties isn't protected ifself
  125. MethodInfo mi = this.Type.GetProperty ("SmtpServer").GetGetMethod ();
  126. Assert.IsNotNull (mi, "get");
  127. return mi.Invoke (null, null);
  128. }
  129. public override Type Type {
  130. get { return typeof (SmtpMail); }
  131. }
  132. }
  133. }