OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h
" | 5 #include "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h
" |
6 | 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 |
7 #if defined(USE_PLATFORM_STATE_STORE) | 11 #if defined(USE_PLATFORM_STATE_STORE) |
8 | 12 |
9 #include <stdint.h> | 13 #include <stdint.h> |
10 | 14 |
11 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
12 #include "base/macros.h" | 16 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/values.h" | 17 #include "base/values.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace safe_browsing { | 20 namespace safe_browsing { |
18 namespace platform_state_store { | 21 namespace platform_state_store { |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 // The serialized form of the dict created by CreateTestIncidentsSentPref. | 25 // The serialized form of the dict created by CreateTestIncidentsSentPref. |
23 const uint8_t kTestData[] = { | 26 const uint8_t kTestData[] = { |
24 0x0a, 0x19, 0x08, 0x00, 0x12, 0x15, 0x0a, 0x09, 0x0a, 0x04, 0x77, | 27 0x0a, 0x19, 0x08, 0x00, 0x12, 0x15, 0x0a, 0x09, 0x0a, 0x04, 0x77, |
25 0x68, 0x61, 0x3f, 0x10, 0x94, 0x4d, 0x0a, 0x08, 0x0a, 0x04, 0x77, | 28 0x68, 0x61, 0x3f, 0x10, 0x94, 0x4d, 0x0a, 0x08, 0x0a, 0x04, 0x77, |
26 0x68, 0x61, 0x61, 0x10, 0x00, 0x0a, 0x1a, 0x08, 0x02, 0x12, 0x16, | 29 0x68, 0x61, 0x61, 0x10, 0x00, 0x0a, 0x1a, 0x08, 0x02, 0x12, 0x16, |
27 0x0a, 0x09, 0x0a, 0x05, 0x62, 0x6c, 0x6f, 0x72, 0x66, 0x10, 0x05, | 30 0x0a, 0x09, 0x0a, 0x05, 0x62, 0x6c, 0x6f, 0x72, 0x66, 0x10, 0x05, |
28 0x0a, 0x09, 0x0a, 0x04, 0x73, 0x70, 0x61, 0x6d, 0x10, 0xd2, 0x09 | 31 0x0a, 0x09, 0x0a, 0x04, 0x73, 0x70, 0x61, 0x6d, 0x10, 0xd2, 0x09 |
29 }; | 32 }; |
30 | 33 |
31 // Returns a dict with some sample data in it. | 34 // Returns a dict with some sample data in it. |
32 scoped_ptr<base::DictionaryValue> CreateTestIncidentsSentPref() { | 35 std::unique_ptr<base::DictionaryValue> CreateTestIncidentsSentPref() { |
33 static const char kData[] = | 36 static const char kData[] = |
34 "{" | 37 "{" |
35 "\"2\":{\"spam\":\"1234\",\"blorf\":\"5\"}," | 38 "\"2\":{\"spam\":\"1234\",\"blorf\":\"5\"}," |
36 "\"0\":{\"whaa\":\"0\",\"wha?\":\"9876\"}" | 39 "\"0\":{\"whaa\":\"0\",\"wha?\":\"9876\"}" |
37 "}"; | 40 "}"; |
38 base::JSONReader reader; | 41 base::JSONReader reader; |
39 | 42 |
40 scoped_ptr<base::Value> root(reader.Read(kData)); | 43 std::unique_ptr<base::Value> root(reader.Read(kData)); |
41 EXPECT_TRUE(root); | 44 EXPECT_TRUE(root); |
42 base::DictionaryValue* incidents_sent = nullptr; | 45 base::DictionaryValue* incidents_sent = nullptr; |
43 EXPECT_TRUE(root->GetAsDictionary(&incidents_sent)); | 46 EXPECT_TRUE(root->GetAsDictionary(&incidents_sent)); |
44 // Relinquish ownership to |incidents_sent|. | 47 // Relinquish ownership to |incidents_sent|. |
45 ignore_result(root.release()); | 48 ignore_result(root.release()); |
46 return make_scoped_ptr(incidents_sent); | 49 return base::WrapUnique(incidents_sent); |
47 } | 50 } |
48 | 51 |
49 } // namespace | 52 } // namespace |
50 | 53 |
51 // Tests that DeserializeIncidentsSent handles an empty payload properly. | 54 // Tests that DeserializeIncidentsSent handles an empty payload properly. |
52 TEST(PlatformStateStoreTest, DeserializeEmpty) { | 55 TEST(PlatformStateStoreTest, DeserializeEmpty) { |
53 scoped_ptr<base::DictionaryValue> deserialized(new base::DictionaryValue); | 56 std::unique_ptr<base::DictionaryValue> deserialized( |
| 57 new base::DictionaryValue); |
54 PlatformStateStoreLoadResult load_result = | 58 PlatformStateStoreLoadResult load_result = |
55 DeserializeIncidentsSent(std::string(), deserialized.get()); | 59 DeserializeIncidentsSent(std::string(), deserialized.get()); |
56 ASSERT_EQ(PlatformStateStoreLoadResult::SUCCESS, load_result); | 60 ASSERT_EQ(PlatformStateStoreLoadResult::SUCCESS, load_result); |
57 ASSERT_TRUE(deserialized->empty()); | 61 ASSERT_TRUE(deserialized->empty()); |
58 } | 62 } |
59 | 63 |
60 // Tests that serialize followed by deserialize doesn't lose data. | 64 // Tests that serialize followed by deserialize doesn't lose data. |
61 TEST(PlatformStateStoreTest, RoundTrip) { | 65 TEST(PlatformStateStoreTest, RoundTrip) { |
62 scoped_ptr<base::DictionaryValue> incidents_sent( | 66 std::unique_ptr<base::DictionaryValue> incidents_sent( |
63 CreateTestIncidentsSentPref()); | 67 CreateTestIncidentsSentPref()); |
64 std::string data; | 68 std::string data; |
65 | 69 |
66 SerializeIncidentsSent(incidents_sent.get(), &data); | 70 SerializeIncidentsSent(incidents_sent.get(), &data); |
67 | 71 |
68 // Make sure the serialized data matches expectations to ensure compatibility. | 72 // Make sure the serialized data matches expectations to ensure compatibility. |
69 ASSERT_EQ(std::string(reinterpret_cast<const char*>(&kTestData[0]), | 73 ASSERT_EQ(std::string(reinterpret_cast<const char*>(&kTestData[0]), |
70 sizeof(kTestData)), data); | 74 sizeof(kTestData)), data); |
71 | 75 |
72 scoped_ptr<base::DictionaryValue> deserialized(new base::DictionaryValue); | 76 std::unique_ptr<base::DictionaryValue> deserialized( |
| 77 new base::DictionaryValue); |
73 PlatformStateStoreLoadResult load_result = | 78 PlatformStateStoreLoadResult load_result = |
74 DeserializeIncidentsSent(data, deserialized.get()); | 79 DeserializeIncidentsSent(data, deserialized.get()); |
75 ASSERT_EQ(PlatformStateStoreLoadResult::SUCCESS, load_result); | 80 ASSERT_EQ(PlatformStateStoreLoadResult::SUCCESS, load_result); |
76 ASSERT_TRUE(deserialized->Equals(incidents_sent.get())); | 81 ASSERT_TRUE(deserialized->Equals(incidents_sent.get())); |
77 } | 82 } |
78 | 83 |
79 } // namespace platform_state_store | 84 } // namespace platform_state_store |
80 } // namespace safe_browsing | 85 } // namespace safe_browsing |
81 | 86 |
82 #endif // USE_PLATFORM_STATE_STORE | 87 #endif // USE_PLATFORM_STATE_STORE |
OLD | NEW |