2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / NetCodeGroupTest.cs
1 //
2 // MonoTests.System.Security.Policy.NetCodeGroupTest
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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
30 using NUnit.Framework;
31 using System;
32 using System.Collections;
33 using System.Security;
34 using System.Security.Policy;
35 using System.Security.Permissions;
36
37 namespace MonoTests.System.Security.Policy {
38
39         [TestFixture]
40         public class NetCodeGroupTest  {
41
42                 [Test]
43                 [ExpectedException (typeof (ArgumentNullException))]
44                 public void Constructor_Null () 
45                 {
46                         NetCodeGroup cg = new NetCodeGroup ((IMembershipCondition)null);
47                 }
48
49                 [Test]
50                 public void Constructor () 
51                 {
52                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
53                         Assert.IsNotNull (cg.MembershipCondition, "MembershipCondition");
54                         Assert.IsNull (cg.PolicyStatement, "PolicyStatement");
55                         // documented as always null
56                         Assert.IsNull (cg.AttributeString, "AttributeString");
57 #if NET_2_0
58                         // seems it's easier to change code than to change code ;)
59                         Assert.AreEqual ("Same site Web", cg.PermissionSetName, "PermissionSetName");
60 #else
61                         // documented as always "Same site Web" but it's "Same site Web." (missing .)
62                         Assert.AreEqual ("Same site Web.", cg.PermissionSetName, "PermissionSetName");
63 #endif
64                 }
65
66                 [Test]
67                 public void MergeLogic () 
68                 {
69                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
70                         Assert.AreEqual ("Union", cg.MergeLogic, "MergeLogic");
71                 }
72
73                 [Test]
74                 public void Copy () 
75                 {
76                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
77                         NetCodeGroup cg2 = (NetCodeGroup) cg.Copy ();
78                         Assert.AreEqual (cg.AttributeString, cg2.AttributeString, "AttributeString");
79                         Assert.AreEqual (cg.Children.Count, cg2.Children.Count, "Children");
80                         Assert.AreEqual (cg.Description, cg2.Description, "Description");
81                         Assert.AreEqual (cg.MergeLogic, cg2.MergeLogic, "MergeLogic");
82                         Assert.AreEqual (cg.Name, cg2.Name, "Name");
83                         Assert.AreEqual (cg.PermissionSetName, cg2.PermissionSetName, "PermissionSetName");
84                         Assert.AreEqual (cg.ToXml ().ToString (), cg2.ToXml ().ToString (), "ToXml");
85                 }
86
87                 [Test]
88                 public void CopyWithChildren () 
89                 {
90                         NetCodeGroup cgChild = new NetCodeGroup (new AllMembershipCondition ());
91                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
92                         cg.AddChild (cgChild);
93                         NetCodeGroup cg2 = (NetCodeGroup) cg.Copy ();
94                         Assert.AreEqual (cg.Children.Count, cg2.Children.Count, "Children");
95                         Assert.AreEqual (cg.ToXml ().ToString (), cg2.ToXml ().ToString (), "ToXml");
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void Resolve_Null () 
101                 {
102                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
103                         cg.Resolve (null);
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentNullException))]
108                 public void ResolveMatchingCodeGroups_Null () 
109                 {
110                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
111                         cg.ResolveMatchingCodeGroups (null);
112                 }
113
114                 [Test]
115                 public void ToFromXmlRoundtrip () 
116                 {
117                         NetCodeGroup cg = new NetCodeGroup (new AllMembershipCondition ());
118                         cg.Name = "SomeName";
119                         cg.Description = "Some Description";
120                         Assert.IsTrue (cg.Equals (cg), "Equals (itself)");
121                         SecurityElement se = cg.ToXml ();
122
123                         NetCodeGroup cg2 = new NetCodeGroup (new AllMembershipCondition());
124                         cg2.Name = "SomeOtherName";
125                         cg2.Description = "Some Other Description";
126                         Assert.IsTrue (!cg.Equals (cg2), "Equals (another)");
127
128                         cg2.FromXml (se);
129                         Assert.IsTrue (cg.Equals (cg2), "Equals (FromXml)");
130                 }
131         }
132 }