forked from LineageOS/android_system_update_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_browser_proxy_resolver_unittest.cc
180 lines (154 loc) · 6.03 KB
/
chrome_browser_proxy_resolver_unittest.cc
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
//
// Copyright (C) 2011 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/chrome_browser_proxy_resolver.h"
#include <deque>
#include <string>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <base/macros.h>
#include <brillo/errors/error.h>
#include "network_proxy/dbus-proxies.h"
#include "network_proxy/dbus-proxy-mocks.h"
#include "update_engine/dbus_test_utils.h"
using ::testing::DoAll;
using ::testing::SaveArg;
using ::testing::StrEq;
using ::testing::_;
using org::chromium::NetworkProxyServiceInterfaceProxyMock;
using std::deque;
using std::string;
using std::vector;
namespace chromeos_update_engine {
namespace {
// Callback for ProxyResolver::GetProxiesForUrl() that copies |src| to |dest|.
void CopyProxies(deque<string>* dest, const deque<string>& src) {
*dest = src;
}
} // namespace
class ChromeBrowserProxyResolverTest : public ::testing::Test {
public:
ChromeBrowserProxyResolverTest() = default;
~ChromeBrowserProxyResolverTest() override = default;
protected:
// Adds a GoogleMock expectation for a call to |dbus_proxy_|'s
// ResolveProxyAsync method to resolve |url|.
void AddResolveProxyExpectation(const std::string& url) {
EXPECT_CALL(dbus_proxy_, ResolveProxyAsync(StrEq(url), _, _, _))
.WillOnce(DoAll(SaveArg<1>(&success_callback_),
SaveArg<2>(&error_callback_)));
}
NetworkProxyServiceInterfaceProxyMock dbus_proxy_;
ChromeBrowserProxyResolver resolver_{&dbus_proxy_};
// Callbacks that were passed to |dbus_proxy_|'s ResolveProxyAsync method.
base::Callback<void(const std::string&, const std::string&)>
success_callback_;
base::Callback<void(brillo::Error*)> error_callback_;
private:
DISALLOW_COPY_AND_ASSIGN(ChromeBrowserProxyResolverTest);
};
TEST_F(ChromeBrowserProxyResolverTest, Parse) {
// Test ideas from
// http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list_unittest.cc
vector<string> inputs = {
"PROXY foopy:10",
" DIRECT", // leading space.
"PROXY foopy1 ; proxy foopy2;\t DIRECT",
"proxy foopy1 ; SOCKS foopy2",
"DIRECT ; proxy foopy1 ; DIRECT ; SOCKS5 foopy2;DIRECT ",
"DIRECT ; proxy foopy1:80; DIRECT ; DIRECT",
"PROXY-foopy:10",
"PROXY",
"PROXY foopy1 ; JUNK ; JUNK ; SOCKS5 foopy2 ; ;",
"HTTP foopy1; SOCKS5 foopy2",
};
vector<deque<string>> outputs = {
{"http://foopy:10", kNoProxy},
{kNoProxy},
{"http://foopy1", "http://foopy2", kNoProxy},
{"http://foopy1", "socks4://foopy2", kNoProxy},
{kNoProxy, "http://foopy1", kNoProxy, "socks5://foopy2", kNoProxy},
{kNoProxy, "http://foopy1:80", kNoProxy, kNoProxy},
{kNoProxy},
{kNoProxy},
{"http://foopy1", "socks5://foopy2", kNoProxy},
{"socks5://foopy2", kNoProxy},
};
ASSERT_EQ(inputs.size(), outputs.size());
for (size_t i = 0; i < inputs.size(); i++) {
deque<string> results =
ChromeBrowserProxyResolver::ParseProxyString(inputs[i]);
deque<string>& expected = outputs[i];
EXPECT_EQ(results.size(), expected.size()) << "i = " << i;
if (expected.size() != results.size())
continue;
for (size_t j = 0; j < expected.size(); j++) {
EXPECT_EQ(expected[j], results[j]) << "i = " << i;
}
}
}
TEST_F(ChromeBrowserProxyResolverTest, Success) {
const char kUrl[] = "http://example.com/blah";
const char kProxyConfig[] = "SOCKS5 192.168.52.83:5555;DIRECT";
AddResolveProxyExpectation(kUrl);
deque<string> proxies;
resolver_.GetProxiesForUrl(kUrl, base::Bind(&CopyProxies, &proxies));
// Run the D-Bus success callback and verify that the proxies are passed to
// the supplied function.
ASSERT_FALSE(success_callback_.is_null());
success_callback_.Run(kProxyConfig, string());
ASSERT_EQ(2u, proxies.size());
EXPECT_EQ("socks5://192.168.52.83:5555", proxies[0]);
EXPECT_EQ(kNoProxy, proxies[1]);
}
TEST_F(ChromeBrowserProxyResolverTest, Failure) {
const char kUrl[] = "http://example.com/blah";
AddResolveProxyExpectation(kUrl);
deque<string> proxies;
resolver_.GetProxiesForUrl(kUrl, base::Bind(&CopyProxies, &proxies));
// Run the D-Bus error callback and verify that the supplied function is
// instructed to use a direct connection.
ASSERT_FALSE(error_callback_.is_null());
brillo::ErrorPtr error = brillo::Error::Create(FROM_HERE, "", "", "");
error_callback_.Run(error.get());
ASSERT_EQ(1u, proxies.size());
EXPECT_EQ(kNoProxy, proxies[0]);
}
TEST_F(ChromeBrowserProxyResolverTest, CancelCallback) {
const char kUrl[] = "http://example.com/blah";
AddResolveProxyExpectation(kUrl);
int called = 0;
auto callback = base::Bind(
[](int* called, const deque<string>& proxies) { (*called)++; }, &called);
ProxyRequestId request = resolver_.GetProxiesForUrl(kUrl, callback);
// Cancel the request and then run the D-Bus success callback. The original
// callback shouldn't be run.
EXPECT_TRUE(resolver_.CancelProxyRequest(request));
ASSERT_FALSE(success_callback_.is_null());
success_callback_.Run("DIRECT", string());
EXPECT_EQ(0, called);
}
TEST_F(ChromeBrowserProxyResolverTest, CancelCallbackTwice) {
const char kUrl[] = "http://example.com/blah";
AddResolveProxyExpectation(kUrl);
deque<string> proxies;
ProxyRequestId request =
resolver_.GetProxiesForUrl(kUrl, base::Bind(&CopyProxies, &proxies));
// Cancel the same request twice. The second call should fail.
EXPECT_TRUE(resolver_.CancelProxyRequest(request));
EXPECT_FALSE(resolver_.CancelProxyRequest(request));
}
} // namespace chromeos_update_engine