Merge pull request #1909 from esdrubal/reflection
[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 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Web.Util;
32
33 using StandAloneRunnerSupport;
34 using StandAloneTests;
35
36 using NUnit.Framework;
37
38 using StandAloneTests.RequestValidator.Generated;
39
40 namespace StandAloneTests.RequestValidator
41 {
42         class RequestValidatorCallSet
43         {
44                 List <Dictionary <string, object>> callSets;
45
46                 public List <Dictionary <string, object>> CallSets {
47                         get {
48                                 if (callSets == null)
49                                         callSets = new List <Dictionary <string, object>> ();
50
51                                 return callSets;
52                         }
53                 }
54
55                 public string Name {
56                         get;
57                         protected set;
58                 }
59                 
60                 protected void RegisterCallSet (Dictionary <string, object> callSet) 
61                 {
62                         if (callSet == null || callSet.Count == 0)
63                                 return;
64                         
65                         CallSets.Add (callSet);
66                 }
67
68                 public bool ContainsCallSet (Dictionary <string, object> callSet)
69                 {
70                         foreach (var dict in CallSets)
71                                 if (DictionariesEqual (dict, callSet))
72                                         return true;
73
74                         return false;
75                 }
76
77                 bool DictionariesEqual (Dictionary <string, object> first, Dictionary <string, object> second)
78                 {
79                         if (first == null ^ second == null)
80                                 return false;
81                         
82                         if (first.Count != second.Count)
83                                 return false;
84                         
85                         object left, right;
86                         foreach (string s in first.Keys) {
87                                 if (s == "calledFrom")
88                                         continue;
89                                 
90                                 if (!second.TryGetValue (s, out left))
91                                         return false;
92
93                                 right = first [s];
94                                 if (left == null ^ right == null)
95                                         return false;
96
97                                 if (left == null)
98                                         continue;
99                                 
100                                 if (!left.Equals (right))
101                                         return false;
102                         }
103
104                         return true;
105                 }
106         }
107
108         static class RequestValidatorCallSetContainer
109         {
110                 public static List <RequestValidatorCallSet> CallSets {
111                         get;
112                         private set;
113                 }
114                 
115                 static RequestValidatorCallSetContainer ()
116                 {
117                         CallSets  = new List <RequestValidatorCallSet> ();
118                 }
119
120                 public static RequestValidatorCallSet GetCallSet (string name)
121                 {
122                         foreach (RequestValidatorCallSet cs in CallSets)
123                                 if (String.Compare (cs.Name, name, StringComparison.Ordinal) == 0)
124                                         return cs;
125
126                         return null;
127                 }
128                 
129                 public static void Register (RequestValidatorCallSet callSet)
130                 {
131                         CallSets.Add (callSet);
132                 }
133         }
134
135         [TestCase ("RequestValidator", "4.0 extensible request validation tests.")]
136         public sealed class RequestValidatorTests : ITestCase
137         {
138                 public string PhysicalPath {
139                         get { return Path.Combine (Consts.BasePhysicalDir, "RequestValidator"); }
140                 }
141                 
142                 public string VirtualPath  {
143                         get { return "/"; }
144                 }
145
146                 public bool SetUp (List <TestRunItem> runItems)
147                 {
148                         GeneratedCallSets.Register ();
149
150                         runItems.Add (new TestRunItem ("Default.aspx", Default_Aspx));
151                         runItems.Add (new TestRunItem ("Default.aspx?key=invalid<script>value</script>", Default_Aspx_Script));
152                         
153                         return true;
154                 }
155
156                 string SummarizeCallSet (Dictionary <string, object> callSet)
157                 {
158                         return String.Format (@"                      URL: {0}
159           Context present: {1}
160                     Value: {2}
161 Request validation source: {3}
162            Collection key: {4}
163  Validation failure index: {5}
164              Return value: {6}
165 ",
166                                               callSet ["rawUrl"],
167                                               callSet ["context"],
168                                               callSet ["value"],
169                                               (int)callSet ["requestValidationSource"],
170                                               callSet ["collectionKey"],
171                                               callSet ["validationFailureIndex"],
172                                               callSet ["returnValue"]);
173                 }
174                 
175                 void Default_Aspx (string result, TestRunItem runItem)
176                 {
177                         if (runItem == null)
178                                 throw new ArgumentNullException ("runItem");
179                         CompareCallSets (runItem, "000");
180                 }
181
182                 void Default_Aspx_Script (string result, TestRunItem runItem)
183                 {
184                         if (runItem == null)
185                                 throw new ArgumentNullException ("runItem");
186
187                         CompareCallSets (runItem, "001");
188                 }
189
190                 void CompareCallSets (TestRunItem runItem, string name)
191                 {
192                         var dict = runItem.TestRunData as List <Dictionary <string, object>>;
193                         if (dict == null || dict.Count == 0)
194                                 Assert.Fail ("No call data recorded.");
195
196                         RequestValidatorCallSet cs = RequestValidatorCallSetContainer.GetCallSet (name);
197                         if (cs == null)
198                                 Assert.Fail ("Call set \"{0}\" not found.", name);
199
200                         foreach (Dictionary <string, object> calls in dict) {
201                                 if (!cs.ContainsCallSet (calls))
202                                         Assert.Fail ("{0}: call sequence not found:{1}{2}", name, Environment.NewLine, SummarizeCallSet (calls));
203                         }
204                         
205                 }
206         }
207 }
208