update readme (#21797)
[mono-project.git] / mcs / class / corlib / Test / System.Security / SecurityElementTest.cs
blob7ba4672d4a2341c71ce885b802389bf70cac5f2f
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)
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.
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.Security;
36 using NUnit.Framework;
38 namespace MonoTests.System.Security {
40 [TestFixture]
41 public class SecurityElementTest {
43 SecurityElement elem;
45 [SetUp]
46 public void SetUp ()
48 elem = CreateElement ();
51 private SecurityElement CreateElement ()
53 SecurityElement elem = new SecurityElement ("IPermission");
54 elem.AddAttribute ("class", "System");
55 elem.AddAttribute ("version", "1");
57 SecurityElement child = new SecurityElement ("ConnectAccess");
58 elem.AddChild (child);
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);
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);
72 return elem;
75 [Test]
76 public void Constructor1 ()
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");
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");
91 [Test]
92 public void Constructor1_Tag_Invalid ()
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");
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");
119 [Test]
120 public void Constructor1_Tag_Null ()
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");
134 [Test]
135 public void Constructor2 ()
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");
144 [Test]
145 public void Constructor2_Tag_Invalid ()
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");
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");
172 [Test]
173 public void Constructor2_Tag_Null ()
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");
187 [Test]
188 public void Constructor2_Text_Null ()
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");
197 [Test]
198 public void AddAttribute_Name_Null ()
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");
212 [Test]
213 public void AddAttribute_Value_Null ()
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");
227 [Test]
228 [ExpectedException (typeof (ArgumentException))]
229 public void AddAttribute_Name_Invalid ()
231 elem.AddAttribute ("<invalid>", "valid");
234 [Test]
235 [ExpectedException (typeof (ArgumentException))]
236 public void AddAttribute_Value_Invalid ()
238 elem.AddAttribute ("valid", "invalid\"");
241 [Test]
242 public void AddAttribute_InvalidValue2 ()
244 elem.AddAttribute ("valid", "valid&");
245 // in xml world this is actually not considered valid
246 // but it is by MS.Net
249 [Test]
250 [ExpectedException (typeof (ArgumentException))]
251 public void AddAttribute_InvalidValue3 ()
253 elem.AddAttribute ("valid", "<invalid>");
256 [Test]
257 [ExpectedException (typeof (ArgumentException))]
258 public void AddAttribute_Duplicate ()
260 elem.AddAttribute ("valid", "first time");
261 elem.AddAttribute ("valid", "second time");
264 [Test]
265 public void AddAttribute ()
267 elem.AddAttribute ("valid", "valid\'");
270 [Test]
271 public void AddChild_Null ()
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");
285 [Test]
286 public void AddChild ()
288 int n = elem.Children.Count;
289 // add itself
290 elem.AddChild (elem);
291 Assert.AreEqual ((n + 1), elem.Children.Count, "Count");
294 [Test]
295 [Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304549
296 public void Attributes_Name_Invalid ()
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");
313 [Test]
314 [Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304549
315 public void Attributes_Name_Invalid_MS ()
317 Hashtable h = elem.Attributes;
318 h.Add ("<invalid>", "valid");
319 try {
320 elem.Attributes = h;
321 Assert.Fail ();
322 } catch (InvalidCastException) {
326 [Test]
327 public void Attributes_Value_Invalid ()
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");
344 [Test]
345 public void Attributes ()
347 Hashtable h = elem.Attributes;
349 h = elem.Attributes;
350 h.Add ("foo", "bar");
351 Assert.IsTrue (elem.Attributes.Count != h.Count, "#1");
353 elem.Attributes = h;
354 Assert.IsNotNull (elem.Attribute ("foo"), "#2");
357 [Test]
358 public void Equal ()
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");
374 [Test]
375 public void Escape ()
377 Assert.AreEqual ("foo&lt;&gt;&quot;&apos;&amp; bar",
378 SecurityElement.Escape ("foo<>\"'& bar"), "#1");
379 Assert.IsNull (SecurityElement.Escape (null), "#2");
382 [Test]
383 public void IsValidAttributeName ()
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");
395 [Test]
396 public void IsValidAttributeValue ()
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");
408 [Test]
409 public void IsValidTag ()
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");
421 [Test]
422 public void IsValidText ()
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");
434 [Test]
435 public void SearchForChildByTag_Null ()
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");
449 [Test]
450 public void SearchForChildByTag ()
452 SecurityElement child = elem.SearchForChildByTag ("doesnotexist");
453 Assert.IsNull (child, "#1");
455 child = elem.SearchForChildByTag ("ENDPOINT");
456 Assert.IsNull (child, "#2");
458 child = (SecurityElement) elem.Children [0];
459 child = child.SearchForChildByTag ("ENDPOINT");
460 Assert.AreEqual ("All", child.Attribute ("transport"), "#3");
463 [Test]
464 public void SearchForTextOfTag_Tag_Null ()
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");
478 [Test]
479 public void SearchForTextOfTag ()
481 string s = elem.SearchForTextOfTag ("ENDPOINT");
482 Assert.AreEqual ("some text", s);
485 [Test]
486 public void Tag ()
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");
510 [Test]
511 public void Tag_Invalid ()
513 SecurityElement se = new SecurityElement ("Values");
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");
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");
540 [Test]
541 public void Tag_Null ()
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");
555 [Test]
556 public void Text ()
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");
570 [Test]
571 public void Text_Invalid ()
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");
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");
598 [Test]
599 public void MultipleAttributes ()
601 SecurityElement se = new SecurityElement ("Multiple");
602 se.AddAttribute ("Attribute1", "One");
603 se.AddAttribute ("Attribute2", "Two");
605 string expected = String.Format ("<Multiple Attribute1=\"One\"{0}Attribute2=\"Two\"/>{0}", Environment.NewLine);
606 Assert.AreEqual (expected, se.ToString (), "ToString()");
609 [Test]
610 public void Copy ()
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");
619 [Test]
620 public void FromString_Null ()
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");
634 [Test]
635 [ExpectedException (typeof (XmlSyntaxException))]
636 public void FromString_Empty ()
638 SecurityElement.FromString (String.Empty);
641 [Test]
642 [ExpectedException (typeof (XmlSyntaxException))]
643 public void FromString_NonXml ()
645 SecurityElement.FromString ("mono");
648 [Test]
649 public void FromString ()
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");
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");
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");
670 [Test]
671 [Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304583
672 public void FromString_Quote_Delimiter ()
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");
682 [Test]
683 [Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304583
684 public void FromString_Quote_Delimiter_MS ()
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");
694 [Test] // bug #333699
695 public void FromString_EntityReferences ()
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>";
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");
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");
719 [Test] // bug #
720 [Category ("NotWorking")]
721 public void FromString_CharacterReferences ()
723 const string xml = @"
724 <value name=""name&#38;address"">Suds&#x26;Soda&#38;</value>";
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");
735 [Test] // bug #333699 (ugh, mostly a dup)
736 public void TestToString ()
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");
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");
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");