Home | History | Annotate | Download | only in importer
      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
      3 // found in the LICENSE file.
      4 
      5 #include "testing/gtest/include/gtest/gtest.h"
      6 
      7 #include "base/file_path.h"
      8 #include "base/file_util.h"
      9 #include "base/path_service.h"
     10 #include "base/string_util.h"
     11 #include "base/utf_string_conversions.h"
     12 #include "chrome/browser/importer/firefox2_importer.h"
     13 #include "chrome/browser/importer/firefox_importer_unittest_utils.h"
     14 #include "chrome/browser/importer/firefox_importer_utils.h"
     15 #include "chrome/browser/importer/nss_decryptor.h"
     16 #include "chrome/common/chrome_paths.h"
     17 
     18 // The following 2 tests require the use of the NSSDecryptor, on OSX this needs
     19 // to run in a separate process, so we use a proxy object so we can share the
     20 // same test between platforms.
     21 TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) {
     22   FilePath nss_path;
     23   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path));
     24 #ifdef OS_MACOSX
     25   nss_path = nss_path.AppendASCII("firefox2_nss_mac");
     26 #else
     27   nss_path = nss_path.AppendASCII("firefox2_nss");
     28 #endif  // !OS_MACOSX
     29   FilePath db_path;
     30   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path));
     31   db_path = db_path.AppendASCII("firefox2_profile");
     32 
     33   FFUnitTestDecryptorProxy decryptor_proxy;
     34   ASSERT_TRUE(decryptor_proxy.Setup(nss_path));
     35 
     36   EXPECT_TRUE(decryptor_proxy.DecryptorInit(nss_path, db_path));
     37   EXPECT_EQ(ASCIIToUTF16("hello"),
     38       decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECBJ"
     39                               "M63MpT9rtBAjMCm7qo/EhlA=="));
     40   // Test UTF-16 encoding.
     41   EXPECT_EQ(WideToUTF16(L"\x4E2D"),
     42       decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECN9"
     43                               "OQ5ZFmhb8BAiFo1Z+fUvaIQ=="));
     44 }
     45 
     46 TEST(FirefoxImporterTest, Firefox3NSS3Decryptor) {
     47   FilePath nss_path;
     48   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path));
     49 #ifdef OS_MACOSX
     50   nss_path = nss_path.AppendASCII("firefox3_nss_mac");
     51 #else
     52   nss_path = nss_path.AppendASCII("firefox3_nss");
     53 #endif  // !OS_MACOSX
     54   FilePath db_path;
     55   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path));
     56   db_path = db_path.AppendASCII("firefox3_profile");
     57 
     58   FFUnitTestDecryptorProxy decryptor_proxy;
     59   ASSERT_TRUE(decryptor_proxy.Setup(nss_path));
     60 
     61   EXPECT_TRUE(decryptor_proxy.DecryptorInit(nss_path, db_path));
     62   EXPECT_EQ(ASCIIToUTF16("hello"),
     63       decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECKa"
     64                               "jtRg4qFSHBAhv9luFkXgDJA=="));
     65   // Test UTF-16 encoding.
     66   EXPECT_EQ(WideToUTF16(L"\x4E2D"),
     67       decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECLW"
     68                               "qqiccfQHWBAie74hxnULxlw=="));
     69 }
     70 
     71 TEST(FirefoxImporterTest, Firefox2BookmarkParse) {
     72   bool result;
     73 
     74   // Tests charset.
     75   std::string charset;
     76   result = Firefox2Importer::ParseCharsetFromLine(
     77       "<META HTTP-EQUIV=\"Content-Type\" "
     78       "CONTENT=\"text/html; charset=UTF-8\">",
     79       &charset);
     80   EXPECT_TRUE(result);
     81   EXPECT_EQ("UTF-8", charset);
     82 
     83   // Escaped characters in name.
     84   string16 folder_name;
     85   bool is_toolbar_folder;
     86   base::Time folder_add_date;
     87   result = Firefox2Importer::ParseFolderNameFromLine(
     88       "<DT><H3 ADD_DATE=\"1207558707\" >&lt; &gt;"
     89       " &amp; &quot; &#39; \\ /</H3>",
     90       charset, &folder_name, &is_toolbar_folder, &folder_add_date);
     91   EXPECT_TRUE(result);
     92   EXPECT_EQ(ASCIIToUTF16("< > & \" ' \\ /"), folder_name);
     93   EXPECT_FALSE(is_toolbar_folder);
     94   EXPECT_TRUE(base::Time::FromTimeT(1207558707) == folder_add_date);
     95 
     96   // Empty name and toolbar folder attribute.
     97   result = Firefox2Importer::ParseFolderNameFromLine(
     98       "<DT><H3 PERSONAL_TOOLBAR_FOLDER=\"true\"></H3>",
     99       charset, &folder_name, &is_toolbar_folder, &folder_add_date);
    100   EXPECT_TRUE(result);
    101   EXPECT_EQ(ASCIIToUTF16(""), folder_name);
    102   EXPECT_TRUE(is_toolbar_folder);
    103 
    104   // Unicode characters in title and shortcut.
    105   string16 title;
    106   GURL url, favicon;
    107   string16 shortcut;
    108   string16 post_data;
    109   base::Time add_date;
    110   result = Firefox2Importer::ParseBookmarkFromLine(
    111       "<DT><A HREF=\"http://chinese.site.cn/path?query=1#ref\" "
    112       "SHORTCUTURL=\"\xE4\xB8\xAD\">\xE4\xB8\xAD\xE6\x96\x87</A>",
    113       charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
    114   EXPECT_TRUE(result);
    115   EXPECT_EQ(L"\x4E2D\x6587", UTF16ToWide(title));
    116   EXPECT_EQ("http://chinese.site.cn/path?query=1#ref", url.spec());
    117   EXPECT_EQ(L"\x4E2D", UTF16ToWide(shortcut));
    118   EXPECT_EQ(ASCIIToUTF16(""), post_data);
    119   EXPECT_TRUE(base::Time() == add_date);
    120 
    121   // No shortcut, and url contains %22 ('"' character).
    122   result = Firefox2Importer::ParseBookmarkFromLine(
    123       "<DT><A HREF=\"http://domain.com/?q=%22<>%22\">name</A>",
    124       charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
    125   EXPECT_TRUE(result);
    126   EXPECT_EQ(ASCIIToUTF16("name"), title);
    127   EXPECT_EQ("http://domain.com/?q=%22%3C%3E%22", url.spec());
    128   EXPECT_EQ(ASCIIToUTF16(""), shortcut);
    129   EXPECT_EQ(ASCIIToUTF16(""), post_data);
    130   EXPECT_TRUE(base::Time() == add_date);
    131 
    132   result = Firefox2Importer::ParseBookmarkFromLine(
    133       "<DT><A HREF=\"http://domain.com/?g=";\"\">name</A>",
    134       charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
    135   EXPECT_TRUE(result);
    136   EXPECT_EQ(ASCIIToUTF16("name"), title);
    137   EXPECT_EQ("http://domain.com/?g=%22", url.spec());
    138   EXPECT_EQ(ASCIIToUTF16(""), shortcut);
    139   EXPECT_EQ(ASCIIToUTF16(""), post_data);
    140   EXPECT_TRUE(base::Time() == add_date);
    141 
    142   // Creation date.
    143   result = Firefox2Importer::ParseBookmarkFromLine(
    144       "<DT><A HREF=\"http://site/\" ADD_DATE=\"1121301154\">name</A>",
    145       charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
    146   EXPECT_TRUE(result);
    147   EXPECT_EQ(ASCIIToUTF16("name"), title);
    148   EXPECT_EQ(GURL("http://site/"), url);
    149   EXPECT_EQ(ASCIIToUTF16(""), shortcut);
    150   EXPECT_EQ(ASCIIToUTF16(""), post_data);
    151   EXPECT_TRUE(base::Time::FromTimeT(1121301154) == add_date);
    152 
    153   // Post-data
    154   result = Firefox2Importer::ParseBookmarkFromLine(
    155       "<DT><A HREF=\"http://localhost:8080/test/hello.html\" ADD_DATE=\""
    156       "1212447159\" LAST_VISIT=\"1212447251\" LAST_MODIFIED=\"1212447248\""
    157       "SHORTCUTURL=\"post\" ICON=\"data:\" POST_DATA=\"lname%3D%25s\""
    158       "LAST_CHARSET=\"UTF-8\" ID=\"rdf:#$weKaR3\">Test Post keyword</A>",
    159       charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
    160   EXPECT_TRUE(result);
    161   EXPECT_EQ(ASCIIToUTF16("Test Post keyword"), title);
    162   EXPECT_EQ("http://localhost:8080/test/hello.html", url.spec());
    163   EXPECT_EQ(ASCIIToUTF16("post"), shortcut);
    164   EXPECT_EQ(ASCIIToUTF16("lname%3D%25s"), post_data);
    165   EXPECT_TRUE(base::Time::FromTimeT(1212447159) == add_date);
    166 
    167   // Invalid case.
    168   result = Firefox2Importer::ParseBookmarkFromLine(
    169       "<DT><A HREF=\"http://domain.com/?q=%22",
    170       charset, &title, &url, &favicon, &shortcut, &add_date, &post_data);
    171   EXPECT_FALSE(result);
    172   EXPECT_EQ(ASCIIToUTF16(""), title);
    173   EXPECT_EQ("", url.spec());
    174   EXPECT_EQ(ASCIIToUTF16(""), shortcut);
    175   EXPECT_EQ(ASCIIToUTF16(""), post_data);
    176   EXPECT_TRUE(base::Time() == add_date);
    177 
    178   // Epiphany format.
    179   result = Firefox2Importer::ParseMinimumBookmarkFromLine(
    180       "<dt><a href=\"http://www.google.com/\">Google</a></dt>",
    181       charset, &title, &url);
    182   EXPECT_TRUE(result);
    183   EXPECT_EQ(ASCIIToUTF16("Google"), title);
    184   EXPECT_EQ("http://www.google.com/", url.spec());
    185 }
    186 
    187 TEST(FirefoxImporterTest, Firefox2BookmarkFileImport) {
    188   FilePath path;
    189   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
    190   path = path.AppendASCII("firefox2_importer");
    191 
    192   // Import all bookmarks from a file which include an empty folder entry.
    193   FilePath empty_folder_path = path.AppendASCII("empty_folder.html");
    194   std::set<GURL> default_urls;
    195   string16 first_folder_name;
    196   Firefox2Importer* importer = new Firefox2Importer();
    197   importer->AddRef();
    198   std::vector<ProfileWriter::BookmarkEntry> bookmarks;
    199   importer->ImportBookmarksFile(empty_folder_path, default_urls, false,
    200                                 first_folder_name, importer, &bookmarks,
    201                                 NULL, NULL);
    202   EXPECT_EQ(3, static_cast<int>(bookmarks.size()));
    203   std::vector<ProfileWriter::BookmarkEntry>::iterator it;
    204   ProfileWriter::BookmarkEntry entry;
    205   std::vector<string16>::iterator path_it;
    206   if (bookmarks.size() == 3) {
    207     it = bookmarks.begin();
    208     entry = *it++;
    209     EXPECT_EQ(ASCIIToUTF16("Empty"), entry.title);
    210     EXPECT_TRUE(entry.is_folder);
    211     EXPECT_EQ(base::Time::FromTimeT(1295938143), entry.creation_time);
    212     EXPECT_EQ(2, static_cast<int>(entry.path.size()));
    213     if (entry.path.size() == 2) {
    214       path_it = entry.path.begin();
    215       EXPECT_EQ(ASCIIToUTF16(""), *path_it++);
    216       EXPECT_EQ(ASCIIToUTF16("Empty's Parent"), *path_it);
    217     }
    218 
    219     entry = *it++;
    220     EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title);
    221     EXPECT_FALSE(entry.is_folder);
    222     EXPECT_EQ(base::Time::FromTimeT(1234567890), entry.creation_time);
    223     EXPECT_EQ(2, static_cast<int>(entry.path.size()));
    224     if (entry.path.size() == 2) {
    225       path_it = entry.path.begin();
    226       EXPECT_EQ(ASCIIToUTF16(""), *path_it++);
    227       EXPECT_EQ(ASCIIToUTF16("Not Empty"), *path_it);
    228     }
    229     EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec());
    230 
    231     entry = *it++;
    232     EXPECT_EQ(ASCIIToUTF16("Google"), entry.title);
    233     EXPECT_FALSE(entry.is_folder);
    234     EXPECT_EQ(base::Time::FromTimeT(0000000000), entry.creation_time);
    235     EXPECT_EQ(2, static_cast<int>(entry.path.size()));
    236     if (entry.path.size() == 2) {
    237       path_it = entry.path.begin();
    238       EXPECT_EQ(ASCIIToUTF16(""), *path_it++);
    239       EXPECT_EQ(ASCIIToUTF16("Not Empty But Default"), *path_it);
    240     }
    241     EXPECT_EQ("http://www.google.com/", entry.url.spec());
    242   }
    243 
    244   // Import non-default bookmarks from a file.
    245   bookmarks.clear();
    246   default_urls.insert(GURL("http://www.google.com/"));
    247   importer->ImportBookmarksFile(empty_folder_path, default_urls, false,
    248                                 first_folder_name, importer, &bookmarks,
    249                                 NULL, NULL);
    250   EXPECT_EQ(2, static_cast<int>(bookmarks.size()));
    251   if (bookmarks.size() == 2) {
    252     it = bookmarks.begin();
    253     entry = *it++;
    254     EXPECT_EQ(ASCIIToUTF16("Empty"), entry.title);
    255     EXPECT_TRUE(entry.is_folder);
    256     EXPECT_EQ(base::Time::FromTimeT(1295938143), entry.creation_time);
    257     EXPECT_EQ(2, static_cast<int>(entry.path.size()));
    258     if (entry.path.size() == 2) {
    259       path_it = entry.path.begin();
    260       EXPECT_EQ(ASCIIToUTF16(""), *path_it++);
    261       EXPECT_EQ(ASCIIToUTF16("Empty's Parent"), *path_it);
    262     }
    263 
    264     entry = *it++;
    265     EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title);
    266     EXPECT_FALSE(entry.is_folder);
    267     EXPECT_EQ(base::Time::FromTimeT(1234567890), entry.creation_time);
    268     EXPECT_EQ(2, static_cast<int>(entry.path.size()));
    269     if (entry.path.size() == 2) {
    270       path_it = entry.path.begin();
    271       EXPECT_EQ(ASCIIToUTF16(""), *path_it++);
    272       EXPECT_EQ(ASCIIToUTF16("Not Empty"), *path_it);
    273     }
    274     EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec());
    275   }
    276 
    277   // Import Epiphany bookmarks from a file
    278   FilePath epiphany_path = path.AppendASCII("epiphany.html");
    279   bookmarks.clear();
    280   default_urls.clear();
    281   importer->ImportBookmarksFile(epiphany_path, default_urls, false,
    282                                 first_folder_name, importer, &bookmarks,
    283                                 NULL, NULL);
    284   EXPECT_EQ(2, static_cast<int>(bookmarks.size()));
    285   if (bookmarks.size() == 2) {
    286     it = bookmarks.begin();
    287     entry = *it++;
    288     EXPECT_EQ(ASCIIToUTF16("[Tamura Yukari.com]"), entry.title);
    289     EXPECT_EQ("http://www.tamurayukari.com/", entry.url.spec());
    290     EXPECT_EQ(0, static_cast<int>(entry.path.size()));
    291     entry = *it++;
    292     EXPECT_EQ(ASCIIToUTF16("Google"), entry.title);
    293     EXPECT_EQ("http://www.google.com/", entry.url.spec());
    294     EXPECT_EQ(0, static_cast<int>(entry.path.size()));
    295   }
    296 
    297   importer->Release();
    298 }
    299