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/files/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"
18 #if defined(OS_MACOSX)
26 inline bool isnan(double num
) { return !!_isnan(num
); }
29 base::NullableString16
NS16(const char* s
) {
30 return s
? base::NullableString16(base::ASCIIToUTF16(s
), false) :
31 base::NullableString16();
34 //-----------------------------------------------------------------------------
37 void ExpectEquality(const T
& a
, const T
& b
) {
42 void ExpectEquality(const std::vector
<T
>& a
, const std::vector
<T
>& b
) {
43 EXPECT_EQ(a
.size(), b
.size());
44 for (size_t i
= 0; i
< std::min(a
.size(), b
.size()); ++i
)
45 ExpectEquality(a
[i
], b
[i
]);
49 void ExpectEquality(const ExplodedHttpBodyElement
& a
,
50 const ExplodedHttpBodyElement
& b
) {
51 EXPECT_EQ(a
.type
, b
.type
);
52 EXPECT_EQ(a
.data
, b
.data
);
53 EXPECT_EQ(a
.file_path
, b
.file_path
);
54 EXPECT_EQ(a
.filesystem_url
, b
.filesystem_url
);
55 EXPECT_EQ(a
.file_start
, b
.file_start
);
56 EXPECT_EQ(a
.file_length
, b
.file_length
);
57 if (!(isnan(a
.file_modification_time
) && isnan(b
.file_modification_time
)))
58 EXPECT_DOUBLE_EQ(a
.file_modification_time
, b
.file_modification_time
);
59 EXPECT_EQ(a
.blob_uuid
, b
.blob_uuid
);
63 void ExpectEquality(const ExplodedHttpBody
& a
, const ExplodedHttpBody
& b
) {
64 EXPECT_EQ(a
.http_content_type
, b
.http_content_type
);
65 EXPECT_EQ(a
.identifier
, b
.identifier
);
66 EXPECT_EQ(a
.contains_passwords
, b
.contains_passwords
);
67 EXPECT_EQ(a
.is_null
, b
.is_null
);
68 ExpectEquality(a
.elements
, b
.elements
);
72 void ExpectEquality(const ExplodedFrameState
& a
, const ExplodedFrameState
& b
) {
73 EXPECT_EQ(a
.url_string
, b
.url_string
);
74 EXPECT_EQ(a
.referrer
, b
.referrer
);
75 EXPECT_EQ(a
.referrer_policy
, b
.referrer_policy
);
76 EXPECT_EQ(a
.target
, b
.target
);
77 EXPECT_EQ(a
.state_object
, b
.state_object
);
78 ExpectEquality(a
.document_state
, b
.document_state
);
79 EXPECT_EQ(a
.pinch_viewport_scroll_offset
, b
.pinch_viewport_scroll_offset
);
80 EXPECT_EQ(a
.scroll_offset
, b
.scroll_offset
);
81 EXPECT_EQ(a
.item_sequence_number
, b
.item_sequence_number
);
82 EXPECT_EQ(a
.document_sequence_number
, b
.document_sequence_number
);
83 EXPECT_EQ(a
.page_scale_factor
, b
.page_scale_factor
);
84 ExpectEquality(a
.http_body
, b
.http_body
);
85 ExpectEquality(a
.children
, b
.children
);
88 void ExpectEquality(const ExplodedPageState
& a
, const ExplodedPageState
& b
) {
89 ExpectEquality(a
.referenced_files
, b
.referenced_files
);
90 ExpectEquality(a
.top
, b
.top
);
93 //-----------------------------------------------------------------------------
95 class PageStateSerializationTest
: public testing::Test
{
97 void PopulateFrameState(ExplodedFrameState
* frame_state
) {
98 // Invent some data for the various fields.
99 frame_state
->url_string
= NS16("http://dev.chromium.org/");
100 frame_state
->referrer
= NS16("https://www.google.com/search?q=dev.chromium.org");
101 frame_state
->referrer_policy
= blink::WebReferrerPolicyAlways
;
102 frame_state
->target
= NS16("foo");
103 frame_state
->state_object
= NS16(NULL
);
104 frame_state
->document_state
.push_back(NS16("1"));
105 frame_state
->document_state
.push_back(NS16("q"));
106 frame_state
->document_state
.push_back(NS16("text"));
107 frame_state
->document_state
.push_back(NS16("dev.chromium.org"));
108 frame_state
->pinch_viewport_scroll_offset
= gfx::PointF(10, 15);
109 frame_state
->scroll_offset
= gfx::Point(0, 100);
110 frame_state
->item_sequence_number
= 1;
111 frame_state
->document_sequence_number
= 2;
112 frame_state
->frame_sequence_number
= 3;
113 frame_state
->page_scale_factor
= 2.0;
116 void PopulateHttpBody(ExplodedHttpBody
* http_body
,
117 std::vector
<base::NullableString16
>* referenced_files
) {
118 http_body
->is_null
= false;
119 http_body
->identifier
= 12345;
120 http_body
->contains_passwords
= false;
121 http_body
->http_content_type
= NS16("text/foo");
123 ExplodedHttpBodyElement e1
;
124 e1
.type
= blink::WebHTTPBody::Element::TypeData
;
126 http_body
->elements
.push_back(e1
);
128 ExplodedHttpBodyElement e2
;
129 e2
.type
= blink::WebHTTPBody::Element::TypeFile
;
130 e2
.file_path
= NS16("file.txt");
132 e2
.file_length
= 1024;
133 e2
.file_modification_time
= 9999.0;
134 http_body
->elements
.push_back(e2
);
136 referenced_files
->push_back(e2
.file_path
);
139 void PopulateFrameStateForBackwardsCompatTest(
140 ExplodedFrameState
* frame_state
,
142 frame_state
->url_string
= NS16("http://chromium.org/");
143 frame_state
->referrer
= NS16("http://google.com/");
144 frame_state
->referrer_policy
= blink::WebReferrerPolicyDefault
;
146 frame_state
->target
= NS16("target");
147 frame_state
->pinch_viewport_scroll_offset
= gfx::PointF(-1, -1);
148 frame_state
->scroll_offset
= gfx::Point(42, -42);
149 frame_state
->item_sequence_number
= 123;
150 frame_state
->document_sequence_number
= 456;
151 frame_state
->frame_sequence_number
= 789;
152 frame_state
->page_scale_factor
= 2.0f
;
154 frame_state
->document_state
.push_back(
155 NS16("\n\r?% WebKit serialized form state version 8 \n\r=&"));
156 frame_state
->document_state
.push_back(NS16("form key"));
157 frame_state
->document_state
.push_back(NS16("1"));
158 frame_state
->document_state
.push_back(NS16("foo"));
159 frame_state
->document_state
.push_back(NS16("file"));
160 frame_state
->document_state
.push_back(NS16("2"));
161 frame_state
->document_state
.push_back(NS16("file.txt"));
162 frame_state
->document_state
.push_back(NS16("displayName"));
165 frame_state
->http_body
.http_content_type
= NS16("foo/bar");
166 frame_state
->http_body
.identifier
= 789;
167 frame_state
->http_body
.is_null
= false;
169 ExplodedHttpBodyElement e1
;
170 e1
.type
= blink::WebHTTPBody::Element::TypeData
;
171 e1
.data
= "first data block";
172 frame_state
->http_body
.elements
.push_back(e1
);
174 ExplodedHttpBodyElement e2
;
175 e2
.type
= blink::WebHTTPBody::Element::TypeFile
;
176 e2
.file_path
= NS16("file.txt");
177 frame_state
->http_body
.elements
.push_back(e2
);
179 ExplodedHttpBodyElement e3
;
180 e3
.type
= blink::WebHTTPBody::Element::TypeData
;
181 e3
.data
= "data the second";
182 frame_state
->http_body
.elements
.push_back(e3
);
184 ExplodedFrameState child_state
;
185 PopulateFrameStateForBackwardsCompatTest(&child_state
, true);
186 frame_state
->children
.push_back(child_state
);
190 void PopulatePageStateForBackwardsCompatTest(ExplodedPageState
* page_state
) {
191 page_state
->referenced_files
.push_back(NS16("file.txt"));
192 PopulateFrameStateForBackwardsCompatTest(&page_state
->top
, false);
195 void TestBackwardsCompat(int version
) {
196 const char* suffix
= "";
198 #if defined(OS_ANDROID)
199 // Unfortunately, the format of version 11 is different on Android, so we
200 // need to use a special reference file.
206 PathService::Get(content::DIR_TEST_DATA
, &path
);
207 path
= path
.AppendASCII("page_state").AppendASCII(
208 base::StringPrintf("serialized_v%d%s.dat", version
, suffix
));
210 std::string file_contents
;
211 if (!base::ReadFileToString(path
, &file_contents
)) {
212 ADD_FAILURE() << "File not found: " << path
.value();
216 std::string trimmed_contents
;
217 EXPECT_TRUE(base::RemoveChars(file_contents
, "\r\n", &trimmed_contents
));
220 EXPECT_TRUE(base::Base64Decode(trimmed_contents
, &encoded
));
222 ExplodedPageState output
;
223 #if defined(OS_ANDROID)
224 // Because version 11 of the file format unfortunately bakes in the device
225 // scale factor on Android, perform this test by assuming a preset device
226 // scale factor, ignoring the device scale factor of the current device.
227 const float kPresetDeviceScaleFactor
= 2.0f
;
228 EXPECT_TRUE(DecodePageStateWithDeviceScaleFactorForTesting(
230 kPresetDeviceScaleFactor
,
233 EXPECT_TRUE(DecodePageState(encoded
, &output
));
236 ExplodedPageState expected
;
237 PopulatePageStateForBackwardsCompatTest(&expected
);
239 ExpectEquality(expected
, output
);
243 TEST_F(PageStateSerializationTest
, BasicEmpty
) {
244 ExplodedPageState input
;
247 EXPECT_TRUE(EncodePageState(input
, &encoded
));
249 ExplodedPageState output
;
250 EXPECT_TRUE(DecodePageState(encoded
, &output
));
252 ExpectEquality(input
, output
);
255 TEST_F(PageStateSerializationTest
, BasicFrame
) {
256 ExplodedPageState input
;
257 PopulateFrameState(&input
.top
);
260 EXPECT_TRUE(EncodePageState(input
, &encoded
));
262 ExplodedPageState output
;
263 EXPECT_TRUE(DecodePageState(encoded
, &output
));
265 ExpectEquality(input
, output
);
268 TEST_F(PageStateSerializationTest
, BasicFramePOST
) {
269 ExplodedPageState input
;
270 PopulateFrameState(&input
.top
);
271 PopulateHttpBody(&input
.top
.http_body
, &input
.referenced_files
);
274 EXPECT_TRUE(EncodePageState(input
, &encoded
));
276 ExplodedPageState output
;
277 EXPECT_TRUE(DecodePageState(encoded
, &output
));
279 ExpectEquality(input
, output
);
282 TEST_F(PageStateSerializationTest
, BasicFrameSet
) {
283 ExplodedPageState input
;
284 PopulateFrameState(&input
.top
);
286 // Add some child frames.
287 for (int i
= 0; i
< 4; ++i
) {
288 ExplodedFrameState child_state
;
289 PopulateFrameState(&child_state
);
290 input
.top
.children
.push_back(child_state
);
294 EXPECT_TRUE(EncodePageState(input
, &encoded
));
296 ExplodedPageState output
;
297 EXPECT_TRUE(DecodePageState(encoded
, &output
));
299 ExpectEquality(input
, output
);
302 TEST_F(PageStateSerializationTest
, BasicFrameSetPOST
) {
303 ExplodedPageState input
;
304 PopulateFrameState(&input
.top
);
306 // Add some child frames.
307 for (int i
= 0; i
< 4; ++i
) {
308 ExplodedFrameState child_state
;
309 PopulateFrameState(&child_state
);
311 // Simulate a form POST on a subframe.
313 PopulateHttpBody(&child_state
.http_body
, &input
.referenced_files
);
315 input
.top
.children
.push_back(child_state
);
319 EncodePageState(input
, &encoded
);
321 ExplodedPageState output
;
322 DecodePageState(encoded
, &output
);
324 ExpectEquality(input
, output
);
327 TEST_F(PageStateSerializationTest
, BadMessagesTest1
) {
332 for (int i
= 0; i
< 6; ++i
)
337 std::string
s(static_cast<const char*>(p
.data()), p
.size());
339 ExplodedPageState output
;
340 EXPECT_FALSE(DecodePageState(s
, &output
));
343 TEST_F(PageStateSerializationTest
, BadMessagesTest2
) {
349 for (int i
= 0; i
< 6; ++i
)
352 p
.WriteData(reinterpret_cast<const char*>(&d
), sizeof(d
));
361 p
.WriteInt(blink::WebHTTPBody::Element::TypeData
);
363 std::string
s(static_cast<const char*>(p
.data()), p
.size());
365 ExplodedPageState output
;
366 EXPECT_FALSE(DecodePageState(s
, &output
));
369 TEST_F(PageStateSerializationTest
, DumpExpectedPageStateForBackwardsCompat
) {
370 // Change to #if 1 to enable this code. Use this code to generate data, based
371 // on the current serialization format, for the BackwardsCompat_vXX tests.
373 ExplodedPageState state
;
374 PopulatePageStateForBackwardsCompatTest(&state
);
377 EXPECT_TRUE(EncodePageState(state
, &encoded
));
380 base::Base64Encode(encoded
, &base64
);
383 PathService::Get(base::DIR_TEMP
, &path
);
384 path
= path
.AppendASCII("expected.dat");
386 FILE* fp
= base::OpenFile(path
, "wb");
389 const size_t kRowSize
= 76;
390 for (size_t offset
= 0; offset
< base64
.size(); offset
+= kRowSize
) {
391 size_t length
= std::min(base64
.size() - offset
, kRowSize
);
392 std::string
segment(&base64
[offset
], length
);
393 segment
.push_back('\n');
394 ASSERT_EQ(1U, fwrite(segment
.data(), segment
.size(), 1, fp
));
401 #if !defined(OS_ANDROID)
402 // TODO(darin): Re-enable for Android once this test accounts for systems with
403 // a device scale factor not equal to 2.
404 TEST_F(PageStateSerializationTest
, BackwardsCompat_v11
) {
405 TestBackwardsCompat(11);
409 TEST_F(PageStateSerializationTest
, BackwardsCompat_v12
) {
410 TestBackwardsCompat(12);
413 TEST_F(PageStateSerializationTest
, BackwardsCompat_v13
) {
414 TestBackwardsCompat(13);
417 TEST_F(PageStateSerializationTest
, BackwardsCompat_v14
) {
418 TestBackwardsCompat(14);
421 TEST_F(PageStateSerializationTest
, BackwardsCompat_v15
) {
422 TestBackwardsCompat(15);
425 TEST_F(PageStateSerializationTest
, BackwardsCompat_v16
) {
426 TestBackwardsCompat(16);
429 TEST_F(PageStateSerializationTest
, BackwardsCompat_v18
) {
430 TestBackwardsCompat(18);
433 TEST_F(PageStateSerializationTest
, BackwardsCompat_v20
) {
434 TestBackwardsCompat(20);
438 } // namespace content