DBDataPermissionTest.cs 27 KB

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