/// </summary>
public class AllTests : TestCase {
- public AllTests(string name) : base(name) {}
+ public AllTests (string name) : base (name) {}
public static ITest Suite {
get
{
- TestSuite suite = new TestSuite();
+ TestSuite suite = new TestSuite ();
suite.AddTest (IPAddressTest.Suite);
suite.AddTest (IPEndPointTest.Suite);
suite.AddTest (CookieTest.Suite);
suite.AddTest (CookieCollectionTest.Suite);
- // suite.AddTest (CookieContainerTest.Suite);
+ suite.AddTest (CookieContainerTest.Suite);
return suite;
}
}
try {\r
c.Name = "xxx\r\n";\r
Fail ("#2g"); \r
- } catch (CookieException ttt) {\r
+ } catch (CookieException) {\r
AssertEquals ("#2h", String.Empty, c.Name); \r
} \r
try {\r
+2002-04-47 Lawrence Pit <loz@cable.a2000.nl>
+
+ * SecurityElement.cs: fixed bugs, implemented several methods
+
2002-03-12 Duncan Mak <duncan@ximian.com>
* SecurityException.cs: Inherit from SystemException, not Exception.
//
// Author:
// Miguel de Icaza (miguel@ximian.com)
+// Lawrence Pit (loz@cable.a2000.nl)
//
// (C) Ximian, Inc. http://www.ximian.com
using System.Collections;
using System.Text;
-namespace System.Security {
-
- [MonoTODO ("See bottom of the class for missing methods")]
+namespace System.Security
+{
[Serializable]
- public sealed class SecurityElement {
+ public sealed class SecurityElement
+ {
string text;
string tag;
+ Hashtable attributes;
+ ArrayList children;
- public SecurityElement (string tag, string text)
+ // these values are determined by a simple test program against the MS.Net implementation:
+ // for (int i = 0; i < 256; i++) {
+ // if (!SecurityElement.IsValidTag ("" + ((char) i))) {
+ // System.Console.WriteLine ("TAG: " + i);
+ // }
+ // }
+ // note: this is actually an incorrect implementation of MS, as for example the &
+ // character is not a valid character in tag names.
+ private static char [] invalid_tag_chars = new char [] { ' ', '<', '>' };
+ private static char [] invalid_text_chars = new char [] { '<', '>' };
+ private static char [] invalid_attr_name_chars = new char [] { ' ', '<', '>' };
+ private static char [] invalid_attr_value_chars = new char [] { '"', '<', '>' };
+ private static char [] invalid_chars = new char [] { '<', '>', '"', '\'', '&' };
+
+ public SecurityElement (string tag) : this (tag, null)
{
- if (tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
- if (text.IndexOfAny (invalid_chars) != -1 ||
- tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
-
- this.tag = tag;
- this.text = text;
}
- public SecurityElement (string tag)
+
+ public SecurityElement (string tag, string text)
{
- if (tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
-
- this.tag = tag;
+ this.Tag = tag;
+ this.Text = (text == null) ? String.Empty : text;
}
-
- Hashtable attributes;
+
public Hashtable Attributes {
get {
- return attributes;
+ if (attributes == null)
+ return null;
+
+ Hashtable result = new Hashtable ();
+ IDictionaryEnumerator e = attributes.GetEnumerator ();
+ while (e.MoveNext ())
+ result.Add (e.Key, e.Value);
+ return result;
}
- set {
- attributes = value;
+ set {
+ if (value == null || value.Count == 0) {
+ attributes = null;
+ return;
+ }
+
+ Hashtable result = new Hashtable ();
+ IDictionaryEnumerator e = value.GetEnumerator ();
+ while (e.MoveNext ()) {
+ string key = (string) e.Key;
+ string val = (string) e.Value;
+ if (IsValidAttributeName (key))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + key);
+ if (IsValidAttributeValue (val))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + key);
+ result.Add (key, val);
+ }
+ attributes = result;
}
}
- ArrayList children;
public ArrayList Children {
get {
return children;
}
set {
- if (value != null){
- foreach (object o in children){
+ if (value != null) {
+ foreach (object o in value) {
if (o == null)
throw new ArgumentNullException ();
+ // shouldn't we also throw an exception
+ // when o isn't an instance of SecurityElement?
}
}
children = value;
set {
if (value == null)
throw new ArgumentNullException ();
- if (tag.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
+ if (!IsValidTag (value))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + value);
tag = value;
}
}
}
set {
- if (value != null && (value.IndexOfAny (invalid_chars) != -1))
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
-
+ if (!IsValidText (value))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + text);
text = value;
}
}
// The hashtable will throw ArgumentException if name is already there
//
- if (name.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
+ if (!IsValidAttributeName (name))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + name);
- if (value.IndexOfAny (invalid_chars) != -1)
- throw new ArgumentException (Locale.GetText ("Invalid XML string"));
+ if (!IsValidAttributeValue (value))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + value);
attributes.Add (name, value);
}
{
if (other == null)
return false;
+
+ if (this == other)
+ return true;
- if (text != other.text)
+ if (this.text != other.text)
return false;
- if (tag != other.tag)
+ if (this.tag != other.tag)
+ return false;
+
+ if (this.attributes == null && other.attributes != null && other.attributes.Count != 0)
+ return false;
+
+ if (other.attributes == null && this.attributes != null && this.attributes.Count != 0)
return false;
- throw new Exception ("IMPLEMENT ME: Compare attributes and children");
+ if (this.attributes != null && other.attributes != null) {
+ if (this.attributes.Count != other.attributes.Count)
+ return false;
+ IDictionaryEnumerator e = attributes.GetEnumerator ();
+ while (e.MoveNext ())
+ if (other.attributes [e.Key] != e.Value)
+ return false;
+ }
+
+ if (this.children == null && other.children != null & other.children.Count != 0)
+ return false;
+
+ if (other.children == null && this.children != null & this.children.Count != 0)
+ return false;
+
+ if (this.children != null && other.children != null) {
+ if (this.children.Count != other.children.Count)
+ return false;
+ for (int i = 0; i < this.children.Count; i++)
+ if (!((SecurityElement) this.children [i]).Equal ((SecurityElement) other.children [i]))
+ return false;
+ }
+
+ return true;
}
- static char [] invalid_chars = new char [] { '<', '>', '"', '\'', '&' };
-
public static string Escape (string str)
{
StringBuilder sb;
sb = new StringBuilder ();
int len = str.Length;
- for (int i = 0; i < len; i++){
+ for (int i = 0; i < len; i++) {
char c = str [i];
- switch (c){
+ switch (c) {
case '<': sb.Append ("<"); break;
case '>': sb.Append (">"); break;
case '"': sb.Append ("""); break;
return sb.ToString ();
}
- public static bool IsInvalidAttributeName (string name)
+ public static bool IsValidAttributeName (string name)
{
- return name.IndexOfAny (invalid_chars) != -1;
+ return name != null && name.IndexOfAny (invalid_attr_name_chars) == -1;
}
- public static bool IsInvalidAttributeValue (string value)
+ public static bool IsValidAttributeValue (string value)
{
- return value.IndexOfAny (invalid_chars) != -1;
+ return value != null && value.IndexOfAny (invalid_attr_value_chars) == -1;
}
- public static bool IsInvalidTag (string value)
+ public static bool IsValidTag (string value)
{
- return value.IndexOfAny (invalid_chars) != -1;
+ return value != null && value.IndexOfAny (invalid_tag_chars) == -1;
}
- public static bool IsInvalidText (string value)
+ public static bool IsValidText (string value)
{
- return value.IndexOfAny (invalid_chars) != -1;
+ return value != null && value.IndexOfAny (invalid_text_chars) == -1;
}
- //
- // TODO:
- //
- // SearchForChildByTag
- // SearchForTextOfTag
- // ToString
+ public SecurityElement SearchForChildByTag (string tag)
+ {
+ if (tag == null)
+ throw new ArgumentNullException ("tag");
+
+ if (this.children == null)
+ return null;
+
+ for (int i = 0; i < children.Count; i++) {
+ SecurityElement elem = (SecurityElement) children [i];
+ if (elem.tag == tag)
+ return elem;
+ }
+ return null;
+ }
+
+ public string SearchForTextOfTag (string tag)
+ {
+ if (tag == null)
+ throw new ArgumentNullException ("tag");
+
+ if (this.tag == tag)
+ return this.text;
+
+ if (this.children == null)
+ return null;
+
+ for (int i = 0; i < children.Count; i++) {
+ string result = ((SecurityElement) children [i]).SearchForTextOfTag (tag);
+ if (result != null)
+ return result;
+ }
+
+ return null;
+ }
+
+ public override string ToString ()
+ {
+ StringBuilder s = new StringBuilder ();
+ ToXml (ref s, 0);
+ return s.ToString ();
+ }
+
+ private void ToXml(ref StringBuilder s, int level)
+ {
+ s.Append (' ', level << 2);
+ s.Append ("<");
+ s.Append (tag);
+
+ if (attributes != null) {
+ IDictionaryEnumerator e = attributes.GetEnumerator ();
+ while (e.MoveNext ()) {
+ s.Append (" ")
+ .Append (e.Key)
+ .Append ("=\"")
+ .Append (e.Value)
+ .Append ("\"");
+ }
+ }
+
+ if ((text == null || text == String.Empty) &&
+ (children == null || children.Count == 0))
+ s.Append ("/>");
+ else {
+ s.Append (">").Append (text);
+ if (children != null) {
+ for (int i = 0; i < children.Count; i++) {
+ s.Append (Environment.NewLine);
+ ((SecurityElement) children [i]).ToXml (ref s, level + 1);
+ }
+ }
+ s.Append (Environment.NewLine)
+ .Append (' ', level << 2)
+ .Append ("</")
+ .Append (tag)
+ .Append (">");
+ }
+ }
}
}
TestSuite suite = new TestSuite();\r
suite.AddTest(System.AllTests.Suite);\r
suite.AddTest(System.Collections.AllTests.Suite);\r
+ suite.AddTest(System.Security.AllTests.Suite);\r
suite.AddTest(System.Security.Cryptography.AllTests.Suite);\r
suite.AddTest(System.IO.AllTests.Suite);\r
suite.AddTest(System.Net.AllTests.Suite);\r
+2002-04-27 Lawrence Pit <loz@cable.a2000.nl>\r
+\r
+ * System.Security/: added directory\r
+ * AllTests.cs: added Syste,Security suite.\r
+\r
2002-04-15 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* System.Net/: added directory.
/// </summary>
public class AllTests : TestCase {
- public AllTests(string name) : base(name) {}
+ public AllTests (string name) : base (name) {}
public static ITest Suite {
get
{
- TestSuite suite = new TestSuite();
+ TestSuite suite = new TestSuite ();
suite.AddTest (IPAddressTest.Suite);
suite.AddTest (IPEndPointTest.Suite);
suite.AddTest (CookieTest.Suite);
suite.AddTest (CookieCollectionTest.Suite);
- // suite.AddTest (CookieContainerTest.Suite);
+ suite.AddTest (CookieContainerTest.Suite);
return suite;
}
}
try {\r
c.Name = "xxx\r\n";\r
Fail ("#2g"); \r
- } catch (CookieException ttt) {\r
+ } catch (CookieException) {\r
AssertEquals ("#2h", String.Empty, c.Name); \r
} \r
try {\r
--- /dev/null
+//
+// TestSuite.System.Security.AllSecurityTests.cs
+//
+// Lawrence Pit <loz@cable.a2000.nl>
+//
+
+using System;
+using NUnit.Framework;
+
+namespace MonoTests.System.Security {
+ /// <summary>
+ /// Combines all available unit tests into one test suite.
+ /// </summary>
+ public class AllTests : TestCase {
+
+ public AllTests (string name) : base (name) {}
+
+ public static ITest Suite {
+ get {
+ TestSuite suite = new TestSuite ();
+ suite.AddTest (SecurityElementTest.Suite);
+ return suite;
+ }
+ }
+ }
+}
+
--- /dev/null
+2002-04-27 Lawrence Pit <loz@cable.a2000.nl>
+
+ * ChangeLog: added
+ * AllTests.cs: added
+ * SecurityElementTest.cs: added
+
--- /dev/null
+//\r
+// SecurityElementTest.cs - NUnit Test Cases for System.Security.SecurityElement\r
+//\r
+// Author:\r
+// Lawrence Pit (loz@cable.a2000.nl)\r
+//\r
+\r
+using NUnit.Framework;\r
+using System;\r
+using System.Collections;\r
+using System.Security;\r
+\r
+namespace MonoTests.System.Security\r
+{\r
+\r
+public class SecurityElementTest : TestCase\r
+{\r
+ SecurityElement elem;\r
+ \r
+ public SecurityElementTest () :\r
+ base ("[MonoTests.System.Security.SecurityElementTest]") {}\r
+\r
+ public SecurityElementTest (string name) : base (name) {}\r
+\r
+ protected override void SetUp () \r
+ {\r
+ elem = CreateElement ();\r
+ }\r
+\r
+ protected override void TearDown () {}\r
+ \r
+ private SecurityElement CreateElement ()\r
+ {\r
+ SecurityElement elem = new SecurityElement ("IPermission");\r
+ elem.AddAttribute ("class", "System");\r
+ elem.AddAttribute ("version", "1");\r
+ \r
+ SecurityElement child = new SecurityElement ("ConnectAccess"); \r
+ elem.AddChild (child);\r
+ \r
+ SecurityElement grandchild = new SecurityElement ("ENDPOINT", "some text"); \r
+ grandchild.AddAttribute ("transport", "All");\r
+ grandchild.AddAttribute ("host", "localhost");\r
+ grandchild.AddAttribute ("port", "8080");\r
+ child.AddChild (grandchild);\r
+\r
+ SecurityElement grandchild2 = new SecurityElement ("ENDPOINT"); \r
+ grandchild2.AddAttribute ("transport", "Tcp");\r
+ grandchild2.AddAttribute ("host", "www.ximian.com");\r
+ grandchild2.AddAttribute ("port", "All");\r
+ child.AddChild (grandchild2); \r
+ \r
+ return elem; \r
+ }\r
+\r
+ public static ITest Suite\r
+ {\r
+ get {\r
+ return new TestSuite (typeof (SecurityElementTest));\r
+ }\r
+ }\r
+\r
+ public void TestConstructors ()\r
+ {\r
+ \r
+ }\r
+ \r
+ public void TestAddAttribute ()\r
+ {\r
+ try {\r
+ elem.AddAttribute (null, "valid");\r
+ Fail ("#1");\r
+ } catch (ArgumentNullException) { }\r
+ try {\r
+ elem.AddAttribute ("valid", null);\r
+ Fail ("#2");\r
+ } catch (ArgumentNullException) { }\r
+ try {\r
+ elem.AddAttribute ("<invalid>", "valid");\r
+ Fail ("#3");\r
+ } catch (ArgumentException) { }\r
+ try {\r
+ elem.AddAttribute ("valid", "invalid\"");\r
+ Fail ("#4");\r
+ } catch (ArgumentException) { }\r
+ try {\r
+ elem.AddAttribute ("valid", "valid\'"); \r
+ } catch (ArgumentException) { Fail ("#5"); }\r
+ try { \r
+ elem.AddAttribute ("valid", "valid&");\r
+ Fail ("#6");\r
+ // in xml world this is actually not considered valid\r
+ // but it is by MS.Net\r
+ } catch (ArgumentException) { }\r
+ try {\r
+ elem.AddAttribute ("valid", "<invalid>");\r
+ Fail ("#7"); \r
+ } catch (ArgumentException) { }\r
+ }\r
+ \r
+ public void TestAttributes ()\r
+ {\r
+ Hashtable h = elem.Attributes;\r
+ \r
+ /*\r
+ // this will result in an InvalidCastException on MS.Net\r
+ // I have no clue why\r
+ \r
+ h.Add ("<invalid>", "valid");\r
+ try {\r
+ elem.Attributes = h;\r
+ Fail ("#1");\r
+ } catch (ArgumentException) { }\r
+ */\r
+ \r
+ h = elem.Attributes;\r
+ h.Add ("valid", "\"invalid\"");\r
+ try {\r
+ elem.Attributes = h;\r
+ Fail ("#2");\r
+ } catch (ArgumentException) { }\r
+ \r
+ h = elem.Attributes;\r
+ h.Add ("foo", "bar");\r
+ Assert ("#3", elem.Attributes.Count != h.Count);\r
+ \r
+ elem.Attributes = h;\r
+ Assert ("#4", elem.Attribute ("foo") != null);\r
+ }\r
+ \r
+ public void TestEqual ()\r
+ {\r
+ SecurityElement elem2 = CreateElement ();\r
+ Assert ("#1", elem.Equal (elem2));\r
+ SecurityElement child = (SecurityElement) elem2.Children [0];\r
+ child = (SecurityElement) child.Children [1];\r
+ child.Text = "some text";\r
+ Assert ("#1", !elem.Equal (elem2));\r
+ }\r
+ \r
+ public void TestEscape ()\r
+ {\r
+ AssertEquals ("#1", SecurityElement.Escape ("foo<>\"'& bar"), "foo<>"'& bar");\r
+ }\r
+ \r
+ public void TestIsValidAttributeName ()\r
+ {\r
+ Assert ("#1", !SecurityElement.IsValidAttributeName ("x x")); \r
+ Assert ("#2", !SecurityElement.IsValidAttributeName ("x<x")); \r
+ Assert ("#3", !SecurityElement.IsValidAttributeName ("x>x"));\r
+ Assert ("#4", SecurityElement.IsValidAttributeName ("x\"x"));\r
+ Assert ("#5", SecurityElement.IsValidAttributeName ("x'x"));\r
+ Assert ("#6", SecurityElement.IsValidAttributeName ("x&x")); \r
+ }\r
+\r
+ public void TestIsValidAttributeValue ()\r
+ {\r
+ Assert ("#1", SecurityElement.IsValidAttributeValue ("x x")); \r
+ Assert ("#2", !SecurityElement.IsValidAttributeValue ("x<x")); \r
+ Assert ("#3", !SecurityElement.IsValidAttributeValue ("x>x"));\r
+ Assert ("#4", !SecurityElement.IsValidAttributeValue ("x\"x"));\r
+ Assert ("#5", SecurityElement.IsValidAttributeValue ("x'x"));\r
+ Assert ("#6", SecurityElement.IsValidAttributeValue ("x&x")); \r
+ }\r
+\r
+ public void TestIsValidTag ()\r
+ {\r
+ Assert ("#1", !SecurityElement.IsValidTag ("x x")); \r
+ Assert ("#2", !SecurityElement.IsValidTag ("x<x")); \r
+ Assert ("#3", !SecurityElement.IsValidTag ("x>x"));\r
+ Assert ("#4", SecurityElement.IsValidTag ("x\"x"));\r
+ Assert ("#5", SecurityElement.IsValidTag ("x'x"));\r
+ Assert ("#6", SecurityElement.IsValidTag ("x&x"));\r
+ }\r
+\r
+ public void TestIsValidText ()\r
+ {\r
+ Assert ("#1", SecurityElement.IsValidText ("x x")); \r
+ Assert ("#2", !SecurityElement.IsValidText ("x<x")); \r
+ Assert ("#3", !SecurityElement.IsValidText ("x>x"));\r
+ Assert ("#4", SecurityElement.IsValidText ("x\"x"));\r
+ Assert ("#5", SecurityElement.IsValidText ("x'x"));\r
+ Assert ("#6", SecurityElement.IsValidText ("x&x"));\r
+ }\r
+ \r
+ public void TestSearchForChildByTag ()\r
+ {\r
+ SecurityElement child = null;\r
+ try {\r
+ child = elem.SearchForChildByTag (null);\r
+ Fail ("#1 should have thrown an ArgumentNullException");\r
+ } catch (ArgumentNullException) { }\r
+\r
+ child = elem.SearchForChildByTag ("doesnotexist");\r
+ AssertEquals ("#2", child, null);\r
+ \r
+ child = elem.SearchForChildByTag ("ENDPOINT");\r
+ AssertEquals ("#3", child, null);\r
+ \r
+ child = (SecurityElement) elem.Children [0];\r
+ child = child.SearchForChildByTag ("ENDPOINT");\r
+ AssertEquals ("#4", child.Attribute ("transport"), "All");\r
+ }\r
+ \r
+ public void TestSearchForTextOfTag ()\r
+ {\r
+ try {\r
+ string t1 = elem.SearchForTextOfTag (null);\r
+ Fail ("#1 should have thrown an ArgumentNullException");\r
+ } catch (ArgumentNullException) { }\r
+ \r
+ string t2 = elem.SearchForTextOfTag ("ENDPOINT");\r
+ AssertEquals ("#2", t2, "some text");\r
+ }\r
+}\r
+\r
+}\r
+\r