summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/safe_search_util_unittest.cc
blob: 14653a0b808a92057ca8dcb80b76ae912e8870e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/safe_search_util.h"

#include "base/message_loop/message_loop.h"
#include "base/strings/string_piece.h"
#include "chrome/common/url_constants.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

class SafeSearchUtilTest : public ::testing::Test {
 protected:
  SafeSearchUtilTest() {}
  ~SafeSearchUtilTest() override {}

  scoped_ptr<net::URLRequest> CreateRequest(const std::string& url) {
    return context_.CreateRequest(GURL(url), net::DEFAULT_PRIORITY, NULL);
  }

  scoped_ptr<net::URLRequest> CreateYoutubeRequest() {
    return CreateRequest("http://www.youtube.com");
  }

  scoped_ptr<net::URLRequest> CreateNonYoutubeRequest() {
    return CreateRequest("http://www.notyoutube.com");
  }

  // Does a request using the |url_string| URL and verifies that the expected
  // string is equal to the query part (between ? and #) of the final url of
  // that request.
  void CheckAddedParameters(const std::string& url_string,
                            const std::string& expected_query_parameters) {
    // Show the URL in the trace so we know where we failed.
    SCOPED_TRACE(url_string);

    scoped_ptr<net::URLRequest> request(CreateRequest(url_string));
    GURL result(url_string);
    safe_search_util::ForceGoogleSafeSearch(request.get(), &result);

    EXPECT_EQ(expected_query_parameters, result.query());
  }

  base::MessageLoop message_loop_;
  net::TestURLRequestContext context_;
};

TEST_F(SafeSearchUtilTest, AddGoogleSafeSearchParams) {
  const std::string kSafeParameter = chrome::kSafeSearchSafeParameter;
  const std::string kSsuiParameter = chrome::kSafeSearchSsuiParameter;
  const std::string kBothParameters = kSafeParameter + "&" + kSsuiParameter;

  // Test the home page.
  CheckAddedParameters("http://google.com/", kBothParameters);

  // Test the search home page.
  CheckAddedParameters("http://google.com/webhp",
                       kBothParameters);

  // Test different valid search pages with parameters.
  CheckAddedParameters("http://google.com/search?q=google",
                       "q=google&" + kBothParameters);

  CheckAddedParameters("http://google.com/?q=google",
                       "q=google&" + kBothParameters);

  CheckAddedParameters("http://google.com/webhp?q=google",
                       "q=google&" + kBothParameters);

  // Test the valid pages with safe set to off.
  CheckAddedParameters("http://google.com/search?q=google&safe=off",
                       "q=google&" + kBothParameters);

  CheckAddedParameters("http://google.com/?q=google&safe=off",
                       "q=google&" + kBothParameters);

  CheckAddedParameters("http://google.com/webhp?q=google&safe=off",
                       "q=google&" + kBothParameters);

  CheckAddedParameters("http://google.com/webhp?q=google&%73afe=off",
                       "q=google&%73afe=off&" + kBothParameters);

  // Test the home page, different TLDs.
  CheckAddedParameters("http://google.de/", kBothParameters);
  CheckAddedParameters("http://google.ro/", kBothParameters);
  CheckAddedParameters("http://google.nl/", kBothParameters);

  // Test the search home page, different TLD.
  CheckAddedParameters("http://google.de/webhp", kBothParameters);

  // Test the search page with parameters, different TLD.
  CheckAddedParameters("http://google.de/search?q=google",
                       "q=google&" + kBothParameters);

  // Test the home page with parameters, different TLD.
  CheckAddedParameters("http://google.de/?q=google",
                       "q=google&" + kBothParameters);

  // Test the search page with the parameters set.
  CheckAddedParameters("http://google.de/?q=google&" + kBothParameters,
                       "q=google&" + kBothParameters);

  // Test some possibly tricky combinations.
  CheckAddedParameters("http://google.com/?q=goog&" + kSafeParameter +
                       "&ssui=one",
                       "q=goog&" + kBothParameters);

  CheckAddedParameters("http://google.de/?q=goog&unsafe=active&" +
                       kSsuiParameter,
                       "q=goog&unsafe=active&" + kBothParameters);

  CheckAddedParameters("http://google.de/?q=goog&safe=off&ssui=off",
                       "q=goog&" + kBothParameters);

  // Test various combinations where we should not add anything.
  CheckAddedParameters("http://google.com/?q=goog&" + kSsuiParameter + "&" +
                       kSafeParameter,
                       "q=goog&" + kBothParameters);

  CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&q=goog&" +
                       kSafeParameter,
                       "q=goog&" + kBothParameters);

  CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&" +
                       kSafeParameter + "&q=goog",
                       "q=goog&" + kBothParameters);

  // Test that another website is not affected, without parameters.
  CheckAddedParameters("http://google.com/finance", std::string());

  // Test that another website is not affected, with parameters.
  CheckAddedParameters("http://google.com/finance?q=goog", "q=goog");

  // Test with percent-encoded data (%26 is &)
  CheckAddedParameters("http://google.com/?q=%26%26%26&" + kSsuiParameter +
                       "&" + kSafeParameter + "&param=%26%26%26",
                       "q=%26%26%26&param=%26%26%26&" + kBothParameters);
}

TEST_F(SafeSearchUtilTest, SetYoutubeHeader) {
  scoped_ptr<net::URLRequest> request = CreateYoutubeRequest();
  net::HttpRequestHeaders headers;
  safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers);
  std::string value;
  EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value));
  EXPECT_EQ("Active", value);
}

TEST_F(SafeSearchUtilTest, OverrideYoutubeHeader) {
  scoped_ptr<net::URLRequest> request = CreateYoutubeRequest();
  net::HttpRequestHeaders headers;
  headers.SetHeader("Youtube-Safety-Mode", "Off");
  safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers);
  std::string value;
  EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value));
  EXPECT_EQ("Active", value);
}

TEST_F(SafeSearchUtilTest, DoesntTouchNonYoutubeURL) {
  scoped_ptr<net::URLRequest> request = CreateNonYoutubeRequest();
  net::HttpRequestHeaders headers;
  headers.SetHeader("Youtube-Safety-Mode", "Off");
  safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers);
  std::string value;
  EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value));
  EXPECT_EQ("Off", value);
}