1 // Copyright (c) 2011 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.
7 #include "base/pickle.h"
8 #include "base/string_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebHTTPBody.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPoint.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
13 #include "webkit/glue/glue_serialize.h"
14 #include "webkit/glue/web_io_operators.h"
16 using WebKit::WebData
;
17 using WebKit::WebHistoryItem
;
18 using WebKit::WebHTTPBody
;
19 using WebKit::WebPoint
;
20 using WebKit::WebString
;
21 using WebKit::WebUChar
;
22 using WebKit::WebVector
;
25 class GlueSerializeTest
: public testing::Test
{
27 // Makes a FormData with some random data.
28 WebHTTPBody
MakeFormData() {
29 WebHTTPBody http_body
;
30 http_body
.initialize();
32 const char d1
[] = "first data block";
33 http_body
.appendData(WebData(d1
, sizeof(d1
)-1));
35 http_body
.appendFile(WebString::fromUTF8("file.txt"));
37 const char d2
[] = "data the second";
38 http_body
.appendData(WebData(d2
, sizeof(d2
)-1));
43 // Constructs a HistoryItem with some random data and an optional child.
44 WebHistoryItem
MakeHistoryItem(bool with_form_data
, bool pregnant
) {
48 item
.setURLString(WebString::fromUTF8("urlString"));
49 item
.setOriginalURLString(WebString::fromUTF8("originalURLString"));
50 item
.setTarget(WebString::fromUTF8("target"));
51 item
.setParent(WebString::fromUTF8("parent"));
52 item
.setTitle(WebString::fromUTF8("title"));
53 item
.setAlternateTitle(WebString::fromUTF8("alternateTitle"));
54 item
.setLastVisitedTime(13.37);
55 item
.setScrollOffset(WebPoint(42, -42));
56 item
.setIsTargetItem(true);
57 item
.setVisitCount(42*42);
59 WebVector
<WebString
> document_state(size_t(3));
60 document_state
[0] = WebString::fromUTF8("state1");
61 document_state
[1] = WebString::fromUTF8("state2");
62 document_state
[2] = WebString::fromUTF8("state AWESOME");
63 item
.setDocumentState(document_state
);
64 item
.setPageScaleFactor(1.0f
);
68 item
.setHTTPBody(MakeFormData());
69 item
.setHTTPContentType(WebString::fromUTF8("formContentType"));
72 // Setting the FormInfo causes the referrer to be set, so we set the
73 // referrer after setting the form info.
74 item
.setReferrer(WebString::fromUTF8("referrer"));
78 item
.appendToChildren(MakeHistoryItem(false, false));
83 // Checks that a == b.
84 void HistoryItemExpectEqual(const WebHistoryItem
& a
,
85 const WebHistoryItem
& b
) {
86 EXPECT_EQ(string16(a
.urlString()), string16(b
.urlString()));
87 EXPECT_EQ(string16(a
.originalURLString()), string16(b
.originalURLString()));
88 EXPECT_EQ(string16(a
.target()), string16(b
.target()));
89 EXPECT_EQ(string16(a
.parent()), string16(b
.parent()));
90 EXPECT_EQ(string16(a
.title()), string16(b
.title()));
91 EXPECT_EQ(string16(a
.alternateTitle()), string16(b
.alternateTitle()));
92 EXPECT_EQ(a
.lastVisitedTime(), b
.lastVisitedTime());
93 EXPECT_EQ(a
.scrollOffset(), b
.scrollOffset());
94 EXPECT_EQ(a
.isTargetItem(), b
.isTargetItem());
95 EXPECT_EQ(a
.visitCount(), b
.visitCount());
96 EXPECT_EQ(string16(a
.referrer()), string16(b
.referrer()));
97 EXPECT_EQ(a
.pageScaleFactor(), b
.pageScaleFactor());
99 const WebVector
<WebString
>& a_docstate
= a
.documentState();
100 const WebVector
<WebString
>& b_docstate
= b
.documentState();
101 EXPECT_EQ(a_docstate
.size(), b_docstate
.size());
102 for (size_t i
= 0, c
= a_docstate
.size(); i
< c
; ++i
)
103 EXPECT_EQ(string16(a_docstate
[i
]), string16(b_docstate
[i
]));
106 const WebHTTPBody
& a_body
= a
.httpBody();
107 const WebHTTPBody
& b_body
= b
.httpBody();
108 EXPECT_EQ(!a_body
.isNull(), !b_body
.isNull());
109 if (!a_body
.isNull() && !b_body
.isNull()) {
110 EXPECT_EQ(a_body
.elementCount(), b_body
.elementCount());
111 WebHTTPBody::Element a_elem
, b_elem
;
112 for (size_t i
= 0; a_body
.elementAt(i
, a_elem
) &&
113 b_body
.elementAt(i
, b_elem
); ++i
) {
114 EXPECT_EQ(a_elem
.type
, b_elem
.type
);
115 if (a_elem
.type
== WebHTTPBody::Element::TypeData
) {
116 EXPECT_EQ(std::string(a_elem
.data
.data(), a_elem
.data
.size()),
117 std::string(b_elem
.data
.data(), b_elem
.data
.size()));
119 EXPECT_EQ(string16(a_elem
.filePath
), string16(b_elem
.filePath
));
123 EXPECT_EQ(string16(a
.httpContentType()), string16(b
.httpContentType()));
126 const WebVector
<WebHistoryItem
>& a_children
= a
.children();
127 const WebVector
<WebHistoryItem
>& b_children
= b
.children();
128 EXPECT_EQ(a_children
.size(), b_children
.size());
129 for (size_t i
= 0, c
= a_children
.size(); i
< c
; ++i
)
130 HistoryItemExpectEqual(a_children
[i
], b_children
[i
]);
134 // Test old versions of serialized data to ensure that newer versions of code
135 // can still read history items written by previous versions.
136 TEST_F(GlueSerializeTest
, BackwardsCompatibleTest
) {
137 const WebHistoryItem
& item
= MakeHistoryItem(false, false);
139 // Make sure version 11 (current version) can read versions 1 through 10.
140 for (int i
= 1; i
<= 10; i
++) {
141 std::string serialized_item
;
142 webkit_glue::HistoryItemToVersionedString(item
, i
, &serialized_item
);
143 const WebHistoryItem
& deserialized_item
=
144 webkit_glue::HistoryItemFromString(serialized_item
);
145 ASSERT_FALSE(item
.isNull());
146 ASSERT_FALSE(deserialized_item
.isNull());
147 HistoryItemExpectEqual(item
, deserialized_item
);
151 // Makes sure that a HistoryItem remains intact after being serialized and
153 TEST_F(GlueSerializeTest
, HistoryItemSerializeTest
) {
154 const WebHistoryItem
& item
= MakeHistoryItem(true, true);
155 const std::string
& serialized_item
= webkit_glue::HistoryItemToString(item
);
156 const WebHistoryItem
& deserialized_item
=
157 webkit_glue::HistoryItemFromString(serialized_item
);
159 ASSERT_FALSE(item
.isNull());
160 ASSERT_FALSE(deserialized_item
.isNull());
161 HistoryItemExpectEqual(item
, deserialized_item
);
164 // Checks that broken messages don't take out our process.
165 TEST_F(GlueSerializeTest
, BadMessagesTest
) {
171 for (int i
= 0; i
< 6; ++i
)
175 std::string
s(static_cast<const char*>(p
.data()), p
.size());
176 webkit_glue::HistoryItemFromString(s
);
184 for (int i
= 0; i
< 6; ++i
)
187 p
.WriteData(reinterpret_cast<const char*>(&d
), sizeof(d
));
196 p
.WriteInt(WebHTTPBody::Element::TypeData
);
197 std::string
s(static_cast<const char*>(p
.data()), p
.size());
198 webkit_glue::HistoryItemFromString(s
);