Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / sessions / content / content_serialized_navigation_driver_unittest.cc
blob9a3525c41f95071a34a1f56d546f0ac19ebac519
1 // Copyright 2014 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.
5 #include "components/sessions/content/content_serialized_navigation_driver.h"
7 #include "components/sessions/serialized_navigation_entry.h"
8 #include "components/sessions/serialized_navigation_entry_test_helper.h"
9 #include "content/public/common/page_state.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
13 namespace sessions {
15 // Tests that PageState data is properly sanitized when post data is present.
16 TEST(ContentSerializedNavigationDriverTest, PickleSanitizationWithPostData) {
17 ContentSerializedNavigationDriver* driver =
18 ContentSerializedNavigationDriver::GetInstance();
19 SerializedNavigationEntry navigation =
20 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
21 ASSERT_TRUE(navigation.has_post_data());
23 // When post data is present, the page state should be sanitized.
24 std::string sanitized_page_state =
25 driver->GetSanitizedPageStateForPickle(&navigation);
26 EXPECT_EQ(std::string(), sanitized_page_state);
29 // Tests that PageState data is left unsanitized when post data is absent.
30 TEST(ContentSerializedNavigationDriverTest, PickleSanitizationNoPostData) {
31 ContentSerializedNavigationDriver* driver =
32 ContentSerializedNavigationDriver::GetInstance();
33 SerializedNavigationEntry navigation =
34 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
35 SerializedNavigationEntryTestHelper::SetHasPostData(false, &navigation);
36 ASSERT_FALSE(navigation.has_post_data());
38 std::string sanitized_page_state =
39 driver->GetSanitizedPageStateForPickle(&navigation);
40 EXPECT_EQ(test_data::kEncodedPageState, sanitized_page_state);
43 // Tests that the input data is left unsanitized when the referrer policy is
44 // Always.
45 TEST(ContentSerializedNavigationDriverTest, SanitizeWithReferrerPolicyAlways) {
46 ContentSerializedNavigationDriver* driver =
47 ContentSerializedNavigationDriver::GetInstance();
48 SerializedNavigationEntry navigation =
49 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
50 SerializedNavigationEntryTestHelper::SetReferrerPolicy(
51 blink::WebReferrerPolicyAlways, &navigation);
53 content::PageState page_state =
54 content::PageState::CreateFromURL(test_data::kVirtualURL);
55 SerializedNavigationEntryTestHelper::SetEncodedPageState(
56 page_state.ToEncodedData(), &navigation);
58 driver->Sanitize(&navigation);
59 EXPECT_EQ(test_data::kIndex, navigation.index());
60 EXPECT_EQ(test_data::kUniqueID, navigation.unique_id());
61 EXPECT_EQ(test_data::kReferrerURL, navigation.referrer_url());
62 EXPECT_EQ(blink::WebReferrerPolicyAlways, navigation.referrer_policy());
63 EXPECT_EQ(test_data::kVirtualURL, navigation.virtual_url());
64 EXPECT_EQ(test_data::kTitle, navigation.title());
65 EXPECT_EQ(page_state.ToEncodedData(), navigation.encoded_page_state());
66 EXPECT_EQ(test_data::kTransitionType, navigation.transition_type());
67 EXPECT_EQ(test_data::kHasPostData, navigation.has_post_data());
68 EXPECT_EQ(test_data::kPostID, navigation.post_id());
69 EXPECT_EQ(test_data::kOriginalRequestURL, navigation.original_request_url());
70 EXPECT_EQ(test_data::kIsOverridingUserAgent,
71 navigation.is_overriding_user_agent());
72 EXPECT_EQ(test_data::kTimestamp, navigation.timestamp());
73 EXPECT_EQ(test_data::kSearchTerms, navigation.search_terms());
74 EXPECT_EQ(test_data::kFaviconURL, navigation.favicon_url());
75 EXPECT_EQ(test_data::kHttpStatusCode, navigation.http_status_code());
78 // Tests that the input data is properly sanitized when the referrer policy is
79 // Never.
80 TEST(ContentSerializedNavigationDriverTest, SanitizeWithReferrerPolicyNever) {
81 ContentSerializedNavigationDriver* driver =
82 ContentSerializedNavigationDriver::GetInstance();
83 SerializedNavigationEntry navigation =
84 SerializedNavigationEntryTestHelper::CreateNavigationForTest();
85 SerializedNavigationEntryTestHelper::SetReferrerPolicy(
86 blink::WebReferrerPolicyNever, &navigation);
88 content::PageState page_state =
89 content::PageState::CreateFromURL(test_data::kVirtualURL);
90 SerializedNavigationEntryTestHelper::SetEncodedPageState(
91 page_state.ToEncodedData(), &navigation);
93 driver->Sanitize(&navigation);
95 // Fields that should remain untouched.
96 EXPECT_EQ(test_data::kIndex, navigation.index());
97 EXPECT_EQ(test_data::kUniqueID, navigation.unique_id());
98 EXPECT_EQ(test_data::kVirtualURL, navigation.virtual_url());
99 EXPECT_EQ(test_data::kTitle, navigation.title());
100 EXPECT_EQ(test_data::kTransitionType, navigation.transition_type());
101 EXPECT_EQ(test_data::kHasPostData, navigation.has_post_data());
102 EXPECT_EQ(test_data::kPostID, navigation.post_id());
103 EXPECT_EQ(test_data::kOriginalRequestURL, navigation.original_request_url());
104 EXPECT_EQ(test_data::kIsOverridingUserAgent,
105 navigation.is_overriding_user_agent());
106 EXPECT_EQ(test_data::kTimestamp, navigation.timestamp());
107 EXPECT_EQ(test_data::kSearchTerms, navigation.search_terms());
108 EXPECT_EQ(test_data::kFaviconURL, navigation.favicon_url());
109 EXPECT_EQ(test_data::kHttpStatusCode, navigation.http_status_code());
111 // Fields that were sanitized.
112 EXPECT_EQ(GURL(), navigation.referrer_url());
113 EXPECT_EQ(blink::WebReferrerPolicyDefault, navigation.referrer_policy());
114 EXPECT_EQ(page_state.ToEncodedData(), navigation.encoded_page_state());
117 } // namespace sessions