File: supervised_user_async_url_checker_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (233 lines) | stat: -rw-r--r-- 8,212 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 <string>

#include "base/callback.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/values.h"
#include "chrome/browser/supervised_user/experimental/supervised_user_async_url_checker.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

using testing::_;

namespace {

const char kCx[] = "somecsecx";
const size_t kCacheSize = 2;

const int kSupervisedUserAsyncURLCheckerSafeURLFetcherID = 0;
const int kSupervisedUserAsyncURLCheckerUnsafeURLFetcherID = 1;

const char* kURLs[] = {
  "http://www.randomsite1.com",
  "http://www.randomsite2.com",
  "http://www.randomsite3.com",
  "http://www.randomsite4.com",
  "http://www.randomsite5.com",
  "http://www.randomsite6.com",
  "http://www.randomsite7.com",
  "http://www.randomsite8.com",
  "http://www.randomsite9.com",
};

std::string BuildResponse(const GURL& url) {
  base::DictionaryValue dict;
  base::DictionaryValue* search_info_dict = new base::DictionaryValue;
  std::string result_count = url.is_valid() ? "1" : "0";
  search_info_dict->SetStringWithoutPathExpansion("totalResults",
                                                  result_count);
  dict.SetWithoutPathExpansion("searchInformation", search_info_dict);
  if (result_count != "0") {
    base::ListValue* results_list = new base::ListValue;
    base::DictionaryValue* result_dict = new base::DictionaryValue;
    result_dict->SetStringWithoutPathExpansion("link", url.spec());
    results_list->Append(result_dict);
    dict.SetWithoutPathExpansion("items", results_list);
  }
  std::string result;
  base::JSONWriter::Write(&dict, &result);
  return result;
}

}  // namespace

class SupervisedUserAsyncURLCheckerTest : public testing::Test {
 public:
  SupervisedUserAsyncURLCheckerTest()
      : next_url_(0),
        request_context_(new net::TestURLRequestContextGetter(
            base::MessageLoopProxy::current())),
        checker_(request_context_.get(), kCx, kCacheSize) {
  }

  MOCK_METHOD3(OnCheckDone,
               void(const GURL& url,
                    SupervisedUserURLFilter::FilteringBehavior behavior,
                    bool uncertain));

 protected:
  GURL GetNewURL() {
    CHECK(next_url_ < arraysize(kURLs));
    return GURL(kURLs[next_url_++]);
  }

  // Returns true if the result was returned synchronously (cache hit).
  bool CheckURL(const GURL& url) {
    return checker_.CheckURL(
        url,
        base::Bind(&SupervisedUserAsyncURLCheckerTest::OnCheckDone,
                   base::Unretained(this)));
  }

  net::TestURLFetcher* GetURLFetcher(bool safe) {
    int id = safe ? kSupervisedUserAsyncURLCheckerSafeURLFetcherID
                  : kSupervisedUserAsyncURLCheckerUnsafeURLFetcherID;
    net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(id);
    EXPECT_TRUE(url_fetcher);
    return url_fetcher;
  }

  void SendResponse(bool safe,
                    net::URLRequestStatus::Status status,
                    const std::string& response) {
    net::TestURLFetcher* url_fetcher = GetURLFetcher(safe);
    url_fetcher->set_status(net::URLRequestStatus(status, 0));
    url_fetcher->set_response_code(net::HTTP_OK);
    url_fetcher->SetResponseString(response);
    url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
  }

  void SendValidResponse(bool safe, const GURL& url) {
    SendResponse(safe, net::URLRequestStatus::SUCCESS, BuildResponse(url));
  }

  void SendFailedResponse(bool safe) {
    SendResponse(safe, net::URLRequestStatus::CANCELED, std::string());
  }

  size_t next_url_;
  base::MessageLoop message_loop_;
  scoped_refptr<net::TestURLRequestContextGetter> request_context_;
  net::TestURLFetcherFactory url_fetcher_factory_;
  SupervisedUserAsyncURLChecker checker_;
};

TEST_F(SupervisedUserAsyncURLCheckerTest, Simple) {
  {
    GURL url(GetNewURL());
    EXPECT_FALSE(CheckURL(url));
    // "URL found" response from safe fetcher should immediately give a
    // "not blocked" result.
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::ALLOW, false));
    SendValidResponse(true, url);
  }
  {
    GURL url(GetNewURL());
    EXPECT_FALSE(CheckURL(url));
    // "URL not found" response from safe fetcher should not immediately give a
    // result.
    EXPECT_CALL(*this, OnCheckDone(_, _, _)).Times(0);
    SendValidResponse(true, GURL());
    // "URL found" response from unsafe fetcher should give a "blocked" result.
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::BLOCK, false));
    SendValidResponse(false, url);
  }
  {
    GURL url(GetNewURL());
    EXPECT_FALSE(CheckURL(url));
    // "URL found" response from unsafe fetcher should not immediately give a
    // result.
    EXPECT_CALL(*this, OnCheckDone(_, _, _)).Times(0);
    SendValidResponse(false, url);
    // "URL not found" response from safe fetcher should give a "blocked"
    // result.
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::BLOCK, false));
    SendValidResponse(true, GURL());
  }
  {
    GURL url(GetNewURL());
    EXPECT_FALSE(CheckURL(url));
    // "URL not found" response from unsafe fetcher should immediately give a
    // "not blocked (but uncertain)" result.
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::ALLOW, true));
    SendValidResponse(false, GURL());
  }
}

TEST_F(SupervisedUserAsyncURLCheckerTest, Equivalence) {
  // Leading "www." in the response should be ignored.
  {
    GURL url("http://example.com");
    GURL url_response("http://www.example.com");
    EXPECT_FALSE(CheckURL(url));
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::ALLOW, false));
    SendValidResponse(true, url_response);
  }
  // Scheme should be ignored.
  {
    GURL url("http://www.example2.com");
    GURL url_response("https://www.example2.com");
    EXPECT_FALSE(CheckURL(url));
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::ALLOW, false));
    SendValidResponse(true, url_response);
  }
  // Both at the same time should work as well.
  {
    GURL url("http://example3.com");
    GURL url_response("https://www.example3.com");
    EXPECT_FALSE(CheckURL(url));
    EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::ALLOW, false));
    SendValidResponse(true, url_response);
  }
}

TEST_F(SupervisedUserAsyncURLCheckerTest, Cache) {
  // One more URL than fit in the cache.
  ASSERT_EQ(2u, kCacheSize);
  GURL url1(GetNewURL());
  GURL url2(GetNewURL());
  GURL url3(GetNewURL());

  // Populate the cache.
  EXPECT_FALSE(CheckURL(url1));
  EXPECT_CALL(*this, OnCheckDone(url1, SupervisedUserURLFilter::ALLOW, false));
  SendValidResponse(true, url1);
  EXPECT_FALSE(CheckURL(url2));
  EXPECT_CALL(*this, OnCheckDone(url2, SupervisedUserURLFilter::ALLOW, false));
  SendValidResponse(true, url2);

  // Now we should get results synchronously.
  EXPECT_CALL(*this, OnCheckDone(url2, SupervisedUserURLFilter::ALLOW, false));
  EXPECT_TRUE(CheckURL(url2));
  EXPECT_CALL(*this, OnCheckDone(url1, SupervisedUserURLFilter::ALLOW, false));
  EXPECT_TRUE(CheckURL(url1));

  // Now |url2| is the LRU and should be evicted on the next check.
  EXPECT_FALSE(CheckURL(url3));
  EXPECT_CALL(*this, OnCheckDone(url3, SupervisedUserURLFilter::ALLOW, false));
  SendValidResponse(true, url3);

  EXPECT_FALSE(CheckURL(url2));
  EXPECT_CALL(*this, OnCheckDone(url2, SupervisedUserURLFilter::ALLOW, false));
  SendValidResponse(true, url2);
}

TEST_F(SupervisedUserAsyncURLCheckerTest, CoalesceRequestsToSameURL) {
  GURL url(GetNewURL());
  // Start two checks for the same URL.
  EXPECT_FALSE(CheckURL(url));
  EXPECT_FALSE(CheckURL(url));
  // A single response should answer both checks.
  EXPECT_CALL(*this, OnCheckDone(url, SupervisedUserURLFilter::ALLOW, false))
      .Times(2);
  SendValidResponse(true, url);
}