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
ash / components / arc / net / always_on_vpn_manager_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/components/arc/net/always_on_vpn_manager.h"
#include <string_view>
#include "ash/components/arc/arc_prefs.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
constexpr const char kVpnPackage[] = "com.android.vpn";
const base::Value kVpnPackageValue(kVpnPackage);
void OnGetProperties(bool* success_out,
std::string* package_name_out,
base::OnceClosure callback,
std::optional<base::Value::Dict> result) {
*success_out = result.has_value();
if (result) {
const std::string* value =
result->FindString(shill::kAlwaysOnVpnPackageProperty);
if (value != nullptr)
*package_name_out = *value;
}
std::move(callback).Run();
}
std::string GetAlwaysOnPackageName() {
bool success = false;
std::string package_name;
ash::ShillManagerClient* shill_manager = ash::ShillManagerClient::Get();
base::RunLoop run_loop;
shill_manager->GetProperties(
base::BindOnce(&OnGetProperties, base::Unretained(&success),
base::Unretained(&package_name), run_loop.QuitClosure()));
run_loop.Run();
EXPECT_TRUE(success);
return package_name;
}
} // namespace
namespace arc {
namespace {
class AlwaysOnVpnManagerTest : public testing::Test {
public:
AlwaysOnVpnManagerTest() = default;
AlwaysOnVpnManagerTest(const AlwaysOnVpnManagerTest&) = delete;
AlwaysOnVpnManagerTest& operator=(const AlwaysOnVpnManagerTest&) = delete;
void SetUp() override {
arc::prefs::RegisterProfilePrefs(pref_service()->registry());
}
TestingPrefServiceSimple* pref_service() { return &pref_service_; }
private:
content::BrowserTaskEnvironment task_environment_;
ash::NetworkHandlerTestHelper network_handler_test_helper_;
TestingPrefServiceSimple pref_service_;
};
TEST_F(AlwaysOnVpnManagerTest, SetPackageWhileLockdownUnset) {
auto always_on_manager = std::make_unique<AlwaysOnVpnManager>(
pref_service(), /*delay_lockdown_until_vpn_connected=*/false);
EXPECT_EQ(std::string(), GetAlwaysOnPackageName());
pref_service()->Set(arc::prefs::kAlwaysOnVpnPackage, kVpnPackageValue);
EXPECT_EQ(std::string(), GetAlwaysOnPackageName());
}
TEST_F(AlwaysOnVpnManagerTest, SetPackageWhileLockdownTrue) {
pref_service()->Set(arc::prefs::kAlwaysOnVpnLockdown, base::Value(true));
auto always_on_manager = std::make_unique<AlwaysOnVpnManager>(
pref_service(), /*delay_lockdown_until_vpn_connected=*/false);
EXPECT_EQ(std::string(), GetAlwaysOnPackageName());
pref_service()->Set(arc::prefs::kAlwaysOnVpnPackage, kVpnPackageValue);
EXPECT_EQ(kVpnPackage, GetAlwaysOnPackageName());
pref_service()->Set(arc::prefs::kAlwaysOnVpnPackage,
base::Value(std::string_view()));
EXPECT_EQ(std::string(), GetAlwaysOnPackageName());
}
TEST_F(AlwaysOnVpnManagerTest, SetPackageThatsAlreadySetAtBoot) {
pref_service()->Set(arc::prefs::kAlwaysOnVpnLockdown, base::Value(true));
pref_service()->Set(arc::prefs::kAlwaysOnVpnPackage, kVpnPackageValue);
auto always_on_manager = std::make_unique<AlwaysOnVpnManager>(
pref_service(), /*delay_lockdown_until_vpn_connected=*/false);
EXPECT_EQ(kVpnPackage, GetAlwaysOnPackageName());
}
TEST_F(AlwaysOnVpnManagerTest, SetLockdown) {
pref_service()->Set(arc::prefs::kAlwaysOnVpnPackage, kVpnPackageValue);
auto always_on_manager = std::make_unique<AlwaysOnVpnManager>(
pref_service(), /*delay_lockdown_until_vpn_connected=*/false);
pref_service()->Set(arc::prefs::kAlwaysOnVpnLockdown, base::Value(true));
EXPECT_EQ(kVpnPackage, GetAlwaysOnPackageName());
pref_service()->Set(arc::prefs::kAlwaysOnVpnLockdown, base::Value(false));
EXPECT_EQ(std::string(), GetAlwaysOnPackageName());
}
// Verify that the shill::kAlwaysOnVpnPackageProperty property is not set if the
// browser user traffic is restricted by the AlwaysOnVpnPreConnectUrlAllowlist
// preference.
TEST_F(AlwaysOnVpnManagerTest, EnforceAlwaysOnVpnPreConnectUrlAllowlist) {
auto always_on_manager = std::make_unique<AlwaysOnVpnManager>(
pref_service(), /*delay_lockdown_until_vpn_connected=*/false);
pref_service()->Set(arc::prefs::kAlwaysOnVpnLockdown, base::Value(true));
pref_service()->Set(arc::prefs::kAlwaysOnVpnPackage, kVpnPackageValue);
EXPECT_EQ(kVpnPackage, GetAlwaysOnPackageName());
always_on_manager->SetDelayLockdownUntilVpnConnectedState(/*enabled=*/true);
EXPECT_EQ(std::string(), GetAlwaysOnPackageName());
always_on_manager->SetDelayLockdownUntilVpnConnectedState(
/*enabled=*/false);
EXPECT_EQ(kVpnPackage, GetAlwaysOnPackageName());
}
} // namespace
} // namespace arc