PermissionSetCollectionTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #if NET_2_0 && !TARGET_JVM
  30. using NUnit.Framework;
  31. using System;
  32. using System.Collections;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. namespace MonoTests.System.Security {
  36. // "alternate" IList implementation
  37. class TestList : IList {
  38. private IList l;
  39. public TestList ()
  40. {
  41. l = (IList) new ArrayList ();
  42. }
  43. public int Add (object value)
  44. {
  45. return l.Add (value);
  46. }
  47. public void Clear ()
  48. {
  49. l.Clear ();
  50. }
  51. public bool Contains (object value)
  52. {
  53. return l.Contains (value);
  54. }
  55. public int IndexOf (object value)
  56. {
  57. return l.IndexOf (value);
  58. }
  59. public void Insert (int index, object value)
  60. {
  61. l.Insert (index, value);
  62. }
  63. public bool IsFixedSize {
  64. get { return l.IsFixedSize; }
  65. }
  66. public bool IsReadOnly {
  67. get { return l.IsReadOnly; }
  68. }
  69. public void Remove (object value)
  70. {
  71. l.Remove (value);
  72. }
  73. public void RemoveAt (int index)
  74. {
  75. l.RemoveAt (index);
  76. }
  77. public object this [int index] {
  78. get { return l [index]; }
  79. set { l [index] = value; }
  80. }
  81. public void CopyTo (Array array, int index)
  82. {
  83. l.CopyTo (array, index);
  84. }
  85. public int Count {
  86. get { return l.Count; }
  87. }
  88. public bool IsSynchronized {
  89. get { return l.IsSynchronized; }
  90. }
  91. public object SyncRoot {
  92. get { return l.SyncRoot; }
  93. }
  94. public IEnumerator GetEnumerator ()
  95. {
  96. return l.GetEnumerator ();
  97. }
  98. }
  99. [TestFixture]
  100. public class PermissionSetCollectionTest {
  101. [Test]
  102. public void Constructor ()
  103. {
  104. PermissionSetCollection psc = new PermissionSetCollection ();
  105. Assert.AreEqual (0, psc.Count, "Count");
  106. Assert.IsFalse (psc.IsSynchronized, "IsSynchronized");
  107. Assert.AreEqual (0, psc.PermissionSets.Count, "PermissionSets.Count");
  108. Assert.AreEqual (psc.ToXml ().ToString (), psc.ToString (), "ToXml().ToString()==ToString()");
  109. Assert.IsNotNull (psc.GetEnumerator (), "GetEnumerator");
  110. }
  111. [Test]
  112. [ExpectedException (typeof (NotSupportedException))]
  113. public void SyncRoot ()
  114. {
  115. PermissionSetCollection psc = new PermissionSetCollection ();
  116. Assert.IsNull (psc.SyncRoot, "SyncRoot");
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentNullException))]
  120. public void Add_Null ()
  121. {
  122. PermissionSetCollection psc = new PermissionSetCollection ();
  123. psc.Add (null);
  124. }
  125. [Test]
  126. public void Add ()
  127. {
  128. PermissionSet none = new PermissionSet (PermissionState.None);
  129. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  130. PermissionSetCollection psc = new PermissionSetCollection ();
  131. Assert.AreEqual (0, psc.PermissionSets.Count, "Count-0");
  132. Assert.AreEqual (0, psc.Count, "Count-0");
  133. psc.Add (none);
  134. Assert.AreEqual (1, psc.PermissionSets.Count, "Count-1");
  135. // re-add same permissionset
  136. psc.Add (none);
  137. Assert.AreEqual (2, psc.PermissionSets.Count, "Count-2");
  138. psc.Add (unr);
  139. Assert.AreEqual (3, psc.PermissionSets.Count, "Count-3");
  140. Assert.AreEqual (3, psc.Count, "Count-3");
  141. }
  142. [Test]
  143. public void Copy ()
  144. {
  145. PermissionSet none = new PermissionSet (PermissionState.None);
  146. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  147. PermissionSetCollection psc = new PermissionSetCollection ();
  148. PermissionSetCollection copy = psc.Copy ();
  149. Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0");
  150. psc.Add (none);
  151. Assert.AreEqual (0, copy.PermissionSets.Count, "Count-0b");
  152. copy = psc.Copy ();
  153. Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
  154. psc.Add (none); // re-add same permissionset
  155. Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1b");
  156. copy = psc.Copy ();
  157. Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2");
  158. psc.Add (unr);
  159. Assert.AreEqual (2, copy.PermissionSets.Count, "Count-2b");
  160. copy = psc.Copy ();
  161. Assert.AreEqual (3, copy.PermissionSets.Count, "Count-3");
  162. Assert.AreEqual (3, copy.Count, "Count-3");
  163. }
  164. [Test]
  165. public void Copy_References ()
  166. {
  167. PermissionSet none = new PermissionSet (PermissionState.None);
  168. PermissionSetCollection psc = new PermissionSetCollection ();
  169. psc.Add (none);
  170. PermissionSetCollection copy = psc.Copy ();
  171. Assert.AreEqual (1, copy.PermissionSets.Count, "Count-1");
  172. string before = psc.ToString ();
  173. none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
  174. Assert.AreEqual (none.ToString (), psc.PermissionSets[0].ToString (), "psc");
  175. Assert.AreEqual (before, copy.ToString (), "copy");
  176. }
  177. [Test]
  178. [ExpectedException (typeof (NotSupportedException))]
  179. public void CopyTo ()
  180. {
  181. PermissionSetCollection psc = new PermissionSetCollection ();
  182. psc.CopyTo (null, 0);
  183. }
  184. [Test]
  185. [ExpectedException (typeof (NotSupportedException))]
  186. public void CopyTo_ICollection ()
  187. {
  188. PermissionSetCollection psc = new PermissionSetCollection ();
  189. ICollection c = (psc as ICollection);
  190. c.CopyTo (null, 0);
  191. }
  192. [Test]
  193. [ExpectedException (typeof (ArgumentNullException))]
  194. public void FromXml_Null ()
  195. {
  196. PermissionSetCollection psc = new PermissionSetCollection ();
  197. psc.FromXml (null);
  198. }
  199. [Test]
  200. [ExpectedException (typeof (ArgumentException))]
  201. public void FromXml_BadName ()
  202. {
  203. PermissionSetCollection psc = new PermissionSetCollection ();
  204. SecurityElement se = new SecurityElement ("PermissionZetCollection");
  205. psc.FromXml (se);
  206. }
  207. [Test]
  208. public void FromXml_Roundtrip ()
  209. {
  210. PermissionSetCollection psc = new PermissionSetCollection ();
  211. string expected = psc.ToString ();
  212. SecurityElement se = psc.ToXml ();
  213. psc.FromXml (se);
  214. string actual = psc.ToString ();
  215. Assert.AreEqual (expected, actual, "Empty");
  216. PermissionSet none = new PermissionSet (PermissionState.None);
  217. psc.Add (none);
  218. expected = psc.ToString ();
  219. se = psc.ToXml ();
  220. psc.FromXml (se);
  221. actual = psc.ToString ();
  222. Assert.AreEqual (expected, actual, "1-None");
  223. none.AddPermission (new SecurityPermission (SecurityPermissionFlag.Assertion));
  224. expected = psc.ToString ();
  225. se = psc.ToXml ();
  226. psc.FromXml (se);
  227. actual = psc.ToString ();
  228. Assert.AreEqual (expected, actual, "1-Assertion");
  229. Assert.AreEqual (1, psc.Count, "1");
  230. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  231. psc.Add (unr);
  232. expected = psc.ToString ();
  233. se = psc.ToXml ();
  234. psc.FromXml (se);
  235. actual = psc.ToString ();
  236. Assert.AreEqual (expected, actual, "2-Assertion+Unrestricted");
  237. Assert.AreEqual (2, psc.Count, "2");
  238. }
  239. [Test]
  240. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  241. public void GetSet_Negative ()
  242. {
  243. PermissionSetCollection psc = new PermissionSetCollection ();
  244. psc.GetSet (-1);
  245. }
  246. [Test]
  247. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  248. public void GetSet_Zero_Empty ()
  249. {
  250. PermissionSetCollection psc = new PermissionSetCollection ();
  251. psc.GetSet (0);
  252. }
  253. [Test]
  254. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  255. public void GetSet_MaxInt ()
  256. {
  257. PermissionSetCollection psc = new PermissionSetCollection ();
  258. psc.GetSet (Int32.MaxValue);
  259. }
  260. [Test]
  261. public void GetSet ()
  262. {
  263. PermissionSetCollection psc = new PermissionSetCollection ();
  264. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  265. psc.Add (unr);
  266. PermissionSet ps = psc.GetSet (0);
  267. Assert.AreEqual (unr.ToString (), ps.ToString (), "Same XML");
  268. Assert.IsTrue (Object.ReferenceEquals (unr, ps), "Same Object Reference");
  269. }
  270. [Test]
  271. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  272. public void RemoveSet_Negative ()
  273. {
  274. PermissionSetCollection psc = new PermissionSetCollection ();
  275. psc.RemoveSet (-1);
  276. }
  277. [Test]
  278. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  279. public void RemoveSet_Zero_Empty ()
  280. {
  281. PermissionSetCollection psc = new PermissionSetCollection ();
  282. psc.RemoveSet (0);
  283. }
  284. [Test]
  285. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  286. public void RemoveSet_MaxInt ()
  287. {
  288. PermissionSetCollection psc = new PermissionSetCollection ();
  289. psc.RemoveSet (Int32.MaxValue);
  290. }
  291. [Test]
  292. public void RemoveSet ()
  293. {
  294. PermissionSetCollection psc = new PermissionSetCollection ();
  295. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  296. psc.Add (unr);
  297. psc.RemoveSet (0);
  298. Assert.AreEqual (0, psc.Count, "Count");
  299. }
  300. [Test]
  301. public void ToXml ()
  302. {
  303. PermissionSetCollection psc = new PermissionSetCollection ();
  304. SecurityElement se = psc.ToXml ();
  305. Assert.IsNull (se.Children, "Children==null for 0");
  306. PermissionSet unr = new PermissionSet (PermissionState.Unrestricted);
  307. psc.Add (unr);
  308. se = psc.ToXml ();
  309. Assert.AreEqual (1, se.Children.Count, "Children==1");
  310. Assert.AreEqual (unr.ToString (), se.Children[0].ToString (), "XML");
  311. }
  312. }
  313. }
  314. #endif