[eglib] Prefer <langinfo.h> to <localcharset.h>
[mono.git] / mcs / class / System.Web / Test / standalone-tests / RequestValidator.cs
1 //
2 // Authors:
3 //   Marek Habersack (mhabersack@novell.com)
4 //
5 // (C) 2010 Novell, Inc http://novell.com/
6 //
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if NET_4_0
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Web.Util;
33
34 using StandAloneRunnerSupport;
35 using StandAloneTests;
36
37 using NUnit.Framework;
38
39 using StandAloneTests.RequestValidator.Generated;
40
41 namespace StandAloneTests.RequestValidator
42 {
43         class RequestValidatorCallSet
44         {
45                 List <Dictionary <string, object>> callSets;
46
47                 public List <Dictionary <string, object>> CallSets {
48                         get {
49                                 if (callSets == null)
50                                         callSets = new List <Dictionary <string, object>> ();
51
52                                 return callSets;
53                         }
54                 }
55
56                 public string Name {
57                         get;
58                         protected set;
59                 }
60                 
61                 protected void RegisterCallSet (Dictionary <string, object> callSet) 
62                 {
63                         if (callSet == null || callSet.Count == 0)
64                                 return;
65                         
66                         CallSets.Add (callSet);
67                 }
68
69                 public bool ContainsCallSet (Dictionary <string, object> callSet)
70                 {
71                         foreach (var dict in CallSets)
72                                 if (DictionariesEqual (dict, callSet))
73                                         return true;
74
75                         return false;
76                 }
77
78                 bool DictionariesEqual (Dictionary <string, object> first, Dictionary <string, object> second)
79                 {
80                         if (first == null ^ second == null)
81                                 return false;
82                         
83                         if (first.Count != second.Count)
84                                 return false;
85                         
86                         object left, right;
87                         foreach (string s in first.Keys) {
88                                 if (s == "calledFrom")
89                                         continue;
90                                 
91                                 if (!second.TryGetValue (s, out left))
92                                         return false;
93
94                                 right = first [s];
95                                 if (left == null ^ right == null)
96                                         return false;
97
98                                 if (left == null)
99                                         continue;
100                                 
101                                 if (!left.Equals (right))
102                                         return false;
103                         }
104
105                         return true;
106                 }
107         }
108
109         static class RequestValidatorCallSetContainer
110         {
111                 public static List <RequestValidatorCallSet> CallSets {
112                         get;
113                         private set;
114                 }
115                 
116                 static RequestValidatorCallSetContainer ()
117                 {
118                         CallSets  = new List <RequestValidatorCallSet> ();
119                 }
120
121                 public static RequestValidatorCallSet GetCallSet (string name)
122                 {
123                         foreach (RequestValidatorCallSet cs in CallSets)
124                                 if (String.Compare (cs.Name, name, StringComparison.Ordinal) == 0)
125                                         return cs;
126
127                         return null;
128                 }
129                 
130                 public static void Register (RequestValidatorCallSet callSet)
131                 {
132                         CallSets.Add (callSet);
133                 }
134         }
135
136         [TestCase ("RequestValidator", "4.0 extensible request validation tests.")]
137         public sealed class RequestValidatorTests : ITestCase
138         {
139                 public string PhysicalPath {
140                         get { return Path.Combine (Consts.BasePhysicalDir, "RequestValidator"); }
141                 }
142                 
143                 public string VirtualPath  {
144                         get { return "/"; }
145                 }
146
147                 public bool SetUp (List <TestRunItem> runItems)
148                 {
149                         GeneratedCallSets.Register ();
150
151                         runItems.Add (new TestRunItem ("Default.aspx", Default_Aspx));
152                         runItems.Add (new TestRunItem ("Default.aspx?key=invalid<script>value</script>", Default_Aspx_Script));
153                         
154                         return true;
155                 }
156
157                 string SummarizeCallSet (Dictionary <string, object> callSet)
158                 {
159                         return String.Format (@"                      URL: {0}
160           Context present: {1}
161                     Value: {2}
162 Request validation source: {3}
163            Collection key: {4}
164  Validation failure index: {5}
165              Return value: {6}
166 ",
167                                               callSet ["rawUrl"],
168                                               callSet ["context"],
169                                               callSet ["value"],
170                                               (int)callSet ["requestValidationSource"],
171                                               callSet ["collectionKey"],
172                                               callSet ["validationFailureIndex"],
173                                               callSet ["returnValue"]);
174                 }
175                 
176                 void Default_Aspx (string result, TestRunItem runItem)
177                 {
178                         if (runItem == null)
179                                 throw new ArgumentNullException ("runItem");
180                         CompareCallSets (runItem, "000");
181                 }
182
183                 void Default_Aspx_Script (string result, TestRunItem runItem)
184                 {
185                         if (runItem == null)
186                                 throw new ArgumentNullException ("runItem");
187
188                         CompareCallSets (runItem, "001");
189                 }
190
191                 void CompareCallSets (TestRunItem runItem, string name)
192                 {
193                         var dict = runItem.TestRunData as List <Dictionary <string, object>>;
194                         if (dict == null || dict.Count == 0)
195                                 Assert.Fail ("No call data recorded.");
196
197                         RequestValidatorCallSet cs = RequestValidatorCallSetContainer.GetCallSet (name);
198                         if (cs == null)
199                                 Assert.Fail ("Call set \"{0}\" not found.", name);
200
201                         foreach (Dictionary <string, object> calls in dict) {
202                                 if (!cs.ContainsCallSet (calls))
203                                         Assert.Fail ("{0}: call sequence not found:{1}{2}", name, Environment.NewLine, SummarizeCallSet (calls));
204                         }
205                         
206                 }
207         }
208 }
209
210 #endif