xref: /aosp_15_r20/external/libbrillo/brillo/http/http_proxy_test.cc (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2017 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #include <brillo/http/http_proxy.h>
6*1a96fba6SXin Li 
7*1a96fba6SXin Li #include <memory>
8*1a96fba6SXin Li #include <string>
9*1a96fba6SXin Li #include <utility>
10*1a96fba6SXin Li #include <vector>
11*1a96fba6SXin Li 
12*1a96fba6SXin Li #include <base/bind.h>
13*1a96fba6SXin Li #include <brillo/http/http_transport.h>
14*1a96fba6SXin Li #include <chromeos/dbus/service_constants.h>
15*1a96fba6SXin Li #include <dbus/mock_bus.h>
16*1a96fba6SXin Li #include <dbus/mock_object_proxy.h>
17*1a96fba6SXin Li #include <gtest/gtest.h>
18*1a96fba6SXin Li 
19*1a96fba6SXin Li using ::testing::_;
20*1a96fba6SXin Li using ::testing::ElementsAre;
21*1a96fba6SXin Li using ::testing::Invoke;
22*1a96fba6SXin Li using ::testing::Return;
23*1a96fba6SXin Li 
24*1a96fba6SXin Li namespace {
25*1a96fba6SXin Li constexpr char kTestUrl[] = "http://www.example.com/test";
26*1a96fba6SXin Li }  // namespace
27*1a96fba6SXin Li 
28*1a96fba6SXin Li namespace brillo {
29*1a96fba6SXin Li namespace http {
30*1a96fba6SXin Li 
31*1a96fba6SXin Li class HttpProxyTest : public testing::Test {
32*1a96fba6SXin Li  public:
ResolveProxyHandlerAsync(dbus::MethodCall * method_call,int timeout_msec,dbus::ObjectProxy::ResponseCallback * callback)33*1a96fba6SXin Li   void ResolveProxyHandlerAsync(dbus::MethodCall* method_call,
34*1a96fba6SXin Li                                 int timeout_msec,
35*1a96fba6SXin Li                                 dbus::ObjectProxy::ResponseCallback* callback) {
36*1a96fba6SXin Li     if (null_dbus_response_) {
37*1a96fba6SXin Li       std::move(*callback).Run(nullptr);
38*1a96fba6SXin Li       return;
39*1a96fba6SXin Li     }
40*1a96fba6SXin Li     std::move(*callback).Run(CreateDBusResponse(method_call).get());
41*1a96fba6SXin Li   }
42*1a96fba6SXin Li 
ResolveProxyHandler(dbus::MethodCall * method_call,int timeout_msec)43*1a96fba6SXin Li   std::unique_ptr<dbus::Response> ResolveProxyHandler(
44*1a96fba6SXin Li       dbus::MethodCall* method_call, int timeout_msec) {
45*1a96fba6SXin Li     if (null_dbus_response_) {
46*1a96fba6SXin Li       return std::unique_ptr<dbus::Response>();
47*1a96fba6SXin Li     }
48*1a96fba6SXin Li     return CreateDBusResponse(method_call);
49*1a96fba6SXin Li   }
50*1a96fba6SXin Li 
51*1a96fba6SXin Li   MOCK_METHOD(void,
52*1a96fba6SXin Li               GetProxiesCallback,
53*1a96fba6SXin Li               (bool, const std::vector<std::string>&));
54*1a96fba6SXin Li 
55*1a96fba6SXin Li  protected:
HttpProxyTest()56*1a96fba6SXin Li   HttpProxyTest() {
57*1a96fba6SXin Li     dbus::Bus::Options options;
58*1a96fba6SXin Li     options.bus_type = dbus::Bus::SYSTEM;
59*1a96fba6SXin Li     bus_ = new dbus::MockBus(options);
60*1a96fba6SXin Li     object_proxy_ = new dbus::MockObjectProxy(
61*1a96fba6SXin Li         bus_.get(), chromeos::kNetworkProxyServiceName,
62*1a96fba6SXin Li         dbus::ObjectPath(chromeos::kNetworkProxyServicePath));
63*1a96fba6SXin Li     EXPECT_CALL(*bus_, GetObjectProxy(chromeos::kNetworkProxyServiceName,
64*1a96fba6SXin Li                                       dbus::ObjectPath(
65*1a96fba6SXin Li                                           chromeos::kNetworkProxyServicePath)))
66*1a96fba6SXin Li         .WillOnce(Return(object_proxy_.get()));
67*1a96fba6SXin Li   }
68*1a96fba6SXin Li 
CreateDBusResponse(dbus::MethodCall * method_call)69*1a96fba6SXin Li   std::unique_ptr<dbus::Response> CreateDBusResponse(
70*1a96fba6SXin Li       dbus::MethodCall* method_call) {
71*1a96fba6SXin Li     EXPECT_EQ(method_call->GetInterface(),
72*1a96fba6SXin Li               chromeos::kNetworkProxyServiceInterface);
73*1a96fba6SXin Li     EXPECT_EQ(method_call->GetMember(),
74*1a96fba6SXin Li               chromeos::kNetworkProxyServiceResolveProxyMethod);
75*1a96fba6SXin Li     method_call->SetSerial(1);  // Needs to be non-zero or it fails.
76*1a96fba6SXin Li     std::unique_ptr<dbus::Response> response =
77*1a96fba6SXin Li         dbus::Response::FromMethodCall(method_call);
78*1a96fba6SXin Li     dbus::MessageWriter writer(response.get());
79*1a96fba6SXin Li     writer.AppendString(proxy_info_);
80*1a96fba6SXin Li     if (invalid_dbus_response_) {
81*1a96fba6SXin Li       return response;
82*1a96fba6SXin Li     }
83*1a96fba6SXin Li     writer.AppendString(proxy_err_);
84*1a96fba6SXin Li     return response;
85*1a96fba6SXin Li   }
86*1a96fba6SXin Li 
87*1a96fba6SXin Li   scoped_refptr<dbus::MockBus> bus_;
88*1a96fba6SXin Li   scoped_refptr<dbus::MockObjectProxy> object_proxy_;
89*1a96fba6SXin Li 
90*1a96fba6SXin Li   std::string proxy_info_;
91*1a96fba6SXin Li   std::string proxy_err_;
92*1a96fba6SXin Li   bool null_dbus_response_ = false;
93*1a96fba6SXin Li   bool invalid_dbus_response_ = false;
94*1a96fba6SXin Li 
95*1a96fba6SXin Li  private:
96*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(HttpProxyTest);
97*1a96fba6SXin Li };
98*1a96fba6SXin Li 
TEST_F(HttpProxyTest,DBusNullResponseFails)99*1a96fba6SXin Li TEST_F(HttpProxyTest, DBusNullResponseFails) {
100*1a96fba6SXin Li   std::vector<std::string> proxies;
101*1a96fba6SXin Li   null_dbus_response_ = true;
102*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
103*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
104*1a96fba6SXin Li   EXPECT_FALSE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
105*1a96fba6SXin Li }
106*1a96fba6SXin Li 
TEST_F(HttpProxyTest,DBusInvalidResponseFails)107*1a96fba6SXin Li TEST_F(HttpProxyTest, DBusInvalidResponseFails) {
108*1a96fba6SXin Li   std::vector<std::string> proxies;
109*1a96fba6SXin Li   invalid_dbus_response_ = true;
110*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
111*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
112*1a96fba6SXin Li   EXPECT_FALSE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
113*1a96fba6SXin Li }
114*1a96fba6SXin Li 
TEST_F(HttpProxyTest,NoProxies)115*1a96fba6SXin Li TEST_F(HttpProxyTest, NoProxies) {
116*1a96fba6SXin Li   std::vector<std::string> proxies;
117*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
118*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
119*1a96fba6SXin Li   EXPECT_TRUE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
120*1a96fba6SXin Li   EXPECT_THAT(proxies, ElementsAre(kDirectProxy));
121*1a96fba6SXin Li }
122*1a96fba6SXin Li 
TEST_F(HttpProxyTest,MultipleProxiesWithoutDirect)123*1a96fba6SXin Li TEST_F(HttpProxyTest, MultipleProxiesWithoutDirect) {
124*1a96fba6SXin Li   proxy_info_ = "proxy example.com; socks5 foo.com;";
125*1a96fba6SXin Li   std::vector<std::string> proxies;
126*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
127*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
128*1a96fba6SXin Li   EXPECT_TRUE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
129*1a96fba6SXin Li   EXPECT_THAT(proxies, ElementsAre("http://example.com", "socks5://foo.com",
130*1a96fba6SXin Li                                    kDirectProxy));
131*1a96fba6SXin Li }
132*1a96fba6SXin Li 
TEST_F(HttpProxyTest,MultipleProxiesWithDirect)133*1a96fba6SXin Li TEST_F(HttpProxyTest, MultipleProxiesWithDirect) {
134*1a96fba6SXin Li   proxy_info_ = "socks foo.com; Https example.com ; badproxy example2.com ; "
135*1a96fba6SXin Li                 "socks5 test.com  ; proxy foobar.com; DIRECT ";
136*1a96fba6SXin Li   std::vector<std::string> proxies;
137*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, CallMethodAndBlock(_, _))
138*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandler));
139*1a96fba6SXin Li   EXPECT_TRUE(GetChromeProxyServers(bus_, kTestUrl, &proxies));
140*1a96fba6SXin Li   EXPECT_THAT(proxies, ElementsAre("socks4://foo.com", "https://example.com",
141*1a96fba6SXin Li                                    "socks5://test.com", "http://foobar.com",
142*1a96fba6SXin Li                                    kDirectProxy));
143*1a96fba6SXin Li }
144*1a96fba6SXin Li 
TEST_F(HttpProxyTest,DBusNullResponseFailsAsync)145*1a96fba6SXin Li TEST_F(HttpProxyTest, DBusNullResponseFailsAsync) {
146*1a96fba6SXin Li   null_dbus_response_ = true;
147*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, DoCallMethod(_, _, _))
148*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandlerAsync));
149*1a96fba6SXin Li   EXPECT_CALL(*this, GetProxiesCallback(false, _));
150*1a96fba6SXin Li   GetChromeProxyServersAsync(
151*1a96fba6SXin Li       bus_, kTestUrl,
152*1a96fba6SXin Li       base::Bind(&HttpProxyTest::GetProxiesCallback, base::Unretained(this)));
153*1a96fba6SXin Li }
154*1a96fba6SXin Li 
TEST_F(HttpProxyTest,DBusInvalidResponseFailsAsync)155*1a96fba6SXin Li TEST_F(HttpProxyTest, DBusInvalidResponseFailsAsync) {
156*1a96fba6SXin Li   invalid_dbus_response_ = true;
157*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, DoCallMethod(_, _, _))
158*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandlerAsync));
159*1a96fba6SXin Li   EXPECT_CALL(*this, GetProxiesCallback(false, _));
160*1a96fba6SXin Li   GetChromeProxyServersAsync(
161*1a96fba6SXin Li       bus_, kTestUrl,
162*1a96fba6SXin Li       base::Bind(&HttpProxyTest::GetProxiesCallback, base::Unretained(this)));
163*1a96fba6SXin Li }
164*1a96fba6SXin Li 
165*1a96fba6SXin Li // We don't need to test all the proxy cases with async because that will be
166*1a96fba6SXin Li // using the same internal parsing code.
TEST_F(HttpProxyTest,MultipleProxiesWithDirectAsync)167*1a96fba6SXin Li TEST_F(HttpProxyTest, MultipleProxiesWithDirectAsync) {
168*1a96fba6SXin Li   proxy_info_ = "socks foo.com; Https example.com ; badproxy example2.com ; "
169*1a96fba6SXin Li                 "socks5 test.com  ; proxy foobar.com; DIRECT ";
170*1a96fba6SXin Li   std::vector<std::string> expected = {
171*1a96fba6SXin Li       "socks4://foo.com", "https://example.com", "socks5://test.com",
172*1a96fba6SXin Li       "http://foobar.com", kDirectProxy};
173*1a96fba6SXin Li   EXPECT_CALL(*object_proxy_, DoCallMethod(_, _, _))
174*1a96fba6SXin Li       .WillOnce(Invoke(this, &HttpProxyTest::ResolveProxyHandlerAsync));
175*1a96fba6SXin Li   EXPECT_CALL(*this, GetProxiesCallback(true, expected));
176*1a96fba6SXin Li   GetChromeProxyServersAsync(
177*1a96fba6SXin Li       bus_, kTestUrl,
178*1a96fba6SXin Li       base::Bind(&HttpProxyTest::GetProxiesCallback, base::Unretained(this)));
179*1a96fba6SXin Li }
180*1a96fba6SXin Li 
181*1a96fba6SXin Li }  // namespace http
182*1a96fba6SXin Li }  // namespace brillo
183