OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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_util.h" | |
8 #include "base/path_service.h" | |
9 #include "chrome/browser/firefox2_importer.h" | |
10 #include "chrome/browser/firefox_importer_utils.h" | |
11 #include "chrome/browser/firefox_profile_lock.h" | |
12 #include "chrome/common/chrome_paths.h" | |
13 | |
14 TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) { | |
15 std::wstring nss_path; | |
16 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); | |
17 file_util::AppendToPath(&nss_path, L"firefox2_nss"); | |
18 std::wstring db_path; | |
19 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); | |
20 file_util::AppendToPath(&db_path, L"firefox2_profile"); | |
21 NSSDecryptor decryptor; | |
22 EXPECT_TRUE(decryptor.Init(nss_path, db_path)); | |
23 EXPECT_EQ(L"hello", decryptor.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAE" | |
24 "wFAYIKoZIhvcNAwcECBJM63MpT9rtBAjMCm7qo/EhlA==")); | |
25 // Test UTF-16 encoding. | |
26 EXPECT_EQ(L"\x4E2D", decryptor.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAE" | |
27 "wFAYIKoZIhvcNAwcECN9OQ5ZFmhb8BAiFo1Z+fUvaIQ==")); | |
28 } | |
29 | |
30 TEST(FirefoxImporterTest, Firefox3NSS3Decryptor) { | |
31 std::wstring nss_path; | |
32 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); | |
33 file_util::AppendToPath(&nss_path, L"firefox3_nss"); | |
34 std::wstring db_path; | |
35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); | |
36 file_util::AppendToPath(&db_path, L"firefox3_profile"); | |
37 NSSDecryptor decryptor; | |
38 EXPECT_TRUE(decryptor.Init(nss_path, db_path)); | |
39 EXPECT_EQ(L"hello", decryptor.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAE" | |
40 "wFAYIKoZIhvcNAwcECKajtRg4qFSHBAhv9luFkXgDJA==")); | |
41 // Test UTF-16 encoding. | |
42 EXPECT_EQ(L"\x4E2D", decryptor.Decrypt("MDIEEPgAAAAAAAAAAAAAAAAAAAE" | |
43 "wFAYIKoZIhvcNAwcECLWqqiccfQHWBAie74hxnULxlw==")); | |
44 } | |
45 | |
46 TEST(FirefoxImporterTest, Firefox2BookmarkParse) { | |
47 bool result; | |
48 | |
49 // Tests charset. | |
50 std::string charset; | |
51 result = Firefox2Importer::ParseCharsetFromLine( | |
52 "<META HTTP-EQUIV=\"Content-Type\" " | |
53 "CONTENT=\"text/html; charset=UTF-8\">", | |
54 &charset); | |
55 EXPECT_TRUE(result); | |
56 EXPECT_EQ("UTF-8", charset); | |
57 | |
58 // Escaped characters in name. | |
59 std::wstring folder_name; | |
60 bool is_toolbar_folder; | |
61 result = Firefox2Importer::ParseFolderNameFromLine( | |
62 "<DT><H3 ADD_DATE=\"1207558707\" >< >" | |
63 " & " ' \\ /</H3>", | |
64 charset, &folder_name, &is_toolbar_folder); | |
65 EXPECT_TRUE(result); | |
66 EXPECT_EQ(L"< > & \" ' \\ /", folder_name); | |
67 EXPECT_EQ(false, is_toolbar_folder); | |
68 | |
69 // Empty name and toolbar folder attribute. | |
70 result = Firefox2Importer::ParseFolderNameFromLine( | |
71 "<DT><H3 PERSONAL_TOOLBAR_FOLDER=\"true\"></H3>", | |
72 charset, &folder_name, &is_toolbar_folder); | |
73 EXPECT_TRUE(result); | |
74 EXPECT_EQ(L"", folder_name); | |
75 EXPECT_EQ(true, is_toolbar_folder); | |
76 | |
77 // Unicode characters in title and shortcut. | |
78 std::wstring title; | |
79 GURL url, favicon; | |
80 std::wstring shortcut; | |
81 std::wstring post_data; | |
82 Time add_date; | |
83 result = Firefox2Importer::ParseBookmarkFromLine( | |
84 "<DT><A HREF=\"http://chinese.site.cn/path?query=1#ref\" " | |
85 "SHORTCUTURL=\"\xE4\xB8\xAD\">\xE4\xB8\xAD\xE6\x96\x87</A>", | |
86 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); | |
87 EXPECT_TRUE(result); | |
88 EXPECT_EQ(L"\x4E2D\x6587", title); | |
89 EXPECT_EQ("http://chinese.site.cn/path?query=1#ref", url.spec()); | |
90 EXPECT_EQ(L"\x4E2D", shortcut); | |
91 EXPECT_EQ(L"", post_data); | |
92 EXPECT_TRUE(Time() == add_date); | |
93 | |
94 // No shortcut, and url contains %22 ('"' character). | |
95 result = Firefox2Importer::ParseBookmarkFromLine( | |
96 "<DT><A HREF=\"http://domain.com/?q=%22<>%22\">name</A>", | |
97 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); | |
98 EXPECT_TRUE(result); | |
99 EXPECT_EQ(L"name", title); | |
100 EXPECT_EQ("http://domain.com/?q=\"%3C%3E\"", url.spec()); | |
101 EXPECT_EQ(L"", shortcut); | |
102 EXPECT_EQ(L"", post_data); | |
103 EXPECT_TRUE(Time() == add_date); | |
104 | |
105 // Creation date. | |
106 result = Firefox2Importer::ParseBookmarkFromLine( | |
107 "<DT><A HREF=\"http://site/\" ADD_DATE=\"1121301154\">name</A>", | |
108 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); | |
109 EXPECT_TRUE(result); | |
110 EXPECT_EQ(L"name", title); | |
111 EXPECT_EQ(GURL("http://site/"), url); | |
112 EXPECT_EQ(L"", shortcut); | |
113 EXPECT_EQ(L"", post_data); | |
114 EXPECT_TRUE(Time::FromTimeT(1121301154) == add_date); | |
115 | |
116 // Post-data | |
117 result = Firefox2Importer::ParseBookmarkFromLine( | |
118 "<DT><A HREF=\"http://localhost:8080/test/hello.html\" ADD_DATE=\"" | |
119 "1212447159\" LAST_VISIT=\"1212447251\" LAST_MODIFIED=\"1212447248\"" | |
120 "SHORTCUTURL=\"post\" ICON=\"data:\" POST_DATA=\"lname%3D%25s\"" | |
121 "LAST_CHARSET=\"UTF-8\" ID=\"rdf:#$weKaR3\">Test Post keyword</A>", | |
122 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); | |
123 EXPECT_TRUE(result); | |
124 EXPECT_EQ(L"Test Post keyword", title); | |
125 EXPECT_EQ("http://localhost:8080/test/hello.html", url.spec()); | |
126 EXPECT_EQ(L"post", shortcut); | |
127 EXPECT_EQ(L"lname%3D%25s", post_data); | |
128 EXPECT_TRUE(Time::FromTimeT(1212447159) == add_date); | |
129 | |
130 // Invalid case. | |
131 result = Firefox2Importer::ParseBookmarkFromLine( | |
132 "<DT><A HREF=\"http://domain.com/?q=%22", | |
133 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); | |
134 EXPECT_FALSE(result); | |
135 EXPECT_EQ(L"", title); | |
136 EXPECT_EQ("", url.spec()); | |
137 EXPECT_EQ(L"", shortcut); | |
138 EXPECT_EQ(L"", post_data); | |
139 EXPECT_TRUE(Time() == add_date); | |
140 } | |
141 | |
142 // Tests basic functionality and verifies that the lock file is deleted after | |
143 // use. | |
144 TEST(FirefoxImporterTest, ProfileLock) { | |
145 std::wstring test_path; | |
146 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | |
147 std::wstring lock_file_path = test_path; | |
148 file_util::AppendToPath(&lock_file_path, FirefoxProfileLock::kLockFileName); | |
149 | |
150 scoped_ptr<FirefoxProfileLock> lock; | |
151 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | |
152 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | |
153 lock.reset(new FirefoxProfileLock(test_path)); | |
154 EXPECT_TRUE(lock->HasAcquired()); | |
155 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | |
156 lock->Unlock(); | |
157 EXPECT_FALSE(lock->HasAcquired()); | |
158 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | |
159 lock->Lock(); | |
160 EXPECT_TRUE(lock->HasAcquired()); | |
161 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | |
162 lock->Lock(); | |
163 EXPECT_TRUE(lock->HasAcquired()); | |
164 lock->Unlock(); | |
165 EXPECT_FALSE(lock->HasAcquired()); | |
166 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | |
167 } | |
168 | |
169 // If for some reason the lock file is left behind by the previous owner, we | |
170 // should still be able to lock it, at least in the Windows implementation. | |
171 TEST(FirefoxImporterTest, ProfileLockOrphaned) { | |
172 std::wstring test_path; | |
173 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | |
174 std::wstring lock_file_path = test_path; | |
175 file_util::AppendToPath(&lock_file_path, FirefoxProfileLock::kLockFileName); | |
176 | |
177 // Create the orphaned lock file. | |
178 HANDLE lock_file = CreateFile(lock_file_path.c_str(), | |
179 GENERIC_READ | GENERIC_WRITE, | |
180 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, | |
181 NULL); | |
182 CloseHandle(lock_file); | |
183 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | |
184 | |
185 scoped_ptr<FirefoxProfileLock> lock; | |
186 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | |
187 lock.reset(new FirefoxProfileLock(test_path)); | |
188 EXPECT_TRUE(lock->HasAcquired()); | |
189 lock->Unlock(); | |
190 EXPECT_FALSE(lock->HasAcquired()); | |
191 } | |
192 | |
193 // Tests two locks contending for the same lock file. | |
194 TEST(FirefoxImporterTest, ProfileLockContention) { | |
195 std::wstring test_path; | |
196 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | |
197 | |
198 scoped_ptr<FirefoxProfileLock> lock1; | |
199 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); | |
200 lock1.reset(new FirefoxProfileLock(test_path)); | |
201 EXPECT_TRUE(lock1->HasAcquired()); | |
202 | |
203 scoped_ptr<FirefoxProfileLock> lock2; | |
204 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); | |
205 lock2.reset(new FirefoxProfileLock(test_path)); | |
206 EXPECT_FALSE(lock2->HasAcquired()); | |
207 | |
208 lock1->Unlock(); | |
209 EXPECT_FALSE(lock1->HasAcquired()); | |
210 | |
211 lock2->Lock(); | |
212 EXPECT_TRUE(lock2->HasAcquired()); | |
213 lock2->Unlock(); | |
214 EXPECT_FALSE(lock2->HasAcquired()); | |
215 } | |
216 | |
OLD | NEW |