OleDbPermissionAttributeTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // OleDbPermissionAttributeTest.cs -
  3. // NUnit Test Cases for OleDbPermissionAttribute
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 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.Data;
  32. using System.Data.OleDb;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. namespace MonoTests.System.Data.OleDb {
  36. [TestFixture]
  37. public class OleDbPermissionAttributeTest {
  38. [Test]
  39. public void Default ()
  40. {
  41. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  42. Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
  43. Assert.IsFalse (a.Unrestricted, "Unrestricted");
  44. Assert.IsFalse (a.AllowBlankPassword, "AllowBlankPassword");
  45. Assert.AreEqual (String.Empty, a.ConnectionString, "ConnectionString");
  46. Assert.AreEqual (KeyRestrictionBehavior.AllowOnly, a.KeyRestrictionBehavior, "KeyRestrictionBehavior");
  47. Assert.AreEqual (String.Empty, a.KeyRestrictions, "KeyRestrictions");
  48. #if NET_2_0
  49. Assert.IsFalse (a.ShouldSerializeConnectionString (), "ShouldSerializeConnectionString");
  50. Assert.IsFalse (a.ShouldSerializeKeyRestrictions (), "ShouldSerializeConnectionString");
  51. #endif
  52. OleDbPermission odp = (OleDbPermission)a.CreatePermission ();
  53. Assert.IsFalse (odp.IsUnrestricted (), "IsUnrestricted");
  54. }
  55. [Test]
  56. public void Action ()
  57. {
  58. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  59. Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
  60. a.Action = SecurityAction.Demand;
  61. Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
  62. a.Action = SecurityAction.Deny;
  63. Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
  64. a.Action = SecurityAction.InheritanceDemand;
  65. Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
  66. a.Action = SecurityAction.LinkDemand;
  67. Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
  68. a.Action = SecurityAction.PermitOnly;
  69. Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
  70. a.Action = SecurityAction.RequestMinimum;
  71. Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
  72. a.Action = SecurityAction.RequestOptional;
  73. Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
  74. a.Action = SecurityAction.RequestRefuse;
  75. Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
  76. #if NET_2_0
  77. a.Action = SecurityAction.DemandChoice;
  78. Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
  79. a.Action = SecurityAction.InheritanceDemandChoice;
  80. Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
  81. a.Action = SecurityAction.LinkDemandChoice;
  82. Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
  83. #endif
  84. }
  85. [Test]
  86. public void Action_Invalid ()
  87. {
  88. OleDbPermissionAttribute a = new OleDbPermissionAttribute ((SecurityAction)Int32.MinValue);
  89. // no validation in attribute
  90. }
  91. [Test]
  92. public void Unrestricted ()
  93. {
  94. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  95. a.Unrestricted = true;
  96. OleDbPermission odp = (OleDbPermission)a.CreatePermission ();
  97. Assert.IsTrue (odp.IsUnrestricted (), "IsUnrestricted");
  98. Assert.IsFalse (a.AllowBlankPassword, "AllowBlankPassword");
  99. Assert.AreEqual (String.Empty, a.ConnectionString, "ConnectionString");
  100. Assert.AreEqual (KeyRestrictionBehavior.AllowOnly, a.KeyRestrictionBehavior, "KeyRestrictionBehavior");
  101. Assert.AreEqual (String.Empty, a.KeyRestrictions, "KeyRestrictions");
  102. a.Unrestricted = false;
  103. odp = (OleDbPermission)a.CreatePermission ();
  104. Assert.IsFalse (odp.IsUnrestricted (), "!IsUnrestricted");
  105. }
  106. [Test]
  107. public void AllowBlankPassword ()
  108. {
  109. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  110. Assert.IsFalse (a.AllowBlankPassword, "Default");
  111. a.AllowBlankPassword = true;
  112. Assert.IsTrue (a.AllowBlankPassword, "True");
  113. a.AllowBlankPassword = false;
  114. Assert.IsFalse (a.AllowBlankPassword, "False");
  115. }
  116. [Test]
  117. public void ConnectionString ()
  118. {
  119. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  120. a.ConnectionString = String.Empty;
  121. Assert.AreEqual (String.Empty, a.ConnectionString, "Empty");
  122. a.ConnectionString = "Mono";
  123. Assert.AreEqual ("Mono", a.ConnectionString, "Mono");
  124. a.ConnectionString = null;
  125. Assert.AreEqual (String.Empty, a.ConnectionString, "Empty(null)");
  126. }
  127. [Test]
  128. public void KeyRestrictionBehavior_All ()
  129. {
  130. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  131. a.KeyRestrictionBehavior = KeyRestrictionBehavior.AllowOnly;
  132. Assert.AreEqual (KeyRestrictionBehavior.AllowOnly, a.KeyRestrictionBehavior, "AllowOnly");
  133. a.KeyRestrictionBehavior = KeyRestrictionBehavior.PreventUsage;
  134. Assert.AreEqual (KeyRestrictionBehavior.PreventUsage, a.KeyRestrictionBehavior, "PreventUsage");
  135. }
  136. [Test]
  137. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  138. public void KeyRestrictionBehavior_Invalid ()
  139. {
  140. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  141. a.KeyRestrictionBehavior = (KeyRestrictionBehavior)Int32.MinValue;
  142. }
  143. [Test]
  144. public void KeyRestriction ()
  145. {
  146. OleDbPermissionAttribute a = new OleDbPermissionAttribute (SecurityAction.Assert);
  147. a.KeyRestrictions = String.Empty;
  148. Assert.AreEqual (String.Empty, a.KeyRestrictions, "Empty");
  149. a.KeyRestrictions = "Mono";
  150. Assert.AreEqual ("Mono", a.KeyRestrictions, "Mono");
  151. a.KeyRestrictions = null;
  152. Assert.AreEqual (String.Empty, a.KeyRestrictions, "Empty(null)");
  153. }
  154. [Test]
  155. public void Attributes ()
  156. {
  157. Type t = typeof (OleDbPermissionAttribute);
  158. Assert.IsTrue (t.IsSerializable, "IsSerializable");
  159. object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
  160. Assert.AreEqual (1, attrs.Length, "AttributeUsage");
  161. AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
  162. Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
  163. Assert.IsFalse (aua.Inherited, "Inherited");
  164. AttributeTargets at = AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method;
  165. Assert.AreEqual (at, aua.ValidOn, "ValidOn");
  166. }
  167. }
  168. }