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/float_util.h"
10 #include "base/path_service.h"
11 #include "base/pickle.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "content/common/page_state_serialization.h"
16 #include "content/public/common/content_paths.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 base::NullableString16
NS16(const char* s
) {
23 return s
? base::NullableString16(base::ASCIIToUTF16(s
), false) :
24 base::NullableString16();
27 //-----------------------------------------------------------------------------
30 void ExpectEquality(const T
& a
, const T
& b
) {
35 void ExpectEquality(const std::vector
<T
>& a
, const std::vector
<T
>& b
) {
36 EXPECT_EQ(a
.size(), b
.size());
37 for (size_t i
= 0; i
< std::min(a
.size(), b
.size()); ++i
)
38 ExpectEquality(a
[i
], b
[i
]);
42 void ExpectEquality(const ExplodedHttpBodyElement
& a
,
43 const ExplodedHttpBodyElement
& b
) {
44 EXPECT_EQ(a
.type
, b
.type
);
45 EXPECT_EQ(a
.data
, b
.data
);
46 EXPECT_EQ(a
.file_path
, b
.file_path
);
47 EXPECT_EQ(a
.filesystem_url
, b
.filesystem_url
);
48 EXPECT_EQ(a
.file_start
, b
.file_start
);
49 EXPECT_EQ(a
.file_length
, b
.file_length
);
50 if (!(base::IsNaN(a
.file_modification_time
) &&
51 base::IsNaN(b
.file_modification_time
))) {
52 EXPECT_DOUBLE_EQ(a
.file_modification_time
, b
.file_modification_time
);
54 EXPECT_EQ(a
.blob_uuid
, b
.blob_uuid
);
58 void ExpectEquality(const ExplodedHttpBody
& a
, const ExplodedHttpBody
& b
) {
59 EXPECT_EQ(a
.http_content_type
, b
.http_content_type
);
60 EXPECT_EQ(a
.identifier
, b
.identifier
);
61 EXPECT_EQ(a
.contains_passwords
, b
.contains_passwords
);
62 EXPECT_EQ(a
.is_null
, b
.is_null
);
63 ExpectEquality(a
.elements
, b
.elements
);
67 void ExpectEquality(const ExplodedFrameState
& a
, const ExplodedFrameState
& b
) {
68 EXPECT_EQ(a
.url_string
, b
.url_string
);
69 EXPECT_EQ(a
.referrer
, b
.referrer
);
70 EXPECT_EQ(a
.referrer_policy
, b
.referrer_policy
);
71 EXPECT_EQ(a
.target
, b
.target
);
72 EXPECT_EQ(a
.state_object
, b
.state_object
);
73 ExpectEquality(a
.document_state
, b
.document_state
);
74 EXPECT_EQ(a
.pinch_viewport_scroll_offset
, b
.pinch_viewport_scroll_offset
);
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
->pinch_viewport_scroll_offset
= gfx::PointF(10, 15);
104 frame_state
->scroll_offset
= gfx::Point(0, 100);
105 frame_state
->item_sequence_number
= 1;
106 frame_state
->document_sequence_number
= 2;
107 frame_state
->frame_sequence_number
= 3;
108 frame_state
->page_scale_factor
= 2.0;
111 void PopulateHttpBody(ExplodedHttpBody
* http_body
,
112 std::vector
<base::NullableString16
>* referenced_files
) {
113 http_body
->is_null
= false;
114 http_body
->identifier
= 12345;
115 http_body
->contains_passwords
= false;
116 http_body
->http_content_type
= NS16("text/foo");
118 ExplodedHttpBodyElement e1
;
119 e1
.type
= blink::WebHTTPBody::Element::TypeData
;
121 http_body
->elements
.push_back(e1
);
123 ExplodedHttpBodyElement e2
;
124 e2
.type
= blink::WebHTTPBody::Element::TypeFile
;
125 e2
.file_path
= NS16("file.txt");
127 e2
.file_length
= 1024;
128 e2
.file_modification_time
= 9999.0;
129 http_body
->elements
.push_back(e2
);
131 referenced_files
->push_back(e2
.file_path
);
134 void PopulateFrameStateForBackwardsCompatTest(
135 ExplodedFrameState
* frame_state
,
137 frame_state
->url_string
= NS16("http://chromium.org/");
138 frame_state
->referrer
= NS16("http://google.com/");
139 frame_state
->referrer_policy
= blink::WebReferrerPolicyDefault
;
141 frame_state
->target
= NS16("target");
142 frame_state
->pinch_viewport_scroll_offset
= gfx::PointF(-1, -1);
143 frame_state
->scroll_offset
= gfx::Point(42, -42);
144 frame_state
->item_sequence_number
= 123;
145 frame_state
->document_sequence_number
= 456;
146 frame_state
->frame_sequence_number
= 789;
147 frame_state
->page_scale_factor
= 2.0f
;
149 frame_state
->document_state
.push_back(
150 NS16("\n\r?% WebKit serialized form state version 8 \n\r=&"));
151 frame_state
->document_state
.push_back(NS16("form key"));
152 frame_state
->document_state
.push_back(NS16("1"));
153 frame_state
->document_state
.push_back(NS16("foo"));
154 frame_state
->document_state
.push_back(NS16("file"));
155 frame_state
->document_state
.push_back(NS16("2"));
156 frame_state
->document_state
.push_back(NS16("file.txt"));
157 frame_state
->document_state
.push_back(NS16("displayName"));
160 frame_state
->http_body
.http_content_type
= NS16("foo/bar");
161 frame_state
->http_body
.identifier
= 789;
162 frame_state
->http_body
.is_null
= false;
164 ExplodedHttpBodyElement e1
;
165 e1
.type
= blink::WebHTTPBody::Element::TypeData
;
166 e1
.data
= "first data block";
167 frame_state
->http_body
.elements
.push_back(e1
);
169 ExplodedHttpBodyElement e2
;
170 e2
.type
= blink::WebHTTPBody::Element::TypeFile
;
171 e2
.file_path
= NS16("file.txt");
172 frame_state
->http_body
.elements
.push_back(e2
);
174 ExplodedHttpBodyElement e3
;
175 e3
.type
= blink::WebHTTPBody::Element::TypeData
;
176 e3
.data
= "data the second";
177 frame_state
->http_body
.elements
.push_back(e3
);
179 ExplodedFrameState child_state
;
180 PopulateFrameStateForBackwardsCompatTest(&child_state
, true);
181 frame_state
->children
.push_back(child_state
);
185 void PopulatePageStateForBackwardsCompatTest(ExplodedPageState
* page_state
) {
186 page_state
->referenced_files
.push_back(NS16("file.txt"));
187 PopulateFrameStateForBackwardsCompatTest(&page_state
->top
, false);
190 void TestBackwardsCompat(int version
) {
191 const char* suffix
= "";
193 #if defined(OS_ANDROID)
194 // Unfortunately, the format of version 11 is different on Android, so we
195 // need to use a special reference file.
201 PathService::Get(content::DIR_TEST_DATA
, &path
);
202 path
= path
.AppendASCII("page_state").AppendASCII(
203 base::StringPrintf("serialized_v%d%s.dat", version
, suffix
));
205 std::string file_contents
;
206 if (!base::ReadFileToString(path
, &file_contents
)) {
207 ADD_FAILURE() << "File not found: " << path
.value();
211 std::string trimmed_contents
;
212 EXPECT_TRUE(base::RemoveChars(file_contents
, "\r\n", &trimmed_contents
));
215 EXPECT_TRUE(base::Base64Decode(trimmed_contents
, &encoded
));
217 ExplodedPageState output
;
218 #if defined(OS_ANDROID)
219 // Because version 11 of the file format unfortunately bakes in the device
220 // scale factor on Android, perform this test by assuming a preset device
221 // scale factor, ignoring the device scale factor of the current device.
222 const float kPresetDeviceScaleFactor
= 2.0f
;
223 EXPECT_TRUE(DecodePageStateWithDeviceScaleFactorForTesting(
225 kPresetDeviceScaleFactor
,
228 EXPECT_TRUE(DecodePageState(encoded
, &output
));
231 ExplodedPageState expected
;
232 PopulatePageStateForBackwardsCompatTest(&expected
);
234 ExpectEquality(expected
, output
);
238 TEST_F(PageStateSerializationTest
, BasicEmpty
) {
239 ExplodedPageState input
;
242 EXPECT_TRUE(EncodePageState(input
, &encoded
));
244 ExplodedPageState output
;
245 EXPECT_TRUE(DecodePageState(encoded
, &output
));
247 ExpectEquality(input
, output
);
250 TEST_F(PageStateSerializationTest
, BasicFrame
) {
251 ExplodedPageState input
;
252 PopulateFrameState(&input
.top
);
255 EXPECT_TRUE(EncodePageState(input
, &encoded
));
257 ExplodedPageState output
;
258 EXPECT_TRUE(DecodePageState(encoded
, &output
));
260 ExpectEquality(input
, output
);
263 TEST_F(PageStateSerializationTest
, BasicFramePOST
) {
264 ExplodedPageState input
;
265 PopulateFrameState(&input
.top
);
266 PopulateHttpBody(&input
.top
.http_body
, &input
.referenced_files
);
269 EXPECT_TRUE(EncodePageState(input
, &encoded
));
271 ExplodedPageState output
;
272 EXPECT_TRUE(DecodePageState(encoded
, &output
));
274 ExpectEquality(input
, output
);
277 TEST_F(PageStateSerializationTest
, BasicFrameSet
) {
278 ExplodedPageState input
;
279 PopulateFrameState(&input
.top
);
281 // Add some child frames.
282 for (int i
= 0; i
< 4; ++i
) {
283 ExplodedFrameState child_state
;
284 PopulateFrameState(&child_state
);
285 input
.top
.children
.push_back(child_state
);
289 EXPECT_TRUE(EncodePageState(input
, &encoded
));
291 ExplodedPageState output
;
292 EXPECT_TRUE(DecodePageState(encoded
, &output
));
294 ExpectEquality(input
, output
);
297 TEST_F(PageStateSerializationTest
, BasicFrameSetPOST
) {
298 ExplodedPageState input
;
299 PopulateFrameState(&input
.top
);
301 // Add some child frames.
302 for (int i
= 0; i
< 4; ++i
) {
303 ExplodedFrameState child_state
;
304 PopulateFrameState(&child_state
);
306 // Simulate a form POST on a subframe.
308 PopulateHttpBody(&child_state
.http_body
, &input
.referenced_files
);
310 input
.top
.children
.push_back(child_state
);
314 EncodePageState(input
, &encoded
);
316 ExplodedPageState output
;
317 DecodePageState(encoded
, &output
);
319 ExpectEquality(input
, output
);
322 TEST_F(PageStateSerializationTest
, BadMessagesTest1
) {
327 for (int i
= 0; i
< 6; ++i
)
332 std::string
s(static_cast<const char*>(p
.data()), p
.size());
334 ExplodedPageState output
;
335 EXPECT_FALSE(DecodePageState(s
, &output
));
338 TEST_F(PageStateSerializationTest
, BadMessagesTest2
) {
344 for (int i
= 0; i
< 6; ++i
)
347 p
.WriteData(reinterpret_cast<const char*>(&d
), sizeof(d
));
356 p
.WriteInt(blink::WebHTTPBody::Element::TypeData
);
358 std::string
s(static_cast<const char*>(p
.data()), p
.size());
360 ExplodedPageState output
;
361 EXPECT_FALSE(DecodePageState(s
, &output
));
364 TEST_F(PageStateSerializationTest
, DumpExpectedPageStateForBackwardsCompat
) {
365 // Change to #if 1 to enable this code. Use this code to generate data, based
366 // on the current serialization format, for the BackwardsCompat_vXX tests.
368 ExplodedPageState state
;
369 PopulatePageStateForBackwardsCompatTest(&state
);
372 EXPECT_TRUE(EncodePageState(state
, &encoded
));
375 base::Base64Encode(encoded
, &base64
);
378 PathService::Get(base::DIR_TEMP
, &path
);
379 path
= path
.AppendASCII("expected.dat");
381 FILE* fp
= base::OpenFile(path
, "wb");
384 const size_t kRowSize
= 76;
385 for (size_t offset
= 0; offset
< base64
.size(); offset
+= kRowSize
) {
386 size_t length
= std::min(base64
.size() - offset
, kRowSize
);
387 std::string
segment(&base64
[offset
], length
);
388 segment
.push_back('\n');
389 ASSERT_EQ(1U, fwrite(segment
.data(), segment
.size(), 1, fp
));
396 #if !defined(OS_ANDROID)
397 // TODO(darin): Re-enable for Android once this test accounts for systems with
398 // a device scale factor not equal to 2.
399 TEST_F(PageStateSerializationTest
, BackwardsCompat_v11
) {
400 TestBackwardsCompat(11);
404 TEST_F(PageStateSerializationTest
, BackwardsCompat_v12
) {
405 TestBackwardsCompat(12);
408 TEST_F(PageStateSerializationTest
, BackwardsCompat_v13
) {
409 TestBackwardsCompat(13);
412 TEST_F(PageStateSerializationTest
, BackwardsCompat_v14
) {
413 TestBackwardsCompat(14);
416 TEST_F(PageStateSerializationTest
, BackwardsCompat_v15
) {
417 TestBackwardsCompat(15);
420 TEST_F(PageStateSerializationTest
, BackwardsCompat_v16
) {
421 TestBackwardsCompat(16);
424 TEST_F(PageStateSerializationTest
, BackwardsCompat_v18
) {
425 TestBackwardsCompat(18);
428 TEST_F(PageStateSerializationTest
, BackwardsCompat_v20
) {
429 TestBackwardsCompat(20);
433 } // namespace content