1 // Copyright (c) 2012 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 "ui/base/clipboard/custom_data_helper.h" 6 7 #include <utility> 8 9 #include "base/pickle.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 using base::ASCIIToUTF16; 14 15 namespace ui { 16 17 namespace { 18 19 void PrepareEmptyTestData(Pickle* pickle) { 20 std::map<base::string16, base::string16> data; 21 WriteCustomDataToPickle(data, pickle); 22 } 23 24 void PrepareTestData(Pickle* pickle) { 25 std::map<base::string16, base::string16> data; 26 data.insert(std::make_pair(ASCIIToUTF16("abc"), base::string16())); 27 data.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1"))); 28 data.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23"))); 29 WriteCustomDataToPickle(data, pickle); 30 } 31 32 TEST(CustomDataHelperTest, EmptyReadTypes) { 33 Pickle pickle; 34 PrepareEmptyTestData(&pickle); 35 36 std::vector<base::string16> types; 37 ReadCustomDataTypes(pickle.data(), pickle.size(), &types); 38 EXPECT_EQ(0u, types.size()); 39 } 40 41 TEST(CustomDataHelperTest, EmptyReadSingleType) { 42 Pickle pickle; 43 PrepareEmptyTestData(&pickle); 44 45 base::string16 result; 46 ReadCustomDataForType(pickle.data(), 47 pickle.size(), 48 ASCIIToUTF16("f"), 49 &result); 50 EXPECT_EQ(base::string16(), result); 51 } 52 53 TEST(CustomDataHelperTest, EmptyReadMap) { 54 Pickle pickle; 55 PrepareEmptyTestData(&pickle); 56 57 std::map<base::string16, base::string16> result; 58 ReadCustomDataIntoMap(pickle.data(), pickle.size(), &result); 59 EXPECT_EQ(0u, result.size()); 60 } 61 62 TEST(CustomDataHelperTest, ReadTypes) { 63 Pickle pickle; 64 PrepareTestData(&pickle); 65 66 std::vector<base::string16> types; 67 ReadCustomDataTypes(pickle.data(), pickle.size(), &types); 68 69 std::vector<base::string16> expected; 70 expected.push_back(ASCIIToUTF16("abc")); 71 expected.push_back(ASCIIToUTF16("de")); 72 expected.push_back(ASCIIToUTF16("f")); 73 EXPECT_EQ(expected, types); 74 } 75 76 TEST(CustomDataHelperTest, ReadSingleType) { 77 Pickle pickle; 78 PrepareTestData(&pickle); 79 80 base::string16 result; 81 ReadCustomDataForType(pickle.data(), 82 pickle.size(), 83 ASCIIToUTF16("abc"), 84 &result); 85 EXPECT_EQ(base::string16(), result); 86 87 ReadCustomDataForType(pickle.data(), 88 pickle.size(), 89 ASCIIToUTF16("de"), 90 &result); 91 EXPECT_EQ(ASCIIToUTF16("1"), result); 92 93 ReadCustomDataForType(pickle.data(), 94 pickle.size(), 95 ASCIIToUTF16("f"), 96 &result); 97 EXPECT_EQ(ASCIIToUTF16("23"), result); 98 } 99 100 TEST(CustomDataHelperTest, ReadMap) { 101 Pickle pickle; 102 PrepareTestData(&pickle); 103 104 std::map<base::string16, base::string16> result; 105 ReadCustomDataIntoMap(pickle.data(), pickle.size(), &result); 106 107 std::map<base::string16, base::string16> expected; 108 expected.insert(std::make_pair(ASCIIToUTF16("abc"), base::string16())); 109 expected.insert(std::make_pair(ASCIIToUTF16("de"), ASCIIToUTF16("1"))); 110 expected.insert(std::make_pair(ASCIIToUTF16("f"), ASCIIToUTF16("23"))); 111 EXPECT_EQ(expected, result); 112 } 113 114 TEST(CustomDataHelperTest, BadReadTypes) { 115 // ReadCustomDataTypes makes the additional guarantee that the contents of the 116 // result vector will not change if the input is malformed. 117 std::vector<base::string16> expected; 118 expected.push_back(ASCIIToUTF16("abc")); 119 expected.push_back(ASCIIToUTF16("de")); 120 expected.push_back(ASCIIToUTF16("f")); 121 122 Pickle malformed; 123 malformed.WriteUInt64(1000); 124 malformed.WriteString16(ASCIIToUTF16("hello")); 125 malformed.WriteString16(ASCIIToUTF16("world")); 126 std::vector<base::string16> actual(expected); 127 ReadCustomDataTypes(malformed.data(), malformed.size(), &actual); 128 EXPECT_EQ(expected, actual); 129 130 Pickle malformed2; 131 malformed2.WriteUInt64(1); 132 malformed2.WriteString16(ASCIIToUTF16("hello")); 133 std::vector<base::string16> actual2(expected); 134 ReadCustomDataTypes(malformed2.data(), malformed2.size(), &actual2); 135 EXPECT_EQ(expected, actual2); 136 } 137 138 TEST(CustomDataHelperTest, BadPickle) { 139 base::string16 result_data; 140 std::map<base::string16, base::string16> result_map; 141 142 Pickle malformed; 143 malformed.WriteUInt64(1000); 144 malformed.WriteString16(ASCIIToUTF16("hello")); 145 malformed.WriteString16(ASCIIToUTF16("world")); 146 147 ReadCustomDataForType(malformed.data(), 148 malformed.size(), 149 ASCIIToUTF16("f"), 150 &result_data); 151 ReadCustomDataIntoMap(malformed.data(), malformed.size(), &result_map); 152 EXPECT_EQ(0u, result_data.size()); 153 EXPECT_EQ(0u, result_map.size()); 154 155 Pickle malformed2; 156 malformed2.WriteUInt64(1); 157 malformed2.WriteString16(ASCIIToUTF16("hello")); 158 159 ReadCustomDataForType(malformed2.data(), 160 malformed2.size(), 161 ASCIIToUTF16("f"), 162 &result_data); 163 ReadCustomDataIntoMap(malformed2.data(), malformed2.size(), &result_map); 164 EXPECT_EQ(0u, result_data.size()); 165 EXPECT_EQ(0u, result_map.size()); 166 } 167 168 } // namespace 169 170 } // namespace ui 171