DBDataPermissionTest.cs 28 KB

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