1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
#include "gtest/gtest.h"
#include "gtest/MozGTestBench.h" // For MOZ_GTEST_BENCH
#include "nsCOMPtr.h"
#include "nsNetCID.h"
#include "nsIURL.h"
#include "nsIStandardURL.h"
#include "nsString.h"
#include "nsPrintfCString.h"
#include "nsComponentManagerUtils.h"
#include "nsIURIMutator.h"
#include "mozilla/ipc/URIUtils.h"
#include "mozilla/Unused.h"
#include "nsSerializationHelper.h"
#include "mozilla/Base64.h"
#include "nsEscape.h"
using namespace mozilla;
// In nsStandardURL.cpp
extern nsresult Test_NormalizeIPv4(const nsACString& host, nsCString& result);
TEST(TestStandardURL, Simple)
{
nsCOMPtr<nsIURI> url;
ASSERT_EQ(NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.SetSpec("http://example.com"_ns)
.Finalize(url),
NS_OK);
ASSERT_TRUE(url);
ASSERT_EQ(NS_MutateURI(url).SetSpec("http://example.com"_ns).Finalize(url),
NS_OK);
nsAutoCString out;
ASSERT_EQ(url->GetSpec(out), NS_OK);
ASSERT_TRUE(out == "http://example.com/"_ns);
ASSERT_EQ(url->Resolve("foo.html?q=45"_ns, out), NS_OK);
ASSERT_TRUE(out == "http://example.com/foo.html?q=45"_ns);
ASSERT_EQ(NS_MutateURI(url).SetScheme("foo"_ns).Finalize(url), NS_OK);
ASSERT_EQ(url->GetScheme(out), NS_OK);
ASSERT_TRUE(out == "foo"_ns);
ASSERT_EQ(url->GetHost(out), NS_OK);
ASSERT_TRUE(out == "example.com"_ns);
ASSERT_EQ(NS_MutateURI(url).SetHost("www.yahoo.com"_ns).Finalize(url), NS_OK);
ASSERT_EQ(url->GetHost(out), NS_OK);
ASSERT_TRUE(out == "www.yahoo.com"_ns);
ASSERT_EQ(NS_MutateURI(url)
.SetPathQueryRef(nsLiteralCString(
"/some-path/one-the-net/about.html?with-a-query#for-you"))
.Finalize(url),
NS_OK);
ASSERT_EQ(url->GetPathQueryRef(out), NS_OK);
ASSERT_TRUE(out ==
nsLiteralCString(
"/some-path/one-the-net/about.html?with-a-query#for-you"));
ASSERT_EQ(NS_MutateURI(url)
.SetQuery(nsLiteralCString(
"a=b&d=c&what-ever-you-want-to-be-called=45"))
.Finalize(url),
NS_OK);
ASSERT_EQ(url->GetQuery(out), NS_OK);
ASSERT_TRUE(out == "a=b&d=c&what-ever-you-want-to-be-called=45"_ns);
ASSERT_EQ(NS_MutateURI(url).SetRef("#some-book-mark"_ns).Finalize(url),
NS_OK);
ASSERT_EQ(url->GetRef(out), NS_OK);
ASSERT_TRUE(out == "some-book-mark"_ns);
}
TEST(TestStandardURL, NormalizeGood)
{
nsCString result;
const char* manual[] = {"0.0.0.0",
"0.0.0.0",
"0",
"0.0.0.0",
"000",
"0.0.0.0",
"0x00",
"0.0.0.0",
"10.20.100.200",
"10.20.100.200",
"255.255.255.255",
"255.255.255.255",
"0XFF.0xFF.0xff.0xFf",
"255.255.255.255",
"0x000ff.0X00FF.0x0ff.0xff",
"255.255.255.255",
"0x000fA.0X00FB.0x0fC.0xfD",
"250.251.252.253",
"0x000fE.0X00FF.0x0fC.0xfD",
"254.255.252.253",
"0x000fa.0x00fb.0x0fc.0xfd",
"250.251.252.253",
"0x000fe.0x00ff.0x0fc.0xfd",
"254.255.252.253",
"0377.0377.0377.0377",
"255.255.255.255",
"0000377.000377.00377.0377",
"255.255.255.255",
"65535",
"0.0.255.255",
"0xfFFf",
"0.0.255.255",
"0x00000ffff",
"0.0.255.255",
"0177777",
"0.0.255.255",
"000177777",
"0.0.255.255",
"0.13.65535",
"0.13.255.255",
"0.22.0xffff",
"0.22.255.255",
"0.123.0177777",
"0.123.255.255",
"65536",
"0.1.0.0",
"0200000",
"0.1.0.0",
"0x10000",
"0.1.0.0"};
for (uint32_t i = 0; i < sizeof(manual) / sizeof(manual[0]); i += 2) {
nsCString encHost(manual[i + 0]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
ASSERT_TRUE(result.Equals(manual[i + 1]));
}
// Make sure we're getting the numbers correctly interpreted:
for (int i = 0; i < 256; i++) {
nsCString encHost = nsPrintfCString("0x%x", i);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
ASSERT_TRUE(result.Equals(nsPrintfCString("0.0.0.%d", i)));
encHost = nsPrintfCString("0%o", i);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
ASSERT_TRUE(result.Equals(nsPrintfCString("0.0.0.%d", i)));
}
// Some random numbers in the range, mixing hex, decimal, octal
for (int i = 0; i < 8; i++) {
int val[4] = {i * 11 + 13, i * 18 + 22, i * 4 + 28, i * 15 + 2};
nsCString encHost =
nsPrintfCString("%d.%d.%d.%d", val[0], val[1], val[2], val[3]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
ASSERT_TRUE(result.Equals(encHost));
nsCString encHostM =
nsPrintfCString("0x%x.0x%x.0x%x.0x%x", val[0], val[1], val[2], val[3]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHostM, result));
ASSERT_TRUE(result.Equals(encHost));
encHostM =
nsPrintfCString("0%o.0%o.0%o.0%o", val[0], val[1], val[2], val[3]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHostM, result));
ASSERT_TRUE(result.Equals(encHost));
encHostM =
nsPrintfCString("0x%x.%d.0%o.%d", val[0], val[1], val[2], val[3]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHostM, result));
ASSERT_TRUE(result.Equals(encHost));
encHostM =
nsPrintfCString("%d.0%o.0%o.0x%x", val[0], val[1], val[2], val[3]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHostM, result));
ASSERT_TRUE(result.Equals(encHost));
encHostM =
nsPrintfCString("0%o.0%o.0x%x.0x%x", val[0], val[1], val[2], val[3]);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHostM, result));
ASSERT_TRUE(result.Equals(encHost));
}
}
TEST(TestStandardURL, NormalizeBad)
{
nsAutoCString result;
const char* manual[] = {
"x22.232.12.32", "122..12.32", "122.12.32.12.32", "122.12.32..",
"122.12.xx.22", "122.12.0xx.22", "0xx.12.01.22", "0x.12.01.22",
"12.12.02x.22", "1q.12.2.22", "122.01f.02.22", "12a.01.02.22",
"12.01.02.20x1", "10x2.01.02.20", "0xx.01.02.20", "10.x.02.20",
"10.00x2.02.20", "10.13.02x2.20", "10.x13.02.20", "10.0x134def.02.20",
"\0.2.2.2", "256.2.2.2", "2.256.2.2", "2.2.256.2",
"2.2.2.256", "2.2.-2.3", "+2.2.2.3", "13.0x2x2.2.3",
"0x2x2.13.2.3"};
for (auto& i : manual) {
nsCString encHost(i);
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost, result));
}
}
TEST(TestStandardURL, From_test_standardurldotjs)
{
// These are test (success and failure) cases from test_standardurl.js
nsAutoCString result;
const char* localIPv4s[] = {
"127.0.0.1",
"127.0.1",
"127.1",
"2130706433",
"0177.00.00.01",
"0177.00.01",
"0177.01",
"00000000000000000000000000177.0000000.0000000.0001",
"000000177.0000001",
"017700000001",
"0x7f.0x00.0x00.0x01",
"0x7f.0x01",
"0x7f000001",
"0x007f.0x0000.0x0000.0x0001",
"000177.0.00000.0x0001",
"127.0.0.1.",
"0X7F.0X00.0X00.0X01",
"0X7F.0X01",
"0X7F000001",
"0X007F.0X0000.0X0000.0X0001",
"000177.0.00000.0X0001"};
for (auto& localIPv4 : localIPv4s) {
nsCString encHost(localIPv4);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
ASSERT_TRUE(result.EqualsLiteral("127.0.0.1"));
}
const char* nonIPv4s[] = {"0xfffffffff", "0x100000000",
"4294967296", "1.2.0x10000",
"1.0x1000000", "256.0.0.1",
"1.256.1", "-1.0.0.0",
"1.2.3.4.5", "010000000000000000",
"2+3", "0.0.0.-1",
"1.2.3.4..", "1..2",
".1.2.3.4", ".127"};
for (auto& nonIPv4 : nonIPv4s) {
nsCString encHost(nonIPv4);
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost, result));
}
const char* oneOrNoDotsIPv4s[] = {"127", "127."};
for (auto& localIPv4 : oneOrNoDotsIPv4s) {
nsCString encHost(localIPv4);
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
ASSERT_TRUE(result.EqualsLiteral("0.0.0.127"));
}
}
#define TEST_COUNT 10000
MOZ_GTEST_BENCH(TestStandardURL, DISABLED_Perf, [] {
nsCOMPtr<nsIURI> url;
ASSERT_EQ(NS_OK, NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.SetSpec("http://example.com"_ns)
.Finalize(url));
nsAutoCString out;
for (int i = TEST_COUNT; i; --i) {
ASSERT_EQ(NS_MutateURI(url).SetSpec("http://example.com"_ns).Finalize(url),
NS_OK);
ASSERT_EQ(url->GetSpec(out), NS_OK);
url->Resolve("foo.html?q=45"_ns, out);
mozilla::Unused << NS_MutateURI(url).SetScheme("foo"_ns).Finalize(url);
url->GetScheme(out);
mozilla::Unused
<< NS_MutateURI(url).SetHost("www.yahoo.com"_ns).Finalize(url);
url->GetHost(out);
mozilla::Unused
<< NS_MutateURI(url)
.SetPathQueryRef(nsLiteralCString(
"/some-path/one-the-net/about.html?with-a-query#for-you"))
.Finalize(url);
url->GetPathQueryRef(out);
mozilla::Unused << NS_MutateURI(url)
.SetQuery(nsLiteralCString(
"a=b&d=c&what-ever-you-want-to-be-called=45"))
.Finalize(url);
url->GetQuery(out);
mozilla::Unused
<< NS_MutateURI(url).SetRef("#some-book-mark"_ns).Finalize(url);
url->GetRef(out);
}
});
// Note the five calls in the loop, so divide by 100k
MOZ_GTEST_BENCH(TestStandardURL, DISABLED_NormalizePerf, [] {
nsAutoCString result;
for (int i = 0; i < 20000; i++) {
nsAutoCString encHost("123.232.12.32");
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost, result));
nsAutoCString encHost2("83.62.12.92");
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost2, result));
nsAutoCString encHost3("8.7.6.5");
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost3, result));
nsAutoCString encHost4("111.159.123.220");
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost4, result));
nsAutoCString encHost5("1.160.204.200");
ASSERT_EQ(NS_OK, Test_NormalizeIPv4(encHost5, result));
}
});
// Bug 1394785 - ignore unstable test on OSX
#ifndef XP_MACOSX
// Note the five calls in the loop, so divide by 100k
MOZ_GTEST_BENCH(TestStandardURL, DISABLED_NormalizePerfFails, [] {
nsAutoCString result;
for (int i = 0; i < 20000; i++) {
nsAutoCString encHost("123.292.12.32");
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost, result));
nsAutoCString encHost2("83.62.12.0x13292");
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost2, result));
nsAutoCString encHost3("8.7.6.0xhello");
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost3, result));
nsAutoCString encHost4("111.159.notonmywatch.220");
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost4, result));
nsAutoCString encHost5("1.160.204.20f");
ASSERT_EQ(NS_ERROR_FAILURE, Test_NormalizeIPv4(encHost5, result));
}
});
#endif
TEST(TestStandardURL, Mutator)
{
nsAutoCString out;
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.SetSpec("http://example.com"_ns)
.Finalize(uri);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(uri->GetSpec(out), NS_OK);
ASSERT_TRUE(out == "http://example.com/"_ns);
rv = NS_MutateURI(uri)
.SetScheme("ftp"_ns)
.SetHost("mozilla.org"_ns)
.SetPathQueryRef("/path?query#ref"_ns)
.Finalize(uri);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(uri->GetSpec(out), NS_OK);
ASSERT_TRUE(out == "ftp://mozilla.org/path?query#ref"_ns);
nsCOMPtr<nsIURL> url;
rv = NS_MutateURI(uri).SetScheme("https"_ns).Finalize(url);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(url->GetSpec(out), NS_OK);
ASSERT_TRUE(out == "https://mozilla.org/path?query#ref"_ns);
}
TEST(TestStandardURL, Deserialize_Bug1392739)
{
mozilla::ipc::StandardURLParams standard_params;
standard_params.urlType() = nsIStandardURL::URLTYPE_STANDARD;
standard_params.spec().Truncate();
standard_params.host() = mozilla::ipc::StandardURLSegment(4294967295, 1);
mozilla::ipc::URIParams params(standard_params);
nsCOMPtr<nsIURIMutator> mutator =
do_CreateInstance(NS_STANDARDURLMUTATOR_CID);
ASSERT_EQ(mutator->Deserialize(params), NS_ERROR_FAILURE);
}
TEST(TestStandardURL, CorruptSerialization)
{
auto spec = "http://user:pass@example.com/path/to/file.ext?query#hash"_ns;
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
.SetSpec(spec)
.Finalize(uri);
ASSERT_EQ(rv, NS_OK);
nsAutoCString serialization;
nsCOMPtr<nsISerializable> serializable = do_QueryInterface(uri);
ASSERT_TRUE(serializable);
// Check that the URL is normally serializable.
ASSERT_EQ(NS_OK, NS_SerializeToString(serializable, serialization));
nsCOMPtr<nsISupports> deserializedObject;
ASSERT_EQ(NS_OK, NS_DeserializeObject(serialization,
getter_AddRefs(deserializedObject)));
nsAutoCString canonicalBin;
Unused << Base64Decode(serialization, canonicalBin);
// The spec serialization begins at byte 49
// If the implementation of nsStandardURL::Write changes, this test will need
// to be adjusted.
#define SPEC_OFFSET 49
ASSERT_EQ(Substring(canonicalBin, SPEC_OFFSET, 7), "http://"_ns);
nsAutoCString corruptedBin = canonicalBin;
// change mScheme.mPos
corruptedBin.BeginWriting()[SPEC_OFFSET + spec.Length()] = 1;
Unused << Base64Encode(corruptedBin, serialization);
ASSERT_EQ(
NS_ERROR_MALFORMED_URI,
NS_DeserializeObject(serialization, getter_AddRefs(deserializedObject)));
corruptedBin = canonicalBin;
// change mScheme.mLen
corruptedBin.BeginWriting()[SPEC_OFFSET + spec.Length() + 4] = 127;
Unused << Base64Encode(corruptedBin, serialization);
ASSERT_EQ(
NS_ERROR_MALFORMED_URI,
NS_DeserializeObject(serialization, getter_AddRefs(deserializedObject)));
}
|