FileNotFoundExceptionCas.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // FileNotFoundExceptionCas.cs - CAS unit tests for
  3. // System.IO.FileNotFoundException
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. namespace MonoCasTests.System.IO {
  35. [TestFixture]
  36. [Category ("CAS")]
  37. public class FileNotFoundExceptionCas {
  38. [SetUp]
  39. public void SetUp ()
  40. {
  41. if (!SecurityManager.SecurityEnabled)
  42. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  43. }
  44. [Test]
  45. public void NoRestriction ()
  46. {
  47. FileNotFoundException fle = new FileNotFoundException ("message", "filename",
  48. new FileNotFoundException ("inner message", "inner filename"));
  49. Assert.AreEqual ("message", fle.Message, "Message");
  50. Assert.AreEqual ("filename", fle.FileName, "FileName");
  51. Assert.IsNull (fle.FusionLog, "FusionLog");
  52. Assert.IsNotNull (fle.ToString (), "ToString");
  53. }
  54. [Test]
  55. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  56. [ExpectedException (typeof (SecurityException))]
  57. public void FullRestriction ()
  58. {
  59. FileNotFoundException fle = new FileNotFoundException ("message", "filename",
  60. new FileNotFoundException ("inner message", "inner filename"));
  61. Assert.AreEqual ("message", fle.Message, "Message");
  62. Assert.AreEqual ("filename", fle.FileName, "FileName");
  63. Assert.IsNull (fle.FusionLog, "FusionLog");
  64. // ToString doesn't work in this case and strangely throws a FileNotFoundException
  65. Assert.IsNotNull (fle.ToString (), "ToString");
  66. }
  67. [Test]
  68. [SecurityPermission (SecurityAction.PermitOnly, ControlEvidence = true, ControlPolicy = true)]
  69. public void GetFusionLog_Pass ()
  70. {
  71. FileNotFoundException fle = new FileNotFoundException ("message", "filename");
  72. Assert.AreEqual ("message", fle.Message, "Message");
  73. Assert.AreEqual ("filename", fle.FileName, "FileName");
  74. Assert.IsNull (fle.FusionLog, "FusionLog");
  75. // note: ToString doesn't work in this case
  76. }
  77. [Test]
  78. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  79. [ExpectedException (typeof (SecurityException))]
  80. public void GetFusionLog_Fail_ControlEvidence ()
  81. {
  82. FileNotFoundException fle = new FileNotFoundException ();
  83. Assert.IsNull (fle.FusionLog, "FusionLog");
  84. }
  85. [Test]
  86. [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
  87. [ExpectedException (typeof (SecurityException))]
  88. public void GetFusionLog_Fail_ControlPolicy ()
  89. {
  90. FileNotFoundException fle = new FileNotFoundException ();
  91. Assert.IsNull (fle.FusionLog, "FusionLog");
  92. // we don't have to throw the exception to have FusionLog
  93. // informations restricted (even if there could be no
  94. // data in this state).
  95. }
  96. [Test]
  97. public void Throw_NoRestriction ()
  98. {
  99. try {
  100. throw new FileNotFoundException ("message", "filename",
  101. new FileNotFoundException ("inner message", "inner filename"));
  102. }
  103. catch (FileNotFoundException fle) {
  104. Assert.AreEqual ("message", fle.Message, "Message");
  105. Assert.AreEqual ("filename", fle.FileName, "FileName");
  106. Assert.IsNull (fle.FusionLog, "FusionLog");
  107. Assert.IsNotNull (fle.ToString (), "ToString");
  108. }
  109. }
  110. [Test]
  111. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  112. [ExpectedException (typeof (SecurityException))]
  113. public void Throw_FullRestriction ()
  114. {
  115. try {
  116. throw new FileNotFoundException ("message", "filename",
  117. new FileNotFoundException ("inner message", "inner filename"));
  118. }
  119. catch (FileNotFoundException fle) {
  120. Assert.AreEqual ("message", fle.Message, "Message");
  121. Assert.AreEqual ("filename", fle.FileName, "FileName");
  122. // both FusionLog and ToString doesn't work in this case
  123. // but strangely we get a FileNotFoundException
  124. Assert.IsNull (fle.FusionLog, "FusionLog");
  125. Assert.IsNotNull (fle.ToString (), "ToString");
  126. }
  127. }
  128. [Test]
  129. [SecurityPermission (SecurityAction.Deny, ControlEvidence = true)]
  130. [ExpectedException (typeof (SecurityException))]
  131. public void Throw_GetFusionLog_Fail_ControlEvidence ()
  132. {
  133. try {
  134. throw new FileNotFoundException ("message", "filename",
  135. new FileNotFoundException ("inner message", "inner filename"));
  136. }
  137. catch (FileNotFoundException fle) {
  138. Assert.IsNull (fle.FusionLog, "FusionLog");
  139. }
  140. }
  141. [Test]
  142. [SecurityPermission (SecurityAction.Deny, ControlPolicy = true)]
  143. [ExpectedException (typeof (SecurityException))]
  144. public void Throw_GetFusionLog_Fail_ControlPolicy ()
  145. {
  146. try {
  147. throw new FileNotFoundException ("message", "filename",
  148. new FileNotFoundException ("inner message", "inner filename"));
  149. }
  150. catch (FileNotFoundException fle) {
  151. Assert.IsNull (fle.FusionLog, "FusionLog");
  152. }
  153. }
  154. }
  155. }