DBDataPermissionTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. #if NET_2_0
  189. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  190. #else
  191. [ExpectedException (typeof (ArgumentException))]
  192. #endif
  193. public void Constructor_PermissionState_Invalid ()
  194. {
  195. PermissionState ps = (PermissionState) Int32.MinValue;
  196. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
  197. }
  198. [Test]
  199. public void Constructor_PermissionState_None ()
  200. {
  201. PermissionState ps = PermissionState.None;
  202. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
  203. Check ("PermissionState_None-1", dbdp, false, false, 0);
  204. dbdp.AllowBlankPassword = true;
  205. Check ("PermissionState_None-1", dbdp, true, false, 0);
  206. }
  207. [Test]
  208. public void Constructor_PermissionState_Unrestricted ()
  209. {
  210. PermissionState ps = PermissionState.Unrestricted;
  211. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
  212. Check ("PermissionState_Unrestricted-1", dbdp, false, true, 0);
  213. dbdp.AllowBlankPassword = true;
  214. Check ("PermissionState_Unrestricted-2", dbdp, true, true, 0);
  215. }
  216. [Test]
  217. public void AllowBlankPassword ()
  218. {
  219. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  220. Assert.IsFalse (dbdp.AllowBlankPassword, "Default");
  221. dbdp.AllowBlankPassword = true;
  222. Assert.IsTrue (dbdp.AllowBlankPassword, "True");
  223. dbdp.Clear ();
  224. // clear the connection list - not the permission itself
  225. Assert.IsTrue (dbdp.AllowBlankPassword, "Clear");
  226. dbdp.AllowBlankPassword = false;
  227. Assert.IsFalse (dbdp.AllowBlankPassword, "False");
  228. }
  229. [Test]
  230. public void Add ()
  231. {
  232. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  233. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  234. Assert.AreEqual (1, dbdp.ToXml ().Children.Count, "Count");
  235. NonAbstractDBDataPermission copy = (NonAbstractDBDataPermission)dbdp.Copy ();
  236. Assert.AreEqual (1, copy.ToXml ().Children.Count, "Copy.Count");
  237. dbdp.Clear ();
  238. Assert.IsNull (dbdp.ToXml ().Children, "Clear");
  239. Assert.AreEqual (1, copy.ToXml ().Children.Count, "Copy.Count-2");
  240. }
  241. [Test]
  242. public void Add_Duplicates ()
  243. {
  244. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  245. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  246. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  247. // no exception but a single element is kept
  248. Assert.AreEqual (1, dbdp.ToXml ().Children.Count, "Count");
  249. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  250. dbdp.Clear ();
  251. Assert.IsNull (dbdp.ToXml ().Children, "Clear");
  252. }
  253. [Test]
  254. public void Add_Differents ()
  255. {
  256. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  257. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  258. string connectString = "Data Source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=Northwind;";
  259. dbdp.Add (connectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  260. Assert.AreEqual (2, dbdp.ToXml ().Children.Count, "Count");
  261. dbdp.Clear ();
  262. Assert.IsNull (dbdp.ToXml ().Children, "Clear");
  263. }
  264. [Test]
  265. public void Add_Unrestricted ()
  266. {
  267. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  268. Assert.IsTrue (dbdp.IsUnrestricted (), "IsUnrestricted-1");
  269. // we lose unrestricted by adding an element
  270. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  271. Assert.IsFalse (dbdp.IsUnrestricted (), "IsUnrestricted-2");
  272. // removing the element doesn't regain unrestricted status
  273. dbdp.Clear ();
  274. Assert.IsFalse (dbdp.IsUnrestricted (), "IsUnrestricted-3");
  275. }
  276. [Test]
  277. public void CreateInstance ()
  278. {
  279. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  280. Assert.AreEqual (typeof (NonAbstractDBDataPermission), dbdp.CreateInstance ().GetType (), "Same type");
  281. }
  282. [Test]
  283. public void Intersect_Null ()
  284. {
  285. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  286. // No intersection with null
  287. Assert.IsNull (elp.Intersect (null), "None N null");
  288. }
  289. [Test]
  290. public void Intersect ()
  291. {
  292. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
  293. NonAbstractDBDataPermission dbdp2 = new NonAbstractDBDataPermission (PermissionState.None);
  294. // 1. None N None
  295. NonAbstractDBDataPermission result = (NonAbstractDBDataPermission) dbdp1.Intersect (dbdp2);
  296. Assert.IsNull (result, "Empty N Empty");
  297. // 2. None N Entry
  298. dbdp2.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  299. result = (NonAbstractDBDataPermission)dbdp1.Intersect (dbdp2);
  300. Assert.IsNull (result, "Empty N Entry");
  301. // 3. Entry N None
  302. result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp1);
  303. Assert.IsNull (result, "Entry N Empty");
  304. // 4. Unrestricted N None
  305. NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  306. result = (NonAbstractDBDataPermission)unr.Intersect (dbdp1);
  307. Check ("(Unrestricted N None)", result, false, false, 0);
  308. // 5. None N Unrestricted
  309. result = (NonAbstractDBDataPermission)dbdp1.Intersect (unr);
  310. Check ("(None N Unrestricted)", result, false, false, 0);
  311. // 6. Unrestricted N Unrestricted
  312. result = (NonAbstractDBDataPermission)unr.Intersect (unr);
  313. Check ("(Unrestricted N Unrestricted)", result, false, true, 0);
  314. // 7. Unrestricted N Entry
  315. result = (NonAbstractDBDataPermission)unr.Intersect (dbdp2);
  316. Check ("(Unrestricted N Entry)", result, false, false, 1);
  317. // 8. Entry N Unrestricted
  318. result = (NonAbstractDBDataPermission)dbdp2.Intersect (unr);
  319. Check ("(Entry N Unrestricted)", result, false, false, 1);
  320. // 9. Entry2 N Entry2
  321. result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp2);
  322. Check ("(Entry2 N Entry2)", result, false, false, 1);
  323. // 10. Entry1 N Entry 2
  324. dbdp1.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.PreventUsage);
  325. result = (NonAbstractDBDataPermission)dbdp1.Intersect (dbdp2);
  326. Assert.IsNull (result, "(Entry1 N Entry2)");
  327. // 11. Entry2 N Entry 1
  328. result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp1);
  329. Assert.IsNull (result, "(Entry2 N Entry1)");
  330. }
  331. [Test]
  332. public void Intersect_AllowBlankPassword ()
  333. {
  334. NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
  335. ptrue.AllowBlankPassword = true;
  336. ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  337. NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
  338. pfalse.AllowBlankPassword = false;
  339. pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  340. NonAbstractDBDataPermission intersect = (NonAbstractDBDataPermission)ptrue.Intersect (ptrue);
  341. Check ("true N true", intersect, true, false, 1);
  342. intersect = (NonAbstractDBDataPermission)ptrue.Intersect (pfalse);
  343. Check ("true N false", intersect, false, false, 1);
  344. intersect = (NonAbstractDBDataPermission)pfalse.Intersect (ptrue);
  345. Check ("false N true", intersect, false, false, 1);
  346. intersect = (NonAbstractDBDataPermission)pfalse.Intersect (pfalse);
  347. Check ("false N false", intersect, false, false, 1);
  348. }
  349. [Test]
  350. [ExpectedException (typeof (ArgumentException))]
  351. public void Intersect_BadPermission ()
  352. {
  353. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  354. dbdp.Intersect (new SecurityPermission (SecurityPermissionFlag.Assertion));
  355. }
  356. [Test]
  357. public void IsSubset_Null ()
  358. {
  359. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  360. Assert.IsTrue (dbdp.IsSubsetOf (null), "Empty-null");
  361. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  362. Assert.IsFalse (dbdp.IsSubsetOf (null), "Element-null");
  363. dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  364. Assert.IsFalse (dbdp.IsSubsetOf (null), "Unrestricted-null");
  365. }
  366. [Test]
  367. public void IsSubset ()
  368. {
  369. NonAbstractDBDataPermission empty = new NonAbstractDBDataPermission (PermissionState.None);
  370. Assert.IsTrue (empty.IsSubsetOf (empty), "Empty-Empty");
  371. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
  372. dbdp1.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  373. Assert.IsTrue (empty.IsSubsetOf (dbdp1), "Empty-1");
  374. Assert.IsFalse (dbdp1.IsSubsetOf (empty), "1-Empty");
  375. Assert.IsTrue (dbdp1.IsSubsetOf (dbdp1), "1-1");
  376. NonAbstractDBDataPermission dbdp2 = (NonAbstractDBDataPermission)dbdp1.Copy ();
  377. dbdp2.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.AllowOnly);
  378. Assert.IsTrue (dbdp1.IsSubsetOf (dbdp2), "1-2");
  379. Assert.IsFalse (dbdp2.IsSubsetOf (dbdp1), "2-1");
  380. Assert.IsTrue (dbdp2.IsSubsetOf (dbdp2), "2-2");
  381. NonAbstractDBDataPermission dbdp3 = new NonAbstractDBDataPermission (PermissionState.None);
  382. dbdp3.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  383. Assert.IsTrue (dbdp3.IsSubsetOf (dbdp1), "3-1");
  384. Assert.IsTrue (dbdp1.IsSubsetOf (dbdp3), "1-3");
  385. Assert.IsTrue (dbdp3.IsSubsetOf (dbdp3), "3-3");
  386. NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  387. Assert.IsTrue (dbdp1.IsSubsetOf (unr), "1-unrestricted");
  388. Assert.IsFalse (unr.IsSubsetOf (dbdp1), "unrestricted-1");
  389. Assert.IsTrue (dbdp2.IsSubsetOf (unr), "2-unrestricted");
  390. Assert.IsFalse (unr.IsSubsetOf (dbdp2), "unrestricted-2");
  391. Assert.IsTrue (dbdp3.IsSubsetOf (unr), "3-unrestricted");
  392. Assert.IsFalse (unr.IsSubsetOf (dbdp3), "unrestricted-3");
  393. Assert.IsTrue (unr.IsSubsetOf (unr), "unrestricted-unrestricted");
  394. }
  395. [Test]
  396. public void IsSubsetOf_AllowBlankPassword ()
  397. {
  398. NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
  399. ptrue.AllowBlankPassword = true;
  400. ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  401. NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
  402. pfalse.AllowBlankPassword = false;
  403. pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  404. Assert.IsTrue (ptrue.IsSubsetOf (ptrue), "true subsetof true");
  405. Assert.IsFalse (ptrue.IsSubsetOf (pfalse), "true subsetof false");
  406. Assert.IsTrue (pfalse.IsSubsetOf (ptrue), "false subsetof true");
  407. Assert.IsTrue (pfalse.IsSubsetOf (pfalse), "false subsetof false");
  408. }
  409. [Test]
  410. public void IsSubsetOf_BothEmpty_KeyRestrictionBehavior ()
  411. {
  412. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  413. pAllow.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  414. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  415. pPrevent.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  416. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "BothEmpty - pAllow subsetof pAllow");
  417. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "BothEmpty - pAllow subsetof pPrevent");
  418. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "BothEmpty - pPrevent subsetof pAllow");
  419. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "BothEmpty - pPrevent subsetof pPrevent");
  420. }
  421. [Test]
  422. public void IsSubsetOf_EmptyAllow_Prevent_KeyRestrictionBehavior ()
  423. {
  424. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  425. pAllow.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  426. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  427. pPrevent.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
  428. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "EmptyAllow_Prevent - pAllow subsetof pAllow");
  429. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "EmptyAllow_Prevent - pAllow subsetof pPrevent");
  430. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "EmptyAllow_Prevent - pPrevent subsetof pAllow");
  431. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "EmptyAllow_Prevent - pPrevent subsetof pPrevent");
  432. }
  433. [Test]
  434. public void IsSubsetOf_Allow_EmptyPrevent_KeyRestrictionBehavior ()
  435. {
  436. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  437. pAllow.Add (defaultConnectString, "data source=;", KeyRestrictionBehavior.AllowOnly);
  438. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  439. pPrevent.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  440. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "Allow_EmptyPrevent - pAllow subsetof pAllow");
  441. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "Allow_EmptyPrevent - pAllow subsetof pPrevent");
  442. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "Allow_EmptyPrevent - pPrevent subsetof pAllow");
  443. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "Allow_EmptyPrevent - pPrevent subsetof pPrevent");
  444. }
  445. [Test]
  446. public void IsSubsetOf_AllowPreventSame_KeyRestrictionBehavior ()
  447. {
  448. NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
  449. pAllow.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.AllowOnly);
  450. NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
  451. pPrevent.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
  452. Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "AllowPreventSame - pAllow subsetof pAllow");
  453. Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "AllowPreventSame - pAllow subsetof pPrevent");
  454. Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "AllowPreventSame - pPrevent subsetof pAllow");
  455. Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "AllowPreventSame - pPrevent subsetof pPrevent");
  456. }
  457. [Test]
  458. public void IsSubsetOf_AllowPreventDifferent_KeyRestrictionBehavior ()
  459. {
  460. NonAbstractDBDataPermission pAllow1 = new NonAbstractDBDataPermission (PermissionState.None);
  461. pAllow1.Add (defaultConnectString, "security=;", KeyRestrictionBehavior.AllowOnly);
  462. NonAbstractDBDataPermission pAllow2 = new NonAbstractDBDataPermission (PermissionState.None);
  463. pAllow2.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.AllowOnly);
  464. NonAbstractDBDataPermission pPrevent1 = new NonAbstractDBDataPermission (PermissionState.None);
  465. pPrevent1.Add (defaultConnectString, "security=;", KeyRestrictionBehavior.PreventUsage);
  466. NonAbstractDBDataPermission pPrevent2 = new NonAbstractDBDataPermission (PermissionState.None);
  467. pPrevent2.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
  468. Assert.IsTrue (pAllow1.IsSubsetOf (pAllow1), "AllowPreventDifferent - pAllow subsetof pAllow");
  469. Assert.IsTrue (pAllow1.IsSubsetOf (pPrevent2), "AllowPreventDifferent - pAllow subsetof pPrevent");
  470. Assert.IsTrue (pPrevent1.IsSubsetOf (pAllow2), "AllowPreventDifferent - pPrevent subsetof pAllow");
  471. Assert.IsTrue (pPrevent1.IsSubsetOf (pPrevent2), "AllowPreventDifferent - pPrevent subsetof pPrevent");
  472. }
  473. [Test]
  474. [ExpectedException (typeof (ArgumentException))]
  475. public void IsSubsetOf_BadPermission ()
  476. {
  477. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  478. dbdp.IsSubsetOf (new SecurityPermission (SecurityPermissionFlag.Assertion));
  479. }
  480. [Test]
  481. public void Union_Null ()
  482. {
  483. NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
  484. NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) dbdp.Union (null);
  485. Check ("Empty U null", dbdp, false, false, 0);
  486. dbdp.AllowBlankPassword = true;
  487. dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  488. union = (NonAbstractDBDataPermission) dbdp.Union (null);
  489. Check ("Element U null", dbdp, true, false, 1);
  490. dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  491. union = (NonAbstractDBDataPermission) dbdp.Union (null);
  492. Check ("Unrestricted U null", dbdp, false, true, 0);
  493. }
  494. [Test]
  495. public void Union ()
  496. {
  497. NonAbstractDBDataPermission empty = new NonAbstractDBDataPermission (PermissionState.None);
  498. NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) empty.Union (empty);
  499. Assert.IsNotNull (union, "Empty U Empty");
  500. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
  501. dbdp1.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  502. union = (NonAbstractDBDataPermission) empty.Union (dbdp1);
  503. Check ("Empty U 1", union, false, false, 1);
  504. NonAbstractDBDataPermission dbdp2 = (NonAbstractDBDataPermission)dbdp1.Copy ();
  505. dbdp2.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.AllowOnly);
  506. union = (NonAbstractDBDataPermission) dbdp1.Union (dbdp2);
  507. Check ("1 U 2", union, false, false, 2);
  508. NonAbstractDBDataPermission dbdp3 = new NonAbstractDBDataPermission (PermissionState.None);
  509. dbdp3.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
  510. union = (NonAbstractDBDataPermission) dbdp2.Union (dbdp3);
  511. Check ("2 U 3", union, false, false, 2);
  512. NonAbstractDBDataPermission dbdp4 = new NonAbstractDBDataPermission (PermissionState.None);
  513. dbdp4.Add (defaultConnectString, "Data Source=;", KeyRestrictionBehavior.PreventUsage);
  514. union = (NonAbstractDBDataPermission) dbdp3.Union (dbdp4);
  515. Check ("3 U 4", union, false, false, 1);
  516. NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  517. union = (NonAbstractDBDataPermission) unr.Union (empty);
  518. Check ("unrestricted U empty", union, false, true, 0);
  519. union = (NonAbstractDBDataPermission)unr.Union (dbdp4);
  520. Check ("unrestricted U 4", union, false, true, 0);
  521. }
  522. [Test]
  523. public void Union_AllowBlankPassword ()
  524. {
  525. NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
  526. ptrue.AllowBlankPassword = true;
  527. ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  528. NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
  529. pfalse.AllowBlankPassword = false;
  530. pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
  531. NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) ptrue.Union (ptrue);
  532. Check ("true U true", union, true, false, 1);
  533. union = (NonAbstractDBDataPermission)ptrue.Union (pfalse);
  534. Check ("true U false", union, true, false, 1);
  535. union = (NonAbstractDBDataPermission)pfalse.Union (ptrue);
  536. Check ("false U true", union, true, false, 1);
  537. union = (NonAbstractDBDataPermission)pfalse.Union (pfalse);
  538. Check ("false U false", union, false, false, 1);
  539. }
  540. [Test]
  541. [ExpectedException (typeof (ArgumentException))]
  542. public void Union_BadPermission ()
  543. {
  544. NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
  545. dbdp1.Union (new SecurityPermission (SecurityPermissionFlag.Assertion));
  546. }
  547. [Test]
  548. [ExpectedException (typeof (ArgumentNullException))]
  549. public void FromXml_Null ()
  550. {
  551. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  552. elp.FromXml (null);
  553. }
  554. [Test]
  555. [ExpectedException (typeof (ArgumentException))]
  556. public void FromXml_WrongTag ()
  557. {
  558. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  559. SecurityElement se = elp.ToXml ();
  560. se.Tag = "IMono";
  561. elp.FromXml (se);
  562. }
  563. [Test]
  564. [ExpectedException (typeof (ArgumentException))]
  565. public void FromXml_WrongTagCase ()
  566. {
  567. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  568. SecurityElement se = elp.ToXml ();
  569. se.Tag = "IPERMISSION"; // instead of IPermission
  570. elp.FromXml (se);
  571. }
  572. [Test]
  573. public void FromXml_WrongClass ()
  574. {
  575. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  576. SecurityElement se = elp.ToXml ();
  577. SecurityElement w = new SecurityElement (se.Tag);
  578. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  579. w.AddAttribute ("version", se.Attribute ("version"));
  580. elp.FromXml (w);
  581. // doesn't care of the class name at that stage
  582. // anyway the class has already be created so...
  583. }
  584. [Test]
  585. public void FromXml_NoClass ()
  586. {
  587. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  588. SecurityElement se = elp.ToXml ();
  589. SecurityElement w = new SecurityElement (se.Tag);
  590. w.AddAttribute ("version", se.Attribute ("version"));
  591. elp.FromXml (w);
  592. // doesn't even care of the class attribute presence
  593. }
  594. [Test]
  595. [ExpectedException (typeof (ArgumentException))]
  596. public void FromXml_WrongVersion ()
  597. {
  598. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  599. SecurityElement se = elp.ToXml ();
  600. se.Attributes.Remove ("version");
  601. se.Attributes.Add ("version", "2");
  602. elp.FromXml (se);
  603. }
  604. [Test]
  605. public void FromXml_NoVersion ()
  606. {
  607. NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
  608. SecurityElement se = elp.ToXml ();
  609. SecurityElement w = new SecurityElement (se.Tag);
  610. w.AddAttribute ("class", se.Attribute ("class"));
  611. elp.FromXml (w);
  612. }
  613. }
  614. }