1 //
2 // SecurityElementTest.cs - NUnit Test Cases for System.Security.SecurityElement
3 //
4 // Authors:
5 //	Lawrence Pit (loz@cable.a2000.nl)
6 //	Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.Security;
35 
36 using NUnit.Framework;
37 
38 namespace MonoTests.System.Security {
39 
40 	[TestFixture]
41 	public class SecurityElementTest {
42 
43 		SecurityElement elem;
44 
45 		[SetUp]
SetUp()46 		public void SetUp ()
47 		{
48 			elem = CreateElement ();
49 		}
50 
CreateElement()51 		private SecurityElement CreateElement ()
52 		{
53 			SecurityElement elem = new SecurityElement ("IPermission");
54 			elem.AddAttribute ("class", "System");
55 			elem.AddAttribute ("version", "1");
56 
57 			SecurityElement child = new SecurityElement ("ConnectAccess");
58 			elem.AddChild (child);
59 
60 			SecurityElement grandchild = new SecurityElement ("ENDPOINT", "some text");
61 			grandchild.AddAttribute ("transport", "All");
62 			grandchild.AddAttribute ("host", "localhost");
63 			grandchild.AddAttribute ("port", "8080");
64 			child.AddChild (grandchild);
65 
66 			SecurityElement grandchild2 = new SecurityElement ("ENDPOINT");
67 			grandchild2.AddAttribute ("transport", "Tcp");
68 			grandchild2.AddAttribute ("host", "www.ximian.com");
69 			grandchild2.AddAttribute ("port", "All");
70 			child.AddChild (grandchild2);
71 
72 			return elem;
73 		}
74 
75 		[Test]
Constructor1()76 		public void Constructor1 ()
77 		{
78 			SecurityElement se = new SecurityElement ("tag");
79 			Assert.IsNull (se.Attributes, "#A1");
80 			Assert.IsNull (se.Children, "#A2");
81 			Assert.AreEqual ("tag", se.Tag, "#A3");
82 			Assert.IsNull (se.Text, "#A4");
83 
84 			se = new SecurityElement (string.Empty);
85 			Assert.IsNull (se.Attributes, "#B1");
86 			Assert.IsNull (se.Children, "#B2");
87 			Assert.AreEqual (string.Empty, se.Tag, "#B3");
88 			Assert.IsNull (se.Text, "#B4");
89 		}
90 
91 		[Test]
Constructor1_Tag_Invalid()92 		public void Constructor1_Tag_Invalid ()
93 		{
94 			try {
95 				new SecurityElement ("Na<me");
96 				Assert.Fail ("#A1");
97 			} catch (ArgumentException ex) {
98 				// Invalid element tag Nam<e
99 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
100 				Assert.IsNull (ex.InnerException, "#A3");
101 				Assert.IsNotNull (ex.Message, "#A4");
102 				Assert.IsTrue (ex.Message.IndexOf ("Na<me") != -1, "#A5");
103 				Assert.IsNull (ex.ParamName, "#A6");
104 			}
105 
106 			try {
107 				new SecurityElement ("Nam>e");
108 				Assert.Fail ("#B1");
109 			} catch (ArgumentException ex) {
110 				// Invalid element tag Nam>e
111 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
112 				Assert.IsNull (ex.InnerException, "#B3");
113 				Assert.IsNotNull (ex.Message, "#B4");
114 				Assert.IsTrue (ex.Message.IndexOf ("Nam>e") != -1, "#B5");
115 				Assert.IsNull (ex.ParamName, "#B6");
116 			}
117 		}
118 
119 		[Test]
Constructor1_Tag_Null()120 		public void Constructor1_Tag_Null ()
121 		{
122 			try {
123 				new SecurityElement (null);
124 				Assert.Fail ("#1");
125 			} catch (ArgumentNullException ex) {
126 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
127 				Assert.IsNull (ex.InnerException, "#3");
128 				Assert.IsNotNull (ex.Message, "#4");
129 				Assert.IsNotNull (ex.ParamName, "#5");
130 				Assert.AreEqual ("tag", ex.ParamName, "#6");
131 			}
132 		}
133 
134 		[Test]
Constructor2()135 		public void Constructor2 ()
136 		{
137 			SecurityElement se = new SecurityElement ("tag", "text");
138 			Assert.IsNull (se.Attributes, "EmptyAttributes");
139 			Assert.IsNull (se.Children, "EmptyChildren");
140 			Assert.AreEqual ("tag", se.Tag, "Tag");
141 			Assert.AreEqual ("text", se.Text, "Text");
142 		}
143 
144 		[Test]
Constructor2_Tag_Invalid()145 		public void Constructor2_Tag_Invalid ()
146 		{
147 			try {
148 				new SecurityElement ("Na<me", "text");
149 				Assert.Fail ("#A1");
150 			} catch (ArgumentException ex) {
151 				// Invalid element tag Nam<e
152 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
153 				Assert.IsNull (ex.InnerException, "#A3");
154 				Assert.IsNotNull (ex.Message, "#A4");
155 				Assert.IsTrue (ex.Message.IndexOf ("Na<me") != -1, "#A5");
156 				Assert.IsNull (ex.ParamName, "#A6");
157 			}
158 
159 			try {
160 				new SecurityElement ("Nam>e", "text");
161 				Assert.Fail ("#B1");
162 			} catch (ArgumentException ex) {
163 				// Invalid element tag Nam>e
164 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
165 				Assert.IsNull (ex.InnerException, "#B3");
166 				Assert.IsNotNull (ex.Message, "#B4");
167 				Assert.IsTrue (ex.Message.IndexOf ("Nam>e") != -1, "#B5");
168 				Assert.IsNull (ex.ParamName, "#B6");
169 			}
170 		}
171 
172 		[Test]
Constructor2_Tag_Null()173 		public void Constructor2_Tag_Null ()
174 		{
175 			try {
176 				new SecurityElement (null, "text");
177 				Assert.Fail ("#1");
178 			} catch (ArgumentNullException ex) {
179 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
180 				Assert.IsNull (ex.InnerException, "#3");
181 				Assert.IsNotNull (ex.Message, "#4");
182 				Assert.IsNotNull (ex.ParamName, "#5");
183 				Assert.AreEqual ("tag", ex.ParamName, "#6");
184 			}
185 		}
186 
187 		[Test]
Constructor2_Text_Null()188 		public void Constructor2_Text_Null ()
189 		{
190 			SecurityElement se = new SecurityElement ("tag", null);
191 			Assert.IsNull (se.Attributes, "EmptyAttributes");
192 			Assert.IsNull (se.Children, "EmptyChildren");
193 			Assert.AreEqual ("tag", se.Tag, "Tag");
194 			Assert.IsNull (se.Text, "Text");
195 		}
196 
197 		[Test]
AddAttribute_Name_Null()198 		public void AddAttribute_Name_Null ()
199 		{
200 			try {
201 				elem.AddAttribute (null, "valid");
202 				Assert.Fail ("#1");
203 			} catch (ArgumentNullException ex) {
204 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
205 				Assert.IsNull (ex.InnerException, "#3");
206 				Assert.IsNotNull (ex.Message, "#4");
207 				Assert.IsNotNull (ex.ParamName, "#5");
208 				Assert.AreEqual ("name", ex.ParamName, "#6");
209 			}
210 		}
211 
212 		[Test]
AddAttribute_Value_Null()213 		public void AddAttribute_Value_Null ()
214 		{
215 			try {
216 				elem.AddAttribute ("valid", null);
217 				Assert.Fail ("#1");
218 			} catch (ArgumentNullException ex) {
219 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
220 				Assert.IsNull (ex.InnerException, "#3");
221 				Assert.IsNotNull (ex.Message, "#4");
222 				Assert.IsNotNull (ex.ParamName, "#5");
223 				Assert.AreEqual ("value", ex.ParamName, "#6");
224 			}
225 		}
226 
227 		[Test]
228 		[ExpectedException (typeof (ArgumentException))]
AddAttribute_Name_Invalid()229 		public void AddAttribute_Name_Invalid ()
230 		{
231 			elem.AddAttribute ("<invalid>", "valid");
232 		}
233 
234 		[Test]
235 		[ExpectedException (typeof (ArgumentException))]
AddAttribute_Value_Invalid()236 		public void AddAttribute_Value_Invalid ()
237 		{
238 			elem.AddAttribute ("valid", "invalid\"");
239 		}
240 
241 		[Test]
AddAttribute_InvalidValue2()242 		public void AddAttribute_InvalidValue2 ()
243 		{
244 			elem.AddAttribute ("valid", "valid&");
245 			// in xml world this is actually not considered valid
246 			// but it is by MS.Net
247 		}
248 
249 		[Test]
250 		[ExpectedException (typeof (ArgumentException))]
AddAttribute_InvalidValue3()251 		public void AddAttribute_InvalidValue3 ()
252 		{
253 			elem.AddAttribute ("valid", "<invalid>");
254 		}
255 
256 		[Test]
257 		[ExpectedException (typeof (ArgumentException))]
AddAttribute_Duplicate()258 		public void AddAttribute_Duplicate ()
259 		{
260 			elem.AddAttribute ("valid", "first time");
261 			elem.AddAttribute ("valid", "second time");
262 		}
263 
264 		[Test]
AddAttribute()265 		public void AddAttribute ()
266 		{
267 			elem.AddAttribute ("valid", "valid\'");
268 		}
269 
270 		[Test]
AddChild_Null()271 		public void AddChild_Null ()
272 		{
273 			try {
274 				elem.AddChild (null);
275 				Assert.Fail ("#1");
276 			} catch (ArgumentNullException ex) {
277 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
278 				Assert.IsNull (ex.InnerException, "#3");
279 				Assert.IsNotNull (ex.Message, "#4");
280 				Assert.IsNotNull (ex.ParamName, "#5");
281 				Assert.AreEqual ("child", ex.ParamName, "#6");
282 			}
283 		}
284 
285 		[Test]
AddChild()286 		public void AddChild ()
287 		{
288 			int n = elem.Children.Count;
289 			// add itself
290 			elem.AddChild (elem);
291 			Assert.AreEqual ((n + 1), elem.Children.Count, "Count");
292 		}
293 
294 		[Test]
295 		[Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304549
Attributes_Name_Invalid()296 		public void Attributes_Name_Invalid ()
297 		{
298 			Hashtable h = elem.Attributes;
299 			h.Add ("<invalid>", "valid");
300 			try {
301 				elem.Attributes = h;
302 				Assert.Fail ("#1");
303 			} catch (ArgumentException ex) {
304 				// Invalid attribute name '<invalid>'
305 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
306 				Assert.IsNull (ex.InnerException, "#3");
307 				Assert.IsNotNull (ex.Message, "#4");
308 				Assert.IsTrue (ex.Message.IndexOf ("<invalid>") != -1, "#5");
309 				Assert.IsNull (ex.ParamName, "#6");
310 			}
311 		}
312 
313 		[Test]
314 		[Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304549
Attributes_Name_Invalid_MS()315 		public void Attributes_Name_Invalid_MS ()
316 		{
317 			Hashtable h = elem.Attributes;
318 			h.Add ("<invalid>", "valid");
319 			try {
320 				elem.Attributes = h;
321 				Assert.Fail ();
322 			} catch (InvalidCastException) {
323 			}
324 		}
325 
326 		[Test]
Attributes_Value_Invalid()327 		public void Attributes_Value_Invalid ()
328 		{
329 			Hashtable h = elem.Attributes;
330 			h.Add ("valid", "\"invalid\"");
331 			try {
332 				elem.Attributes = h;
333 				Assert.Fail ("#1");
334 			} catch (ArgumentException ex) {
335 				// Invalid attribute value '"invalid"'
336 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
337 				Assert.IsNull (ex.InnerException, "#3");
338 				Assert.IsNotNull (ex.Message, "#4");
339 				Assert.IsTrue (ex.Message.IndexOf ("\"invalid\"") != -1, "#5");
340 				Assert.IsNull (ex.ParamName, "#6");
341 			}
342 		}
343 
344 		[Test]
Attributes()345 		public void Attributes ()
346 		{
347 			Hashtable h = elem.Attributes;
348 
349 			h = elem.Attributes;
350 			h.Add ("foo", "bar");
351 			Assert.IsTrue (elem.Attributes.Count != h.Count, "#1");
352 
353 			elem.Attributes = h;
354 			Assert.IsNotNull (elem.Attribute ("foo"), "#2");
355 		}
356 
357 		[Test]
Equal()358 		public void Equal ()
359 		{
360 			int iTest = 0;
361 			SecurityElement elem2 = CreateElement ();
362 			iTest++;
363 			Assert.IsTrue (elem.Equal (elem2), "#1");
364 			iTest++;
365 			SecurityElement child = (SecurityElement) elem2.Children [0];
366 			iTest++;
367 			child = (SecurityElement) child.Children [1];
368 			iTest++;
369 			child.Text = "some text";
370 			iTest++;
371 			Assert.IsFalse (elem.Equal (elem2), "#2");
372 		}
373 
374 		[Test]
Escape()375 		public void Escape ()
376 		{
377 			Assert.AreEqual ("foo&lt;&gt;&quot;&apos;&amp; bar",
378 				SecurityElement.Escape ("foo<>\"'& bar"), "#1");
379 			Assert.IsNull (SecurityElement.Escape (null), "#2");
380 		}
381 
382 		[Test]
IsValidAttributeName()383 		public void IsValidAttributeName ()
384 		{
385 			Assert.IsFalse (SecurityElement.IsValidAttributeName ("x x"), "#1");
386 			Assert.IsFalse (SecurityElement.IsValidAttributeName ("x<x"), "#2");
387 			Assert.IsFalse (SecurityElement.IsValidAttributeName ("x>x"), "#3");
388 			Assert.IsTrue (SecurityElement.IsValidAttributeName ("x\"x"), "#4");
389 			Assert.IsTrue (SecurityElement.IsValidAttributeName ("x'x"), "#5");
390 			Assert.IsTrue (SecurityElement.IsValidAttributeName ("x&x"), "#6");
391 			Assert.IsFalse (SecurityElement.IsValidAttributeName (null), "#7");
392 			Assert.IsTrue (SecurityElement.IsValidAttributeName (string.Empty), "#8");
393 		}
394 
395 		[Test]
IsValidAttributeValue()396 		public void IsValidAttributeValue ()
397 		{
398 			Assert.IsTrue (SecurityElement.IsValidAttributeValue ("x x"), "#1");
399 			Assert.IsFalse (SecurityElement.IsValidAttributeValue ("x<x"), "#2");
400 			Assert.IsFalse (SecurityElement.IsValidAttributeValue ("x>x"), "#3");
401 			Assert.IsFalse (SecurityElement.IsValidAttributeValue ("x\"x"), "#4");
402 			Assert.IsTrue (SecurityElement.IsValidAttributeValue ("x'x"), "#5");
403 			Assert.IsTrue (SecurityElement.IsValidAttributeValue ("x&x"), "#6");
404 			Assert.IsFalse (SecurityElement.IsValidAttributeValue (null), "#7");
405 			Assert.IsTrue (SecurityElement.IsValidAttributeValue (string.Empty), "#8");
406 		}
407 
408 		[Test]
IsValidTag()409 		public void IsValidTag ()
410 		{
411 			Assert.IsFalse (SecurityElement.IsValidTag ("x x"), "#1");
412 			Assert.IsFalse (SecurityElement.IsValidTag ("x<x"), "#2");
413 			Assert.IsFalse (SecurityElement.IsValidTag ("x>x"), "#3");
414 			Assert.IsTrue (SecurityElement.IsValidTag ("x\"x"), "#4");
415 			Assert.IsTrue (SecurityElement.IsValidTag ("x'x"), "#5");
416 			Assert.IsTrue (SecurityElement.IsValidTag ("x&x"), "#6");
417 			Assert.IsFalse (SecurityElement.IsValidTag (null), "#7");
418 			Assert.IsTrue (SecurityElement.IsValidTag (string.Empty), "#8");
419 		}
420 
421 		[Test]
IsValidText()422 		public void IsValidText ()
423 		{
424 			Assert.IsTrue (SecurityElement.IsValidText ("x x"), "#1");
425 			Assert.IsFalse (SecurityElement.IsValidText ("x<x"), "#2");
426 			Assert.IsFalse (SecurityElement.IsValidText ("x>x"), "#3");
427 			Assert.IsTrue (SecurityElement.IsValidText ("x\"x"), "#4");
428 			Assert.IsTrue (SecurityElement.IsValidText ("x'x"), "#5");
429 			Assert.IsTrue (SecurityElement.IsValidText ("x&x"), "#6");
430 			Assert.IsFalse (SecurityElement.IsValidText (null), "#7");
431 			Assert.IsTrue (SecurityElement.IsValidText (string.Empty), "#8");
432 		}
433 
434 		[Test]
SearchForChildByTag_Null()435 		public void SearchForChildByTag_Null ()
436 		{
437 			try {
438 				elem.SearchForChildByTag (null);
439 				Assert.Fail ("#1");
440 			} catch (ArgumentNullException ex) {
441 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
442 				Assert.IsNull (ex.InnerException, "#3");
443 				Assert.IsNotNull (ex.Message, "#4");
444 				Assert.IsNotNull (ex.ParamName, "#5");
445 				Assert.AreEqual ("tag", ex.ParamName, "#6");
446 			}
447 		}
448 
449 		[Test]
SearchForChildByTag()450 		public void SearchForChildByTag ()
451 		{
452 			SecurityElement child = elem.SearchForChildByTag ("doesnotexist");
453 			Assert.IsNull (child, "#1");
454 
455 			child = elem.SearchForChildByTag ("ENDPOINT");
456 			Assert.IsNull (child, "#2");
457 
458 			child = (SecurityElement) elem.Children [0];
459 			child = child.SearchForChildByTag ("ENDPOINT");
460 			Assert.AreEqual ("All", child.Attribute ("transport"), "#3");
461 		}
462 
463 		[Test]
SearchForTextOfTag_Tag_Null()464 		public void SearchForTextOfTag_Tag_Null ()
465 		{
466 			try {
467 				elem.SearchForTextOfTag (null);
468 				Assert.Fail ("#1");
469 			} catch (ArgumentNullException ex) {
470 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
471 				Assert.IsNull (ex.InnerException, "#3");
472 				Assert.IsNotNull (ex.Message, "#4");
473 				Assert.IsNotNull (ex.ParamName, "#5");
474 				Assert.AreEqual ("tag", ex.ParamName, "#6");
475 			}
476 		}
477 
478 		[Test]
SearchForTextOfTag()479 		public void SearchForTextOfTag ()
480 		{
481 			string s = elem.SearchForTextOfTag ("ENDPOINT");
482 			Assert.AreEqual ("some text", s);
483 		}
484 
485 		[Test]
Tag()486 		public void Tag ()
487 		{
488 			SecurityElement se = new SecurityElement ("Values");
489 			Assert.AreEqual ("Values", se.Tag, "#A1");
490 			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
491 				"<Values/>{0}", Environment.NewLine),
492 				se.ToString (), "#A2");
493 			se.Tag = "abc:Name";
494 			Assert.AreEqual ("abc:Name", se.Tag, "#B1");
495 			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
496 				"<abc:Name/>{0}", Environment.NewLine),
497 				se.ToString (), "#B2");
498 			se.Tag = "Name&Address";
499 			Assert.AreEqual ("Name&Address", se.Tag, "#C1");
500 			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
501 				"<Name&Address/>{0}", Environment.NewLine),
502 				se.ToString (), "#C2");
503 			se.Tag = string.Empty;
504 			Assert.AreEqual (string.Empty, se.Tag, "#D1");
505 			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
506 				"</>{0}", Environment.NewLine),
507 				se.ToString (), "#D2");
508 		}
509 
510 		[Test]
Tag_Invalid()511 		public void Tag_Invalid ()
512 		{
513 			SecurityElement se = new SecurityElement ("Values");
514 
515 			try {
516 				se.Tag = "Na<me";
517 				Assert.Fail ("#A1");
518 			} catch (ArgumentException ex) {
519 				// Invalid element tag Nam<e
520 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
521 				Assert.IsNull (ex.InnerException, "#A3");
522 				Assert.IsNotNull (ex.Message, "#A4");
523 				Assert.IsTrue (ex.Message.IndexOf ("Na<me") != -1, "#A5");
524 				Assert.IsNull (ex.ParamName, "#A6");
525 			}
526 
527 			try {
528 				se.Tag = "Nam>e";
529 				Assert.Fail ("#B1");
530 			} catch (ArgumentException ex) {
531 				// Invalid element tag Nam>e
532 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
533 				Assert.IsNull (ex.InnerException, "#B3");
534 				Assert.IsNotNull (ex.Message, "#B4");
535 				Assert.IsTrue (ex.Message.IndexOf ("Nam>e") != -1, "#B5");
536 				Assert.IsNull (ex.ParamName, "#B6");
537 			}
538 		}
539 
540 		[Test]
Tag_Null()541 		public void Tag_Null ()
542 		{
543 			try {
544 				elem.Tag = null;
545 				Assert.Fail ("#1");
546 			} catch (ArgumentNullException ex) {
547 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
548 				Assert.IsNull (ex.InnerException, "#3");
549 				Assert.IsNotNull (ex.Message, "#4");
550 				Assert.IsNotNull (ex.ParamName, "#5");
551 				Assert.AreEqual ("Tag", ex.ParamName, "#6");
552 			}
553 		}
554 
555 		[Test]
Text()556 		public void Text ()
557 		{
558 			elem.Text = "Miguel&S�bastien";
559 			Assert.AreEqual ("Miguel&S�bastien", elem.Text, "#1");
560 			elem.Text = null;
561 			Assert.IsNull (elem.Text, "#2");
562 			elem.Text = "S�bastien\"Miguel";
563 			Assert.AreEqual ("S�bastien\"Miguel", elem.Text, "#3");
564 			elem.Text = string.Empty;
565 			Assert.AreEqual (string.Empty, elem.Text, "#4");
566 			elem.Text = "&lt;sample&amp;practice&unresolved;&gt;";
567 			Assert.AreEqual ("<sample&practice&unresolved;>", elem.Text, "#5");
568 		}
569 
570 		[Test]
Text_Invalid()571 		public void Text_Invalid ()
572 		{
573 			try {
574 				elem.Text = "Mig<uelS�bastien";
575 				Assert.Fail ("#A1");
576 			} catch (ArgumentException ex) {
577 				// Invalid element tag Mig<uelS�bastien
578 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
579 				Assert.IsNull (ex.InnerException, "#A3");
580 				Assert.IsNotNull (ex.Message, "#A4");
581 				Assert.IsTrue (ex.Message.IndexOf ("Mig<uelS�bastien") != -1, "#A5");
582 				Assert.IsNull (ex.ParamName, "#A6");
583 			}
584 
585 			try {
586 				elem.Text = "Mig>uelS�bastien";
587 				Assert.Fail ("#B1");
588 			} catch (ArgumentException ex) {
589 				// Invalid element tag Mig>uelS�bastien
590 				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
591 				Assert.IsNull (ex.InnerException, "#B3");
592 				Assert.IsNotNull (ex.Message, "#B4");
593 				Assert.IsTrue (ex.Message.IndexOf ("Mig>uelS�bastien") != -1, "#B5");
594 				Assert.IsNull (ex.ParamName, "#B6");
595 			}
596 		}
597 
598 		[Test]
MultipleAttributes()599 		public void MultipleAttributes ()
600 		{
601 			SecurityElement se = new SecurityElement ("Multiple");
602 			se.AddAttribute ("Attribute1", "One");
603 			se.AddAttribute ("Attribute2", "Two");
604 
605 			string expected = String.Format ("<Multiple Attribute1=\"One\"{0}Attribute2=\"Two\"/>{0}", Environment.NewLine);
606 			Assert.AreEqual (expected, se.ToString (), "ToString()");
607 		}
608 
609 		[Test]
Copy()610 		public void Copy ()
611 		{
612 			SecurityElement se = SecurityElement.FromString ("<tag attribute=\"value\"><child attr=\"1\">mono</child><child/></tag>");
613 			SecurityElement copy = se.Copy ();
614 			Assert.IsFalse (Object.ReferenceEquals (se, copy), "se!ReferenceEquals");
615 			Assert.IsTrue (Object.ReferenceEquals (se.Children [0], copy.Children [0]), "c1=ReferenceEquals");
616 			Assert.IsTrue (Object.ReferenceEquals (se.Children [1], copy.Children [1]), "c2=ReferenceEquals");
617 		}
618 
619 		[Test]
FromString_Null()620 		public void FromString_Null ()
621 		{
622 			try {
623 				SecurityElement.FromString (null);
624 				Assert.Fail ("#1");
625 			} catch (ArgumentNullException ex) {
626 				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
627 				Assert.IsNull (ex.InnerException, "#3");
628 				Assert.IsNotNull (ex.Message, "#4");
629 				Assert.IsNotNull (ex.ParamName, "#5");
630 				Assert.AreEqual ("xml", ex.ParamName, "#6");
631 			}
632 		}
633 
634 		[Test]
635 		[ExpectedException (typeof (XmlSyntaxException))]
FromString_Empty()636 		public void FromString_Empty ()
637 		{
638 			SecurityElement.FromString (String.Empty);
639 		}
640 
641 		[Test]
642 		[ExpectedException (typeof (XmlSyntaxException))]
FromString_NonXml()643 		public void FromString_NonXml ()
644 		{
645 			SecurityElement.FromString ("mono");
646 		}
647 
648 		[Test]
FromString()649 		public void FromString ()
650 		{
651 			SecurityElement se = SecurityElement.FromString ("<tag attribute=\"value\"><x:child attr=\"1\">mono</x:child><child/></tag>");
652 			Assert.AreEqual ("tag", se.Tag, "#A1");
653 			Assert.IsNull (se.Text, "#A2");
654 			Assert.AreEqual (1, se.Attributes.Count, "#A3");
655 			Assert.AreEqual ("value", se.Attribute ("attribute"), "#A4");
656 			Assert.AreEqual (2, se.Children.Count, "#A5");
657 
658 			SecurityElement child = (SecurityElement) se.Children [0];
659 			Assert.AreEqual ("x:child", child.Tag, "#B1");
660 			Assert.AreEqual ("mono", child.Text, "#B2");
661 			Assert.AreEqual (1, child.Attributes.Count, "#B3");
662 			Assert.AreEqual ("1", child.Attribute ("attr"), "#B4");
663 
664 			child = (SecurityElement) se.Children [1];
665 			Assert.AreEqual ("child", child.Tag, "#C1");
666 			Assert.IsNull (child.Text, "#C2");
667 			Assert.IsNull (child.Attributes, "#C3");
668 		}
669 
670 		[Test]
671 		[Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304583
FromString_Quote_Delimiter()672 		public void FromString_Quote_Delimiter ()
673 		{
674 			const string xml = "<value name='Company'>Novell</value>";
675 			SecurityElement se = SecurityElement.FromString (xml);
676 			Assert.AreEqual ("Company", se.Attribute ("name"), "#1");
677 			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
678 				"<value name=\"Company\">Novell</value>{0}",
679 				Environment.NewLine), se.ToString (), "#2");
680 		}
681 
682 		[Test]
683 		[Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304583
FromString_Quote_Delimiter_MS()684 		public void FromString_Quote_Delimiter_MS ()
685 		{
686 			const string xml = "<value name='Company'>Novell</value>";
687 			SecurityElement se = SecurityElement.FromString (xml);
688 			Assert.AreEqual ("'Company'", se.Attribute ("name"), "#1");
689 			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
690 				"<value name=\"'Company'\">Novell</value>{0}",
691 				Environment.NewLine), se.ToString (), "#2");
692 		}
693 
694 		[Test] // bug #333699
FromString_EntityReferences()695 		public void FromString_EntityReferences ()
696 		{
697 			const string xml = @"
698 				<values>
699 					<value name=""&quot;name&quot;&amp;&lt;address&gt;"">&lt;&apos;Suds&apos; &amp; &quot;Soda&quot;&gt;!</value>
700 				</values>";
701 
702 			SecurityElement se = SecurityElement.FromString (xml);
703 			Assert.IsNotNull (se, "#A1");
704 			Assert.IsNull (se.Attributes, "#A2");
705 			Assert.IsNotNull (se.Children, "#A3");
706 			Assert.AreEqual (1, se.Children.Count, "#A4");
707 			Assert.AreEqual ("values", se.Tag, "#A5");
708 			Assert.IsNull (se.Text, "#A6");
709 
710 			SecurityElement child = se.Children [0] as SecurityElement;
711 			Assert.IsNotNull (child, "#B1");
712 			Assert.IsNotNull (child.Attributes, "#B2");
713 			Assert.AreEqual ("\"name\"&<address>", child.Attribute ("name"), "#B3");
714 			Assert.AreEqual ("value", child.Tag, "#B4");
715 			Assert.AreEqual ("<'Suds' & \"Soda\">!", child.Text, "#B5");
716 			Assert.IsNull (child.Children, "#B6");
717 		}
718 
719 		[Test] // bug #
720 		[Category ("NotWorking")]
FromString_CharacterReferences()721 		public void FromString_CharacterReferences ()
722 		{
723 			const string xml = @"
724 				<value name=""name&#38;address"">Suds&#x26;Soda&#38;</value>";
725 
726 			SecurityElement se = SecurityElement.FromString (xml);
727 			Assert.IsNotNull (se, "#1");
728 			Assert.IsNotNull (se.Attributes, "#2");
729 			Assert.AreEqual ("name&#38;address", se.Attribute ("name"), "#3");
730 			Assert.AreEqual ("value", se.Tag, "#4");
731 			Assert.AreEqual ("Suds&#x26;Soda&#38;", se.Text, "#5");
732 			Assert.IsNull (se.Children, "#6");
733 		}
734 
735 		[Test] // bug #333699 (ugh, mostly a dup)
TestToString()736 		public void TestToString ()
737 		{
738 			SecurityElement values = new SecurityElement ("values");
739 			SecurityElement infoValue = new SecurityElement ("value");
740 			infoValue.AddAttribute ("name", "string");
741 			infoValue.Text = SecurityElement.Escape ("<'Suds' & \"Soda\">!");
742 			values.AddChild (infoValue);
743 			Assert.AreEqual ("<value name=\"string\">&lt;&apos;Suds&apos; &amp; &quot;Soda&quot;&gt;!</value>" + Environment.NewLine, infoValue.ToString (), "#1");
744 			Assert.AreEqual ("<'Suds' & \"Soda\">!", infoValue.Text, "#2");
745 			Assert.IsNull (values.Text, "#3");
746 
747 			Assert.AreEqual (String.Format ("<values>{0}<value name=\"string\">&lt;&apos;Suds&apos; &amp; &quot;Soda&quot;&gt;!</value>{0}</values>{0}", Environment.NewLine), values.ToString (), "#4");
748 
749 			SecurityElement sec = SecurityElement.FromString (values.ToString ());
750 			Assert.AreEqual (1, sec.Children.Count, "#5");
751 			Assert.AreEqual ("<'Suds' & \"Soda\">!", ((SecurityElement) sec.Children [0]).Text, "#6");
752 		}
753 	}
754 }
755