| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "webkit/api/public/WebHTTPBody.h" | 10 #include "webkit/api/public/WebHTTPBody.h" |
| 11 #include "webkit/api/public/WebPoint.h" | 11 #include "webkit/api/public/WebPoint.h" |
| 12 #include "webkit/api/public/WebVector.h" | 12 #include "webkit/api/public/WebVector.h" |
| 13 #include "webkit/glue/glue_serialize.h" | 13 #include "webkit/glue/glue_serialize.h" |
| 14 | 14 |
| 15 using namespace std; | |
| 16 using namespace webkit_glue; | |
| 17 | |
| 18 using WebKit::WebData; | 15 using WebKit::WebData; |
| 19 using WebKit::WebHistoryItem; | 16 using WebKit::WebHistoryItem; |
| 20 using WebKit::WebHTTPBody; | 17 using WebKit::WebHTTPBody; |
| 21 using WebKit::WebPoint; | 18 using WebKit::WebPoint; |
| 22 using WebKit::WebString; | 19 using WebKit::WebString; |
| 23 using WebKit::WebUChar; | 20 using WebKit::WebUChar; |
| 24 using WebKit::WebVector; | 21 using WebKit::WebVector; |
| 25 | 22 |
| 26 namespace { | 23 namespace { |
| 27 class GlueSerializeTest : public testing::Test { | 24 class GlueSerializeTest : public testing::Test { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 const WebHTTPBody& a_body = a.httpBody(); | 103 const WebHTTPBody& a_body = a.httpBody(); |
| 107 const WebHTTPBody& b_body = b.httpBody(); | 104 const WebHTTPBody& b_body = b.httpBody(); |
| 108 EXPECT_EQ(!a_body.isNull(), !b_body.isNull()); | 105 EXPECT_EQ(!a_body.isNull(), !b_body.isNull()); |
| 109 if (!a_body.isNull() && !b_body.isNull()) { | 106 if (!a_body.isNull() && !b_body.isNull()) { |
| 110 EXPECT_EQ(a_body.elementCount(), b_body.elementCount()); | 107 EXPECT_EQ(a_body.elementCount(), b_body.elementCount()); |
| 111 WebHTTPBody::Element a_elem, b_elem; | 108 WebHTTPBody::Element a_elem, b_elem; |
| 112 for (size_t i = 0; a_body.elementAt(i, a_elem) && | 109 for (size_t i = 0; a_body.elementAt(i, a_elem) && |
| 113 b_body.elementAt(i, b_elem); ++i) { | 110 b_body.elementAt(i, b_elem); ++i) { |
| 114 EXPECT_EQ(a_elem.type, b_elem.type); | 111 EXPECT_EQ(a_elem.type, b_elem.type); |
| 115 if (a_elem.type == WebHTTPBody::Element::TypeData) { | 112 if (a_elem.type == WebHTTPBody::Element::TypeData) { |
| 116 EXPECT_EQ(string(a_elem.data.data(), a_elem.data.size()), | 113 EXPECT_EQ(std::string(a_elem.data.data(), a_elem.data.size()), |
| 117 string(b_elem.data.data(), b_elem.data.size())); | 114 std::string(b_elem.data.data(), b_elem.data.size())); |
| 118 } else { | 115 } else { |
| 119 EXPECT_EQ(string16(a_elem.filePath), string16(b_elem.filePath)); | 116 EXPECT_EQ(string16(a_elem.filePath), string16(b_elem.filePath)); |
| 120 } | 117 } |
| 121 } | 118 } |
| 122 } | 119 } |
| 123 EXPECT_EQ(string16(a.httpContentType()), string16(b.httpContentType())); | 120 EXPECT_EQ(string16(a.httpContentType()), string16(b.httpContentType())); |
| 124 | 121 |
| 125 // Children | 122 // Children |
| 126 const WebVector<WebHistoryItem>& a_children = a.children(); | 123 const WebVector<WebHistoryItem>& a_children = a.children(); |
| 127 const WebVector<WebHistoryItem>& b_children = b.children(); | 124 const WebVector<WebHistoryItem>& b_children = b.children(); |
| 128 EXPECT_EQ(a_children.size(), b_children.size()); | 125 EXPECT_EQ(a_children.size(), b_children.size()); |
| 129 for (size_t i = 0, c = a_children.size(); i < c; ++i) | 126 for (size_t i = 0, c = a_children.size(); i < c; ++i) |
| 130 HistoryItemExpectEqual(a_children[i], b_children[i]); | 127 HistoryItemExpectEqual(a_children[i], b_children[i]); |
| 131 } | 128 } |
| 132 }; | 129 }; |
| 133 | 130 |
| 134 // Test old versions of serialized data to ensure that newer versions of code | 131 // Test old versions of serialized data to ensure that newer versions of code |
| 135 // can still read history items written by previous versions. | 132 // can still read history items written by previous versions. |
| 136 TEST_F(GlueSerializeTest, BackwardsCompatibleTest) { | 133 TEST_F(GlueSerializeTest, BackwardsCompatibleTest) { |
| 137 const WebHistoryItem& item = MakeHistoryItem(false, false); | 134 const WebHistoryItem& item = MakeHistoryItem(false, false); |
| 138 | 135 |
| 139 // Make sure version 3 (current version) can read versions 1 and 2. | 136 // Make sure version 3 (current version) can read versions 1 and 2. |
| 140 for (int i = 1; i <= 2; i++) { | 137 for (int i = 1; i <= 2; i++) { |
| 141 string serialized_item; | 138 std::string serialized_item; |
| 142 HistoryItemToVersionedString(item, i, &serialized_item); | 139 webkit_glue::HistoryItemToVersionedString(item, i, &serialized_item); |
| 143 const WebHistoryItem& deserialized_item = | 140 const WebHistoryItem& deserialized_item = |
| 144 HistoryItemFromString(serialized_item); | 141 webkit_glue::HistoryItemFromString(serialized_item); |
| 145 ASSERT_FALSE(item.isNull()); | 142 ASSERT_FALSE(item.isNull()); |
| 146 ASSERT_FALSE(deserialized_item.isNull()); | 143 ASSERT_FALSE(deserialized_item.isNull()); |
| 147 HistoryItemExpectEqual(item, deserialized_item); | 144 HistoryItemExpectEqual(item, deserialized_item); |
| 148 } | 145 } |
| 149 } | 146 } |
| 150 | 147 |
| 151 // Makes sure that a HistoryItem remains intact after being serialized and | 148 // Makes sure that a HistoryItem remains intact after being serialized and |
| 152 // deserialized. | 149 // deserialized. |
| 153 TEST_F(GlueSerializeTest, HistoryItemSerializeTest) { | 150 TEST_F(GlueSerializeTest, HistoryItemSerializeTest) { |
| 154 const WebHistoryItem& item = MakeHistoryItem(true, true); | 151 const WebHistoryItem& item = MakeHistoryItem(true, true); |
| 155 const string& serialized_item = HistoryItemToString(item); | 152 const std::string& serialized_item = webkit_glue::HistoryItemToString(item); |
| 156 const WebHistoryItem& deserialized_item = | 153 const WebHistoryItem& deserialized_item = |
| 157 HistoryItemFromString(serialized_item); | 154 webkit_glue::HistoryItemFromString(serialized_item); |
| 158 | 155 |
| 159 ASSERT_FALSE(item.isNull()); | 156 ASSERT_FALSE(item.isNull()); |
| 160 ASSERT_FALSE(deserialized_item.isNull()); | 157 ASSERT_FALSE(deserialized_item.isNull()); |
| 161 HistoryItemExpectEqual(item, deserialized_item); | 158 HistoryItemExpectEqual(item, deserialized_item); |
| 162 } | 159 } |
| 163 | 160 |
| 164 // Checks that broken messages don't take out our process. | 161 // Checks that broken messages don't take out our process. |
| 165 TEST_F(GlueSerializeTest, BadMessagesTest) { | 162 TEST_F(GlueSerializeTest, BadMessagesTest) { |
| 166 { | 163 { |
| 167 Pickle p; | 164 Pickle p; |
| 168 // Version 1 | 165 // Version 1 |
| 169 p.WriteInt(1); | 166 p.WriteInt(1); |
| 170 // Empty strings. | 167 // Empty strings. |
| 171 for (int i = 0; i < 6; ++i) | 168 for (int i = 0; i < 6; ++i) |
| 172 p.WriteInt(-1); | 169 p.WriteInt(-1); |
| 173 // Bad real number. | 170 // Bad real number. |
| 174 p.WriteInt(-1); | 171 p.WriteInt(-1); |
| 175 std::string s(static_cast<const char*>(p.data()), p.size()); | 172 std::string s(static_cast<const char*>(p.data()), p.size()); |
| 176 HistoryItemFromString(s); | 173 webkit_glue::HistoryItemFromString(s); |
| 177 } | 174 } |
| 178 { | 175 { |
| 179 double d = 0; | 176 double d = 0; |
| 180 Pickle p; | 177 Pickle p; |
| 181 // Version 1 | 178 // Version 1 |
| 182 p.WriteInt(1); | 179 p.WriteInt(1); |
| 183 // Empty strings. | 180 // Empty strings. |
| 184 for (int i = 0; i < 6; ++i) | 181 for (int i = 0; i < 6; ++i) |
| 185 p.WriteInt(-1); | 182 p.WriteInt(-1); |
| 186 // More misc fields. | 183 // More misc fields. |
| 187 p.WriteData(reinterpret_cast<const char*>(&d), sizeof(d)); | 184 p.WriteData(reinterpret_cast<const char*>(&d), sizeof(d)); |
| 188 p.WriteInt(1); | 185 p.WriteInt(1); |
| 189 p.WriteInt(1); | 186 p.WriteInt(1); |
| 190 p.WriteInt(0); | 187 p.WriteInt(0); |
| 191 p.WriteInt(0); | 188 p.WriteInt(0); |
| 192 p.WriteInt(-1); | 189 p.WriteInt(-1); |
| 193 p.WriteInt(0); | 190 p.WriteInt(0); |
| 194 // WebForm | 191 // WebForm |
| 195 p.WriteInt(1); | 192 p.WriteInt(1); |
| 196 p.WriteInt(WebHTTPBody::Element::TypeData); | 193 p.WriteInt(WebHTTPBody::Element::TypeData); |
| 197 std::string s(static_cast<const char*>(p.data()), p.size()); | 194 std::string s(static_cast<const char*>(p.data()), p.size()); |
| 198 HistoryItemFromString(s); | 195 webkit_glue::HistoryItemFromString(s); |
| 199 } | 196 } |
| 200 } | 197 } |
| 201 | 198 |
| 202 | 199 } // namespace |
| 203 } // namespace | |
| OLD | NEW |