1 // Copyright 2013 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/base64.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/pickle.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/common/page_state_serialization.h"
15 #include "content/public/common/content_paths.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 inline bool isnan(double num
) { return !!_isnan(num
); }
25 base::NullableString16
NS16(const char* s
) {
26 return s
? base::NullableString16(base::ASCIIToUTF16(s
), false) :
27 base::NullableString16();
30 //-----------------------------------------------------------------------------
33 void ExpectEquality(const T
& a
, const T
& b
) {
38 void ExpectEquality(const std::vector
<T
>& a
, const std::vector
<T
>& b
) {
39 EXPECT_EQ(a
.size(), b
.size());
40 for (size_t i
= 0; i
< std::min(a
.size(), b
.size()); ++i
)
41 ExpectEquality(a
[i
], b
[i
]);
45 void ExpectEquality(const ExplodedHttpBodyElement
& a
,
46 const ExplodedHttpBodyElement
& b
) {
47 EXPECT_EQ(a
.type
, b
.type
);
48 EXPECT_EQ(a
.data
, b
.data
);
49 EXPECT_EQ(a
.file_path
, b
.file_path
);
50 EXPECT_EQ(a
.filesystem_url
, b
.filesystem_url
);
51 EXPECT_EQ(a
.file_start
, b
.file_start
);
52 EXPECT_EQ(a
.file_length
, b
.file_length
);
53 if (!(isnan(a
.file_modification_time
) && isnan(b
.file_modification_time
)))
54 EXPECT_DOUBLE_EQ(a
.file_modification_time
, b
.file_modification_time
);
55 EXPECT_EQ(a
.blob_uuid
, b
.blob_uuid
);
59 void ExpectEquality(const ExplodedHttpBody
& a
, const ExplodedHttpBody
& b
) {
60 EXPECT_EQ(a
.http_content_type
, b
.http_content_type
);
61 EXPECT_EQ(a
.identifier
, b
.identifier
);
62 EXPECT_EQ(a
.contains_passwords
, b
.contains_passwords
);
63 EXPECT_EQ(a
.is_null
, b
.is_null
);
64 ExpectEquality(a
.elements
, b
.elements
);
68 void ExpectEquality(const ExplodedFrameState
& a
, const ExplodedFrameState
& b
) {
69 EXPECT_EQ(a
.url_string
, b
.url_string
);
70 EXPECT_EQ(a
.referrer
, b
.referrer
);
71 EXPECT_EQ(a
.referrer_policy
, b
.referrer_policy
);
72 EXPECT_EQ(a
.target
, b
.target
);
73 EXPECT_EQ(a
.state_object
, b
.state_object
);
74 ExpectEquality(a
.document_state
, b
.document_state
);
75 EXPECT_EQ(a
.scroll_offset
, b
.scroll_offset
);
76 EXPECT_EQ(a
.item_sequence_number
, b
.item_sequence_number
);
77 EXPECT_EQ(a
.document_sequence_number
, b
.document_sequence_number
);
78 EXPECT_EQ(a
.page_scale_factor
, b
.page_scale_factor
);
79 ExpectEquality(a
.http_body
, b
.http_body
);
80 ExpectEquality(a
.children
, b
.children
);
83 void ExpectEquality(const ExplodedPageState
& a
, const ExplodedPageState
& b
) {
84 ExpectEquality(a
.referenced_files
, b
.referenced_files
);
85 ExpectEquality(a
.top
, b
.top
);
88 //-----------------------------------------------------------------------------
90 class PageStateSerializationTest
: public testing::Test
{
92 void PopulateFrameState(ExplodedFrameState
* frame_state
) {
93 // Invent some data for the various fields.
94 frame_state
->url_string
= NS16("http://dev.chromium.org/");
95 frame_state
->referrer
= NS16("https://www.google.com/search?q=dev.chromium.org");
96 frame_state
->referrer_policy
= blink::WebReferrerPolicyAlways
;
97 frame_state
->target
= NS16("foo");
98 frame_state
->state_object
= NS16(NULL
);
99 frame_state
->document_state
.push_back(NS16("1"));
100 frame_state
->document_state
.push_back(NS16("q"));
101 frame_state
->document_state
.push_back(NS16("text"));
102 frame_state
->document_state
.push_back(NS16("dev.chromium.org"));
103 frame_state
->scroll_offset
= gfx::Point(0, 100);
104 frame_state
->item_sequence_number
= 1;
105 frame_state
->document_sequence_number
= 2;
106 frame_state
->page_scale_factor
= 2.0;
109 void PopulateHttpBody(ExplodedHttpBody
* http_body
,
110 std::vector
<base::NullableString16
>* referenced_files
) {
111 http_body
->is_null
= false;
112 http_body
->identifier
= 12345;
113 http_body
->contains_passwords
= false;
114 http_body
->http_content_type
= NS16("text/foo");
116 ExplodedHttpBodyElement e1
;
117 e1
.type
= blink::WebHTTPBody::Element::TypeData
;
119 http_body
->elements
.push_back(e1
);
121 ExplodedHttpBodyElement e2
;
122 e2
.type
= blink::WebHTTPBody::Element::TypeFile
;
123 e2
.file_path
= NS16("file.txt");
125 e2
.file_length
= 1024;
126 e2
.file_modification_time
= 9999.0;
127 http_body
->elements
.push_back(e2
);
129 referenced_files
->push_back(e2
.file_path
);
132 void PopulateFrameStateForBackwardsCompatTest(
133 ExplodedFrameState
* frame_state
,
135 frame_state
->url_string
= NS16("http://chromium.org/");
136 frame_state
->referrer
= NS16("http://google.com/");
137 frame_state
->referrer_policy
= blink::WebReferrerPolicyDefault
;
139 frame_state
->target
= NS16("target");
140 frame_state
->scroll_offset
= gfx::Point(42, -42);
141 frame_state
->item_sequence_number
= 123;
142 frame_state
->document_sequence_number
= 456;
143 frame_state
->page_scale_factor
= 2.0f
;
145 frame_state
->document_state
.push_back(
146 NS16("\n\r?% WebKit serialized form state version 8 \n\r=&"));
147 frame_state
->document_state
.push_back(NS16("form key"));
148 frame_state
->document_state
.push_back(NS16("1"));
149 frame_state
->document_state
.push_back(NS16("foo"));
150 frame_state
->document_state
.push_back(NS16("file"));
151 frame_state
->document_state
.push_back(NS16("2"));
152 frame_state
->document_state
.push_back(NS16("file.txt"));
153 frame_state
->document_state
.push_back(NS16("displayName"));
156 frame_state
->http_body
.http_content_type
= NS16("foo/bar");
157 frame_state
->http_body
.identifier
= 789;
158 frame_state
->http_body
.is_null
= false;
160 ExplodedHttpBodyElement e1
;
161 e1
.type
= blink::WebHTTPBody::Element::TypeData
;
162 e1
.data
= "first data block";
163 frame_state
->http_body
.elements
.push_back(e1
);
165 ExplodedHttpBodyElement e2
;
166 e2
.type
= blink::WebHTTPBody::Element::TypeFile
;
167 e2
.file_path
= NS16("file.txt");
168 frame_state
->http_body
.elements
.push_back(e2
);
170 ExplodedHttpBodyElement e3
;
171 e3
.type
= blink::WebHTTPBody::Element::TypeData
;
172 e3
.data
= "data the second";
173 frame_state
->http_body
.elements
.push_back(e3
);
175 ExplodedFrameState child_state
;
176 PopulateFrameStateForBackwardsCompatTest(&child_state
, true);
177 frame_state
->children
.push_back(child_state
);
181 void PopulatePageStateForBackwardsCompatTest(ExplodedPageState
* page_state
) {
182 page_state
->referenced_files
.push_back(NS16("file.txt"));
183 PopulateFrameStateForBackwardsCompatTest(&page_state
->top
, false);
186 void TestBackwardsCompat(int version
) {
187 const char* suffix
= "";
189 #if defined(OS_ANDROID)
190 // Unfortunately, the format of version 11 is different on Android, so we
191 // need to use a special reference file.
197 PathService::Get(content::DIR_TEST_DATA
, &path
);
198 path
= path
.AppendASCII("page_state").AppendASCII(
199 base::StringPrintf("serialized_v%d%s.dat", version
, suffix
));
201 std::string file_contents
;
202 if (!base::ReadFileToString(path
, &file_contents
)) {
203 ADD_FAILURE() << "File not found: " << path
.value();
207 std::string trimmed_contents
;
208 EXPECT_TRUE(base::RemoveChars(file_contents
, "\r\n", &trimmed_contents
));
211 EXPECT_TRUE(base::Base64Decode(trimmed_contents
, &encoded
));
213 ExplodedPageState output
;
214 #if defined(OS_ANDROID)
215 // Because version 11 of the file format unfortunately bakes in the device
216 // scale factor on Android, perform this test by assuming a preset device
217 // scale factor, ignoring the device scale factor of the current device.
218 const float kPresetDeviceScaleFactor
= 2.0f
;
219 EXPECT_TRUE(DecodePageStateWithDeviceScaleFactorForTesting(
221 kPresetDeviceScaleFactor
,
224 EXPECT_TRUE(DecodePageState(encoded
, &output
));
227 ExplodedPageState expected
;
228 PopulatePageStateForBackwardsCompatTest(&expected
);
230 ExpectEquality(expected
, output
);
234 TEST_F(PageStateSerializationTest
, BasicEmpty
) {
235 ExplodedPageState input
;
238 EXPECT_TRUE(EncodePageState(input
, &encoded
));
240 ExplodedPageState output
;
241 EXPECT_TRUE(DecodePageState(encoded
, &output
));
243 ExpectEquality(input
, output
);
246 TEST_F(PageStateSerializationTest
, BasicFrame
) {
247 ExplodedPageState input
;
248 PopulateFrameState(&input
.top
);
251 EXPECT_TRUE(EncodePageState(input
, &encoded
));
253 ExplodedPageState output
;
254 EXPECT_TRUE(DecodePageState(encoded
, &output
));
256 ExpectEquality(input
, output
);
259 TEST_F(PageStateSerializationTest
, BasicFramePOST
) {
260 ExplodedPageState input
;
261 PopulateFrameState(&input
.top
);
262 PopulateHttpBody(&input
.top
.http_body
, &input
.referenced_files
);
265 EXPECT_TRUE(EncodePageState(input
, &encoded
));
267 ExplodedPageState output
;
268 EXPECT_TRUE(DecodePageState(encoded
, &output
));
270 ExpectEquality(input
, output
);
273 TEST_F(PageStateSerializationTest
, BasicFrameSet
) {
274 ExplodedPageState input
;
275 PopulateFrameState(&input
.top
);
277 // Add some child frames.
278 for (int i
= 0; i
< 4; ++i
) {
279 ExplodedFrameState child_state
;
280 PopulateFrameState(&child_state
);
281 input
.top
.children
.push_back(child_state
);
285 EXPECT_TRUE(EncodePageState(input
, &encoded
));
287 ExplodedPageState output
;
288 EXPECT_TRUE(DecodePageState(encoded
, &output
));
290 ExpectEquality(input
, output
);
293 TEST_F(PageStateSerializationTest
, BasicFrameSetPOST
) {
294 ExplodedPageState input
;
295 PopulateFrameState(&input
.top
);
297 // Add some child frames.
298 for (int i
= 0; i
< 4; ++i
) {
299 ExplodedFrameState child_state
;
300 PopulateFrameState(&child_state
);
302 // Simulate a form POST on a subframe.
304 PopulateHttpBody(&child_state
.http_body
, &input
.referenced_files
);
306 input
.top
.children
.push_back(child_state
);
310 EncodePageState(input
, &encoded
);
312 ExplodedPageState output
;
313 DecodePageState(encoded
, &output
);
315 ExpectEquality(input
, output
);
318 TEST_F(PageStateSerializationTest
, BadMessagesTest1
) {
323 for (int i
= 0; i
< 6; ++i
)
328 std::string
s(static_cast<const char*>(p
.data()), p
.size());
330 ExplodedPageState output
;
331 EXPECT_FALSE(DecodePageState(s
, &output
));
334 TEST_F(PageStateSerializationTest
, BadMessagesTest2
) {
340 for (int i
= 0; i
< 6; ++i
)
343 p
.WriteData(reinterpret_cast<const char*>(&d
), sizeof(d
));
352 p
.WriteInt(blink::WebHTTPBody::Element::TypeData
);
354 std::string
s(static_cast<const char*>(p
.data()), p
.size());
356 ExplodedPageState output
;
357 EXPECT_FALSE(DecodePageState(s
, &output
));
360 TEST_F(PageStateSerializationTest
, DumpExpectedPageStateForBackwardsCompat
) {
361 // Change to #if 1 to enable this code. Use this code to generate data, based
362 // on the current serialization format, for the BackwardsCompat_vXX tests.
364 ExplodedPageState state
;
365 PopulatePageStateForBackwardsCompatTest(&state
);
368 EXPECT_TRUE(EncodePageState(state
, &encoded
));
371 base::Base64Encode(encoded
, &base64
);
374 PathService::Get(base::DIR_TEMP
, &path
);
375 path
= path
.AppendASCII("expected.dat");
377 FILE* fp
= base::OpenFile(path
, "wb");
380 const size_t kRowSize
= 76;
381 for (size_t offset
= 0; offset
< base64
.size(); offset
+= kRowSize
) {
382 size_t length
= std::min(base64
.size() - offset
, kRowSize
);
383 std::string
segment(&base64
[offset
], length
);
384 segment
.push_back('\n');
385 ASSERT_EQ(1U, fwrite(segment
.data(), segment
.size(), 1, fp
));
392 #if !defined(OS_ANDROID)
393 // TODO(darin): Re-enable for Android once this test accounts for systems with
394 // a device scale factor not equal to 2.
395 TEST_F(PageStateSerializationTest
, BackwardsCompat_v11
) {
396 TestBackwardsCompat(11);
400 TEST_F(PageStateSerializationTest
, BackwardsCompat_v12
) {
401 TestBackwardsCompat(12);
404 TEST_F(PageStateSerializationTest
, BackwardsCompat_v13
) {
405 TestBackwardsCompat(13);
408 TEST_F(PageStateSerializationTest
, BackwardsCompat_v14
) {
409 TestBackwardsCompat(14);
412 TEST_F(PageStateSerializationTest
, BackwardsCompat_v15
) {
413 TestBackwardsCompat(15);
416 TEST_F(PageStateSerializationTest
, BackwardsCompat_v16
) {
417 TestBackwardsCompat(16);
420 TEST_F(PageStateSerializationTest
, BackwardsCompat_v18
) {
421 TestBackwardsCompat(18);
425 } // namespace content