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.
7 #include "base/callback.h"
8 #include "base/json/json_writer.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/values.h"
13 #include "chrome/browser/supervised_user/experimental/supervised_user_async_url_checker.h"
14 #include "net/url_request/test_url_fetcher_factory.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
24 const char kCx
[] = "somecsecx";
25 const size_t kCacheSize
= 2;
27 const int kSupervisedUserAsyncURLCheckerSafeURLFetcherID
= 0;
28 const int kSupervisedUserAsyncURLCheckerUnsafeURLFetcherID
= 1;
30 const char* kURLs
[] = {
31 "http://www.randomsite1.com",
32 "http://www.randomsite2.com",
33 "http://www.randomsite3.com",
34 "http://www.randomsite4.com",
35 "http://www.randomsite5.com",
36 "http://www.randomsite6.com",
37 "http://www.randomsite7.com",
38 "http://www.randomsite8.com",
39 "http://www.randomsite9.com",
42 std::string
BuildResponse(const GURL
& url
) {
43 base::DictionaryValue dict
;
44 base::DictionaryValue
* search_info_dict
= new base::DictionaryValue
;
45 std::string result_count
= url
.is_valid() ? "1" : "0";
46 search_info_dict
->SetStringWithoutPathExpansion("totalResults",
48 dict
.SetWithoutPathExpansion("searchInformation", search_info_dict
);
49 if (result_count
!= "0") {
50 base::ListValue
* results_list
= new base::ListValue
;
51 base::DictionaryValue
* result_dict
= new base::DictionaryValue
;
52 result_dict
->SetStringWithoutPathExpansion("link", url
.spec());
53 results_list
->Append(result_dict
);
54 dict
.SetWithoutPathExpansion("items", results_list
);
57 base::JSONWriter::Write(&dict
, &result
);
63 class SupervisedUserAsyncURLCheckerTest
: public testing::Test
{
65 SupervisedUserAsyncURLCheckerTest()
67 request_context_(new net::TestURLRequestContextGetter(
68 base::MessageLoopProxy::current())),
69 checker_(request_context_
.get(), kCx
, kCacheSize
) {
72 MOCK_METHOD3(OnCheckDone
,
74 SupervisedUserURLFilter::FilteringBehavior behavior
,
79 CHECK(next_url_
< arraysize(kURLs
));
80 return GURL(kURLs
[next_url_
++]);
83 // Returns true if the result was returned synchronously (cache hit).
84 bool CheckURL(const GURL
& url
) {
85 return checker_
.CheckURL(
87 base::Bind(&SupervisedUserAsyncURLCheckerTest::OnCheckDone
,
88 base::Unretained(this)));
91 net::TestURLFetcher
* GetURLFetcher(bool safe
) {
92 int id
= safe
? kSupervisedUserAsyncURLCheckerSafeURLFetcherID
93 : kSupervisedUserAsyncURLCheckerUnsafeURLFetcherID
;
94 net::TestURLFetcher
* url_fetcher
= url_fetcher_factory_
.GetFetcherByID(id
);
95 EXPECT_TRUE(url_fetcher
);
99 void SendResponse(bool safe
,
100 net::URLRequestStatus::Status status
,
101 const std::string
& response
) {
102 net::TestURLFetcher
* url_fetcher
= GetURLFetcher(safe
);
103 url_fetcher
->set_status(net::URLRequestStatus(status
, 0));
104 url_fetcher
->set_response_code(net::HTTP_OK
);
105 url_fetcher
->SetResponseString(response
);
106 url_fetcher
->delegate()->OnURLFetchComplete(url_fetcher
);
109 void SendValidResponse(bool safe
, const GURL
& url
) {
110 SendResponse(safe
, net::URLRequestStatus::SUCCESS
, BuildResponse(url
));
113 void SendFailedResponse(bool safe
) {
114 SendResponse(safe
, net::URLRequestStatus::CANCELED
, std::string());
118 base::MessageLoop message_loop_
;
119 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
120 net::TestURLFetcherFactory url_fetcher_factory_
;
121 SupervisedUserAsyncURLChecker checker_
;
124 TEST_F(SupervisedUserAsyncURLCheckerTest
, Simple
) {
126 GURL
url(GetNewURL());
127 EXPECT_FALSE(CheckURL(url
));
128 // "URL found" response from safe fetcher should immediately give a
129 // "not blocked" result.
130 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::ALLOW
, false));
131 SendValidResponse(true, url
);
134 GURL
url(GetNewURL());
135 EXPECT_FALSE(CheckURL(url
));
136 // "URL not found" response from safe fetcher should not immediately give a
138 EXPECT_CALL(*this, OnCheckDone(_
, _
, _
)).Times(0);
139 SendValidResponse(true, GURL());
140 // "URL found" response from unsafe fetcher should give a "blocked" result.
141 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::BLOCK
, false));
142 SendValidResponse(false, url
);
145 GURL
url(GetNewURL());
146 EXPECT_FALSE(CheckURL(url
));
147 // "URL found" response from unsafe fetcher should not immediately give a
149 EXPECT_CALL(*this, OnCheckDone(_
, _
, _
)).Times(0);
150 SendValidResponse(false, url
);
151 // "URL not found" response from safe fetcher should give a "blocked"
153 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::BLOCK
, false));
154 SendValidResponse(true, GURL());
157 GURL
url(GetNewURL());
158 EXPECT_FALSE(CheckURL(url
));
159 // "URL not found" response from unsafe fetcher should immediately give a
160 // "not blocked (but uncertain)" result.
161 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::ALLOW
, true));
162 SendValidResponse(false, GURL());
166 TEST_F(SupervisedUserAsyncURLCheckerTest
, Equivalence
) {
167 // Leading "www." in the response should be ignored.
169 GURL
url("http://example.com");
170 GURL
url_response("http://www.example.com");
171 EXPECT_FALSE(CheckURL(url
));
172 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::ALLOW
, false));
173 SendValidResponse(true, url_response
);
175 // Scheme should be ignored.
177 GURL
url("http://www.example2.com");
178 GURL
url_response("https://www.example2.com");
179 EXPECT_FALSE(CheckURL(url
));
180 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::ALLOW
, false));
181 SendValidResponse(true, url_response
);
183 // Both at the same time should work as well.
185 GURL
url("http://example3.com");
186 GURL
url_response("https://www.example3.com");
187 EXPECT_FALSE(CheckURL(url
));
188 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::ALLOW
, false));
189 SendValidResponse(true, url_response
);
193 TEST_F(SupervisedUserAsyncURLCheckerTest
, Cache
) {
194 // One more URL than fit in the cache.
195 ASSERT_EQ(2u, kCacheSize
);
196 GURL
url1(GetNewURL());
197 GURL
url2(GetNewURL());
198 GURL
url3(GetNewURL());
200 // Populate the cache.
201 EXPECT_FALSE(CheckURL(url1
));
202 EXPECT_CALL(*this, OnCheckDone(url1
, SupervisedUserURLFilter::ALLOW
, false));
203 SendValidResponse(true, url1
);
204 EXPECT_FALSE(CheckURL(url2
));
205 EXPECT_CALL(*this, OnCheckDone(url2
, SupervisedUserURLFilter::ALLOW
, false));
206 SendValidResponse(true, url2
);
208 // Now we should get results synchronously.
209 EXPECT_CALL(*this, OnCheckDone(url2
, SupervisedUserURLFilter::ALLOW
, false));
210 EXPECT_TRUE(CheckURL(url2
));
211 EXPECT_CALL(*this, OnCheckDone(url1
, SupervisedUserURLFilter::ALLOW
, false));
212 EXPECT_TRUE(CheckURL(url1
));
214 // Now |url2| is the LRU and should be evicted on the next check.
215 EXPECT_FALSE(CheckURL(url3
));
216 EXPECT_CALL(*this, OnCheckDone(url3
, SupervisedUserURLFilter::ALLOW
, false));
217 SendValidResponse(true, url3
);
219 EXPECT_FALSE(CheckURL(url2
));
220 EXPECT_CALL(*this, OnCheckDone(url2
, SupervisedUserURLFilter::ALLOW
, false));
221 SendValidResponse(true, url2
);
224 TEST_F(SupervisedUserAsyncURLCheckerTest
, CoalesceRequestsToSameURL
) {
225 GURL
url(GetNewURL());
226 // Start two checks for the same URL.
227 EXPECT_FALSE(CheckURL(url
));
228 EXPECT_FALSE(CheckURL(url
));
229 // A single response should answer both checks.
230 EXPECT_CALL(*this, OnCheckDone(url
, SupervisedUserURLFilter::ALLOW
, false))
232 SendValidResponse(true, url
);