PermissionSetCollectionTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // PermissionSetCollectionTest.cs
  3. // - NUnit Test Cases for PermissionSetCollection
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. namespace MonoTests.System.Security {
  35. // "alternate" IList implementation
  36. class TestList : IList {
  37. private IList l;
  38. public TestList ()
  39. {
  40. l = (IList) new ArrayList ();
  41. }
  42. public int Add (object value)
  43. {
  44. return l.Add (value);
  45. }
  46. public void Clear ()
  47. {
  48. l.Clear ();
  49. }
  50. public bool Contains (object value)
  51. {
  52. return l.Contains (value);
  53. }
  54. public int IndexOf (object value)
  55. {
  56. return l.IndexOf (value);
  57. }
  58. public void Insert (int index, object value)
  59. {
  60. l.Insert (index, value);
  61. }
  62. public bool IsFixedSize {
  63. get { return l.IsFixedSize; }
  64. }
  65. public bool IsReadOnly {
  66. get { return l.IsReadOnly; }
  67. }
  68. public void Remove (object value)
  69. {
  70. l.Remove (value);
  71. }
  72. public void RemoveAt (int index)
  73. {
  74. l.RemoveAt (index);
  75. }
  76. public object this [int index] {
  77. get { return l [index]; }
  78. set { l [index] = value; }
  79. }
  80. public void CopyTo (Array array, int index)
  81. {
  82. l.CopyTo (array, index);
  83. }
  84. public int Count {
  85. get { return l.Count; }
  86. }
  87. public bool IsSynchronized {
  88. get { return l.IsSynchronized; }
  89. }
  90. public object SyncRoot {
  91. get { return l.SyncRoot; }
  92. }
  93. public IEnumerator GetEnumerator ()
  94. {
  95. return l.GetEnumerator ();
  96. }
  97. }
  98. [TestFixture]
  99. public class PermissionSetCollectionTest {
  100. [Test]
  101. public void Constructor ()
  102. {
  103. PermissionSetCollection psc = new PermissionSetCollection ();
  104. Assert.AreEqual (0, psc.Count, "Count");
  105. Assert.IsFalse (psc.IsSynchronized, "IsSynchronized");
  106. Assert.AreEqual (0, psc.PermissionSets.Count, "PermissionSets.Count");
  107. Assert.AreEqual (psc.ToXml ().ToString (), psc.ToString (), "ToXml().ToString()==ToString()");
  108. Assert.IsNotNull (psc.GetEnumerator (), "GetEnumerator");
  109. }
  110. [Test]
  111. [ExpectedException (typeof (NotSupportedException))]
  112. public void SyncRoot ()
  113. {
  114. PermissionSetCollection psc = new PermissionSetCollection ();
  115. Assert.IsNull (psc.SyncRoot, "SyncRoot");
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentNullException))]
  119. public void Add_Null ()
  120. {
  121. PermissionSetCollection psc = new PermissionSetCollection ();
  122. psc.Add (null);
  123. }
  124. [Test]
  125. public void Add ()
  126. {
  127. PermissionSet none = new PermissionSet (PermissionState.None);
  128. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  129. PermissionSetCollection psc = new PermissionSetCollection ();
  130. Assert.AreEqual (0, psc.PermissionSets.Count, "Count-0");
  131. Assert.AreEqual (0, psc.Count, "Count-0");
  132. psc.Add (none);
  133. Assert.AreEqual (1, psc.PermissionSets.Count, "Count-1");
  134. // re-add same permissionset
  135. psc.Add (none);
  136. Assert.AreEqual (2, psc.PermissionSets.Count, "Count-2");
  137. psc.Add (unr);
  138. Assert.AreEqual (3, psc.PermissionSets.Count, "Count-3");
  139. Assert.AreEqual (3, psc.Count, "Count-3");
  140. }
  141. [Test]
  142. public void Copy ()
  143. {
  144. PermissionSet none = new PermissionSet (PermissionState.None);
  145. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  146. PermissionSetCollection psc = new PermissionSetCollection ();
  147. PermissionSetCollection copy = psc.Copy ();
  148. Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0");
  149. psc.Add (none);
  150. Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0b");
  151. copy = psc.Copy ();
  152. Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
  153. psc.Add (none); // re-add same permissionset
  154. Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1b");
  155. copy = psc.Copy ();
  156. Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2");
  157. psc.Add (unr);
  158. Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2b");
  159. copy = psc.Copy ();
  160. Assert.AreEqual (3, copy.PermissionSets.Count, "Count-3");
  161. Assert.AreEqual (3, copy.Count, "Count-3");
  162. }
  163. [Test]
  164. public void Copy_References ()
  165. {
  166. PermissionSet none = new PermissionSet (PermissionState.None);
  167. PermissionSetCollection psc = new PermissionSetCollection ();
  168. psc.Add (none);
  169. PermissionSetCollection copy = psc.Copy ();
  170. Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
  171. string before = psc.ToString ();
  172. none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
  173. Assert.AreEqual (none.ToString (), psc.PermissionSets[0].ToString (), "psc");
  174. Assert.AreEqual (before, copy.ToString (), "copy");
  175. }
  176. [Test]
  177. [ExpectedException (typeof (NotSupportedException))]
  178. public void CopyTo ()
  179. {
  180. PermissionSetCollection psc = new PermissionSetCollection ();
  181. psc.CopyTo (null, 0);
  182. }
  183. [Test]
  184. [ExpectedException (typeof (NotSupportedException))]
  185. public void CopyTo_ICollection ()
  186. {
  187. PermissionSetCollection psc = new PermissionSetCollection ();
  188. ICollection c = (psc as ICollection);
  189. c.CopyTo (null, 0);
  190. }
  191. [Test]
  192. [ExpectedException (typeof (ArgumentNullException))]
  193. public void FromXml_Null ()
  194. {
  195. PermissionSetCollection psc = new PermissionSetCollection ();
  196. psc.FromXml (null);
  197. }
  198. [Test]
  199. [ExpectedException (typeof (ArgumentException))]
  200. public void FromXml_BadName ()
  201. {
  202. PermissionSetCollection psc = new PermissionSetCollection ();
  203. SecurityElement se = new SecurityElement ("PermissionZetCollection");
  204. psc.FromXml (se);
  205. }
  206. [Test]
  207. public void FromXml_Roundtrip ()
  208. {
  209. PermissionSetCollection psc = new PermissionSetCollection ();
  210. string expected = psc.ToString ();
  211. SecurityElement se = psc.ToXml ();
  212. psc.FromXml (se);
  213. string actual = psc.ToString ();
  214. Assert.AreEqual (expected, actual, "Empty");
  215. PermissionSet none = new PermissionSet (PermissionState.None);
  216. psc.Add (none);
  217. expected = psc.ToString ();
  218. se = psc.ToXml ();
  219. psc.FromXml (se);
  220. actual = psc.ToString ();
  221. Assert.AreEqual (expected, actual, "1-None");
  222. none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
  223. expected = psc.ToString ();
  224. se = psc.ToXml ();
  225. psc.FromXml (se);
  226. actual = psc.ToString ();
  227. Assert.AreEqual (expected, actual, "1-Assertion");
  228. Assert.AreEqual (1, psc.Count, "1");
  229. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  230. psc.Add (unr);
  231. expected = psc.ToString ();
  232. se = psc.ToXml ();
  233. psc.FromXml (se);
  234. actual = psc.ToString ();
  235. Assert.AreEqual (expected, actual, "2-Assertion+Unrestricted");
  236. Assert.AreEqual (2, psc.Count, "2");
  237. }
  238. [Test]
  239. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  240. public void GetSet_Negative ()
  241. {
  242. PermissionSetCollection psc = new PermissionSetCollection ();
  243. psc.GetSet (-1);
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  247. public void GetSet_Zero_Empty ()
  248. {
  249. PermissionSetCollection psc = new PermissionSetCollection ();
  250. psc.GetSet (0);
  251. }
  252. [Test]
  253. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  254. public void GetSet_MaxInt ()
  255. {
  256. PermissionSetCollection psc = new PermissionSetCollection ();
  257. psc.GetSet (Int32.MaxValue);
  258. }
  259. [Test]
  260. public void GetSet ()
  261. {
  262. PermissionSetCollection psc = new PermissionSetCollection ();
  263. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  264. psc.Add (unr);
  265. PermissionSet ps = psc.GetSet (0);
  266. Assert.AreEqual (unr.ToString (), ps.ToString (), "Same XML");
  267. Assert.IsTrue (Object.ReferenceEquals (unr, ps), "Same Object Reference");
  268. }
  269. [Test]
  270. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  271. public void RemoveSet_Negative ()
  272. {
  273. PermissionSetCollection psc = new PermissionSetCollection ();
  274. psc.RemoveSet (-1);
  275. }
  276. [Test]
  277. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  278. public void RemoveSet_Zero_Empty ()
  279. {
  280. PermissionSetCollection psc = new PermissionSetCollection ();
  281. psc.RemoveSet (0);
  282. }
  283. [Test]
  284. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  285. public void RemoveSet_MaxInt ()
  286. {
  287. PermissionSetCollection psc = new PermissionSetCollection ();
  288. psc.RemoveSet (Int32.MaxValue);
  289. }
  290. [Test]
  291. public void RemoveSet ()
  292. {
  293. PermissionSetCollection psc = new PermissionSetCollection ();
  294. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  295. psc.Add (unr);
  296. psc.RemoveSet (0);
  297. Assert.AreEqual (0, psc.Count, "Count");
  298. }
  299. [Test]
  300. public void ToXml ()
  301. {
  302. PermissionSetCollection psc = new PermissionSetCollection ();
  303. SecurityElement se = psc.ToXml ();
  304. Assert.IsNull (se.Children, "Children==null for 0");
  305. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  306. psc.Add (unr);
  307. se = psc.ToXml ();
  308. Assert.AreEqual (1, se.Children.Count, "Children==1");
  309. Assert.AreEqual (unr.ToString (), se.Children[0].ToString (), "XML");
  310. }
  311. }
  312. }