Commit 80792614 authored by joi@chromium.org's avatar joi@chromium.org

Regression test for anti-DDoS bugs in ChromeNetworkDelegate.

This ensures:
a) That when g_never_throttle_requests_ is false, only
extension requests may be throttled (rather than the inverse);
b) That setting the flag applies to all instances of
ChromeNetworkDelegate, not just the one it is set on.

If we'd had these tests earlier, they would have prevented the
bugs below from being introduced.

TBR=thakis@chromium.org
BUG=141081,141643


Review URL: https://chromiumcodereview.appspot.com/10828284

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151683 0039d316-1c4b-4281-b951-d872f2087c98
parent 88c773bf
......@@ -63,6 +63,8 @@ class ChromeNetworkDelegate : public net::NetworkDelegate {
static void AllowAccessToAllFiles();
private:
friend class ChromeNetworkDelegateTest;
// NetworkDelegate implementation.
virtual int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
......
// Copyright (c) 2012 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/chrome_network_delegate.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/prefs/pref_member.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
class ChromeNetworkDelegateTest : public testing::Test {
protected:
ChromeNetworkDelegateTest()
: forwarder_(new extensions::EventRouterForwarder()) {
}
virtual void SetUp() OVERRIDE {
never_throttle_requests_original_value_ =
ChromeNetworkDelegate::g_never_throttle_requests_;
ChromeNetworkDelegate::g_never_throttle_requests_ = false;
}
virtual void TearDown() OVERRIDE {
ChromeNetworkDelegate::g_never_throttle_requests_ =
never_throttle_requests_original_value_;
}
scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() {
return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate(
forwarder_.get(), NULL, NULL, NULL, NULL, &pref_member_, NULL));
}
// Implementation moved here for access to private bits.
void NeverThrottleLogicImpl() {
scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
TestURLRequestContext context;
TestURLRequest extension_request(
GURL("http://example.com/"), NULL, &context);
extension_request.set_first_party_for_cookies(
GURL("chrome-extension://abcdef/bingo.html"));
TestURLRequest web_page_request(
GURL("http://example.com/"), NULL, &context);
web_page_request.set_first_party_for_cookies(
GURL("http://example.com/helloworld.html"));
ASSERT_TRUE(delegate->OnCanThrottleRequest(extension_request));
ASSERT_FALSE(delegate->OnCanThrottleRequest(web_page_request));
delegate->NeverThrottleRequests();
ASSERT_TRUE(ChromeNetworkDelegate::g_never_throttle_requests_);
ASSERT_FALSE(delegate->OnCanThrottleRequest(extension_request));
ASSERT_FALSE(delegate->OnCanThrottleRequest(web_page_request));
// Verify that the flag applies to later instances of the
// ChromeNetworkDelegate.
//
// We test the side effects of the flag rather than just the flag
// itself (which we did above) to help ensure that a changed
// implementation would show the same behavior, i.e. all instances
// of ChromeNetworkDelegate after the flag is set obey the flag.
scoped_ptr<ChromeNetworkDelegate> second_delegate(CreateNetworkDelegate());
ASSERT_FALSE(delegate->OnCanThrottleRequest(extension_request));
ASSERT_FALSE(delegate->OnCanThrottleRequest(web_page_request));
}
private:
bool never_throttle_requests_original_value_;
MessageLoopForIO message_loop_;
scoped_refptr<extensions::EventRouterForwarder> forwarder_;
BooleanPrefMember pref_member_;
};
TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) {
NeverThrottleLogicImpl();
}
......@@ -1372,6 +1372,7 @@
'browser/nacl_host/pnacl_file_host_unittest.cc',
'browser/net/chrome_fraudulent_certificate_reporter_unittest.cc',
'browser/net/chrome_net_log_unittest.cc',
'browser/net/chrome_network_delegate_unittest.cc',
'browser/net/clear_on_exit_policy_unittest.cc',
'browser/net/connection_tester_unittest.cc',
'browser/net/gaia/gaia_oauth_fetcher_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment