1 // This should output "PROXY success:80" if all the tests pass.
2 // Otherwise it will output "PROXY failure:<num-failures>".
4 // This aims to unit-test the PAC library functions, which are
5 // exposed in the PAC's execution environment. (Namely, dnsDomainLevels,
8 function FindProxyForURL(url
, host
) {
9 var numTestsFailed
= 0;
12 for (var test
in Tests
) {
13 var t
= new TestContext(test
);
23 if (numTestsFailed
== 0) {
24 return "PROXY success:80";
26 return "PROXY failure:" + numTestsFailed
;
29 // --------------------------
31 // --------------------------
35 Tests
.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"));
42 Tests
.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"));
48 Tests
.testIsInNet = function(t
) {
50 isInNet("192.89.132.25", "192.89.132.25", "255.255.255.255"));
52 isInNet("193.89.132.25", "192.89.132.25", "255.255.255.255"));
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"));
58 isInNet("192.89.132.a", "192.89.0.0", "255.255.0.0"));
61 Tests
.testIsPlainHostName = function(t
) {
62 t
.expectTrue(isPlainHostName("google"));
63 t
.expectFalse(isPlainHostName("google.com"));
66 Tests
.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"));
72 Tests
.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"));
79 Tests
.testSortIpAddressList = function(t
) {
80 t
.expectEquals("::1;::2;::3", sortIpAddressList("::2;::3;::1"));
82 "2001:4898:28:3:201:2ff:feea:fc14;fe80::5efe:157:9d3b:8b16;157.59.139.22",
83 sortIpAddressList("157.59.139.22;" +
84 "2001:4898:28:3:201:2ff:feea:fc14;" +
85 "fe80::5efe:157:9d3b:8b16"));
87 // Single IP address (v4 and v6).
88 t
.expectEquals("127.0.0.1", sortIpAddressList("127.0.0.1"));
89 t
.expectEquals("::1", sortIpAddressList("::1"))
91 // Verify that IPv6 address is not re-written (not reduced).
92 t
.expectEquals("0:0::1;192.168.1.1", sortIpAddressList("192.168.1.1;0:0::1"));
94 // Input is already sorted.
95 t
.expectEquals("::1;192.168.1.3", sortIpAddressList("::1;192.168.1.3"));
97 // Same-valued IP addresses (also tests stability).
98 t
.expectEquals("0::1;::1;0:0::1", sortIpAddressList("0::1;::1;0:0::1"));
100 // Contains extra semi-colons.
101 t
.expectEquals("127.0.0.1", sortIpAddressList(";127.0.0.1;"));
103 // Contains whitespace (spaces and tabs).
104 t
.expectEquals("192.168.0.1;192.168.0.2",
105 sortIpAddressList("192.168.0.1; 192.168.0.2"));
106 t
.expectEquals("127.0.0.0;127.0.0.1;127.0.0.2",
107 sortIpAddressList("127.0.0.1; 127.0.0.2; 127.0.0.0"));
110 t
.expectFalse(sortIpAddressList(""));
111 t
.expectFalse(sortIpAddressList(" "));
112 t
.expectFalse(sortIpAddressList(";"));
113 t
.expectFalse(sortIpAddressList(";;"));
114 t
.expectFalse(sortIpAddressList(" ; ; "));
116 // Invalid IP addresses.
117 t
.expectFalse(sortIpAddressList("256.0.0.1"));
118 t
.expectFalse(sortIpAddressList("192.168.1.1;0:0:0:1;127.0.0.1"));
120 // Call sortIpAddressList() with wonky arguments.
121 t
.expectEquals(null, sortIpAddressList());
122 t
.expectEquals(null, sortIpAddressList(null));
123 t
.expectEquals(null, sortIpAddressList(null, null));
126 Tests
.testIsInNetEx = function(t
) {
127 t
.expectTrue(isInNetEx("198.95.249.79", "198.95.249.79/32"));
128 t
.expectTrue(isInNetEx("198.95.115.10", "198.95.0.0/16"));
129 t
.expectTrue(isInNetEx("198.95.1.1", "198.95.0.0/16"));
130 t
.expectTrue(isInNetEx("198.95.1.1", "198.95.3.3/16"));
131 t
.expectTrue(isInNetEx("0:0:0:0:0:0:7f00:1", "0:0:0:0:0:0:7f00:1/32"));
132 t
.expectTrue(isInNetEx("3ffe:8311:ffff:abcd:1234:dead:beef:101",
133 "3ffe:8311:ffff::/48"));
135 // IPv4 and IPv6 mix.
136 t
.expectFalse(isInNetEx("127.0.0.1", "0:0:0:0:0:0:7f00:1/16"));
137 t
.expectFalse(isInNetEx("192.168.24.3", "fe80:0:0:0:0:0:c0a8:1803/32"));
139 t
.expectFalse(isInNetEx("198.95.249.78", "198.95.249.79/32"));
140 t
.expectFalse(isInNetEx("198.96.115.10", "198.95.0.0/16"));
141 t
.expectFalse(isInNetEx("3fff:8311:ffff:abcd:1234:dead:beef:101",
142 "3ffe:8311:ffff::/48"));
144 // Call isInNetEx with wonky arguments.
145 t
.expectEquals(null, isInNetEx());
146 t
.expectEquals(null, isInNetEx(null));
147 t
.expectEquals(null, isInNetEx(null, null));
148 t
.expectEquals(null, isInNetEx(null, null, null));
149 t
.expectEquals(null, isInNetEx("198.95.249.79"));
151 // Invalid IP address.
152 t
.expectFalse(isInNetEx("256.0.0.1", "198.95.249.79"));
153 t
.expectFalse(isInNetEx("127.0.0.1 ", "127.0.0.1/32")); // Extra space.
156 t
.expectFalse(isInNetEx("198.95.115.10", "198.95.0.0/34"));
157 t
.expectFalse(isInNetEx("127.0.0.1", "127.0.0.1")); // Missing '/' in prefix.
160 Tests
.testWeekdayRange = function(t
) {
161 // Test with local time.
162 MockDate
.setCurrent("Tue Mar 03 2009");
163 t
.expectEquals(true, weekdayRange("MON", "FRI"));
164 t
.expectEquals(true, weekdayRange("TUE", "FRI"));
165 t
.expectEquals(true, weekdayRange("TUE", "TUE"));
166 t
.expectEquals(true, weekdayRange("TUE"));
167 t
.expectEquals(false, weekdayRange("WED", "FRI"));
168 t
.expectEquals(false, weekdayRange("SUN", "MON"));
169 t
.expectEquals(false, weekdayRange("SAT"));
170 t
.expectEquals(false, weekdayRange("FRI", "MON"));
172 // Test with GMT time.
173 MockDate
.setCurrent("Tue Mar 03 2009 GMT");
174 t
.expectEquals(true, weekdayRange("MON", "FRI", "GMT"));
175 t
.expectEquals(true, weekdayRange("TUE", "FRI", "GMT"));
176 t
.expectEquals(true, weekdayRange("TUE", "TUE", "GMT"));
177 t
.expectEquals(true, weekdayRange("TUE", "GMT"));
178 t
.expectEquals(false, weekdayRange("WED", "FRI", "GMT"));
179 t
.expectEquals(false, weekdayRange("SUN", "MON", "GMT"));
180 t
.expectEquals(false, weekdayRange("SAT", "GMT"));
183 Tests
.testDateRange = function(t
) {
185 MockDate
.setCurrent("Mar 03 2009");
186 t
.expectEquals(true, dateRange(3));
187 t
.expectEquals(false, dateRange(1));
189 // dateRange(day, "GMT")
190 MockDate
.setCurrent("Mar 03 2009 GMT");
191 t
.expectEquals(true, dateRange(3, "GMT"));
192 t
.expectEquals(false, dateRange(1, "GMT"));
194 // dateRange(day1, day2)
195 MockDate
.setCurrent("Mar 03 2009");
196 t
.expectEquals(true, dateRange(1, 4));
197 t
.expectEquals(false, dateRange(4, 20));
199 // dateRange(day, month)
200 MockDate
.setCurrent("Mar 03 2009");
201 t
.expectEquals(true, dateRange(3, "MAR"));
202 MockDate
.setCurrent("Mar 03 2014");
203 t
.expectEquals(true, dateRange(3, "MAR"));
205 //t.expectEquals(false, dateRange(2, "MAR"));
206 //t.expectEquals(false, dateRange(3, "JAN"));
208 // dateRange(day, month, year)
209 MockDate
.setCurrent("Mar 03 2009");
210 t
.expectEquals(true, dateRange(3, "MAR", 2009));
211 t
.expectEquals(false, dateRange(4, "MAR", 2009));
212 t
.expectEquals(false, dateRange(3, "FEB", 2009));
213 MockDate
.setCurrent("Mar 03 2014");
214 t
.expectEquals(false, dateRange(3, "MAR", 2009));
216 // dateRange(month1, month2)
217 MockDate
.setCurrent("Mar 03 2009");
218 t
.expectEquals(true, dateRange("JAN", "MAR"));
219 t
.expectEquals(true, dateRange("MAR", "APR"));
220 t
.expectEquals(false, dateRange("MAY", "SEP"));
222 // dateRange(day1, month1, day2, month2)
223 MockDate
.setCurrent("Mar 03 2009");
224 t
.expectEquals(true, dateRange(1, "JAN", 3, "MAR"));
225 t
.expectEquals(true, dateRange(3, "MAR", 4, "SEP"));
226 t
.expectEquals(false, dateRange(4, "MAR", 4, "SEP"));
228 // dateRange(month1, year1, month2, year2)
229 MockDate
.setCurrent("Mar 03 2009");
230 t
.expectEquals(true, dateRange("FEB", 2009, "MAR", 2009));
231 MockDate
.setCurrent("Apr 03 2009");
232 t
.expectEquals(true, dateRange("FEB", 2009, "MAR", 2010));
233 t
.expectEquals(false, dateRange("FEB", 2009, "MAR", 2009));
235 // dateRange(day1, month1, year1, day2, month2, year2)
236 MockDate
.setCurrent("Mar 03 2009");
237 t
.expectEquals(true, dateRange(1, "JAN", 2009, 3, "MAR", 2009));
238 t
.expectEquals(true, dateRange(3, "MAR", 2009, 4, "SEP", 2009));
239 t
.expectEquals(true, dateRange(3, "JAN", 2009, 4, "FEB", 2010));
240 t
.expectEquals(false, dateRange(4, "MAR", 2009, 4, "SEP", 2009));
243 Tests
.testTimeRange = function(t
) {
245 MockDate
.setCurrent("Mar 03, 2009 03:34:01");
246 t
.expectEquals(true, timeRange(3));
247 t
.expectEquals(false, timeRange(2));
249 // timeRange(hour1, hour2)
250 MockDate
.setCurrent("Mar 03, 2009 03:34:01");
251 t
.expectEquals(true, timeRange(2, 3));
252 t
.expectEquals(true, timeRange(2, 4));
253 t
.expectEquals(true, timeRange(3, 5));
254 t
.expectEquals(false, timeRange(1, 2));
255 t
.expectEquals(false, timeRange(11, 12));
257 // timeRange(hour1, min1, hour2, min2)
258 MockDate
.setCurrent("Mar 03, 2009 03:34:01");
259 t
.expectEquals(true, timeRange(1, 0, 3, 34));
260 t
.expectEquals(true, timeRange(1, 0, 3, 35));
261 t
.expectEquals(true, timeRange(3, 34, 5, 0));
262 t
.expectEquals(false, timeRange(1, 0, 3, 0));
263 t
.expectEquals(false, timeRange(11, 0, 16, 0));
265 // timeRange(hour1, min1, sec1, hour2, min2, sec2)
266 MockDate
.setCurrent("Mar 03, 2009 03:34:14");
267 t
.expectEquals(true, timeRange(1, 0, 0, 3, 34, 14));
268 t
.expectEquals(false, timeRange(1, 0, 0, 3, 34, 0));
269 t
.expectEquals(true, timeRange(1, 0, 0, 3, 35, 0));
270 t
.expectEquals(true, timeRange(3, 34, 0, 5, 0, 0));
271 t
.expectEquals(false, timeRange(1, 0, 0, 3, 0, 0));
272 t
.expectEquals(false, timeRange(11, 0, 0, 16, 0, 0));
275 // --------------------------
277 // --------------------------
279 // |name| is the name of the test being executed, it will be used when logging
281 function TestContext(name
) {
282 this.numFailures_
= 0;
286 TestContext
.prototype.failed = function() {
287 return this.numFailures_
!= 0;
290 TestContext
.prototype.expectEquals = function(expectation
, actual
) {
291 if (!(expectation
=== actual
)) {
293 this.log("FAIL: expected: " + expectation
+ ", actual: " + actual
);
297 TestContext
.prototype.expectTrue = function(x
) {
298 this.expectEquals(true, x
);
301 TestContext
.prototype.expectFalse = function(x
) {
302 this.expectEquals(false, x
);
305 TestContext
.prototype.log = function(x
) {
306 // Prefix with the test name that generated the log.
308 alert(this.name_
+ ": " + x
);
310 // In case alert() is not defined.
314 // --------------------------
316 // --------------------------
318 function MockDate() {
319 this.wrappedDate_
= new MockDate
.super_(MockDate
.currentDateString_
);
322 // Setup the MockDate so it forwards methods to "this.wrappedDate_" (which is a
323 // real Date object). We can't simply chain the prototypes since Date() doesn't
325 MockDate
.init = function() {
326 MockDate
.super_
= Date
;
328 function createProxyMethod(methodName
) {
330 return this.wrappedDate_
[methodName
]
331 .apply(this.wrappedDate_
, arguments
);
335 for (i
in MockDate
.methodNames_
) {
336 var methodName
= MockDate
.methodNames_
[i
];
337 // Don't define the closure directly in the loop body, since Javascript's
338 // crazy scoping rules mean |methodName| actually bleeds out of the loop!
339 MockDate
.prototype[methodName
] = createProxyMethod(methodName
);
342 // Replace the native Date() with our mock.
346 // Unfortunately Date()'s methods are non-enumerable, therefore list manually.
347 MockDate
.methodNames_
= [
348 "toString", "toDateString", "toTimeString", "toLocaleString",
349 "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime",
350 "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth",
351 "getDate", "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours",
352 "getMinutes", "getUTCMinutes", "getSeconds", "getUTCSeconds",
353 "getMilliseconds", "getUTCMilliseconds", "getTimezoneOffset", "setTime",
354 "setMilliseconds", "setUTCMilliseconds", "setSeconds", "setUTCSeconds",
355 "setMinutes", "setUTCMinutes", "setHours", "setUTCHours", "setDate",
356 "setUTCDate", "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear",
357 "toGMTString", "toUTCString", "getYear", "setYear"
360 MockDate
.setCurrent = function(currentDateString
) {
361 MockDate
.currentDateString_
= currentDateString
;
364 // Bind the methods to proxy requests to the wrapped Date().