aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/http/http_proxy_test.cc
blob: a0d1bfa1bbae12af5116ed81241d9dd35e81623d (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
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2017 The Chromium OS 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 <brillo/http/http_proxy.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <base/bind.h>
#include <brillo/http/http_transport.h>
#include <chromeos/dbus/service_constants.h>
#include <dbus/mock_bus.h>
#include <dbus/mock_object_proxy.h>
#include <gtest/gtest.h>

using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Invoke;
using ::testing::Return;

namespace {
constexpr char kTestUrl[] = "http://www.example.com/test";
}  // namespace

namespace brillo {
namespace http {

class HttpProxyTest : public testing::Test {
 public:
  void ResolveProxyHandlerAsync(dbus::MethodCall* method_call,
                                int timeout_msec,
                                dbus::ObjectProxy::ResponseCallback* callback) {
    if (null_dbus_response_) {
      std::move(*callback).Run(nullptr);
      return;
    }
    std::move(*callback).Run(CreateDBusResponse(method_call).get());
  }

  std::unique_ptr<dbus::Response> ResolveProxyHandler(
      dbus::MethodCall* method_call, int timeout_msec) {
    if (null_dbus_response_) {
      return std::unique_ptr<dbus::Response>();
    }
    return CreateDBusResponse(method_call);
  }

  MOCK_METHOD(void,
              GetProxiesCallback,
              (bool, const std::vector<std::string>&));

 protected:
  HttpProxyTest() {
    dbus::Bus::Options options;
    options.bus_type = dbus::Bus::SYSTEM;
    bus_ = new dbus::MockBus(options);
    object_proxy_ = new dbus::MockObjectProxy(
        bus_.get(), chromeos::kNetworkProxyServiceName,
        dbus::ObjectPath(chromeos::kNetworkProxyServicePath));
    EXPECT_CALL(*bus_, GetObjectProxy(chromeos::kNetworkProxyServiceName,
                                      dbus::ObjectPath(
                                          chromeos::kNetworkProxyServicePath)))
        .WillOnce(Return(object_proxy_.get()));
  }

  std::unique_ptr<dbus::Response> CreateDBusResponse(
      dbus::MethodCall* method_call) {
    EXPECT_EQ(method_call->GetInterface(),
              chromeos::kNetworkProxyServiceInterface);
    EXPECT_EQ(method_call->GetMember(),
              chromeos::kNetworkProxyServiceResolveProxyMethod);
    method_call->SetSerial(1);  // Needs to be non-zero or it fails.
    std::unique_ptr<dbus::Response> response =
        dbus::Response::FromMethodCall(method_call);
    dbus::MessageWriter writer(response.get());
    writer.AppendString(proxy_info_);
    if (invalid_dbus_response_) {
      return response;
    }
    writer.AppendString(proxy_err_);
    return response;
  }

  scoped_refptr<dbus::MockBus> bus_;
  scoped_refptr<dbus::MockObjectProxy> object_proxy_;

  std::string proxy_info_;
  std::string proxy_err_;
  bool null_dbus_response_ = false;
  bool invalid_dbus_response_ = false;

 private:
  DISALLOW_COPY_AND_ASSIGN(HttpProxyTest);
};

TEST_F(HttpProxyTest, DBusNullResponseFails) {
  std::vector<std::string> proxies;
  null_dbus_response_ = true;
  EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
  EXPECT_FALSE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
}

TEST_F(HttpProxyTest, DBusInvalidResponseFails) {
  std::vector<std::string> proxies;
  invalid_dbus_response_ = true;
  EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
  EXPECT_FALSE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
}

TEST_F(HttpProxyTest, NoProxies) {
  std::vector<std::string> proxies;
  EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
  EXPECT_TRUE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
  EXPECT_THAT(proxies, ElementsAre(kDirectProxy));
}

TEST_F(HttpProxyTest, MultipleProxiesWithoutDirect) {
  proxy_info_ = "proxy example.com; socks5 foo.com;";
  std::vector<std::string> proxies;
  EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
  EXPECT_TRUE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
  EXPECT_THAT(proxies, ElementsAre("http://example.com", "socks5://foo.com",
                                   kDirectProxy));
}

TEST_F(HttpProxyTest, MultipleProxiesWithDirect) {
  proxy_info_ = "socks foo.com; Https example.com ; badproxy example2.com ; "
                "socks5 test.com  ; proxy foobar.com; DIRECT ";
  std::vector<std::string> proxies;
  EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
  EXPECT_TRUE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
  EXPECT_THAT(proxies, ElementsAre("socks4://foo.com", "https://example.com",
                                   "socks5://test.com", "http://foobar.com",
                                   kDirectProxy));
}

TEST_F(HttpProxyTest, DBusNullResponseFailsAsync) {
  null_dbus_response_ = true;
  EXPECT_CALL(*object_proxy_, DoCallMethod(_, _, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandlerAsync));
  EXPECT_CALL(*this, GetProxiesCallback(false, _));
  GetChromeProxyServersAsync(
      bus_, kTestUrl,
      base::Bind(&HttpProxyTest::GetProxiesCallback, base::Unretained(this)));
}

TEST_F(HttpProxyTest, DBusInvalidResponseFailsAsync) {
  invalid_dbus_response_ = true;
  EXPECT_CALL(*object_proxy_, DoCallMethod(_, _, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandlerAsync));
  EXPECT_CALL(*this, GetProxiesCallback(false, _));
  GetChromeProxyServersAsync(
      bus_, kTestUrl,
      base::Bind(&HttpProxyTest::GetProxiesCallback, base::Unretained(this)));
}

// We don't need to test all the proxy cases with async because that will be
// using the same internal parsing code.
TEST_F(HttpProxyTest, MultipleProxiesWithDirectAsync) {
  proxy_info_ = "socks foo.com; Https example.com ; badproxy example2.com ; "
                "socks5 test.com  ; proxy foobar.com; DIRECT ";
  std::vector<std::string> expected = {
      "socks4://foo.com", "https://example.com", "socks5://test.com",
      "http://foobar.com", kDirectProxy};
  EXPECT_CALL(*object_proxy_, DoCallMethod(_, _, _))
      .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandlerAsync));
  EXPECT_CALL(*this, GetProxiesCallback(true, expected));
  GetChromeProxyServersAsync(
      bus_, kTestUrl,
      base::Bind(&HttpProxyTest::GetProxiesCallback, base::Unretained(this)));
}

}  // namespace http
}  // namespace brillo