2 // SecurityElementTest.cs - NUnit Test Cases for System.Security.SecurityElement
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Sebastien Pouliot (spouliot@motus.com)
8 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
11 using NUnit
.Framework
;
13 using System
.Collections
;
14 using System
.Security
;
16 namespace MonoTests
.System
.Security
{
19 public class SecurityElementTest
: Assertion
{
26 elem
= CreateElement ();
29 private SecurityElement
CreateElement ()
31 SecurityElement elem
= new SecurityElement ("IPermission");
32 elem
.AddAttribute ("class", "System");
33 elem
.AddAttribute ("version", "1");
35 SecurityElement child
= new SecurityElement ("ConnectAccess");
36 elem
.AddChild (child
);
38 SecurityElement grandchild
= new SecurityElement ("ENDPOINT", "some text");
39 grandchild
.AddAttribute ("transport", "All");
40 grandchild
.AddAttribute ("host", "localhost");
41 grandchild
.AddAttribute ("port", "8080");
42 child
.AddChild (grandchild
);
44 SecurityElement grandchild2
= new SecurityElement ("ENDPOINT");
45 grandchild2
.AddAttribute ("transport", "Tcp");
46 grandchild2
.AddAttribute ("host", "www.ximian.com");
47 grandchild2
.AddAttribute ("port", "All");
48 child
.AddChild (grandchild2
);
54 public void ConstructorsTagTest ()
56 SecurityElement se
= new SecurityElement ("tag", "text");
57 AssertNull ("EmptyAttributes", se
.Attributes
);
58 AssertNull ("EmptyChildren", se
.Children
);
59 AssertEquals ("Tag", "tag", se
.Tag
);
60 AssertEquals ("Text", "text", se
.Text
);
64 [ExpectedException (typeof (ArgumentNullException
))]
65 public void ConstructorsTagNullText ()
67 SecurityElement se
= new SecurityElement (null, "text");
71 public void ConstructorsTagTextNull ()
73 SecurityElement se
= new SecurityElement ("tag", null);
74 AssertNull ("EmptyAttributes", se
.Attributes
);
75 AssertNull ("EmptyChildren", se
.Children
);
76 AssertEquals ("Tag", "tag", se
.Tag
);
77 AssertNull ("Text", se
.Text
);
81 public void ConstructorsTag ()
83 SecurityElement se
= new SecurityElement ("tag");
84 AssertNull ("EmptyAttributes", se
.Attributes
);
85 AssertNull ("EmptyChildren", se
.Children
);
86 AssertEquals ("Tag", "tag", se
.Tag
);
87 AssertNull ("Text", se
.Text
);
91 [ExpectedException (typeof (ArgumentNullException
))]
92 public void ConstructorsTagNull ()
94 SecurityElement se
= new SecurityElement (null);
98 [ExpectedException (typeof (ArgumentNullException
))]
99 public void AddAttribute_NameNullValue ()
101 elem
.AddAttribute (null, "valid");
105 [ExpectedException (typeof (ArgumentNullException
))]
106 public void AddAttribute_NameValueNull ()
108 elem
.AddAttribute ("valid", null);
112 [ExpectedException (typeof (ArgumentException
))]
113 public void AddAttribute_InvalidName ()
115 elem
.AddAttribute ("<invalid>", "valid");
119 [ExpectedException (typeof (ArgumentException
))]
120 public void AddAttribute_InvalidValue ()
122 elem
.AddAttribute ("valid", "invalid\"");
126 public void AddAttribute_InvalidValue2 ()
128 elem
.AddAttribute ("valid", "valid&");
129 // in xml world this is actually not considered valid
130 // but it is by MS.Net
134 [ExpectedException (typeof (ArgumentException
))]
135 public void AddAttribute_InvalidValue3 ()
137 elem
.AddAttribute ("valid", "<invalid>");
141 [ExpectedException (typeof (ArgumentException
))]
142 public void AddAttribute_Duplicate ()
144 elem
.AddAttribute ("valid", "first time");
145 elem
.AddAttribute ("valid", "second time");
149 public void AddAttribute ()
151 elem
.AddAttribute ("valid", "valid\'");
155 [ExpectedException (typeof (ArgumentNullException
))]
156 public void AddChild_Null ()
158 elem
.AddChild (null);
162 public void AddChild ()
164 int n
= elem
.Children
.Count
;
166 elem
.AddChild (elem
);
167 AssertEquals ("Count", (n
+1), elem
.Children
.Count
);
171 [ExpectedException (typeof (ArgumentException
))]
172 [Ignore ("this will result in an InvalidCastException on MS.Net - I have no clue why")]
173 public void Attributes_StrangeCase ()
175 Hashtable h
= elem
.Attributes
;
176 h
.Add ("<invalid>", "valid");
181 [ExpectedException (typeof (ArgumentException
))]
182 public void Attributes_ArgumentException ()
184 Hashtable h
= elem
.Attributes
;
185 h
.Add ("valid", "\"invalid\"");
190 public void Attributes ()
192 Hashtable h
= elem
.Attributes
;
195 h
.Add ("foo", "bar");
196 Assert ("#1", elem
.Attributes
.Count
!= h
.Count
);
199 AssertNotNull ("#2", elem
.Attribute ("foo"));
206 SecurityElement elem2
= CreateElement ();
208 Assert ("#1", elem
.Equal (elem2
));
210 SecurityElement child
= (SecurityElement
) elem2
.Children
[0];
212 child
= (SecurityElement
) child
.Children
[1];
214 child
.Text
= "some text";
216 Assert ("#2", !elem
.Equal (elem2
));
220 public void Escape ()
222 AssertEquals ("#1", "foo<>"'& bar", SecurityElement
.Escape ("foo<>\"'& bar"));
226 public void IsValidAttributeName ()
228 Assert ("#1", !SecurityElement
.IsValidAttributeName ("x x"));
229 Assert ("#2", !SecurityElement
.IsValidAttributeName ("x<x"));
230 Assert ("#3", !SecurityElement
.IsValidAttributeName ("x>x"));
231 Assert ("#4", SecurityElement
.IsValidAttributeName ("x\"x"));
232 Assert ("#5", SecurityElement
.IsValidAttributeName ("x'x"));
233 Assert ("#6", SecurityElement
.IsValidAttributeName ("x&x"));
237 public void IsValidAttributeValue ()
239 Assert ("#1", SecurityElement
.IsValidAttributeValue ("x x"));
240 Assert ("#2", !SecurityElement
.IsValidAttributeValue ("x<x"));
241 Assert ("#3", !SecurityElement
.IsValidAttributeValue ("x>x"));
242 Assert ("#4", !SecurityElement
.IsValidAttributeValue ("x\"x"));
243 Assert ("#5", SecurityElement
.IsValidAttributeValue ("x'x"));
244 Assert ("#6", SecurityElement
.IsValidAttributeValue ("x&x"));
248 public void IsValidTag ()
250 Assert ("#1", !SecurityElement
.IsValidTag ("x x"));
251 Assert ("#2", !SecurityElement
.IsValidTag ("x<x"));
252 Assert ("#3", !SecurityElement
.IsValidTag ("x>x"));
253 Assert ("#4", SecurityElement
.IsValidTag ("x\"x"));
254 Assert ("#5", SecurityElement
.IsValidTag ("x'x"));
255 Assert ("#6", SecurityElement
.IsValidTag ("x&x"));
259 public void IsValidText ()
261 Assert ("#1", SecurityElement
.IsValidText ("x x"));
262 Assert ("#2", !SecurityElement
.IsValidText ("x<x"));
263 Assert ("#3", !SecurityElement
.IsValidText ("x>x"));
264 Assert ("#4", SecurityElement
.IsValidText ("x\"x"));
265 Assert ("#5", SecurityElement
.IsValidText ("x'x"));
266 Assert ("#6", SecurityElement
.IsValidText ("x&x"));
270 [ExpectedException (typeof (ArgumentNullException
))]
271 public void SearchForChildByTag_Null ()
273 SecurityElement child
= elem
.SearchForChildByTag (null);
277 public void SearchForChildByTag ()
279 SecurityElement child
= elem
.SearchForChildByTag ("doesnotexist");
280 AssertNull ("#1", child
);
282 child
= elem
.SearchForChildByTag ("ENDPOINT");
283 AssertNull ("#2", child
);
285 child
= (SecurityElement
) elem
.Children
[0];
286 child
= child
.SearchForChildByTag ("ENDPOINT");
287 AssertEquals ("#3", "All", child
.Attribute ("transport"));
291 [ExpectedException (typeof (ArgumentNullException
))]
292 public void SearchForTextOfTag_Null ()
294 string s
= elem
.SearchForTextOfTag (null);
298 public void SearchForTextOfTag ()
300 string s
= elem
.SearchForTextOfTag ("ENDPOINT");
301 AssertEquals ("SearchForTextOfTag", "some text", s
);
305 [ExpectedException (typeof (ArgumentNullException
))]
306 public void Tag_Null ()
309 AssertNull ("Tag", elem
.Tag
);
313 public void Text_Null ()
316 AssertNull ("Text", elem
.Text
);
320 public void MultipleAttributes ()
322 SecurityElement se
= new SecurityElement ("Multiple");
323 se
.AddAttribute ("Attribute1", "One");
324 se
.AddAttribute ("Attribute2", "Two");
325 string expected
= String
.Format ("<Multiple Attribute1=\"One\"{0} Attribute2=\"Two\"/>{0}", Environment
.NewLine
);
326 AssertEquals ("ToString()", expected
, se
.ToString ());