1 //
2 // Copyright (C) 2017 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/proxy_resolver.h"
18
19 #include <deque>
20 #include <string>
21
22 #include <gtest/gtest.h>
23
24 #include <base/bind.h>
25 #include <brillo/bind_lambda.h>
26 #include <brillo/message_loops/fake_message_loop.h>
27
28 using std::deque;
29 using std::string;
30
31 namespace chromeos_update_engine {
32
33 class ProxyResolverTest : public ::testing::Test {
34 protected:
35 virtual ~ProxyResolverTest() = default;
36
SetUp()37 void SetUp() override { loop_.SetAsCurrent(); }
38
TearDown()39 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
40
41 brillo::FakeMessageLoop loop_{nullptr};
42 DirectProxyResolver resolver_;
43 };
44
TEST_F(ProxyResolverTest,DirectProxyResolverCallbackTest)45 TEST_F(ProxyResolverTest, DirectProxyResolverCallbackTest) {
46 bool called = false;
47 deque<string> callback_proxies;
48 auto callback = base::Bind(
49 [](bool* called,
50 deque<string>* callback_proxies,
51 const deque<string>& proxies) {
52 *called = true;
53 *callback_proxies = proxies;
54 },
55 &called,
56 &callback_proxies);
57
58 EXPECT_NE(kProxyRequestIdNull,
59 resolver_.GetProxiesForUrl("http://foo", callback));
60 // Check the callback is not called until the message loop runs.
61 EXPECT_FALSE(called);
62 loop_.Run();
63 EXPECT_TRUE(called);
64 EXPECT_EQ(kNoProxy, callback_proxies.front());
65 }
66
TEST_F(ProxyResolverTest,DirectProxyResolverCancelCallbackTest)67 TEST_F(ProxyResolverTest, DirectProxyResolverCancelCallbackTest) {
68 bool called = false;
69 auto callback = base::Bind(
70 [](bool* called, const deque<string>& proxies) { *called = true; },
71 &called);
72
73 ProxyRequestId request = resolver_.GetProxiesForUrl("http://foo", callback);
74 EXPECT_FALSE(called);
75 EXPECT_TRUE(resolver_.CancelProxyRequest(request));
76 loop_.Run();
77 EXPECT_FALSE(called);
78 }
79
TEST_F(ProxyResolverTest,DirectProxyResolverSimultaneousCallbacksTest)80 TEST_F(ProxyResolverTest, DirectProxyResolverSimultaneousCallbacksTest) {
81 int called = 0;
82 auto callback = base::Bind(
83 [](int* called, const deque<string>& proxies) { (*called)++; }, &called);
84
85 resolver_.GetProxiesForUrl("http://foo", callback);
86 resolver_.GetProxiesForUrl("http://bar", callback);
87 EXPECT_EQ(0, called);
88 loop_.Run();
89 EXPECT_EQ(2, called);
90 }
91
92 } // namespace chromeos_update_engine
93