1// Copyright (c) 2009 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.
4
5#ifndef NET_BASE_NET_TEST_SUITE_H_
6#define NET_BASE_NET_TEST_SUITE_H_
7
8#include "base/message_loop.h"
9#include "base/ref_counted.h"
10#include "base/test/test_suite.h"
11#include "net/base/mock_host_resolver.h"
12
13class NetTestSuite : public TestSuite {
14 public:
15  NetTestSuite(int argc, char** argv) : TestSuite(argc, argv) {
16  }
17
18  virtual void Initialize() {
19    TestSuite::Initialize();
20    InitializeTestThread();
21  }
22
23  // Called from within Initialize(), but separate so that derived classes
24  // can initialize the NetTestSuite instance only and not
25  // TestSuite::Initialize().  TestSuite::Initialize() performs some global
26  // initialization that can only be done once.
27  void InitializeTestThread() {
28    host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL);
29    scoped_host_resolver_proc_.Init(host_resolver_proc_.get());
30    // In case any attempts are made to resolve host names, force them all to
31    // be mapped to localhost.  This prevents DNS queries from being sent in
32    // the process of running these unit tests.
33    host_resolver_proc_->AddRule("*", "127.0.0.1");
34
35    message_loop_.reset(new MessageLoopForIO());
36  }
37
38  virtual void Shutdown() {
39    // We want to destroy this here before the TestSuite continues to tear down
40    // the environment.
41    message_loop_.reset();
42
43    TestSuite::Shutdown();
44  }
45
46 private:
47  scoped_ptr<MessageLoop> message_loop_;
48  scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_proc_;
49  net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_;
50};
51
52#endif  // NET_BASE_NET_TEST_SUITE_H_
53