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

Side by Side Diff: chrome/browser/history/top_sites_database_unittest.cc

Issue 560543002: [Top Sites] Encoding redirects field in TopSitesDatabase, and adding validations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map> 5 #include "chrome/browser/history/top_sites_database.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/history/history_types.h" 11 #include "chrome/browser/history/history_types.h"
12 #include "chrome/browser/history/top_sites_database.h"
13 #include "chrome/common/chrome_paths.h" 12 #include "chrome/common/chrome_paths.h"
14 #include "chrome/tools/profiles/thumbnail-inl.h" 13 #include "chrome/tools/profiles/thumbnail-inl.h"
15 #include "sql/connection.h" 14 #include "sql/connection.h"
16 #include "sql/recovery.h" 15 #include "sql/recovery.h"
17 #include "sql/test/scoped_error_ignorer.h" 16 #include "sql/test/scoped_error_ignorer.h"
18 #include "sql/test/test_helpers.h" 17 #include "sql/test/test_helpers.h"
19 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/sqlite/sqlite3.h" 19 #include "third_party/sqlite/sqlite3.h"
21 #include "url/gurl.h" 20 #include "url/gurl.h"
22 21
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 // Remove a forced URL. 468 // Remove a forced URL.
470 db.RemoveURL(url2); 469 db.RemoveURL(url2);
471 470
472 db.GetPageThumbnails(&urls, &thumbnails); 471 db.GetPageThumbnails(&urls, &thumbnails);
473 ASSERT_EQ(4u, urls.size()); 472 ASSERT_EQ(4u, urls.size());
474 ASSERT_EQ(4u, thumbnails.size()); 473 ASSERT_EQ(4u, thumbnails.size());
475 EXPECT_EQ(mapsUrl, urls[0].url); 474 EXPECT_EQ(mapsUrl, urls[0].url);
476 EXPECT_EQ(kUrl0, urls[1].url); 475 EXPECT_EQ(kUrl0, urls[1].url);
477 } 476 }
478 477
478 TEST_F(TopSitesDatabaseTest, EncodeCSVString) {
479 std::vector<std::string> empty_list;
480 EXPECT_EQ("", TopSitesDatabase::EncodeCSVString(empty_list));
481
482 std::vector<std::string> empty_string_list;
483 empty_string_list.push_back("");
484 EXPECT_EQ("\"\"", TopSitesDatabase::EncodeCSVString(empty_string_list));
485 empty_string_list.push_back("");
486 EXPECT_EQ("\"\",\"\"", TopSitesDatabase::EncodeCSVString(empty_string_list));
487
488 // "\n" is treated like any other character.
489 std::vector<std::string> single_list;
490 single_list.push_back("abczABCZ-123 !@#$%^&*(),.'\"\n\r_");
491 EXPECT_EQ("\"abczABCZ-123 !@#$%^&*(),.'\"\"\n\r_\"",
492 TopSitesDatabase::EncodeCSVString(single_list));
493
494 std::vector<std::string> double_list;
495 double_list.push_back(" a \"string\", this is ");
496 double_list.push_back("http://www.test.com:80/path?query#ref");
497 EXPECT_EQ("\" a \"\"string\"\", this is \","
498 "\"http://www.test.com:80/path?query#ref\"",
499 TopSitesDatabase::EncodeCSVString(double_list));
500
501 std::vector<std::string> multi_list;
502 multi_list.push_back("test");
503 multi_list.push_back("");
504 multi_list.push_back(",");
505 multi_list.push_back("http://www.google.com");
506 multi_list.push_back(" ");
507 multi_list.push_back("\r\n");
508 multi_list.push_back("\"quote\"");
509 multi_list.push_back("data:text/plain,this text has space");
510 EXPECT_EQ("\"test\",\"\",\",\",\"http://www.google.com\","
511 "\" \",\"\r\n\",\"\"\"quote\"\"\","
512 "\"data:text/plain,this text has space\"",
513 TopSitesDatabase::EncodeCSVString(multi_list));
514 }
515
516 TEST_F(TopSitesDatabaseTest, DecodeCSVStringSucceed) {
517 std::vector<std::string> res;
518 res.push_back("this gets overwritten");
519
520 EXPECT_TRUE(TopSitesDatabase::DecodeCSVString("", &res));
521 EXPECT_TRUE(res.empty());
522
523 EXPECT_TRUE(TopSitesDatabase::DecodeCSVString("\"\"", &res));
524 EXPECT_EQ(1U, res.size());
525 EXPECT_EQ("", res[0]);
526
527 EXPECT_TRUE(TopSitesDatabase::DecodeCSVString("\"\",\"\"", &res));
528 EXPECT_EQ(2U, res.size());
529 EXPECT_EQ("", res[0]);
530 EXPECT_EQ("", res[1]);
531
532 // "\n" is treated like any other character.
533 EXPECT_TRUE(TopSitesDatabase::DecodeCSVString(
534 "\"abczABCZ-123 !@#$%^&*(),.'\"\"\n\r_\"", &res));
535 EXPECT_EQ(1U, res.size());
536 EXPECT_EQ("abczABCZ-123 !@#$%^&*(),.'\"\n\r_", res[0]);
537
538 EXPECT_TRUE(TopSitesDatabase::DecodeCSVString(
539 "\" a \"\"string\"\", this is \","
540 "\"http://www.test.com:80/path?query#ref\"", &res));
541 EXPECT_EQ(2U, res.size());
542 EXPECT_EQ(" a \"string\", this is ", res[0]);
543 EXPECT_EQ("http://www.test.com:80/path?query#ref", res[1]);
544
545 EXPECT_TRUE(TopSitesDatabase::DecodeCSVString(
546 "\"test\",\"\",\",\",\"http://www.google.com\","
547 "\" \",\"\r\n\",\"\"\"quote\"\"\","
548 "\"data:text/plain,this text has space\"", &res));
549 EXPECT_EQ(8U, res.size());
550 EXPECT_EQ("test", res[0]);
551 EXPECT_EQ("", res[1]);
552 EXPECT_EQ(",", res[2]);
553 EXPECT_EQ("http://www.google.com", res[3]);
554 EXPECT_EQ(" ", res[4]);
555 EXPECT_EQ("\r\n", res[5]);
556 EXPECT_EQ("\"quote\"", res[6]);
557 EXPECT_EQ("data:text/plain,this text has space", res[7]);
558 }
559
560 TEST_F(TopSitesDatabaseTest, DecodeCSVStringFail) {
561 std::vector<std::string> dummy;
562 dummy.push_back("Can't touch this");
563 // Missing quotes.
564 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("test", &dummy));
565 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("test,blah", &dummy));
566 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"test\",blah", &dummy));
567 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"test", &dummy));
568 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("test\"", &dummy));
569 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"\"\"", &dummy));
570 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"\",\"\"\"", &dummy));
571 // Misplaced comma.
572 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString(",\"A\"", &dummy));
573 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"A\",", &dummy));
574 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"A\",,\"B\"", &dummy));
575 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"A\",\"B\",\"C\",", &dummy));
576 // Garbage after quote.
577 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"test\"a\"", &dummy));
578 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"\"test\"", &dummy));
579 // We're strict with trailing white spaces.
580 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"no\", \"space\"", &dummy));
581 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"no\" ,\"space\"", &dummy));
582 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString(" \"bad\"", &dummy));
583 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"bad\" ", &dummy));
584 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString("\"bad\"\n", &dummy));
585 // Other.
586 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString(",", &dummy));
587 EXPECT_FALSE(TopSitesDatabase::DecodeCSVString(",,,,,,", &dummy));
588 // Verify |dummy| is not touched.
589 EXPECT_EQ(1U, dummy.size());
590 EXPECT_EQ("Can't touch this", dummy[0]);
591 }
592
593 TEST_F(TopSitesDatabaseTest, EncodeRedirects) {
594 RedirectList redirects1;
595 EXPECT_EQ("", TopSitesDatabase::EncodeRedirects(redirects1));
596 redirects1.push_back(GURL("http://www.google.com"));
597 EXPECT_EQ("\"http://www.google.com/\"",
598 TopSitesDatabase::EncodeRedirects(redirects1));
599 redirects1.push_back(GURL("https://www.youtube.ca/"));
600 EXPECT_EQ("\"http://www.google.com/\",\"https://www.youtube.ca/\"",
601 TopSitesDatabase::EncodeRedirects(redirects1));
602 redirects1.push_back(GURL("http://www.test.org:137/path?query#ref"));
603 EXPECT_EQ("\"http://www.google.com/\",\"https://www.youtube.ca/\","
604 "\"http://www.test.org:137/path?query#ref\"",
605 TopSitesDatabase::EncodeRedirects(redirects1));
606
607 RedirectList redirects2;
608 redirects2.push_back(GURL("http://www.google.com"));
609 // Adding invalid URL.
610 redirects2.push_back(GURL("this is not valid URL"));
611 // Adding data URL.
612 redirects2.push_back(GURL("data:text/plain,\"this has space\""));
613 // Invalid URL should be ignored.
614 EXPECT_EQ("\"http://www.google.com/\","
615 "\"data:text/plain,\"\"this has space\"\"\"",
616 TopSitesDatabase::EncodeRedirects(redirects2));
617 }
618
619 TEST_F(TopSitesDatabaseTest, DecodeRedirects) {
620 RedirectList redirects1;
621 TopSitesDatabase::DecodeRedirects("", &redirects1);
622 EXPECT_EQ(0u, redirects1.size());
623
624 RedirectList redirects2;
625 TopSitesDatabase::DecodeRedirects(
626 "\"http://www.google.com/\",\"https://www.youtube.ca/\","
627 "\"http://www.test.org:137/path?query#ref\"", &redirects2);
628 EXPECT_EQ(3U, redirects2.size());
629 EXPECT_EQ(GURL("http://www.google.com"), redirects2[0]);
630 EXPECT_EQ(GURL("https://www.youtube.ca/"), redirects2[1]);
631 EXPECT_EQ(GURL("http://www.test.org:137/path?query#ref"), redirects2[2]);
632
633 RedirectList redirects3;
634 TopSitesDatabase::DecodeRedirects(
635 "\"http://www.google.com\","
636 "\"this is not valid URL\",\"\","
637 "\"data:text/plain,\"\"this has space\"\"\"", &redirects3);
638 EXPECT_EQ(2U, redirects3.size());
639 EXPECT_EQ(GURL("http://www.google.com"), redirects3[0]);
640 EXPECT_EQ(GURL("data:text/plain,\"this has space\""), redirects3[1]);
641 }
642
643 // Fallback case when redirects are not stored as CSV strings.
644 TEST_F(TopSitesDatabaseTest, DecodeRedirectsFallback) {
645 RedirectList redirects1;
646 TopSitesDatabase::DecodeRedirects("http://www.chromium.org", &redirects1);
647 EXPECT_EQ(1u, redirects1.size());
648 EXPECT_EQ(GURL("http://www.chromium.org"), redirects1[0]);
649
650 // Valid case, note multiple spaces.
651 RedirectList redirects2;
652 TopSitesDatabase::DecodeRedirects(
653 " http://www.google.com https://www.youtube.ca"
654 " http://www.test.org:80/path?query#ref ",
655 &redirects2);
656 EXPECT_EQ(3u, redirects2.size());
657 EXPECT_EQ(GURL("http://www.google.com"), redirects2[0]);
658 EXPECT_EQ(GURL("https://www.youtube.ca"), redirects2[1]);
659 EXPECT_EQ(GURL("http://www.test.org:80/path?query#ref"), redirects2[2]);
660
661 // This might appear in old database, and should be ignored.
662 RedirectList redirects3;
663 TopSitesDatabase::DecodeRedirects("data:text/plain,this text has space",
664 &redirects3);
665 EXPECT_EQ(1u, redirects3.size());
666 EXPECT_EQ(GURL("data:text/plain,this"), redirects3[0]);
667
668 // Unfortunate case that creates dubious results.
669 RedirectList redirects4;
670 TopSitesDatabase::DecodeRedirects("Data:text/plain,this http://a has space",
671 &redirects4);
672 EXPECT_EQ(2u, redirects4.size());
673 EXPECT_EQ(GURL("Data:text/plain,this"), redirects4[0]);
674 EXPECT_EQ(GURL("http://a"), redirects4[1]);
675 }
676
479 } // namespace history 677 } // namespace history
OLDNEW
« chrome/browser/history/top_sites_database.cc ('K') | « chrome/browser/history/top_sites_database.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698