// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "testing/gtest/include/gtest/gtest.h" #include "base/file_path.h" #include "base/file_util.h" #include "base/path_service.h" #include "base/utf_string_conversions.h" #include "chrome/browser/importer/firefox2_importer.h" #include "chrome/browser/importer/firefox_importer_unittest_utils.h" #include "chrome/browser/importer/firefox_importer_utils.h" #include "chrome/browser/importer/nss_decryptor.h" #include "chrome/common/chrome_paths.h" using base::Time; // The following 2 tests require the use of the NSSDecryptor, on OSX this needs // to run in a separate process, so we use a proxy object so we can share the // same test between platforms. TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) { FilePath nss_path; ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); #ifdef OS_MACOSX nss_path = nss_path.AppendASCII("firefox2_nss_mac"); #else nss_path = nss_path.AppendASCII("firefox2_nss"); #endif // !OS_MACOSX FilePath db_path; ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); db_path = db_path.AppendASCII("firefox2_profile"); FFUnitTestDecryptorProxy decryptor_proxy; ASSERT_TRUE(decryptor_proxy.Setup(nss_path.ToWStringHack())); EXPECT_TRUE(decryptor_proxy.DecryptorInit(nss_path.ToWStringHack(), db_path.ToWStringHack())); EXPECT_EQ(ASCIIToUTF16("hello"), decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECBJ" "M63MpT9rtBAjMCm7qo/EhlA==")); // Test UTF-16 encoding. EXPECT_EQ(WideToUTF16(L"\x4E2D"), decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECN9" "OQ5ZFmhb8BAiFo1Z+fUvaIQ==")); } TEST(FirefoxImporterTest, Firefox3NSS3Decryptor) { FilePath nss_path; ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); #ifdef OS_MACOSX nss_path = nss_path.AppendASCII("firefox3_nss_mac"); #else nss_path = nss_path.AppendASCII("firefox3_nss"); #endif // !OS_MACOSX FilePath db_path; ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); db_path = db_path.AppendASCII("firefox3_profile"); FFUnitTestDecryptorProxy decryptor_proxy; ASSERT_TRUE(decryptor_proxy.Setup(nss_path.ToWStringHack())); EXPECT_TRUE(decryptor_proxy.DecryptorInit(nss_path.ToWStringHack(), db_path.ToWStringHack())); EXPECT_EQ(ASCIIToUTF16("hello"), decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECKa" "jtRg4qFSHBAhv9luFkXgDJA==")); // Test UTF-16 encoding. EXPECT_EQ(WideToUTF16(L"\x4E2D"), decryptor_proxy.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECLW" "qqiccfQHWBAie74hxnULxlw==")); } TEST(FirefoxImporterTest, Firefox2BookmarkParse) { bool result; // Tests charset. std::string charset; result = Firefox2Importer::ParseCharsetFromLine( "", &charset); EXPECT_TRUE(result); EXPECT_EQ("UTF-8", charset); // Escaped characters in name. std::wstring folder_name; bool is_toolbar_folder; result = Firefox2Importer::ParseFolderNameFromLine( "

< >" " & " ' \\ /

", charset, &folder_name, &is_toolbar_folder); EXPECT_TRUE(result); EXPECT_EQ(L"< > & \" ' \\ /", folder_name); EXPECT_EQ(false, is_toolbar_folder); // Empty name and toolbar folder attribute. result = Firefox2Importer::ParseFolderNameFromLine( "

", charset, &folder_name, &is_toolbar_folder); EXPECT_TRUE(result); EXPECT_EQ(L"", folder_name); EXPECT_EQ(true, is_toolbar_folder); // Unicode characters in title and shortcut. std::wstring title; GURL url, favicon; std::wstring shortcut; std::wstring post_data; Time add_date; result = Firefox2Importer::ParseBookmarkFromLine( "
\xE4\xB8\xAD\xE6\x96\x87", charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); EXPECT_TRUE(result); EXPECT_EQ(L"\x4E2D\x6587", title); EXPECT_EQ("http://chinese.site.cn/path?query=1#ref", url.spec()); EXPECT_EQ(L"\x4E2D", shortcut); EXPECT_EQ(L"", post_data); EXPECT_TRUE(Time() == add_date); // No shortcut, and url contains %22 ('"' character). result = Firefox2Importer::ParseBookmarkFromLine( "
%22\">name", charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); EXPECT_TRUE(result); EXPECT_EQ(L"name", title); EXPECT_EQ("http://domain.com/?q=%22%3C%3E%22", url.spec()); EXPECT_EQ(L"", shortcut); EXPECT_EQ(L"", post_data); EXPECT_TRUE(Time() == add_date); result = Firefox2Importer::ParseBookmarkFromLine( "
name", charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); EXPECT_TRUE(result); EXPECT_EQ(L"name", title); EXPECT_EQ("http://domain.com/?g=%22", url.spec()); EXPECT_EQ(L"", shortcut); EXPECT_EQ(L"", post_data); EXPECT_TRUE(Time() == add_date); // Creation date. result = Firefox2Importer::ParseBookmarkFromLine( "
name", charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); EXPECT_TRUE(result); EXPECT_EQ(L"name", title); EXPECT_EQ(GURL("http://site/"), url); EXPECT_EQ(L"", shortcut); EXPECT_EQ(L"", post_data); EXPECT_TRUE(Time::FromTimeT(1121301154) == add_date); // Post-data result = Firefox2Importer::ParseBookmarkFromLine( "
Test Post keyword", charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); EXPECT_TRUE(result); EXPECT_EQ(L"Test Post keyword", title); EXPECT_EQ("http://localhost:8080/test/hello.html", url.spec()); EXPECT_EQ(L"post", shortcut); EXPECT_EQ(L"lname%3D%25s", post_data); EXPECT_TRUE(Time::FromTimeT(1212447159) == add_date); // Invalid case. result = Firefox2Importer::ParseBookmarkFromLine( "