DBDataPermissionTest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //
  2. // DBDataPermissionTest.cs - NUnit Test Cases for DBDataPermission
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2004 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.Data;
  31. using System.Data.Common;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.IO;
  35. namespace MonoTests.System.Data.Common {
  36. public class NonAbstractDBDataPermission : DBDataPermission {
  37. #if !NET_2_0
  38. public NonAbstractDBDataPermission ()
  39. : base ()
  40. {
  41. }
  42. public NonAbstractDBDataPermission (DBDataPermission permission, bool allowBlankPassword)
  43. : base (permission)
  44. {
  45. AllowBlankPassword = allowBlankPassword;
  46. }
  47. #else
  48. // make Copy and CreateInstance work :)
  49. public NonAbstractDBDataPermission ()
  50. : base (PermissionState.None)
  51. {
  52. }
  53. #endif
  54. public NonAbstractDBDataPermission (PermissionState state)
  55. : base (state)
  56. {
  57. }
  58. public NonAbstractDBDataPermission (DBDataPermission permission)
  59. : base (permission)
  60. {
  61. }
  62. public NonAbstractDBDataPermission (DBDataPermissionAttribute permissionAttribute)
  63. : base (permissionAttribute)
  64. {
  65. }
  66. public new void Clear ()
  67. {
  68. base.Clear ();
  69. }
  70. public new DBDataPermission CreateInstance ()
  71. {
  72. return base.CreateInstance ();
  73. }
  74. }
  75. [TestFixture]
  76. #if MOBILE
  77. [Ignore ("CAS is not supported and parts will be linked away")]
  78. #endif
  79. public class DBDataPermissionTest {
  80. private const string defaultConnectString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;";
  81. private const string defaultConnectString2 = "Data Source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=Northwind;";
  82. private void Check (string msg, NonAbstractDBDataPermission dbdp, bool blank, bool unrestricted, int count)
  83. {
  84. Assert.AreEqual (blank, dbdp.AllowBlankPassword, msg + ".AllowBlankPassword");
  85. Assert.AreEqual (unrestricted, dbdp.IsUnrestricted (), msg + ".IsUnrestricted");
  86. if (count == 0)
  87. Assert.IsNull (dbdp.ToXml ().Children, msg + ".Count != 0");
  88. else
  89. Assert.AreEqual (count, dbdp.ToXml ().Children.Count, msg + ".Count");
  90. }
  91. [Test]
  92. [ExpectedException (typeof (ArgumentNullException))]
  93. public void Constructor_DBDataPermission_Null ()
  94. {
  95. DBDataPermission p = null;
  96. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (p);
  97. }
  98. [Test]
  99. public void Constructor_DBDataPermission ()
  100. {
  101. DBDataPermission p = new NonAbstractDBDataPermission (PermissionState.None);
  102. p.AllowBlankPassword = true;
  103. p.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  104. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (p);
  105. Check ("DBDataPermission", dbdp, true, false, 1);
  106. }
  107. [Test]
  108. [ExpectedException (typeof (ArgumentNullException))]
  109. public void Constructor_DBDataPermissionAttribute_Null ()
  110. {
  111. DBDataPermissionAttribute a = null;
  112. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (a);
  113. }
  114. [Test]
  115. public void Constructor_DBDataPermissionAttribute ()
  116. {
  117. DBDataPermissionAttribute a = new NonAbstractDBDataPermissionAttribute (SecurityAction.Assert);
  118. a.AllowBlankPassword = true;
  119. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (a);
  120. Check ("DBDataPermissionAttribute-0", dbdp, true, false, 0);
  121. a.Unrestricted = true;
  122. dbdp = new NonAbstractDBDataPermission (a);
  123. Check ("DBDataPermissionAttribute-1", dbdp, false, true, 0);
  124. // Unrestricted "bypass" the AllowBlankPassword (so it report false)
  125. a.ConnectionString = defaultConnectString;
  126. dbdp = new NonAbstractDBDataPermission (a);
  127. Check ("DBDataPermissionAttribute-2", dbdp, false, true, 0);
  128. // Unrestricted "bypass" the ConnectionString (so it report 0 childs)
  129. a.Unrestricted = false;
  130. dbdp = new NonAbstractDBDataPermission (a);
  131. Check ("DBDataPermissionAttribute-3", dbdp, true, false, 1);
  132. }
  133. [Test]
  134. #if NET_2_0
  135. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  136. #else
  137. [ExpectedException (typeof (ArgumentException))]
  138. #endif
  139. public void Constructor_PermissionState_Invalid ()
  140. {
  141. PermissionState ps = (PermissionState) Int32.MinValue;
  142. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
  143. }
  144. [Test]
  145. public void Constructor_PermissionState_None ()
  146. {
  147. PermissionState ps = PermissionState.None;
  148. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
  149. Check ("PermissionState_None-1", dbdp, false, false, 0);
  150. dbdp.AllowBlankPassword = true;
  151. Check ("PermissionState_None-1", dbdp, true, false, 0);
  152. }
  153. [Test]
  154. public void Constructor_PermissionState_Unrestricted ()
  155. {
  156. PermissionState ps = PermissionState.Unrestricted;
  157. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
  158. Check ("PermissionState_Unrestricted-1", dbdp, false, true, 0);
  159. dbdp.AllowBlankPassword = true;
  160. Check ("PermissionState_Unrestricted-2", dbdp, true, true, 0);
  161. }
  162. [Test]
  163. public void AllowBlankPassword ()
  164. {
  165. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  166. Assert.IsFalse (dbdp.AllowBlankPassword, "Default");
  167. dbdp.AllowBlankPassword = true;
  168. Assert.IsTrue (dbdp.AllowBlankPassword, "True");
  169. dbdp.Clear ();
  170. // clear the connection list - not the permission itself
  171. Assert.IsTrue (dbdp.AllowBlankPassword, "Clear");
  172. dbdp.AllowBlankPassword = false;
  173. Assert.IsFalse (dbdp.AllowBlankPassword, "False");
  174. }
  175. [Test]
  176. public void Add ()
  177. {
  178. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  179. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  180. Assert.AreEqual (1, dbdp.ToXml ().Children.Count, "Count");
  181. NonAbstractDBDataPermission copy = (NonAbstractDBDataPermission)dbdp.Copy ();
  182. Assert.AreEqual (1, copy.ToXml ().Children.Count, "Copy.Count");
  183. dbdp.Clear ();
  184. Assert.IsNull (dbdp.ToXml ().Children, "Clear");
  185. Assert.AreEqual (1, copy.ToXml ().Children.Count, "Copy.Count-2");
  186. }
  187. [Test]
  188. public void Add_Duplicates ()
  189. {
  190. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  191. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  192. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  193. // no exception but a single element is kept
  194. Assert.AreEqual (1, dbdp.ToXml ().Children.Count, "Count");
  195. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  196. dbdp.Clear ();
  197. Assert.IsNull (dbdp.ToXml ().Children, "Clear");
  198. }
  199. [Test]
  200. public void Add_Differents ()
  201. {
  202. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  203. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  204. string connectString = "Data Source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=Northwind;";
  205. dbdp.Add (connectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  206. Assert.AreEqual (2, dbdp.ToXml ().Children.Count, "Count");
  207. dbdp.Clear ();
  208. Assert.IsNull (dbdp.ToXml ().Children, "Clear");
  209. }
  210. [Test]
  211. public void Add_Unrestricted ()
  212. {
  213. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  214. Assert.IsTrue (dbdp.IsUnrestricted (), "IsUnrestricted-1");
  215. // we lose unrestricted by adding an element
  216. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  217. Assert.IsFalse (dbdp.IsUnrestricted (), "IsUnrestricted-2");
  218. // removing the element doesn't regain unrestricted status
  219. dbdp.Clear ();
  220. Assert.IsFalse (dbdp.IsUnrestricted (), "IsUnrestricted-3");
  221. }
  222. [Test]
  223. public void CreateInstance ()
  224. {
  225. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  226. Assert.AreEqual (typeof (NonAbstractDBDataPermission), dbdp.CreateInstance ().GetType (), "Same type");
  227. }
  228. [Test]
  229. public void Intersect_Null ()
  230. {
  231. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  232. // No intersection with null
  233. Assert.IsNull (elp.Intersect (null), "None N null");
  234. }
  235. [Test]
  236. public void Intersect ()
  237. {
  238. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
  239. NonAbstractDBDataPermission dbdp2 = new NonAbstractDBDataPermission (PermissionState.None);
  240. // 1. None N None
  241. NonAbstractDBDataPermission result = (NonAbstractDBDataPermission) dbdp1.Intersect (dbdp2);
  242. Assert.IsNull (result, "Empty N Empty");
  243. // 2. None N Entry
  244. dbdp2.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  245. result = (NonAbstractDBDataPermission)dbdp1.Intersect (dbdp2);
  246. Assert.IsNull (result, "Empty N Entry");
  247. // 3. Entry N None
  248. result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp1);
  249. Assert.IsNull (result, "Entry N Empty");
  250. // 4. Unrestricted N None
  251. NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  252. result = (NonAbstractDBDataPermission)unr.Intersect (dbdp1);
  253. Check ("(Unrestricted N None)", result, false, false, 0);
  254. // 5. None N Unrestricted
  255. result = (NonAbstractDBDataPermission)dbdp1.Intersect (unr);
  256. Check ("(None N Unrestricted)", result, false, false, 0);
  257. // 6. Unrestricted N Unrestricted
  258. result = (NonAbstractDBDataPermission)unr.Intersect (unr);
  259. Check ("(Unrestricted N Unrestricted)", result, false, true, 0);
  260. // 7. Unrestricted N Entry
  261. result = (NonAbstractDBDataPermission)unr.Intersect (dbdp2);
  262. Check ("(Unrestricted N Entry)", result, false, false, 1);
  263. // 8. Entry N Unrestricted
  264. result = (NonAbstractDBDataPermission)dbdp2.Intersect (unr);
  265. Check ("(Entry N Unrestricted)", result, false, false, 1);
  266. // 9. Entry2 N Entry2
  267. result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp2);
  268. Check ("(Entry2 N Entry2)", result, false, false, 1);
  269. // 10. Entry1 N Entry 2
  270. dbdp1.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.PreventUsage);
  271. result = (NonAbstractDBDataPermission)dbdp1.Intersect (dbdp2);
  272. Assert.IsNull (result, "(Entry1 N Entry2)");
  273. // 11. Entry2 N Entry 1
  274. result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp1);
  275. Assert.IsNull (result, "(Entry2 N Entry1)");
  276. }
  277. [Test]
  278. public void Intersect_AllowBlankPassword ()
  279. {
  280. NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
  281. ptrue.AllowBlankPassword = true;
  282. ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  283. NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
  284. pfalse.AllowBlankPassword = false;
  285. pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  286. NonAbstractDBDataPermission intersect = (NonAbstractDBDataPermission)ptrue.Intersect (ptrue);
  287. Check ("true N true", intersect, true, false, 1);
  288. intersect = (NonAbstractDBDataPermission)ptrue.Intersect (pfalse);
  289. Check ("true N false", intersect, false, false, 1);
  290. intersect = (NonAbstractDBDataPermission)pfalse.Intersect (ptrue);
  291. Check ("false N true", intersect, false, false, 1);
  292. intersect = (NonAbstractDBDataPermission)pfalse.Intersect (pfalse);
  293. Check ("false N false", intersect, false, false, 1);
  294. }
  295. [Test]
  296. [ExpectedException (typeof (ArgumentException))]
  297. public void Intersect_BadPermission ()
  298. {
  299. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  300. dbdp.Intersect (new SecurityPermission (SecurityPermissionFlag.Assertion));
  301. }
  302. [Test]
  303. public void IsSubset_Null ()
  304. {
  305. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  306. Assert.IsTrue (dbdp.IsSubsetOf (null), "Empty-null");
  307. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  308. Assert.IsFalse (dbdp.IsSubsetOf (null), "Element-null");
  309. dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  310. Assert.IsFalse (dbdp.IsSubsetOf (null), "Unrestricted-null");
  311. }
  312. [Test]
  313. public void IsSubset ()
  314. {
  315. NonAbstractDBDataPermission empty = new NonAbstractDBDataPermission (PermissionState.None);
  316. Assert.IsTrue (empty.IsSubsetOf (empty), "Empty-Empty");
  317. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
  318. dbdp1.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  319. Assert.IsTrue (empty.IsSubsetOf (dbdp1), "Empty-1");
  320. Assert.IsFalse (dbdp1.IsSubsetOf (empty), "1-Empty");
  321. Assert.IsTrue (dbdp1.IsSubsetOf (dbdp1), "1-1");
  322. NonAbstractDBDataPermission dbdp2 = (NonAbstractDBDataPermission)dbdp1.Copy ();
  323. dbdp2.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.AllowOnly);
  324. Assert.IsTrue (dbdp1.IsSubsetOf (dbdp2), "1-2");
  325. Assert.IsFalse (dbdp2.IsSubsetOf (dbdp1), "2-1");
  326. Assert.IsTrue (dbdp2.IsSubsetOf (dbdp2), "2-2");
  327. NonAbstractDBDataPermission dbdp3 = new NonAbstractDBDataPermission (PermissionState.None);
  328. dbdp3.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  329. Assert.IsTrue (dbdp3.IsSubsetOf (dbdp1), "3-1");
  330. Assert.IsTrue (dbdp1.IsSubsetOf (dbdp3), "1-3");
  331. Assert.IsTrue (dbdp3.IsSubsetOf (dbdp3), "3-3");
  332. NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  333. Assert.IsTrue (dbdp1.IsSubsetOf (unr), "1-unrestricted");
  334. Assert.IsFalse (unr.IsSubsetOf (dbdp1), "unrestricted-1");
  335. Assert.IsTrue (dbdp2.IsSubsetOf (unr), "2-unrestricted");
  336. Assert.IsFalse (unr.IsSubsetOf (dbdp2), "unrestricted-2");
  337. Assert.IsTrue (dbdp3.IsSubsetOf (unr), "3-unrestricted");
  338. Assert.IsFalse (unr.IsSubsetOf (dbdp3), "unrestricted-3");
  339. Assert.IsTrue (unr.IsSubsetOf (unr), "unrestricted-unrestricted");
  340. }
  341. [Test]
  342. public void IsSubsetOf_AllowBlankPassword ()
  343. {
  344. NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
  345. ptrue.AllowBlankPassword = true;
  346. ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  347. NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
  348. pfalse.AllowBlankPassword = false;
  349. pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  350. Assert.IsTrue (ptrue.IsSubsetOf (ptrue), "true subsetof true");
  351. Assert.IsFalse (ptrue.IsSubsetOf (pfalse), "true subsetof false");
  352. Assert.IsTrue (pfalse.IsSubsetOf (ptrue), "false subsetof true");
  353. Assert.IsTrue (pfalse.IsSubsetOf (pfalse), "false subsetof false");
  354. }
  355. [Test]
  356. public void IsSubsetOf_BothEmpty_KeyRestrictionBehavior ()
  357. {
  358. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  359. pAllow.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  360. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  361. pPrevent.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  362. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "BothEmpty - pAllow subsetof pAllow");
  363. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "BothEmpty - pAllow subsetof pPrevent");
  364. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "BothEmpty - pPrevent subsetof pAllow");
  365. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "BothEmpty - pPrevent subsetof pPrevent");
  366. }
  367. [Test]
  368. public void IsSubsetOf_EmptyAllow_Prevent_KeyRestrictionBehavior ()
  369. {
  370. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  371. pAllow.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  372. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  373. pPrevent.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
  374. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "EmptyAllow_Prevent - pAllow subsetof pAllow");
  375. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "EmptyAllow_Prevent - pAllow subsetof pPrevent");
  376. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "EmptyAllow_Prevent - pPrevent subsetof pAllow");
  377. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "EmptyAllow_Prevent - pPrevent subsetof pPrevent");
  378. }
  379. [Test]
  380. public void IsSubsetOf_Allow_EmptyPrevent_KeyRestrictionBehavior ()
  381. {
  382. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  383. pAllow.Add (defaultConnectString, "data source=;", KeyRestrictionBehavior.AllowOnly);
  384. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  385. pPrevent.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  386. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "Allow_EmptyPrevent - pAllow subsetof pAllow");
  387. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "Allow_EmptyPrevent - pAllow subsetof pPrevent");
  388. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "Allow_EmptyPrevent - pPrevent subsetof pAllow");
  389. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "Allow_EmptyPrevent - pPrevent subsetof pPrevent");
  390. }
  391. [Test]
  392. public void IsSubsetOf_AllowPreventSame_KeyRestrictionBehavior ()
  393. {
  394. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  395. pAllow.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.AllowOnly);
  396. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  397. pPrevent.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
  398. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "AllowPreventSame - pAllow subsetof pAllow");
  399. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "AllowPreventSame - pAllow subsetof pPrevent");
  400. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "AllowPreventSame - pPrevent subsetof pAllow");
  401. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "AllowPreventSame - pPrevent subsetof pPrevent");
  402. }
  403. [Test]
  404. public void IsSubsetOf_AllowPreventDifferent_KeyRestrictionBehavior ()
  405. {
  406. NonAbstractDBDataPermission pAllow1 = new NonAbstractDBDataPermission (PermissionState.None);
  407. pAllow1.Add (defaultConnectString, "security=;", KeyRestrictionBehavior.AllowOnly);
  408. NonAbstractDBDataPermission pAllow2 = new NonAbstractDBDataPermission (PermissionState.None);
  409. pAllow2.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.AllowOnly);
  410. NonAbstractDBDataPermission pPrevent1 = new NonAbstractDBDataPermission (PermissionState.None);
  411. pPrevent1.Add (defaultConnectString, "security=;", KeyRestrictionBehavior.PreventUsage);
  412. NonAbstractDBDataPermission pPrevent2 = new NonAbstractDBDataPermission (PermissionState.None);
  413. pPrevent2.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
  414. Assert.IsTrue (pAllow1.IsSubsetOf (pAllow1), "AllowPreventDifferent - pAllow subsetof pAllow");
  415. Assert.IsTrue (pAllow1.IsSubsetOf (pPrevent2), "AllowPreventDifferent - pAllow subsetof pPrevent");
  416. Assert.IsTrue (pPrevent1.IsSubsetOf (pAllow2), "AllowPreventDifferent - pPrevent subsetof pAllow");
  417. Assert.IsTrue (pPrevent1.IsSubsetOf (pPrevent2), "AllowPreventDifferent - pPrevent subsetof pPrevent");
  418. }
  419. [Test]
  420. [ExpectedException (typeof (ArgumentException))]
  421. public void IsSubsetOf_BadPermission ()
  422. {
  423. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  424. dbdp.IsSubsetOf (new SecurityPermission (SecurityPermissionFlag.Assertion));
  425. }
  426. [Test]
  427. public void Union_Null ()
  428. {
  429. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  430. NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) dbdp.Union (null);
  431. Check ("Empty U null", dbdp, false, false, 0);
  432. dbdp.AllowBlankPassword = true;
  433. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  434. union = (NonAbstractDBDataPermission) dbdp.Union (null);
  435. Check ("Element U null", dbdp, true, false, 1);
  436. dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  437. union = (NonAbstractDBDataPermission) dbdp.Union (null);
  438. Check ("Unrestricted U null", dbdp, false, true, 0);
  439. }
  440. [Test]
  441. public void Union ()
  442. {
  443. NonAbstractDBDataPermission empty = new NonAbstractDBDataPermission (PermissionState.None);
  444. NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) empty.Union (empty);
  445. Assert.IsNull (union, "Empty U Empty");
  446. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
  447. dbdp1.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  448. union = (NonAbstractDBDataPermission) empty.Union (dbdp1);
  449. Check ("Empty U 1", union, false, false, 1);
  450. NonAbstractDBDataPermission dbdp2 = (NonAbstractDBDataPermission)dbdp1.Copy ();
  451. dbdp2.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.AllowOnly);
  452. union = (NonAbstractDBDataPermission) dbdp1.Union (dbdp2);
  453. Check ("1 U 2", union, false, false, 2);
  454. NonAbstractDBDataPermission dbdp3 = new NonAbstractDBDataPermission (PermissionState.None);
  455. dbdp3.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  456. union = (NonAbstractDBDataPermission) dbdp2.Union (dbdp3);
  457. Check ("2 U 3", union, false, false, 2);
  458. NonAbstractDBDataPermission dbdp4 = new NonAbstractDBDataPermission (PermissionState.None);
  459. dbdp4.Add (defaultConnectString, "Data Source=;", KeyRestrictionBehavior.PreventUsage);
  460. union = (NonAbstractDBDataPermission) dbdp3.Union (dbdp4);
  461. Check ("3 U 4", union, false, false, 1);
  462. NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  463. union = (NonAbstractDBDataPermission) unr.Union (empty);
  464. Check ("unrestricted U empty", union, false, true, 0);
  465. union = (NonAbstractDBDataPermission)unr.Union (dbdp4);
  466. Check ("unrestricted U 4", union, false, true, 0);
  467. }
  468. [Test]
  469. public void Union_AllowBlankPassword ()
  470. {
  471. NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
  472. ptrue.AllowBlankPassword = true;
  473. ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  474. NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
  475. pfalse.AllowBlankPassword = false;
  476. pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  477. NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) ptrue.Union (ptrue);
  478. Check ("true U true", union, true, false, 1);
  479. union = (NonAbstractDBDataPermission)ptrue.Union (pfalse);
  480. Check ("true U false", union, true, false, 1);
  481. union = (NonAbstractDBDataPermission)pfalse.Union (ptrue);
  482. Check ("false U true", union, true, false, 1);
  483. union = (NonAbstractDBDataPermission)pfalse.Union (pfalse);
  484. Check ("false U false", union, false, false, 1);
  485. }
  486. [Test]
  487. [ExpectedException (typeof (ArgumentException))]
  488. public void Union_BadPermission ()
  489. {
  490. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  491. dbdp1.Union (new SecurityPermission (SecurityPermissionFlag.Assertion));
  492. }
  493. [Test]
  494. [ExpectedException (typeof (ArgumentNullException))]
  495. public void FromXml_Null ()
  496. {
  497. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  498. elp.FromXml (null);
  499. }
  500. [Test]
  501. [ExpectedException (typeof (ArgumentException))]
  502. public void FromXml_WrongTag ()
  503. {
  504. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  505. SecurityElement se = elp.ToXml ();
  506. se.Tag = "IMono";
  507. elp.FromXml (se);
  508. }
  509. [Test]
  510. [ExpectedException (typeof (ArgumentException))]
  511. public void FromXml_WrongTagCase ()
  512. {
  513. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  514. SecurityElement se = elp.ToXml ();
  515. se.Tag = "IPERMISSION"; // instead of IPermission
  516. elp.FromXml (se);
  517. }
  518. [Test]
  519. public void FromXml_WrongClass ()
  520. {
  521. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  522. SecurityElement se = elp.ToXml ();
  523. SecurityElement w = new SecurityElement (se.Tag);
  524. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  525. w.AddAttribute ("version", se.Attribute ("version"));
  526. elp.FromXml (w);
  527. // doesn't care of the class name at that stage
  528. // anyway the class has already be created so...
  529. }
  530. [Test]
  531. public void FromXml_NoClass ()
  532. {
  533. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  534. SecurityElement se = elp.ToXml ();
  535. SecurityElement w = new SecurityElement (se.Tag);
  536. w.AddAttribute ("version", se.Attribute ("version"));
  537. elp.FromXml (w);
  538. // doesn't even care of the class attribute presence
  539. }
  540. [Test]
  541. [ExpectedException (typeof (ArgumentException))]
  542. public void FromXml_WrongVersion ()
  543. {
  544. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  545. SecurityElement se = elp.ToXml ();
  546. se.Attributes.Remove ("version");
  547. se.Attributes.Add ("version", "2");
  548. elp.FromXml (se);
  549. }
  550. [Test]
  551. public void FromXml_NoVersion ()
  552. {
  553. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  554. SecurityElement se = elp.ToXml ();
  555. SecurityElement w = new SecurityElement (se.Tag);
  556. w.AddAttribute ("class", se.Attribute ("class"));
  557. elp.FromXml (w);
  558. }
  559. }
  560. }