Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: src/url_canon_unittest.cc

Issue 99183: Canonicalize IPv6 addresses. (Closed) Base URL: http://google-url.googlecode.com/svn/trunk/
Patch Set: address pmarks comments Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/url_canon_ip.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007, Google Inc. 1 // Copyright 2007, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 {":]", L":]", "", url_parse::Component(), false}, 525 {":]", L":]", "", url_parse::Component(), false},
526 {"[]", L"[]", "", url_parse::Component(), false}, 526 {"[]", L"[]", "", url_parse::Component(), false},
527 {"[:]", L"[:]", "", url_parse::Component(), false}, 527 {"[:]", L"[:]", "", url_parse::Component(), false},
528 // Regular IP address is invalid without bounding '[' and ']'. 528 // Regular IP address is invalid without bounding '[' and ']'.
529 {"2001:db8::1", L"2001:db8::1", "", url_parse::Component(), false}, 529 {"2001:db8::1", L"2001:db8::1", "", url_parse::Component(), false},
530 {"[2001:db8::1", L"[2001:db8::1", "", url_parse::Component(), false}, 530 {"[2001:db8::1", L"[2001:db8::1", "", url_parse::Component(), false},
531 {"2001:db8::1]", L"2001:db8::1]", "", url_parse::Component(), false}, 531 {"2001:db8::1]", L"2001:db8::1]", "", url_parse::Component(), false},
532 // Regular IP addresses. 532 // Regular IP addresses.
533 {"[::]", L"[::]", "[::]", url_parse::Component(0,4), true}, 533 {"[::]", L"[::]", "[::]", url_parse::Component(0,4), true},
534 {"[::1]", L"[::1]", "[::1]", url_parse::Component(0,5), true}, 534 {"[::1]", L"[::1]", "[::1]", url_parse::Component(0,5), true},
535 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::192.168.0.1]", url_parse::Compon ent(0,15), true}, 535 {"[1::]", L"[1::]", "[1::]", url_parse::Component(0,5), true},
536 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:192.168.0.1]", ur l_parse::Component(0,20), true}, 536 {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", url_parse::Component(0 ,10), true},
537 // Only mapped and compat addresses can have IPv4 syntax embedded. 537 {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", url_par se::Component(0,15), true},
538 // BROKEN {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "", url_parse::C omponent(), false}, 538
539 // BROKEN {"[2001::192.168.0.1]", L"[2001::92.168.0.1]", "", url_parse::Comp onent(), false}, 539 // Leading zeros should be stripped.
540 {"[000:01:02:003:004:5:6:007]", L"[000:01:02:003:004:5:6:007]", "[0:1:2:3:4: 5:6:7]", url_parse::Component(0,17), true},
541
542 // Upper case letters should be lowercased.
543 {"[A:b:c:DE:fF:0:1:aC]", L"[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]", ur l_parse::Component(0,20), true},
544
545 // The same address can be written with different contractions, but should
546 // get canonicalized to the same thing.
547 {"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", url_parse::Component (0,14), true},
548 {"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", url_parse::Component (0,14), true},
549
550 // IPv4 addresses
551 // Only mapped and compat addresses can have IPv4 syntax embedded.
552 {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "", url_parse::Component() , false},
553 {"[2001::192.168.0.1]", L"[2001::92.168.0.1]", "", url_parse::Component(), f alse},
554 {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", url_parse::Component (), false},
555
556 // IPv4 with truncation and last component missing.
557 {"[::ffff:276.1.2]", L"[::ffff:276.1.2]", "[::ffff:1401:2]", url_parse::Comp onent(0,15), true},
558
559 // IPv4 using hex.
560 // TODO(eroman): Should this format be disallowed?
561 {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8: 1]", url_parse::Component(0,15), true},
562
563 // There may be zeros surrounding the "::" contraction.
564 {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", url_parse::Component(0,5), true},
565
566 {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", url_parse::Component(0, 13), true},
567
540 // Can only have one "::" contraction in an IPv6 string literal. 568 // Can only have one "::" contraction in an IPv6 string literal.
541 // BROKEN {"[2001::db8::1]", L"[2001::db8::1]", "", url_parse::Component(), false}, 569 {"[2001::db8::1]", L"[2001::db8::1]", "", url_parse::Component(), false},
542 // No more than 2 consecutive ':'s. 570 // No more than 2 consecutive ':'s.
543 // BROKEN {"[2001:db8:::1]", L"[2001:db8:::1]", "", url_parse::Component(), false}, 571 {"[2001:db8:::1]", L"[2001:db8:::1]", "", url_parse::Component(), false},
572 {"[:::]", L"[:::]", "", url_parse::Component(), false},
544 // Non-IP addresses due to invalid characters. 573 // Non-IP addresses due to invalid characters.
545 {"[2001::.com]", L"[2001::.com]", "", url_parse::Component(), false}, 574 {"[2001::.com]", L"[2001::.com]", "", url_parse::Component(), false},
546 // If there are not enough components, the last one should fill them out. 575 // If there are not enough components, the last one should fill them out.
547 // ... omitted at this time ... 576 // ... omitted at this time ...
548 // Too many components means not an IP address. Similarly with too few if using IPv4 compat or mapped addresses. 577 // Too many components means not an IP address. Similarly with too few if using IPv4 compat or mapped addresses.
549 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", url_parse::Component(), fals e}, 578 {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", url_parse::Component(), fals e},
550 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", url_parse::Compone nt(), false}, 579 {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", url_parse::Compone nt(), false},
551 {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", url_parse::Component(), false}, 580 {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", url_parse::Component(), false},
581 // Too many bits (even though 8 comonents, the last one holds 32 bits).
582 {"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", url_pars e::Component(), false},
583
584 // Too many bits specified -- the contraction would have to be zero-length
585 // to not exceed 128 bits.
586 {"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", url_parse: :Component(), false},
587
588 // The contraction is for 16 bits of zero.
589 {"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", url_parse::Co mponent(0,17), true},
590
591 // Cannot have a trailing colon.
592 {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", url_parse::Component(), fa lse},
593 {"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", url_parse: :Component(), false},
594
595 // Cannot have negative numbers.
596 {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", url_parse::Component(), fa lse},
597
598 // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
599 // The scope_id should be included in the canonicalized URL, and is an
600 // unsigned decimal number.
601
602 // Invalid because no ID was given after the percent.
603
604 // Don't allow scope-id
605 {"[1::%1]", L"[1::%1]", "", url_parse::Component(), false},
606 {"[1::%eth0]", L"[1::%eth0]", "", url_parse::Component(), false},
607 {"[1::%]", L"[1::%]", "", url_parse::Component(), false},
608 {"[%]", L"[%]", "", url_parse::Component(), false},
609 {"[::%:]", L"[::%:]", "", url_parse::Component(), false},
610
611 // Don't allow leading or trailing colons.
612 {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", url_parse::Component(), false},
613 {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", url_parse::Component(), false},
614 {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", url_parse::Component(), false},
615
552 // We allow a single trailing dot. 616 // We allow a single trailing dot.
553 // ... omitted at this time ... 617 // ... omitted at this time ...
554 // Two dots in a row means not an IP address. 618 // Two dots in a row means not an IP address.
555 {"[::192.168..1]", L"[::192.168..1]", "", url_parse::Component(), false}, 619 {"[::192.168..1]", L"[::192.168..1]", "", url_parse::Component(), false},
556 // Any non-first components get truncated to one byte. 620 // Any non-first components get truncated to one byte.
557 // ... omitted at this time ... 621 // ... omitted at this time ...
558 // Spaces should be rejected. 622 // Spaces should be rejected.
559 {"[::1 hello]", L"[::1 hello]", "", url_parse::Component(), false}, 623 {"[::1 hello]", L"[::1 hello]", "", url_parse::Component(), false},
560 }; 624 };
561 625
562 for (size_t i = 0; i < arraysize(cases); i++) { 626 for (size_t i = 0; i < arraysize(cases); i++) {
627 // Print some context of what test we were on, to help debug failures.
628 SCOPED_TRACE(cases[i].input8);
629
563 // 8-bit version. 630 // 8-bit version.
564 url_parse::Component component(0, 631 url_parse::Component component(0,
565 static_cast<int>(strlen(cases[i].input8))); 632 static_cast<int>(strlen(cases[i].input8)));
566 633
567 std::string out_str1; 634 std::string out_str1;
568 url_canon::StdStringCanonOutput output1(&out_str1); 635 url_canon::StdStringCanonOutput output1(&out_str1);
569 url_parse::Component out_ip; 636 url_parse::Component out_ip;
570 bool success = url_canon::CanonicalizeIPAddress(cases[i].input8, component, 637 bool success = url_canon::CanonicalizeIPAddress(cases[i].input8, component,
571 &output1, &out_ip); 638 &output1, &out_ip);
572 output1.Complete(); 639 output1.Complete();
(...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after
1472 memset(buf, 0xff, sizeof(buf)); 1539 memset(buf, 0xff, sizeof(buf));
1473 EXPECT_EQ(0, url_canon::_itoa_s(12, buf, 10)); 1540 EXPECT_EQ(0, url_canon::_itoa_s(12, buf, 10));
1474 EXPECT_STREQ("12", buf); 1541 EXPECT_STREQ("12", buf);
1475 EXPECT_EQ('\xFF', buf[3]); 1542 EXPECT_EQ('\xFF', buf[3]);
1476 1543
1477 memset(buf, 0xff, sizeof(buf)); 1544 memset(buf, 0xff, sizeof(buf));
1478 EXPECT_EQ(0, url_canon::_itoa_s(12345, buf, 10)); 1545 EXPECT_EQ(0, url_canon::_itoa_s(12345, buf, 10));
1479 EXPECT_STREQ("12345", buf); 1546 EXPECT_STREQ("12345", buf);
1480 1547
1481 EXPECT_EQ(EINVAL, url_canon::_itoa_s(123456, buf, 10)); 1548 EXPECT_EQ(EINVAL, url_canon::_itoa_s(123456, buf, 10));
1549
1550 // Test that radix 16 is supported.
1551 memset(buf, 0xff, sizeof(buf));
1552 EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 16));
1553 EXPECT_STREQ("4d2", buf);
1554 EXPECT_EQ('\xFF', buf[5]);
1482 } 1555 }
1483 1556
1484 TEST(URLCanonTest, _itow_s) { 1557 TEST(URLCanonTest, _itow_s) {
1485 // We fill the buffer with 0xff to ensure that it's getting properly 1558 // We fill the buffer with 0xff to ensure that it's getting properly
1486 // null-terminated. We also allocate one byte more than what we tell 1559 // null-terminated. We also allocate one byte more than what we tell
1487 // _itoa_s about, and ensure that the extra byte is untouched. 1560 // _itoa_s about, and ensure that the extra byte is untouched.
1488 char16 buf[6]; 1561 char16 buf[6];
1489 const char fill_mem = 0xff; 1562 const char fill_mem = 0xff;
1490 const char16 fill_char = 0xffff; 1563 const char16 fill_char = 0xffff;
1491 memset(buf, fill_mem, sizeof(buf)); 1564 memset(buf, fill_mem, sizeof(buf));
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 url_canon::StdStringCanonOutput repl_output(&repl_str); 1808 url_canon::StdStringCanonOutput repl_output(&repl_str);
1736 url_canon::ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed) ; 1809 url_canon::ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed) ;
1737 repl_output.Complete(); 1810 repl_output.Complete();
1738 1811
1739 // Generate the expected string and check. 1812 // Generate the expected string and check.
1740 std::string expected("file:///foo?"); 1813 std::string expected("file:///foo?");
1741 for (size_t i = 0; i < new_query.length(); i++) 1814 for (size_t i = 0; i < new_query.length(); i++)
1742 expected.push_back('a'); 1815 expected.push_back('a');
1743 EXPECT_TRUE(expected == repl_str); 1816 EXPECT_TRUE(expected == repl_str);
1744 } 1817 }
OLDNEW
« no previous file with comments | « src/url_canon_ip.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698