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.
29 using System.Collections.Generic;
31 using System.Web.Util;
33 using StandAloneRunnerSupport;
34 using StandAloneTests;
36 using NUnit.Framework;
38 using StandAloneTests.RequestValidator.Generated;
40 namespace StandAloneTests.RequestValidator
42 class RequestValidatorCallSet
44 List <Dictionary <string, object>> callSets;
46 public List <Dictionary <string, object>> CallSets {
49 callSets = new List <Dictionary <string, object>> ();
60 protected void RegisterCallSet (Dictionary <string, object> callSet)
62 if (callSet == null || callSet.Count == 0)
65 CallSets.Add (callSet);
68 public bool ContainsCallSet (Dictionary <string, object> callSet)
70 foreach (var dict in CallSets)
71 if (DictionariesEqual (dict, callSet))
77 bool DictionariesEqual (Dictionary <string, object> first, Dictionary <string, object> second)
79 if (first == null ^ second == null)
82 if (first.Count != second.Count)
86 foreach (string s in first.Keys) {
87 if (s == "calledFrom")
90 if (!second.TryGetValue (s, out left))
94 if (left == null ^ right == null)
100 if (!left.Equals (right))
108 static class RequestValidatorCallSetContainer
110 public static List <RequestValidatorCallSet> CallSets {
115 static RequestValidatorCallSetContainer ()
117 CallSets = new List <RequestValidatorCallSet> ();
120 public static RequestValidatorCallSet GetCallSet (string name)
122 foreach (RequestValidatorCallSet cs in CallSets)
123 if (String.Compare (cs.Name, name, StringComparison.Ordinal) == 0)
129 public static void Register (RequestValidatorCallSet callSet)
131 CallSets.Add (callSet);
135 [TestCase ("RequestValidator", "4.0 extensible request validation tests.")]
136 public sealed class RequestValidatorTests : ITestCase
138 public string PhysicalPath {
139 get { return Path.Combine (Consts.BasePhysicalDir, "RequestValidator"); }
142 public string VirtualPath {
146 public bool SetUp (List <TestRunItem> runItems)
148 GeneratedCallSets.Register ();
150 runItems.Add (new TestRunItem ("Default.aspx", Default_Aspx));
151 runItems.Add (new TestRunItem ("Default.aspx?key=invalid<script>value</script>", Default_Aspx_Script));
156 string SummarizeCallSet (Dictionary <string, object> callSet)
158 return String.Format (@" URL: {0}
161 Request validation source: {3}
163 Validation failure index: {5}
169 (int)callSet ["requestValidationSource"],
170 callSet ["collectionKey"],
171 callSet ["validationFailureIndex"],
172 callSet ["returnValue"]);
175 void Default_Aspx (string result, TestRunItem runItem)
178 throw new ArgumentNullException ("runItem");
179 CompareCallSets (runItem, "000");
182 void Default_Aspx_Script (string result, TestRunItem runItem)
185 throw new ArgumentNullException ("runItem");
187 CompareCallSets (runItem, "001");
190 void CompareCallSets (TestRunItem runItem, string name)
192 var dict = runItem.TestRunData as List <Dictionary <string, object>>;
193 if (dict == null || dict.Count == 0)
194 Assert.Fail ("No call data recorded.");
196 RequestValidatorCallSet cs = RequestValidatorCallSetContainer.GetCallSet (name);
198 Assert.Fail ("Call set \"{0}\" not found.", name);
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));