| OLD | NEW |
| (Empty) |
| 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 <string> | |
| 6 | |
| 7 #include "base/pickle.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "third_party/WebKit/public/platform/WebHTTPBody.h" | |
| 11 #include "third_party/WebKit/public/platform/WebPoint.h" | |
| 12 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 13 #include "ui/gfx/screen.h" | |
| 14 #include "webkit/base/file_path_string_conversions.h" | |
| 15 #include "webkit/glue/glue_serialize_deprecated.h" | |
| 16 | |
| 17 using WebKit::WebData; | |
| 18 using WebKit::WebHistoryItem; | |
| 19 using WebKit::WebHTTPBody; | |
| 20 using WebKit::WebPoint; | |
| 21 using WebKit::WebString; | |
| 22 using WebKit::WebUChar; | |
| 23 using WebKit::WebVector; | |
| 24 | |
| 25 namespace { | |
| 26 class GlueSerializeTest : public testing::Test { | |
| 27 public: | |
| 28 // Makes a FormData with some random data. | |
| 29 WebHTTPBody MakeFormData() { | |
| 30 WebHTTPBody http_body; | |
| 31 http_body.initialize(); | |
| 32 | |
| 33 const char d1[] = "first data block"; | |
| 34 http_body.appendData(WebData(d1, sizeof(d1)-1)); | |
| 35 | |
| 36 http_body.appendFile(WebString::fromUTF8("file.txt")); | |
| 37 | |
| 38 const char d2[] = "data the second"; | |
| 39 http_body.appendData(WebData(d2, sizeof(d2)-1)); | |
| 40 | |
| 41 return http_body; | |
| 42 } | |
| 43 | |
| 44 WebHTTPBody MakeFormDataWithPasswords() { | |
| 45 WebHTTPBody http_body = MakeFormData(); | |
| 46 http_body.setContainsPasswordData(true); | |
| 47 return http_body; | |
| 48 } | |
| 49 | |
| 50 // Constructs a HistoryItem with some random data and an optional child. | |
| 51 WebHistoryItem MakeHistoryItem(bool with_form_data, bool pregnant) { | |
| 52 WebHistoryItem item; | |
| 53 item.initialize(); | |
| 54 | |
| 55 item.setURLString(WebString::fromUTF8("urlString")); | |
| 56 item.setOriginalURLString(WebString::fromUTF8("originalURLString")); | |
| 57 item.setTarget(WebString::fromUTF8("target")); | |
| 58 item.setParent(WebString::fromUTF8("parent")); | |
| 59 item.setTitle(WebString::fromUTF8("title")); | |
| 60 item.setAlternateTitle(WebString::fromUTF8("alternateTitle")); | |
| 61 item.setLastVisitedTime(13.37); | |
| 62 item.setScrollOffset(WebPoint(42, -42)); | |
| 63 item.setIsTargetItem(true); | |
| 64 item.setVisitCount(42*42); | |
| 65 item.setPageScaleFactor(2.0f); | |
| 66 item.setItemSequenceNumber(123); | |
| 67 item.setDocumentSequenceNumber(456); | |
| 68 | |
| 69 WebVector<WebString> document_state(size_t(3)); | |
| 70 document_state[0] = WebString::fromUTF8("state1"); | |
| 71 document_state[1] = WebString::fromUTF8("state2"); | |
| 72 document_state[2] = WebString::fromUTF8("state AWESOME"); | |
| 73 item.setDocumentState(document_state); | |
| 74 | |
| 75 // Form Data | |
| 76 if (with_form_data) { | |
| 77 item.setHTTPBody(MakeFormData()); | |
| 78 item.setHTTPContentType(WebString::fromUTF8("formContentType")); | |
| 79 } | |
| 80 | |
| 81 // Setting the FormInfo causes the referrer to be set, so we set the | |
| 82 // referrer after setting the form info. | |
| 83 item.setReferrer(WebString::fromUTF8("referrer")); | |
| 84 | |
| 85 // Children | |
| 86 if (pregnant) | |
| 87 item.appendToChildren(MakeHistoryItem(false, false)); | |
| 88 | |
| 89 return item; | |
| 90 } | |
| 91 | |
| 92 WebHistoryItem MakeHistoryItemWithPasswordData(bool pregnant) { | |
| 93 WebHistoryItem item = MakeHistoryItem(false, pregnant); | |
| 94 item.setHTTPBody(MakeFormDataWithPasswords()); | |
| 95 item.setHTTPContentType(WebString::fromUTF8("formContentType")); | |
| 96 return item; | |
| 97 } | |
| 98 | |
| 99 // Checks that a == b. | |
| 100 void HistoryItemExpectEqual(const WebHistoryItem& a, | |
| 101 const WebHistoryItem& b, | |
| 102 int version) { | |
| 103 HistoryItemExpectBaseDataEqual(a, b, version); | |
| 104 HistoryItemExpectFormDataEqual(a, b); | |
| 105 HistoryItemExpectChildrenEqual(a, b); | |
| 106 } | |
| 107 | |
| 108 void HistoryItemExpectBaseDataEqual(const WebHistoryItem& a, | |
| 109 const WebHistoryItem& b, | |
| 110 int version) { | |
| 111 float expectedPageScaleFactor = a.pageScaleFactor(); | |
| 112 WebPoint expectedScrollOffset = a.scrollOffset(); | |
| 113 #if defined(OS_ANDROID) | |
| 114 if (version == 11) { | |
| 115 expectedScrollOffset.x /= a.pageScaleFactor(); | |
| 116 expectedScrollOffset.y /= a.pageScaleFactor(); | |
| 117 expectedPageScaleFactor /= gfx::Screen::GetNativeScreen() | |
| 118 ->GetPrimaryDisplay().device_scale_factor(); | |
| 119 } | |
| 120 #endif | |
| 121 EXPECT_EQ(base::string16(a.urlString()), base::string16(b.urlString())); | |
| 122 EXPECT_EQ(base::string16(a.originalURLString()), | |
| 123 base::string16(b.originalURLString())); | |
| 124 EXPECT_EQ(base::string16(a.target()), base::string16(b.target())); | |
| 125 EXPECT_EQ(base::string16(a.parent()), base::string16(b.parent())); | |
| 126 EXPECT_EQ(base::string16(a.title()), base::string16(b.title())); | |
| 127 EXPECT_EQ(base::string16(a.alternateTitle()), | |
| 128 base::string16(b.alternateTitle())); | |
| 129 EXPECT_EQ(a.lastVisitedTime(), b.lastVisitedTime()); | |
| 130 EXPECT_EQ(expectedScrollOffset, b.scrollOffset()); | |
| 131 EXPECT_EQ(a.isTargetItem(), b.isTargetItem()); | |
| 132 EXPECT_EQ(a.visitCount(), b.visitCount()); | |
| 133 EXPECT_EQ(base::string16(a.referrer()), base::string16(b.referrer())); | |
| 134 if (version >= 11) | |
| 135 EXPECT_EQ(expectedPageScaleFactor, b.pageScaleFactor()); | |
| 136 if (version >= 9) | |
| 137 EXPECT_EQ(a.itemSequenceNumber(), b.itemSequenceNumber()); | |
| 138 if (version >= 6) | |
| 139 EXPECT_EQ(a.documentSequenceNumber(), b.documentSequenceNumber()); | |
| 140 | |
| 141 const WebVector<WebString>& a_docstate = a.documentState(); | |
| 142 const WebVector<WebString>& b_docstate = b.documentState(); | |
| 143 EXPECT_EQ(a_docstate.size(), b_docstate.size()); | |
| 144 for (size_t i = 0, c = a_docstate.size(); i < c; ++i) | |
| 145 EXPECT_EQ(base::string16(a_docstate[i]), base::string16(b_docstate[i])); | |
| 146 } | |
| 147 | |
| 148 void HistoryItemExpectFormDataEqual(const WebHistoryItem& a, | |
| 149 const WebHistoryItem& b) { | |
| 150 const WebHTTPBody& a_body = a.httpBody(); | |
| 151 const WebHTTPBody& b_body = b.httpBody(); | |
| 152 EXPECT_EQ(!a_body.isNull(), !b_body.isNull()); | |
| 153 if (!a_body.isNull() && !b_body.isNull()) { | |
| 154 EXPECT_EQ(a_body.containsPasswordData(), b_body.containsPasswordData()); | |
| 155 EXPECT_EQ(a_body.elementCount(), b_body.elementCount()); | |
| 156 WebHTTPBody::Element a_elem, b_elem; | |
| 157 for (size_t i = 0; a_body.elementAt(i, a_elem) && | |
| 158 b_body.elementAt(i, b_elem); ++i) { | |
| 159 EXPECT_EQ(a_elem.type, b_elem.type); | |
| 160 if (a_elem.type == WebHTTPBody::Element::TypeData) { | |
| 161 EXPECT_EQ(std::string(a_elem.data.data(), a_elem.data.size()), | |
| 162 std::string(b_elem.data.data(), b_elem.data.size())); | |
| 163 } else { | |
| 164 EXPECT_EQ(base::string16(a_elem.filePath), | |
| 165 base::string16(b_elem.filePath)); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 EXPECT_EQ(base::string16(a.httpContentType()), | |
| 170 base::string16(b.httpContentType())); | |
| 171 } | |
| 172 | |
| 173 void HistoryItemExpectChildrenEqual(const WebHistoryItem& a, | |
| 174 const WebHistoryItem& b) { | |
| 175 const WebVector<WebHistoryItem>& a_children = a.children(); | |
| 176 const WebVector<WebHistoryItem>& b_children = b.children(); | |
| 177 EXPECT_EQ(a_children.size(), b_children.size()); | |
| 178 for (size_t i = 0, c = a_children.size(); i < c; ++i) | |
| 179 HistoryItemExpectEqual(a_children[i], b_children[i], | |
| 180 webkit_glue::HistoryItemCurrentVersion()); | |
| 181 } | |
| 182 }; | |
| 183 | |
| 184 // Test old versions of serialized data to ensure that newer versions of code | |
| 185 // can still read history items written by previous versions. | |
| 186 TEST_F(GlueSerializeTest, BackwardsCompatibleTest) { | |
| 187 const WebHistoryItem& item = MakeHistoryItem(false, false); | |
| 188 | |
| 189 // Make sure current version can read all previous versions. | |
| 190 for (int i = 1; i < webkit_glue::HistoryItemCurrentVersion(); i++) { | |
| 191 std::string serialized_item; | |
| 192 webkit_glue::HistoryItemToVersionedString(item, i, &serialized_item); | |
| 193 const WebHistoryItem& deserialized_item = | |
| 194 webkit_glue::HistoryItemFromString(serialized_item); | |
| 195 ASSERT_FALSE(item.isNull()); | |
| 196 ASSERT_FALSE(deserialized_item.isNull()); | |
| 197 HistoryItemExpectEqual(item, deserialized_item, i); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 // Makes sure that a HistoryItem remains intact after being serialized and | |
| 202 // deserialized. | |
| 203 TEST_F(GlueSerializeTest, HistoryItemSerializeTest) { | |
| 204 const WebHistoryItem& item = MakeHistoryItem(true, true); | |
| 205 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); | |
| 206 const WebHistoryItem& deserialized_item = | |
| 207 webkit_glue::HistoryItemFromString(serialized_item); | |
| 208 | |
| 209 ASSERT_FALSE(item.isNull()); | |
| 210 ASSERT_FALSE(deserialized_item.isNull()); | |
| 211 HistoryItemExpectEqual(item, deserialized_item, | |
| 212 webkit_glue::HistoryItemCurrentVersion()); | |
| 213 } | |
| 214 | |
| 215 // Checks that broken messages don't take out our process. | |
| 216 TEST_F(GlueSerializeTest, BadMessagesTest) { | |
| 217 { | |
| 218 Pickle p; | |
| 219 // Version 1 | |
| 220 p.WriteInt(1); | |
| 221 // Empty strings. | |
| 222 for (int i = 0; i < 6; ++i) | |
| 223 p.WriteInt(-1); | |
| 224 // Bad real number. | |
| 225 p.WriteInt(-1); | |
| 226 std::string s(static_cast<const char*>(p.data()), p.size()); | |
| 227 webkit_glue::HistoryItemFromString(s); | |
| 228 } | |
| 229 { | |
| 230 double d = 0; | |
| 231 Pickle p; | |
| 232 // Version 1 | |
| 233 p.WriteInt(1); | |
| 234 // Empty strings. | |
| 235 for (int i = 0; i < 6; ++i) | |
| 236 p.WriteInt(-1); | |
| 237 // More misc fields. | |
| 238 p.WriteData(reinterpret_cast<const char*>(&d), sizeof(d)); | |
| 239 p.WriteInt(1); | |
| 240 p.WriteInt(1); | |
| 241 p.WriteInt(0); | |
| 242 p.WriteInt(0); | |
| 243 p.WriteInt(-1); | |
| 244 p.WriteInt(0); | |
| 245 // WebForm | |
| 246 p.WriteInt(1); | |
| 247 p.WriteInt(WebHTTPBody::Element::TypeData); | |
| 248 std::string s(static_cast<const char*>(p.data()), p.size()); | |
| 249 webkit_glue::HistoryItemFromString(s); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 TEST_F(GlueSerializeTest, FilePathsFromHistoryState) { | |
| 254 WebHistoryItem item = MakeHistoryItem(false, true); | |
| 255 | |
| 256 // Append file paths to item. | |
| 257 base::FilePath file_path1(FILE_PATH_LITERAL("file.txt")); | |
| 258 base::FilePath file_path2(FILE_PATH_LITERAL("another_file")); | |
| 259 WebHTTPBody http_body; | |
| 260 http_body.initialize(); | |
| 261 http_body.appendFile(webkit_base::FilePathToWebString(file_path1)); | |
| 262 http_body.appendFile(webkit_base::FilePathToWebString(file_path2)); | |
| 263 item.setHTTPBody(http_body); | |
| 264 | |
| 265 std::string serialized_item = webkit_glue::HistoryItemToString(item); | |
| 266 const std::vector<base::FilePath>& file_paths = | |
| 267 webkit_glue::FilePathsFromHistoryState(serialized_item); | |
| 268 ASSERT_EQ(2U, file_paths.size()); | |
| 269 EXPECT_EQ(file_path1, file_paths[0]); | |
| 270 EXPECT_EQ(file_path2, file_paths[1]); | |
| 271 } | |
| 272 | |
| 273 // Makes sure that a HistoryItem containing password data remains intact after | |
| 274 // being serialized and deserialized. | |
| 275 TEST_F(GlueSerializeTest, HistoryItemWithPasswordsSerializeTest) { | |
| 276 const WebHistoryItem& item = MakeHistoryItemWithPasswordData(true); | |
| 277 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); | |
| 278 const WebHistoryItem& deserialized_item = | |
| 279 webkit_glue::HistoryItemFromString(serialized_item); | |
| 280 | |
| 281 ASSERT_FALSE(item.isNull()); | |
| 282 ASSERT_FALSE(deserialized_item.isNull()); | |
| 283 HistoryItemExpectEqual(item, deserialized_item, | |
| 284 webkit_glue::HistoryItemCurrentVersion()); | |
| 285 } | |
| 286 | |
| 287 TEST_F(GlueSerializeTest, RemovePasswordData) { | |
| 288 const WebHistoryItem& item1 = MakeHistoryItemWithPasswordData(true); | |
| 289 std::string serialized_item = webkit_glue::HistoryItemToString(item1); | |
| 290 serialized_item = | |
| 291 webkit_glue::RemovePasswordDataFromHistoryState(serialized_item); | |
| 292 const WebHistoryItem& item2 = | |
| 293 webkit_glue::HistoryItemFromString(serialized_item); | |
| 294 | |
| 295 ASSERT_FALSE(item1.isNull()); | |
| 296 ASSERT_FALSE(item2.isNull()); | |
| 297 | |
| 298 HistoryItemExpectBaseDataEqual(item1, item2, | |
| 299 webkit_glue::HistoryItemCurrentVersion()); | |
| 300 HistoryItemExpectChildrenEqual(item1, item2); | |
| 301 | |
| 302 // Form data was removed, but the identifier was kept. | |
| 303 const WebHTTPBody& body1 = item1.httpBody(); | |
| 304 const WebHTTPBody& body2 = item2.httpBody(); | |
| 305 EXPECT_FALSE(body1.isNull()); | |
| 306 EXPECT_FALSE(body2.isNull()); | |
| 307 EXPECT_GT(body1.elementCount(), 0U); | |
| 308 EXPECT_EQ(0U, body2.elementCount()); | |
| 309 EXPECT_EQ(body1.identifier(), body2.identifier()); | |
| 310 } | |
| 311 | |
| 312 TEST_F(GlueSerializeTest, RemovePasswordDataWithNoPasswordData) { | |
| 313 const WebHistoryItem& item1 = MakeHistoryItem(true, true); | |
| 314 std::string serialized_item = webkit_glue::HistoryItemToString(item1); | |
| 315 serialized_item = | |
| 316 webkit_glue::RemovePasswordDataFromHistoryState(serialized_item); | |
| 317 const WebHistoryItem& item2 = | |
| 318 webkit_glue::HistoryItemFromString(serialized_item); | |
| 319 | |
| 320 ASSERT_FALSE(item1.isNull()); | |
| 321 ASSERT_FALSE(item2.isNull()); | |
| 322 | |
| 323 // Form data was not removed. | |
| 324 HistoryItemExpectEqual(item1, item2, | |
| 325 webkit_glue::HistoryItemCurrentVersion()); | |
| 326 } | |
| 327 | |
| 328 } // namespace | |
| OLD | NEW |