1 // Copyright 2014 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.
5 #include "components/bookmarks/browser/bookmark_codec.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/test/test_bookmark_client.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using base::ASCIIToUTF16
;
25 const char kUrl1Title
[] = "url1";
26 const char kUrl1Url
[] = "http://www.url1.com";
27 const char kUrl2Title
[] = "url2";
28 const char kUrl2Url
[] = "http://www.url2.com";
29 const char kUrl3Title
[] = "url3";
30 const char kUrl3Url
[] = "http://www.url3.com";
31 const char kUrl4Title
[] = "url4";
32 const char kUrl4Url
[] = "http://www.url4.com";
33 const char kFolder1Title
[] = "folder1";
34 const char kFolder2Title
[] = "folder2";
36 const base::FilePath
& GetTestDataDir() {
37 CR_DEFINE_STATIC_LOCAL(base::FilePath
, dir
, ());
39 PathService::Get(base::DIR_SOURCE_ROOT
, &dir
);
40 dir
= dir
.AppendASCII("components");
41 dir
= dir
.AppendASCII("test");
42 dir
= dir
.AppendASCII("data");
47 // Helper to get a mutable bookmark node.
48 BookmarkNode
* AsMutable(const BookmarkNode
* node
) {
49 return const_cast<BookmarkNode
*>(node
);
52 // Helper to verify the two given bookmark nodes.
53 void AssertNodesEqual(const BookmarkNode
* expected
,
54 const BookmarkNode
* actual
) {
55 ASSERT_TRUE(expected
);
57 EXPECT_EQ(expected
->id(), actual
->id());
58 EXPECT_EQ(expected
->GetTitle(), actual
->GetTitle());
59 EXPECT_EQ(expected
->type(), actual
->type());
60 EXPECT_TRUE(expected
->date_added() == actual
->date_added());
61 if (expected
->is_url()) {
62 EXPECT_EQ(expected
->url(), actual
->url());
64 EXPECT_TRUE(expected
->date_folder_modified() ==
65 actual
->date_folder_modified());
66 ASSERT_EQ(expected
->child_count(), actual
->child_count());
67 for (int i
= 0; i
< expected
->child_count(); ++i
)
68 AssertNodesEqual(expected
->GetChild(i
), actual
->GetChild(i
));
72 // Verifies that the two given bookmark models are the same.
73 void AssertModelsEqual(BookmarkModel
* expected
, BookmarkModel
* actual
) {
74 ASSERT_NO_FATAL_FAILURE(AssertNodesEqual(expected
->bookmark_bar_node(),
75 actual
->bookmark_bar_node()));
76 ASSERT_NO_FATAL_FAILURE(
77 AssertNodesEqual(expected
->other_node(), actual
->other_node()));
78 ASSERT_NO_FATAL_FAILURE(
79 AssertNodesEqual(expected
->mobile_node(), actual
->mobile_node()));
84 class BookmarkCodecTest
: public testing::Test
{
86 // Helpers to create bookmark models with different data.
87 BookmarkModel
* CreateTestModel1() {
88 scoped_ptr
<BookmarkModel
> model(client_
.CreateModel());
89 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
90 model
->AddURL(bookmark_bar
, 0, ASCIIToUTF16(kUrl1Title
), GURL(kUrl1Url
));
91 return model
.release();
93 BookmarkModel
* CreateTestModel2() {
94 scoped_ptr
<BookmarkModel
> model(client_
.CreateModel());
95 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
96 model
->AddURL(bookmark_bar
, 0, ASCIIToUTF16(kUrl1Title
), GURL(kUrl1Url
));
97 model
->AddURL(bookmark_bar
, 1, ASCIIToUTF16(kUrl2Title
), GURL(kUrl2Url
));
98 return model
.release();
100 BookmarkModel
* CreateTestModel3() {
101 scoped_ptr
<BookmarkModel
> model(client_
.CreateModel());
102 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
103 model
->AddURL(bookmark_bar
, 0, ASCIIToUTF16(kUrl1Title
), GURL(kUrl1Url
));
104 const BookmarkNode
* folder1
=
105 model
->AddFolder(bookmark_bar
, 1, ASCIIToUTF16(kFolder1Title
));
106 model
->AddURL(folder1
, 0, ASCIIToUTF16(kUrl2Title
), GURL(kUrl2Url
));
107 return model
.release();
110 void GetBookmarksBarChildValue(base::Value
* value
,
112 base::DictionaryValue
** result_value
) {
113 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, value
->GetType());
115 base::DictionaryValue
* d_value
= nullptr;
116 value
->GetAsDictionary(&d_value
);
118 ASSERT_TRUE(d_value
->Get(BookmarkCodec::kRootsKey
, &roots
));
119 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, roots
->GetType());
121 base::DictionaryValue
* roots_d_value
= nullptr;
122 roots
->GetAsDictionary(&roots_d_value
);
123 base::Value
* bb_value
;
125 roots_d_value
->Get(BookmarkCodec::kRootFolderNameKey
, &bb_value
));
126 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, bb_value
->GetType());
128 base::DictionaryValue
* bb_d_value
= nullptr;
129 bb_value
->GetAsDictionary(&bb_d_value
);
130 base::Value
* bb_children_value
;
132 bb_d_value
->Get(BookmarkCodec::kChildrenKey
, &bb_children_value
));
133 ASSERT_EQ(base::Value::TYPE_LIST
, bb_children_value
->GetType());
135 base::ListValue
* bb_children_l_value
= nullptr;
136 bb_children_value
->GetAsList(&bb_children_l_value
);
137 base::Value
* child_value
;
138 ASSERT_TRUE(bb_children_l_value
->Get(index
, &child_value
));
139 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, child_value
->GetType());
141 child_value
->GetAsDictionary(result_value
);
144 base::Value
* EncodeHelper(BookmarkModel
* model
, std::string
* checksum
) {
145 BookmarkCodec encoder
;
146 // Computed and stored checksums should be empty.
147 EXPECT_EQ("", encoder
.computed_checksum());
148 EXPECT_EQ("", encoder
.stored_checksum());
150 scoped_ptr
<base::Value
> value(encoder
.Encode(model
));
151 const std::string
& computed_checksum
= encoder
.computed_checksum();
152 const std::string
& stored_checksum
= encoder
.stored_checksum();
154 // Computed and stored checksums should not be empty and should be equal.
155 EXPECT_FALSE(computed_checksum
.empty());
156 EXPECT_FALSE(stored_checksum
.empty());
157 EXPECT_EQ(computed_checksum
, stored_checksum
);
159 *checksum
= computed_checksum
;
160 return value
.release();
163 bool Decode(BookmarkCodec
* codec
,
164 BookmarkModel
* model
,
165 const base::Value
& value
) {
167 bool result
= codec
->Decode(AsMutable(model
->bookmark_bar_node()),
168 AsMutable(model
->other_node()),
169 AsMutable(model
->mobile_node()),
172 model
->set_next_node_id(max_id
);
173 AsMutable(model
->root_node())->SetMetaInfoMap(codec
->model_meta_info_map());
174 AsMutable(model
->root_node())
175 ->set_sync_transaction_version(codec
->model_sync_transaction_version());
180 BookmarkModel
* DecodeHelper(const base::Value
& value
,
181 const std::string
& expected_stored_checksum
,
182 std::string
* computed_checksum
,
183 bool expected_changes
) {
184 BookmarkCodec decoder
;
185 // Computed and stored checksums should be empty.
186 EXPECT_EQ("", decoder
.computed_checksum());
187 EXPECT_EQ("", decoder
.stored_checksum());
189 scoped_ptr
<BookmarkModel
> model(client_
.CreateModel());
190 EXPECT_TRUE(Decode(&decoder
, model
.get(), value
));
192 *computed_checksum
= decoder
.computed_checksum();
193 const std::string
& stored_checksum
= decoder
.stored_checksum();
195 // Computed and stored checksums should not be empty.
196 EXPECT_FALSE(computed_checksum
->empty());
197 EXPECT_FALSE(stored_checksum
.empty());
199 // Stored checksum should be as expected.
200 EXPECT_EQ(expected_stored_checksum
, stored_checksum
);
202 // The two checksums should be equal if expected_changes is true; otherwise
203 // they should be different.
204 if (expected_changes
)
205 EXPECT_NE(*computed_checksum
, stored_checksum
);
207 EXPECT_EQ(*computed_checksum
, stored_checksum
);
209 return model
.release();
212 void CheckIDs(const BookmarkNode
* node
, std::set
<int64
>* assigned_ids
) {
214 int64 node_id
= node
->id();
215 EXPECT_TRUE(assigned_ids
->find(node_id
) == assigned_ids
->end());
216 assigned_ids
->insert(node_id
);
217 for (int i
= 0; i
< node
->child_count(); ++i
)
218 CheckIDs(node
->GetChild(i
), assigned_ids
);
221 void ExpectIDsUnique(BookmarkModel
* model
) {
222 std::set
<int64
> assigned_ids
;
223 CheckIDs(model
->bookmark_bar_node(), &assigned_ids
);
224 CheckIDs(model
->other_node(), &assigned_ids
);
225 CheckIDs(model
->mobile_node(), &assigned_ids
);
228 TestBookmarkClient client_
;
231 TEST_F(BookmarkCodecTest
, ChecksumEncodeDecodeTest
) {
232 scoped_ptr
<BookmarkModel
> model_to_encode(CreateTestModel1());
233 std::string enc_checksum
;
234 scoped_ptr
<base::Value
> value(
235 EncodeHelper(model_to_encode
.get(), &enc_checksum
));
237 EXPECT_TRUE(value
.get() != NULL
);
239 std::string dec_checksum
;
240 scoped_ptr
<BookmarkModel
> decoded_model(
241 DecodeHelper(*value
.get(), enc_checksum
, &dec_checksum
, false));
244 TEST_F(BookmarkCodecTest
, ChecksumEncodeIdenticalModelsTest
) {
245 // Encode two identical models and make sure the check-sums are same as long
246 // as the data is the same.
247 scoped_ptr
<BookmarkModel
> model1(CreateTestModel1());
248 std::string enc_checksum1
;
249 scoped_ptr
<base::Value
> value1(EncodeHelper(model1
.get(), &enc_checksum1
));
250 EXPECT_TRUE(value1
.get() != NULL
);
252 scoped_ptr
<BookmarkModel
> model2(CreateTestModel1());
253 std::string enc_checksum2
;
254 scoped_ptr
<base::Value
> value2(EncodeHelper(model2
.get(), &enc_checksum2
));
255 EXPECT_TRUE(value2
.get() != NULL
);
257 ASSERT_EQ(enc_checksum1
, enc_checksum2
);
260 TEST_F(BookmarkCodecTest
, ChecksumManualEditTest
) {
261 scoped_ptr
<BookmarkModel
> model_to_encode(CreateTestModel1());
262 std::string enc_checksum
;
263 scoped_ptr
<base::Value
> value(
264 EncodeHelper(model_to_encode
.get(), &enc_checksum
));
266 EXPECT_TRUE(value
.get() != NULL
);
268 // Change something in the encoded value before decoding it.
269 base::DictionaryValue
* child1_value
;
270 GetBookmarksBarChildValue(value
.get(), 0, &child1_value
);
272 ASSERT_TRUE(child1_value
->GetString(BookmarkCodec::kNameKey
, &title
));
273 child1_value
->SetString(BookmarkCodec::kNameKey
, title
+ "1");
275 std::string dec_checksum
;
276 scoped_ptr
<BookmarkModel
> decoded_model1(
277 DecodeHelper(*value
.get(), enc_checksum
, &dec_checksum
, true));
279 // Undo the change and make sure the checksum is same as original.
280 child1_value
->SetString(BookmarkCodec::kNameKey
, title
);
281 scoped_ptr
<BookmarkModel
> decoded_model2(
282 DecodeHelper(*value
.get(), enc_checksum
, &dec_checksum
, false));
285 TEST_F(BookmarkCodecTest
, ChecksumManualEditIDsTest
) {
286 scoped_ptr
<BookmarkModel
> model_to_encode(CreateTestModel3());
288 // The test depends on existence of multiple children under bookmark bar, so
289 // make sure that's the case.
290 int bb_child_count
= model_to_encode
->bookmark_bar_node()->child_count();
291 ASSERT_GT(bb_child_count
, 1);
293 std::string enc_checksum
;
294 scoped_ptr
<base::Value
> value(
295 EncodeHelper(model_to_encode
.get(), &enc_checksum
));
297 EXPECT_TRUE(value
.get() != NULL
);
299 // Change IDs for all children of bookmark bar to be 1.
300 base::DictionaryValue
* child_value
;
301 for (int i
= 0; i
< bb_child_count
; ++i
) {
302 GetBookmarksBarChildValue(value
.get(), i
, &child_value
);
304 ASSERT_TRUE(child_value
->GetString(BookmarkCodec::kIdKey
, &id
));
305 child_value
->SetString(BookmarkCodec::kIdKey
, "1");
308 std::string dec_checksum
;
309 scoped_ptr
<BookmarkModel
> decoded_model(
310 DecodeHelper(*value
.get(), enc_checksum
, &dec_checksum
, true));
312 ExpectIDsUnique(decoded_model
.get());
314 // add a few extra nodes to bookmark model and make sure IDs are still uniuqe.
315 const BookmarkNode
* bb_node
= decoded_model
->bookmark_bar_node();
316 decoded_model
->AddURL(
317 bb_node
, 0, ASCIIToUTF16("new url1"), GURL("http://newurl1.com"));
318 decoded_model
->AddURL(
319 bb_node
, 0, ASCIIToUTF16("new url2"), GURL("http://newurl2.com"));
321 ExpectIDsUnique(decoded_model
.get());
324 TEST_F(BookmarkCodecTest
, PersistIDsTest
) {
325 scoped_ptr
<BookmarkModel
> model_to_encode(CreateTestModel3());
326 BookmarkCodec encoder
;
327 scoped_ptr
<base::Value
> model_value(encoder
.Encode(model_to_encode
.get()));
329 scoped_ptr
<BookmarkModel
> decoded_model(client_
.CreateModel());
330 BookmarkCodec decoder
;
331 ASSERT_TRUE(Decode(&decoder
, decoded_model
.get(), *model_value
.get()));
332 ASSERT_NO_FATAL_FAILURE(
333 AssertModelsEqual(model_to_encode
.get(), decoded_model
.get()));
335 // Add a couple of more items to the decoded bookmark model and make sure
336 // ID persistence is working properly.
337 const BookmarkNode
* bookmark_bar
= decoded_model
->bookmark_bar_node();
338 decoded_model
->AddURL(bookmark_bar
,
339 bookmark_bar
->child_count(),
340 ASCIIToUTF16(kUrl3Title
),
342 const BookmarkNode
* folder2_node
= decoded_model
->AddFolder(
343 bookmark_bar
, bookmark_bar
->child_count(), ASCIIToUTF16(kFolder2Title
));
344 decoded_model
->AddURL(
345 folder2_node
, 0, ASCIIToUTF16(kUrl4Title
), GURL(kUrl4Url
));
347 BookmarkCodec encoder2
;
348 scoped_ptr
<base::Value
> model_value2(encoder2
.Encode(decoded_model
.get()));
350 scoped_ptr
<BookmarkModel
> decoded_model2(client_
.CreateModel());
351 BookmarkCodec decoder2
;
352 ASSERT_TRUE(Decode(&decoder2
, decoded_model2
.get(), *model_value2
.get()));
353 ASSERT_NO_FATAL_FAILURE(
354 AssertModelsEqual(decoded_model
.get(), decoded_model2
.get()));
357 TEST_F(BookmarkCodecTest
, CanDecodeModelWithoutMobileBookmarks
) {
358 base::FilePath test_file
=
359 GetTestDataDir().AppendASCII("bookmarks/model_without_sync.json");
360 ASSERT_TRUE(base::PathExists(test_file
));
362 JSONFileValueSerializer
serializer(test_file
);
363 scoped_ptr
<base::Value
> root(serializer
.Deserialize(NULL
, NULL
));
365 scoped_ptr
<BookmarkModel
> decoded_model(client_
.CreateModel());
366 BookmarkCodec decoder
;
367 ASSERT_TRUE(Decode(&decoder
, decoded_model
.get(), *root
.get()));
368 ExpectIDsUnique(decoded_model
.get());
370 const BookmarkNode
* bbn
= decoded_model
->bookmark_bar_node();
371 ASSERT_EQ(1, bbn
->child_count());
373 const BookmarkNode
* child
= bbn
->GetChild(0);
374 EXPECT_EQ(BookmarkNode::FOLDER
, child
->type());
375 EXPECT_EQ(ASCIIToUTF16("Folder A"), child
->GetTitle());
376 ASSERT_EQ(1, child
->child_count());
378 child
= child
->GetChild(0);
379 EXPECT_EQ(BookmarkNode::URL
, child
->type());
380 EXPECT_EQ(ASCIIToUTF16("Bookmark Manager"), child
->GetTitle());
382 const BookmarkNode
* other
= decoded_model
->other_node();
383 ASSERT_EQ(1, other
->child_count());
385 child
= other
->GetChild(0);
386 EXPECT_EQ(BookmarkNode::FOLDER
, child
->type());
387 EXPECT_EQ(ASCIIToUTF16("Folder B"), child
->GetTitle());
388 ASSERT_EQ(1, child
->child_count());
390 child
= child
->GetChild(0);
391 EXPECT_EQ(BookmarkNode::URL
, child
->type());
392 EXPECT_EQ(ASCIIToUTF16("Get started with Google Chrome"), child
->GetTitle());
394 ASSERT_TRUE(decoded_model
->mobile_node() != NULL
);
397 TEST_F(BookmarkCodecTest
, EncodeAndDecodeMetaInfo
) {
398 // Add meta info and encode.
399 scoped_ptr
<BookmarkModel
> model(CreateTestModel1());
400 model
->SetNodeMetaInfo(model
->root_node(), "model_info", "value1");
401 model
->SetNodeMetaInfo(
402 model
->bookmark_bar_node()->GetChild(0), "node_info", "value2");
403 std::string checksum
;
404 scoped_ptr
<base::Value
> value(EncodeHelper(model
.get(), &checksum
));
405 ASSERT_TRUE(value
.get() != NULL
);
407 // Decode and check for meta info.
408 model
.reset(DecodeHelper(*value
, checksum
, &checksum
, false));
409 std::string meta_value
;
410 EXPECT_TRUE(model
->root_node()->GetMetaInfo("model_info", &meta_value
));
411 EXPECT_EQ("value1", meta_value
);
412 EXPECT_FALSE(model
->root_node()->GetMetaInfo("other_key", &meta_value
));
413 const BookmarkNode
* bbn
= model
->bookmark_bar_node();
414 ASSERT_EQ(1, bbn
->child_count());
415 const BookmarkNode
* child
= bbn
->GetChild(0);
416 EXPECT_TRUE(child
->GetMetaInfo("node_info", &meta_value
));
417 EXPECT_EQ("value2", meta_value
);
418 EXPECT_FALSE(child
->GetMetaInfo("other_key", &meta_value
));
421 TEST_F(BookmarkCodecTest
, EncodeAndDecodeSyncTransactionVersion
) {
422 // Add sync transaction version and encode.
423 scoped_ptr
<BookmarkModel
> model(CreateTestModel2());
424 model
->SetNodeSyncTransactionVersion(model
->root_node(), 1);
425 const BookmarkNode
* bbn
= model
->bookmark_bar_node();
426 model
->SetNodeSyncTransactionVersion(bbn
->GetChild(1), 42);
428 std::string checksum
;
429 scoped_ptr
<base::Value
> value(EncodeHelper(model
.get(), &checksum
));
430 ASSERT_TRUE(value
.get() != NULL
);
432 // Decode and verify.
433 model
.reset(DecodeHelper(*value
, checksum
, &checksum
, false));
434 EXPECT_EQ(1, model
->root_node()->sync_transaction_version());
435 bbn
= model
->bookmark_bar_node();
436 EXPECT_EQ(42, bbn
->GetChild(1)->sync_transaction_version());
437 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion
,
438 bbn
->GetChild(0)->sync_transaction_version());
441 // Verifies that we can still decode the old codec format after changing the
442 // way meta info is stored.
443 TEST_F(BookmarkCodecTest
, CanDecodeMetaInfoAsString
) {
444 base::FilePath test_file
=
445 GetTestDataDir().AppendASCII("bookmarks/meta_info_as_string.json");
446 ASSERT_TRUE(base::PathExists(test_file
));
448 JSONFileValueSerializer
serializer(test_file
);
449 scoped_ptr
<base::Value
> root(serializer
.Deserialize(NULL
, NULL
));
451 scoped_ptr
<BookmarkModel
> model(client_
.CreateModel());
452 BookmarkCodec decoder
;
453 ASSERT_TRUE(Decode(&decoder
, model
.get(), *root
.get()));
455 EXPECT_EQ(1, model
->root_node()->sync_transaction_version());
456 const BookmarkNode
* bbn
= model
->bookmark_bar_node();
457 EXPECT_EQ(BookmarkNode::kInvalidSyncTransactionVersion
,
458 bbn
->GetChild(0)->sync_transaction_version());
459 EXPECT_EQ(42, bbn
->GetChild(1)->sync_transaction_version());
461 const char kSyncTransactionVersionKey
[] = "sync.transaction_version";
462 const char kNormalKey
[] = "key";
463 const char kNestedKey
[] = "nested.key";
464 std::string meta_value
;
466 model
->root_node()->GetMetaInfo(kSyncTransactionVersionKey
, &meta_value
));
468 bbn
->GetChild(1)->GetMetaInfo(kSyncTransactionVersionKey
, &meta_value
));
469 EXPECT_TRUE(bbn
->GetChild(0)->GetMetaInfo(kNormalKey
, &meta_value
));
470 EXPECT_EQ("value", meta_value
);
471 EXPECT_TRUE(bbn
->GetChild(1)->GetMetaInfo(kNormalKey
, &meta_value
));
472 EXPECT_EQ("value2", meta_value
);
473 EXPECT_TRUE(bbn
->GetChild(0)->GetMetaInfo(kNestedKey
, &meta_value
));
474 EXPECT_EQ("value3", meta_value
);
477 } // namespace bookmarks