3 // Marek Habersack (mhabersack@novell.com)
5 // (C) 2010 Novell, Inc http://novell.com/
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
32 using System.Web.Util;
34 using StandAloneRunnerSupport;
35 using StandAloneTests;
37 using NUnit.Framework;
39 using StandAloneTests.RequestValidator.Generated;
41 namespace StandAloneTests.RequestValidator
43 class RequestValidatorCallSet
45 List <Dictionary <string, object>> callSets;
47 public List <Dictionary <string, object>> CallSets {
50 callSets = new List <Dictionary <string, object>> ();
61 protected void RegisterCallSet (Dictionary <string, object> callSet)
63 if (callSet == null || callSet.Count == 0)
66 CallSets.Add (callSet);
69 public bool ContainsCallSet (Dictionary <string, object> callSet)
71 foreach (var dict in CallSets)
72 if (DictionariesEqual (dict, callSet))
78 bool DictionariesEqual (Dictionary <string, object> first, Dictionary <string, object> second)
80 if (first == null ^ second == null)
83 if (first.Count != second.Count)
87 foreach (string s in first.Keys) {
88 if (s == "calledFrom")
91 if (!second.TryGetValue (s, out left))
95 if (left == null ^ right == null)
101 if (!left.Equals (right))
109 static class RequestValidatorCallSetContainer
111 public static List <RequestValidatorCallSet> CallSets {
116 static RequestValidatorCallSetContainer ()
118 CallSets = new List <RequestValidatorCallSet> ();
121 public static RequestValidatorCallSet GetCallSet (string name)
123 foreach (RequestValidatorCallSet cs in CallSets)
124 if (String.Compare (cs.Name, name, StringComparison.Ordinal) == 0)
130 public static void Register (RequestValidatorCallSet callSet)
132 CallSets.Add (callSet);
136 [TestCase ("RequestValidator", "4.0 extensible request validation tests.")]
137 public sealed class RequestValidatorTests : ITestCase
139 public string PhysicalPath {
140 get { return Path.Combine (Consts.BasePhysicalDir, "RequestValidator"); }
143 public string VirtualPath {
147 public bool SetUp (List <TestRunItem> runItems)
149 GeneratedCallSets.Register ();
151 runItems.Add (new TestRunItem ("Default.aspx", Default_Aspx));
152 runItems.Add (new TestRunItem ("Default.aspx?key=invalid<script>value</script>", Default_Aspx_Script));
157 string SummarizeCallSet (Dictionary <string, object> callSet)
159 return String.Format (@" URL: {0}
162 Request validation source: {3}
164 Validation failure index: {5}
170 (int)callSet ["requestValidationSource"],
171 callSet ["collectionKey"],
172 callSet ["validationFailureIndex"],
173 callSet ["returnValue"]);
176 void Default_Aspx (string result, TestRunItem runItem)
179 throw new ArgumentNullException ("runItem");
180 CompareCallSets (runItem, "000");
183 void Default_Aspx_Script (string result, TestRunItem runItem)
186 throw new ArgumentNullException ("runItem");
188 CompareCallSets (runItem, "001");
191 void CompareCallSets (TestRunItem runItem, string name)
193 var dict = runItem.TestRunData as List <Dictionary <string, object>>;
194 if (dict == null || dict.Count == 0)
195 Assert.Fail ("No call data recorded.");
197 RequestValidatorCallSet cs = RequestValidatorCallSetContainer.GetCallSet (name);
199 Assert.Fail ("Call set \"{0}\" not found.", name);
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));