FileLoadExceptionCas.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // FileLoadExceptionCas.cs - CAS unit tests for System.IO.FileLoadException
  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.Security;
  32. using System.Security.Permissions;
  33. namespace MonoCasTests.System.IO {
  34. [TestFixture]
  35. [Category ("CAS")]
  36. public class FileLoadExceptionCas {
  37. [SetUp]
  38. public void SetUp ()
  39. {
  40. if (!SecurityManager.SecurityEnabled)
  41. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  42. }
  43. [Test]
  44. public void NoRestriction ()
  45. {
  46. FileLoadException fle = new FileLoadException ("message", "filename",
  47. new FileLoadException ("inner message", "inner filename"));
  48. Assert.AreEqual ("message", fle.Message, "Message");
  49. Assert.AreEqual ("filename", fle.FileName, "FileName");
  50. Assert.IsNull (fle.FusionLog, "FusionLog");
  51. Assert.IsNotNull (fle.ToString (), "ToString");
  52. }
  53. [Test]
  54. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  55. [ExpectedException (typeof (SecurityException))]
  56. public void FullRestriction ()
  57. {
  58. FileLoadException fle = new FileLoadException ("message", "filename",
  59. new FileLoadException ("inner message", "inner filename"));
  60. Assert.AreEqual ("message", fle.Message, "Message");
  61. Assert.AreEqual ("filename", fle.FileName, "FileName");
  62. Assert.IsNull (fle.FusionLog, "FusionLog");
  63. // ToString doesn't work in this case and strangely throws a FileLoadException
  64. Assert.IsNotNull (fle.ToString (), "ToString");
  65. }
  66. [Test]
  67. [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true, ControlPolicy = true)]
  68. public void GetFusionLog_Pass ()
  69. {
  70. FileLoadException fle = new FileLoadException ("message", "filename");
  71. Assert.AreEqual ("message", fle.Message, "Message");
  72. Assert.AreEqual ("filename", fle.FileName, "FileName");
  73. Assert.IsNull (fle.FusionLog, "FusionLog");
  74. // note: ToString doesn't work in this case
  75. }
  76. [Test]
  77. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  78. [ExpectedException (typeof (SecurityException))]
  79. public void GetFusionLog_Fail_ControlEvidence ()
  80. {
  81. FileLoadException fle = new FileLoadException ();
  82. Assert.IsNull (fle.FusionLog, "FusionLog");
  83. }
  84. [Test]
  85. [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
  86. [ExpectedException (typeof (SecurityException))]
  87. public void GetFusionLog_Fail_ControlPolicy ()
  88. {
  89. FileLoadException fle = new FileLoadException ();
  90. Assert.IsNull (fle.FusionLog, "FusionLog");
  91. // we don't have to throw the exception to have FusionLog
  92. // informations restricted (even if there could be no
  93. // data in this state).
  94. }
  95. [Test]
  96. public void Throw_NoRestriction ()
  97. {
  98. try {
  99. throw new FileLoadException ("message", "filename",
  100. new FileLoadException ("inner message", "inner filename"));
  101. }
  102. catch (FileLoadException fle) {
  103. Assert.AreEqual ("message", fle.Message, "Message");
  104. Assert.AreEqual ("filename", fle.FileName, "FileName");
  105. Assert.IsNull (fle.FusionLog, "FusionLog");
  106. Assert.IsNotNull (fle.ToString (), "ToString");
  107. }
  108. }
  109. [Test]
  110. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  111. [ExpectedException (typeof (SecurityException))]
  112. public void Throw_FullRestriction ()
  113. {
  114. try {
  115. throw new FileLoadException ("message", "filename",
  116. new FileLoadException ("inner message", "inner filename"));
  117. }
  118. catch (FileLoadException fle) {
  119. Assert.AreEqual ("message", fle.Message, "Message");
  120. Assert.AreEqual ("filename", fle.FileName, "FileName");
  121. // both FusionLog and ToString doesn't work in this case
  122. // but strangely we get a FileLoadException
  123. Assert.IsNull (fle.FusionLog, "FusionLog");
  124. Assert.IsNotNull (fle.ToString (), "ToString");
  125. }
  126. }
  127. [Test]
  128. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  129. [ExpectedException (typeof (SecurityException))]
  130. public void Throw_GetFusionLog_Fail_ControlEvidence ()
  131. {
  132. try {
  133. throw new FileLoadException ("message", "filename",
  134. new FileLoadException ("inner message", "inner filename"));
  135. }
  136. catch (FileLoadException fle) {
  137. Assert.IsNull (fle.FusionLog, "FusionLog");
  138. }
  139. }
  140. [Test]
  141. [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
  142. [ExpectedException (typeof (SecurityException))]
  143. public void Throw_GetFusionLog_Fail_ControlPolicy ()
  144. {
  145. try {
  146. throw new FileLoadException ("message", "filename",
  147. new FileLoadException ("inner message", "inner filename"));
  148. }
  149. catch (FileLoadException fle) {
  150. Assert.IsNull (fle.FusionLog, "FusionLog");
  151. }
  152. }
  153. }
  154. }