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