• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This should output "PROXY success:80" if all the tests pass.
2// Otherwise it will output "PROXY failure:<num-failures>".
3//
4// This aims to unit-test the PAC library functions, which are
5// exposed in the PAC's execution environment. (Namely, dnsDomainLevels,
6// timeRange, etc.)
7
8function FindProxyForURL(url, host) {
9  var numTestsFailed = 0;
10
11  // Run all the tests
12  for (var test in Tests) {
13    var t = new TestContext(test);
14
15    // Run the test.
16    Tests[test](t);
17
18    if (t.failed()) {
19      numTestsFailed++;
20    }
21  }
22
23  if (numTestsFailed == 0) {
24    return "PROXY success:80";
25  }
26  return "PROXY failure:" + numTestsFailed;
27}
28
29// --------------------------
30// Tests
31// --------------------------
32
33var Tests = {};
34
35Tests.testDnsDomainIs = function(t) {
36  t.expectTrue(dnsDomainIs("google.com", ".com"));
37  t.expectTrue(dnsDomainIs("google.co.uk", ".co.uk"));
38  t.expectFalse(dnsDomainIs("google.com", ".co.uk"));
39  t.expectFalse(dnsDomainIs("www.adobe.com", ".ad"));
40};
41
42Tests.testDnsDomainLevels = function(t) {
43  t.expectEquals(0, dnsDomainLevels("www"));
44  t.expectEquals(2, dnsDomainLevels("www.google.com"));
45  t.expectEquals(3, dnsDomainLevels("192.168.1.1"));
46};
47
48Tests.testIsInNet = function(t) {
49  t.expectTrue(
50      isInNet("192.89.132.25", "192.89.132.25", "255.255.255.255"));
51  t.expectFalse(
52      isInNet("193.89.132.25", "192.89.132.25", "255.255.255.255"));
53
54  t.expectTrue(isInNet("192.89.132.25", "192.89.0.0", "255.255.0.0"));
55  t.expectFalse(isInNet("193.89.132.25", "192.89.0.0", "255.255.0.0"));
56
57  t.expectFalse(
58      isInNet("192.89.132.a", "192.89.0.0", "255.255.0.0"));
59};
60
61Tests.testIsPlainHostName = function(t) {
62  t.expectTrue(isPlainHostName("google"));
63  t.expectFalse(isPlainHostName("google.com"));
64};
65
66Tests.testLocalHostOrDomainIs = function(t) {
67  t.expectTrue(localHostOrDomainIs("www.google.com", "www.google.com"));
68  t.expectTrue(localHostOrDomainIs("www", "www.google.com"));
69  t.expectFalse(localHostOrDomainIs("maps.google.com", "www.google.com"));
70};
71
72Tests.testShExpMatch = function(t) {
73  t.expectTrue(shExpMatch("foo.jpg", "*.jpg"));
74  t.expectTrue(shExpMatch("foo5.jpg", "*o?.jpg"));
75  t.expectFalse(shExpMatch("foo.jpg", ".jpg"));
76  t.expectFalse(shExpMatch("foo.jpg", "foo"));
77};
78
79Tests.testWeekdayRange = function(t) {
80  // Test with local time.
81  MockDate.setCurrent("Tue Mar 03 2009");
82  t.expectEquals(true, weekdayRange("MON", "FRI"));
83  t.expectEquals(true, weekdayRange("TUE", "FRI"));
84  t.expectEquals(true, weekdayRange("TUE", "TUE"));
85  t.expectEquals(true, weekdayRange("TUE"));
86  t.expectEquals(false, weekdayRange("WED", "FRI"));
87  t.expectEquals(false, weekdayRange("SUN", "MON"));
88  t.expectEquals(false, weekdayRange("SAT"));
89  t.expectEquals(false, weekdayRange("FRI", "MON"));
90
91  // Test with GMT time.
92  MockDate.setCurrent("Tue Mar 03 2009 GMT");
93  t.expectEquals(true, weekdayRange("MON", "FRI", "GMT"));
94  t.expectEquals(true, weekdayRange("TUE", "FRI", "GMT"));
95  t.expectEquals(true, weekdayRange("TUE", "TUE", "GMT"));
96  t.expectEquals(true, weekdayRange("TUE", "GMT"));
97  t.expectEquals(false, weekdayRange("WED", "FRI", "GMT"));
98  t.expectEquals(false, weekdayRange("SUN", "MON", "GMT"));
99  t.expectEquals(false, weekdayRange("SAT", "GMT"));
100};
101
102Tests.testDateRange = function(t) {
103  // dateRange(day)
104  MockDate.setCurrent("Mar 03 2009");
105  t.expectEquals(true, dateRange(3));
106  t.expectEquals(false, dateRange(1));
107
108  // dateRange(day, "GMT")
109  MockDate.setCurrent("Mar 03 2009 GMT");
110  t.expectEquals(true, dateRange(3, "GMT"));
111  t.expectEquals(false, dateRange(1, "GMT"));
112
113  // dateRange(day1, day2)
114  MockDate.setCurrent("Mar 03 2009");
115  t.expectEquals(true, dateRange(1, 4));
116  t.expectEquals(false, dateRange(4, 20));
117
118  // dateRange(day, month)
119  MockDate.setCurrent("Mar 03 2009");
120  t.expectEquals(true, dateRange(3, "MAR"));
121  MockDate.setCurrent("Mar 03 2014");
122  t.expectEquals(true, dateRange(3, "MAR"));
123  // TODO(eroman):
124  //t.expectEquals(false, dateRange(2, "MAR"));
125  //t.expectEquals(false, dateRange(3, "JAN"));
126
127  // dateRange(day, month, year)
128  MockDate.setCurrent("Mar 03 2009");
129  t.expectEquals(true, dateRange(3, "MAR", 2009));
130  t.expectEquals(false, dateRange(4, "MAR", 2009));
131  t.expectEquals(false, dateRange(3, "FEB", 2009));
132  MockDate.setCurrent("Mar 03 2014");
133  t.expectEquals(false, dateRange(3, "MAR", 2009));
134
135  // dateRange(month1, month2)
136  MockDate.setCurrent("Mar 03 2009");
137  t.expectEquals(true, dateRange("JAN", "MAR"));
138  t.expectEquals(true, dateRange("MAR", "APR"));
139  t.expectEquals(false, dateRange("MAY", "SEP"));
140
141  // dateRange(day1, month1, day2, month2)
142  MockDate.setCurrent("Mar 03 2009");
143  t.expectEquals(true, dateRange(1, "JAN", 3, "MAR"));
144  t.expectEquals(true, dateRange(3, "MAR", 4, "SEP"));
145  t.expectEquals(false, dateRange(4, "MAR", 4, "SEP"));
146
147  // dateRange(month1, year1, month2, year2)
148  MockDate.setCurrent("Mar 03 2009");
149  t.expectEquals(true, dateRange("FEB", 2009, "MAR", 2009));
150  MockDate.setCurrent("Apr 03 2009");
151  t.expectEquals(true, dateRange("FEB", 2009, "MAR", 2010));
152  t.expectEquals(false, dateRange("FEB", 2009, "MAR", 2009));
153
154  // dateRange(day1, month1, year1, day2, month2, year2)
155  MockDate.setCurrent("Mar 03 2009");
156  t.expectEquals(true, dateRange(1, "JAN", 2009, 3, "MAR", 2009));
157  t.expectEquals(true, dateRange(3, "MAR", 2009, 4, "SEP", 2009));
158  t.expectEquals(true, dateRange(3, "JAN", 2009, 4, "FEB", 2010));
159  t.expectEquals(false, dateRange(4, "MAR", 2009, 4, "SEP", 2009));
160};
161
162Tests.testTimeRange = function(t) {
163  // timeRange(hour)
164  MockDate.setCurrent("Mar 03, 2009 03:34:01");
165  t.expectEquals(true, timeRange(3));
166  t.expectEquals(false, timeRange(2));
167
168  // timeRange(hour1, hour2)
169  MockDate.setCurrent("Mar 03, 2009 03:34:01");
170  t.expectEquals(true, timeRange(2, 3));
171  t.expectEquals(true, timeRange(2, 4));
172  t.expectEquals(true, timeRange(3, 5));
173  t.expectEquals(false, timeRange(1, 2));
174  t.expectEquals(false, timeRange(11, 12));
175
176  // timeRange(hour1, min1, hour2, min2)
177  MockDate.setCurrent("Mar 03, 2009 03:34:01");
178  t.expectEquals(true, timeRange(1, 0, 3, 34));
179  t.expectEquals(true, timeRange(1, 0, 3, 35));
180  t.expectEquals(true, timeRange(3, 34, 5, 0));
181  t.expectEquals(false, timeRange(1, 0, 3, 0));
182  t.expectEquals(false, timeRange(11, 0, 16, 0));
183
184  // timeRange(hour1, min1, sec1, hour2, min2, sec2)
185  MockDate.setCurrent("Mar 03, 2009 03:34:14");
186  t.expectEquals(true, timeRange(1, 0, 0, 3, 34, 14));
187  t.expectEquals(false, timeRange(1, 0, 0, 3, 34, 0));
188  t.expectEquals(true, timeRange(1, 0, 0, 3, 35, 0));
189  t.expectEquals(true, timeRange(3, 34, 0, 5, 0, 0));
190  t.expectEquals(false, timeRange(1, 0, 0, 3, 0, 0));
191  t.expectEquals(false, timeRange(11, 0, 0, 16, 0, 0));
192};
193
194// --------------------------
195// TestContext
196// --------------------------
197
198// |name| is the name of the test being executed, it will be used when logging
199// errors.
200function TestContext(name) {
201  this.numFailures_ = 0;
202  this.name_ = name;
203};
204
205TestContext.prototype.failed = function() {
206  return this.numFailures_ != 0;
207};
208
209TestContext.prototype.expectEquals = function(expectation, actual) {
210  if (!(expectation === actual)) {
211    this.numFailures_++;
212    this.log("FAIL: expected: " + expectation + ", actual: " + actual);
213  }
214};
215
216TestContext.prototype.expectTrue = function(x) {
217  this.expectEquals(true, x);
218};
219
220TestContext.prototype.expectFalse = function(x) {
221  this.expectEquals(false, x);
222};
223
224TestContext.prototype.log = function(x) {
225  // Prefix with the test name that generated the log.
226  try {
227    alert(this.name_ + ": " + x);
228  } catch(e) {
229    // In case alert() is not defined.
230  }
231};
232
233// --------------------------
234// MockDate
235// --------------------------
236
237function MockDate() {
238  this.wrappedDate_ = new MockDate.super_(MockDate.currentDateString_);
239};
240
241// Setup the MockDate so it forwards methods to "this.wrappedDate_" (which is a
242// real Date object).  We can't simply chain the prototypes since Date() doesn't
243// allow it.
244MockDate.init = function() {
245  MockDate.super_ = Date;
246
247  function createProxyMethod(methodName) {
248    return function() {
249      return this.wrappedDate_[methodName]
250          .apply(this.wrappedDate_, arguments);
251    }
252  };
253
254  for (i in MockDate.methodNames_) {
255    var methodName = MockDate.methodNames_[i];
256    // Don't define the closure directly in the loop body, since Javascript's
257    // crazy scoping rules mean |methodName| actually bleeds out of the loop!
258    MockDate.prototype[methodName] = createProxyMethod(methodName);
259  }
260
261  // Replace the native Date() with our mock.
262  Date = MockDate;
263};
264
265// Unfortunately Date()'s methods are non-enumerable, therefore list manually.
266MockDate.methodNames_ = [
267  "toString", "toDateString", "toTimeString", "toLocaleString",
268  "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime",
269  "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth",
270  "getDate", "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours",
271  "getMinutes", "getUTCMinutes", "getSeconds", "getUTCSeconds",
272  "getMilliseconds", "getUTCMilliseconds", "getTimezoneOffset", "setTime",
273  "setMilliseconds", "setUTCMilliseconds", "setSeconds", "setUTCSeconds",
274  "setMinutes", "setUTCMinutes", "setHours", "setUTCHours", "setDate",
275  "setUTCDate", "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear",
276  "toGMTString", "toUTCString", "getYear", "setYear"
277];
278
279MockDate.setCurrent = function(currentDateString) {
280  MockDate.currentDateString_ = currentDateString;
281}
282
283// Bind the methods to proxy requests to the wrapped Date().
284MockDate.init();
285
286