[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System / Test / System.Diagnostics / DiagnosticsConfigurationHandlerTest.cs
1 //
2 // DiagnosticsConfigurationHandlerTest.cs:
3 //      NUnit Test Cases for System.Diagnostics.DiagnosticsConfigurationHandler
4 //
5 // Authors:
6 //   Jonathan Pryor (jonpryor@vt.edu)
7 //   Martin Willemoes Hansen (mwh@sysrq.dk)
8 //
9 // (C) Jonathan Pryor
10 // (C) 2003 Martin Willemoes Hansen
11 // 
12
13 #if !MOBILE
14
15 using NUnit.Framework;
16 using System;
17 using System.Configuration;
18 using System.Diagnostics;
19 using System.Xml;
20
21 namespace MonoTests.System.Diagnostics
22 {
23         [TestFixture]
24         public class DiagnosticsConfigurationHandlerTest
25         {
26                 private const string XmlFormat = 
27                         "{0}";
28                         /*
29                         "<system.diagnostics>" +
30                         "{0}" +
31                         "</system.diagnostics>";
32                          */
33
34                 private DiagnosticsConfigurationHandler handler = new DiagnosticsConfigurationHandler ();
35
36                 [Test]
37                 [Category ("NotDotNet")]
38                 public void SwitchesTag_Attributes ()
39                 {
40                         string[] attrs = {"invalid=\"yes\""};
41                         ValidateExceptions ("#TST:A", "<switches {0}></switches>", attrs);
42                 }
43
44                 void ValidateExceptions (string name, string format, string[] args)
45                 {
46                         foreach (string arg in args) {
47                                 string xml = string.Format (XmlFormat,
48                                                 string.Format (format, arg));
49                                 try {
50                                         CreateHandler (xml);
51                                         Assert.Fail (string.Format ("{0}:{1}: no exception generated", name, arg));
52                                 } catch (ConfigurationException) {
53                                 } catch (AssertionException) {
54                                         // This is generated by the Assertion.Fail() statement in the try block.
55                                         throw;
56                                 } catch (Exception e) {
57                                         Assert.Fail (string.Format ("{0}:{1}: wrong exception generated: {2} ({3}).", 
58                                                 name, arg, e.ToString(), 
59                                                 e.InnerException == null ? "" : e.InnerException.ToString()));
60                                 }
61                         }
62                 }
63
64                 private void ValidateSuccess (string name, string format, string[] args)
65                 {
66                         foreach (string arg in args) {
67                                 string xml = string.Format (XmlFormat,
68                                                 string.Format (format, arg));
69                                 try {
70                                         CreateHandler (xml);
71                                 } catch (Exception e) {
72                                         Assert.Fail (string.Format ("{0}:{1}: exception generated: {2} ({3}).", 
73                                                 name, arg, e.ToString(),
74                                                 e.InnerException == null ? "" : e.InnerException.ToString()));
75                                 }
76                         }
77                 }
78
79                 private object CreateHandler (string xml)
80                 {
81                         XmlDocument d = new XmlDocument ();
82                         d.LoadXml (xml);
83                         return handler.Create (null, null, d);
84                 }
85
86                 [Test]
87                 [Category ("NotDotNet")]
88                 public void SwitchesTag_Elements ()
89                 {
90                         string[] badElements = {
91                                 // not enough arguments
92                                 "<add />",
93                                 "<add name=\"a\"/>",
94                                 "<add value=\"b\"/>",
95 #if !NET_2_0
96                                 // in the 2.0 profile, we currently allow non-integral
97                                 // values
98                                 //
99                                 // MS actually introduced new configuration classes
100                                 // for the 2.0 profile and did not modify the original
101                                 // classes
102                                 //
103                                 // Once we add 2.0 configuration classes for 
104                                 // system.diagnostics, the origina behavior should
105                                 // be restored and this test should be enabled again
106                                 // for the 2.0 profile
107
108                                 // non-integral value
109                                 "<add name=\"string-value\" value=\"string-value\"/>",
110 #endif
111                                 // too many arguments
112                                 "<add name=\"a\" value=\"b\" extra=\"c\"/>",
113                                 // wrong casing
114                                 "<add Name=\"a\" value=\"b\"/>",
115                                 "<Add Name=\"a\" value=\"b\"/>",
116                                 // missing args
117                                 "<remove />",
118                                 "<remove value=\"b\"/>",
119                                 // too many args
120                                 "<remove name=\"a\" value=\"b\"/>",
121                                 "<clear name=\"a\"/>",
122                                 // invalid element
123                                 "<invalid element=\"a\" here=\"b\"/>"
124                         };
125                         ValidateExceptions ("#TST:IE:Bad", "<switches>{0}</switches>", badElements);
126
127                         string[] goodElements = {
128                                 "<add name=\"a\" value=\"4\"/>",
129                                 "<add name=\"a\" value=\"-2\"/>",
130                                 "<remove name=\"a\"/>",
131                                 "<clear/>"
132                         };
133                         ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
134                 }
135
136                 [Test]
137                 [Category ("NotDotNet")]
138                 public void AssertTag ()
139                 {
140                         string[] goodAttributes = {
141                                 "",
142                                 "assertuienabled=\"true\"",
143                                 "assertuienabled=\"false\" logfilename=\"some file name\"",
144                                 "logfilename=\"some file name\""
145                         };
146                         ValidateSuccess ("#TAT:Good", "<assert {0}/>", goodAttributes);
147
148                         string[] badAttributes = {
149                                 "AssertUiEnabled=\"true\"",
150                                 "LogFileName=\"foo\"",
151                                 "assertuienabled=\"\"",
152                                 "assertuienabled=\"non-boolean-value\""
153                         };
154                         ValidateExceptions ("#TAT:BadAttrs", "<assert {0}/>", badAttributes);
155
156                         string[] badChildren = {
157                                 "<any element=\"here\"/>"
158                         };
159                         ValidateExceptions ("#TAT:BadChildren", "<assert>{0}</assert>", badChildren);
160                 }
161
162                 [Test]
163                 [Category ("NotDotNet")]
164                 public void TraceTag_Attributes ()
165                 {
166                         string[] good = {
167                                 "",
168                                 "autoflush=\"true\"",
169                                 "indentsize=\"4\"",
170                                 "autoflush=\"false\" indentsize=\"10\""
171                         };
172                         ValidateSuccess ("#TTT:A:Good", "<trace {0}/>", good);
173
174                         string[] bad = {
175                                 "AutoFlush=\"true\"",
176                                 "IndentSize=\"false\"",
177                                 "autoflush=\"non-boolean-value\"",
178                                 "autoflush=\"\"",
179                                 "indentsize=\"non-integral-value\"",
180                                 "indentsize=\"\"",
181                                 "extra=\"invalid\""
182                         };
183                         ValidateExceptions ("#TTT:A:Bad", "<trace {0}/>", bad);
184                 }
185
186                 [Test]
187                 [Category ("NotDotNet")]
188                 public void TraceTag_Children ()
189                 {
190                         string[] good = {
191                                 // more about listeners in a different function...
192                                 "<listeners />"
193                         };
194                         ValidateSuccess ("#TTT:C:Good", "<trace>{0}</trace>", good);
195
196                         string[] bad = {
197                                 "<listeners with=\"attribute\"/>",
198                                 "<invalid element=\"here\"/>"
199                         };
200                         ValidateExceptions ("#TTT:C:Bad", "<trace>{0}</trace>", bad);
201                 }
202
203                 [Test]
204                 [Category ("NotDotNet")]
205                 public void TraceTag_Listeners ()
206                 {
207                         const string format = "<trace><listeners>{0}</listeners></trace>";
208                         string[] good = {
209                                 "<clear/>",
210                                 "<add name=\"foo\" " +
211                                         "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" " +
212                                         "initializeData=\"argument.txt\"/>",
213                                 "<remove name=\"foo\"/>",
214                                 "<add name=\"foo\" " +
215                                         "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" />",
216                                 "<remove name=\"foo\"/>"
217                         };
218                         ValidateSuccess ("#TTT:L:Good", format, good);
219
220                         string[] bad = {
221                                 "<invalid tag=\"here\"/>",
222                                 "<clear with=\"args\"/>",
223                                 "<remove/>",
224                                 "<add/>",
225                                 "<remove name=\"foo\" extra=\"arg\"/>",
226 #if NET_2_0 // type is optional (it could indicate a named listener)
227 #else
228                                 "<add name=\"foo\"/>",
229 #endif
230                                 "<add type=\"foo\"/>",
231                                 "<add name=\"foo\" type=\"invalid-type\"/>",
232                         };
233                         ValidateExceptions ("#TTT:L:Bad", format, bad);
234                 }
235         }
236 }
237
238 #endif