blob: 484aae129e07c400dd9301a8f62a1e282e30edc4 [file] [log] [blame]
Alex Deymo35821942017-02-05 04:36:02 +00001//
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>
Alex Deymo35821942017-02-05 04:36:02 +000025#include <brillo/message_loops/fake_message_loop.h>
26
27using std::deque;
28using std::string;
29
30namespace chromeos_update_engine {
31
32class ProxyResolverTest : public ::testing::Test {
33 protected:
34 virtual ~ProxyResolverTest() = default;
35
36 void SetUp() override { loop_.SetAsCurrent(); }
37
38 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
39
40 brillo::FakeMessageLoop loop_{nullptr};
41 DirectProxyResolver resolver_;
42};
43
44TEST_F(ProxyResolverTest, DirectProxyResolverCallbackTest) {
45 bool called = false;
46 deque<string> callback_proxies;
47 auto callback = base::Bind(
48 [](bool* called,
49 deque<string>* callback_proxies,
50 const deque<string>& proxies) {
51 *called = true;
52 *callback_proxies = proxies;
53 },
54 &called,
55 &callback_proxies);
56
57 EXPECT_NE(kProxyRequestIdNull,
58 resolver_.GetProxiesForUrl("http://foo", callback));
59 // Check the callback is not called until the message loop runs.
60 EXPECT_FALSE(called);
61 loop_.Run();
62 EXPECT_TRUE(called);
63 EXPECT_EQ(kNoProxy, callback_proxies.front());
64}
65
66TEST_F(ProxyResolverTest, DirectProxyResolverCancelCallbackTest) {
67 bool called = false;
68 auto callback = base::Bind(
69 [](bool* called, const deque<string>& proxies) { *called = true; },
70 &called);
71
72 ProxyRequestId request = resolver_.GetProxiesForUrl("http://foo", callback);
73 EXPECT_FALSE(called);
74 EXPECT_TRUE(resolver_.CancelProxyRequest(request));
75 loop_.Run();
76 EXPECT_FALSE(called);
77}
78
79TEST_F(ProxyResolverTest, DirectProxyResolverSimultaneousCallbacksTest) {
80 int called = 0;
81 auto callback = base::Bind(
82 [](int* called, const deque<string>& proxies) { (*called)++; }, &called);
83
84 resolver_.GetProxiesForUrl("http://foo", callback);
85 resolver_.GetProxiesForUrl("http://bar", callback);
86 EXPECT_EQ(0, called);
87 loop_.Run();
88 EXPECT_EQ(2, called);
89}
90
91} // namespace chromeos_update_engine