File: vpn_ui_interactive_uitest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (246 lines) | stat: -rw-r--r-- 10,047 bytes parent folder | download | duplicates (5)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2024 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/ash_element_identifiers.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/ash/interactive/interactive_ash_test.h"
#include "chrome/test/base/ash/interactive/settings/interactive_uitest_elements.h"
#include "chromeos/ash/components/dbus/shill/fake_shill_profile_client.h"
#include "chromeos/ash/components/dbus/shill/fake_shill_service_client.h"
#include "chromeos/ash/components/network/network_cert_loader.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "components/user_manager/user_manager.h"
#include "crypto/scoped_nss_types.h"
#include "crypto/scoped_test_nss_db.h"
#include "dbus/object_path.h"
#include "net/cert/nss_cert_database_chromeos.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {
namespace {

DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kOSSettingsId);

constexpr char kProfilePath[] = "/network/profile/0";
constexpr char kVpnServiceName[] = "vpn_service_name";
constexpr char kVpnHostname[] = "vpn_host_name";
constexpr char kOpenVpnUsername[] = "username";
constexpr char kOpenVpnPassword[] = "password123";

void CheckPropertyStringValue(const base::Value::Dict* dict,
                              const std::string& key,
                              const std::string& expected) {
  const std::string* actual = dict->FindString(key);
  ASSERT_TRUE(actual);
  EXPECT_EQ(*actual, expected);
}

class VpnUiInteractiveUiTest : public InteractiveAshTest {
 protected:
  // InteractiveAshTest:
  void SetUpOnMainThread() override {
    InteractiveAshTest::SetUpOnMainThread();

    // Set up context for element tracking for InteractiveBrowserTest.
    SetupContextWidget();

    // Ensure the OS Settings app is installed.
    InstallSystemApps();

    ShillServiceClient::Get()->GetTestInterface()->ClearServices();
    ShillProfileClient::Get()->GetTestInterface()->AddProfile(
        kProfilePath,
        user_manager::UserManager::Get()->GetActiveUser()->username_hash());
    ShillProfileClient::Get()->SetProperty(
        dbus::ObjectPath(kProfilePath), shill::kAlwaysOnVpnModeProperty,
        base::Value(shill::kAlwaysOnVpnModeOff), base::DoNothing(),
        base::DoNothing());

    // Load a fake cert database to user NSSDB. VPN network has to be connected
    // after the NetworkCertLoader finishes loading initial certs.
    test_nsscertdb_ = std::make_unique<net::NSSCertDatabaseChromeOS>(
        crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())),
        crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())));
    NetworkCertLoader::ForceAvailableForNetworkAuthForTesting();
    NetworkCertLoader::Get()->SetUserNSSDB(test_nsscertdb_.get());
  }

 private:
  crypto::ScopedTestNSSDB test_nssdb_;
  std::unique_ptr<net::NSSCertDatabaseChromeOS> test_nsscertdb_;
};

IN_PROC_BROWSER_TEST_F(VpnUiInteractiveUiTest,
                       BuiltInOpenVpnAddConnectDisconnectForget) {
  ui::ElementContext context =
      LaunchSystemWebApp(SystemWebAppType::SETTINGS, kOSSettingsId);

  // Run the following steps with the OS Settings context set as the default.
  RunTestSequenceInContext(
      context,

      Log("Navigating to the internet page"),

      NavigateSettingsToInternetPage(kOSSettingsId),

      Log("Open add built-in VPN dialog"),

      OpenAddBuiltInVpnDialog(kOSSettingsId),

      Log("Verify expected dialog title shows"),

      WaitForElementTextContains(
          kOSSettingsId, settings::InternetConfigDialogTitle(),
          l10n_util::GetStringFUTF8(
              IDS_SETTINGS_INTERNET_JOIN_TYPE,
              l10n_util::GetStringUTF16(IDS_NETWORK_TYPE_VPN))),

      Log("Verify the connect button is disabled before input the config"),

      WaitForElementDisabled(kOSSettingsId,
                             settings::vpn::JoinVpnDialogConnectButton()),

      Log("Enter VPN service name"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogServiceNameInput()),
      ClickElement(kOSSettingsId,
                   settings::vpn::JoinVpnDialogServiceNameInput()),
      SendTextAsKeyEvents(kOSSettingsId, kVpnServiceName),

      Log("Enter VPN hostname"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogHostnameInput()),
      ClickElement(kOSSettingsId, settings::vpn::JoinVpnDialogHostnameInput()),
      SendTextAsKeyEvents(kOSSettingsId, kVpnHostname),

      Log("Choose VPN provider type"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogProviderTypeSelect()),
      ClickElement(kOSSettingsId,
                   settings::vpn::JoinVpnDialogProviderTypeSelect()),
      SelectDropdownElementOption(
          kOSSettingsId, settings::vpn::JoinVpnDialogProviderTypeOptions(),
          l10n_util::GetStringUTF8(IDS_ONC_VPN_TYPE_OPENVPN).c_str()),
      ClickElement(kOSSettingsId, settings::InternetConfigDialogTitle()),

      Log("Enter VPN username"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogOpenVpnUsernameInput()),
      MoveMouseTo(kOSSettingsId,
                  settings::vpn::JoinVpnDialogOpenVpnUsernameInput()),
      ClickElement(kOSSettingsId,
                   settings::vpn::JoinVpnDialogOpenVpnUsernameInput()),
      SendTextAsKeyEvents(kOSSettingsId, kOpenVpnUsername),

      Log("Enter VPN password"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogOpenVpnPasswordInput()),
      ClickElement(kOSSettingsId,
                   settings::vpn::JoinVpnDialogOpenVpnPasswordInput()),
      SendTextAsKeyEvents(kOSSettingsId, kOpenVpnPassword),

      Log("Checking for OTP and save credentials element shows"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogOpenVpnOtpInput()),
      WaitForElementEnabled(
          kOSSettingsId, settings::vpn::JoinVpnDialogSaveCredentialsToggle()),

      Log("Click on \"Connect\" button"),

      WaitForElementEnabled(kOSSettingsId,
                            settings::vpn::JoinVpnDialogConnectButton()),
      ClickElement(kOSSettingsId, settings::vpn::JoinVpnDialogConnectButton()),

      Log("Verify VPN summary item has expected label"),

      WaitForElementEnabled(kOSSettingsId, settings::vpn::VpnSummaryItem()),
      WaitForElementTextContains(kOSSettingsId, settings::vpn::VpnSummaryItem(),
                                 kVpnServiceName),

      Log("Verify the expected VPN service is created in shill"),

      Do([&]() {
        const std::string vpn_service_path =
            ShillServiceClient::Get()
                ->GetTestInterface()
                ->FindServiceMatchingName(kVpnServiceName);
        ASSERT_FALSE(vpn_service_path.empty());
        const base::Value::Dict* properties =
            ShillServiceClient::Get()->GetTestInterface()->GetServiceProperties(
                vpn_service_path);
        ASSERT_TRUE(properties);
        const base::Value::Dict* provider =
            properties->FindDict(shill::kProviderProperty);
        ASSERT_TRUE(provider);
        CheckPropertyStringValue(provider, shill::kHostProperty, kVpnHostname);
        CheckPropertyStringValue(provider, shill::kOpenVPNUserProperty,
                                 kOpenVpnUsername);
        CheckPropertyStringValue(provider, shill::kOpenVPNPasswordProperty,
                                 kOpenVpnPassword);
        CheckPropertyStringValue(provider, shill::kTypeProperty,
                                 shill::kProviderOpenVpn);
      }),

      Log("Navigate to VPN detail subpage"),

      NavigateToInternetDetailsPage(kOSSettingsId, NetworkTypePattern::VPN(),
                                    kVpnServiceName),
      WaitForElementTextContains(
          kOSSettingsId, settings::SettingsSubpageNetworkState(),
          /*text=*/l10n_util::GetStringUTF8(IDS_ONC_CONNECTED).c_str()),

      Log("Checking for the expected UI elements"),

      WaitForElementTextContains(
          kOSSettingsId, ash::settings::vpn::VpnSubpageProviderType(),
          l10n_util::GetStringUTF8(IDS_ONC_VPN_TYPE_OPENVPN).c_str()),
      WaitForElementExists(kOSSettingsId,
                           ash::settings::vpn::VpnSubpageHostnameInput()),

      CheckJsResultAt(
          kOSSettingsId, ash::settings::vpn::VpnSubpageHostnameInput(),
          base::StringPrintf("(el) => el.value === '%s'", kVpnHostname)),
      WaitForElementExists(kOSSettingsId,
                           ash::settings::vpn::VpnSubpageUsernameInput()),
      CheckJsResultAt(
          kOSSettingsId, ash::settings::vpn::VpnSubpageUsernameInput(),
          base::StringPrintf("(el) => el.value === '%s'", kOpenVpnUsername)),

      Log("Disconnect VPN network"),

      WaitForElementEnabled(
          kOSSettingsId,
          ash::settings::SettingsSubpageConnectDisconnectButton()),
      ClickElement(kOSSettingsId,
                   ash::settings::SettingsSubpageConnectDisconnectButton()),
      WaitForElementTextContains(
          kOSSettingsId, settings::SettingsSubpageNetworkState(),
          /*text=*/l10n_util::GetStringUTF8(IDS_ONC_NOT_CONNECTED).c_str()),

      Log("Forget VPN network"),

      WaitForElementEnabled(kOSSettingsId,
                            ash::settings::SettingsSubpageForgetButton()),
      ClickElement(kOSSettingsId, ash::settings::SettingsSubpageForgetButton()),

      Log("Verify the VPN doesn't appear in the list after forget"),

      WaitForElementDisplayNone(kOSSettingsId,
                                ash::settings::vpn::VpnNetworksList()),

      Log("Test complete"));
}

}  // namespace
}  // namespace ash