File: kiosk_iwa_device_attributes_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (220 lines) | stat: -rw-r--r-- 8,566 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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <array>
#include <cstddef>
#include <memory>
#include <string>
#include <string_view>

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/ash/app_mode/test/kiosk_mixin.h"
#include "chrome/browser/ash/app_mode/test/kiosk_test_utils.h"
#include "chrome/browser/ash/login/test/scoped_policy_update.h"
#include "chrome/browser/ash/login/test/test_predicate_waiter.h"
#include "chrome/browser/ash/policy/core/device_policy_cros_test_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_update_server_mixin.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/test_signed_web_bundle_builder.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/host_port_pair.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace ash {

using kiosk::test::LaunchAppManually;
using kiosk::test::TheKioskApp;
using kiosk::test::WaitKioskLaunched;

namespace {

constexpr char kDeviceAnnotatedAssetId[] = "iwa_test_asset_id";
constexpr char kDeviceAnnotatedLocation[] = "iwa_test_location";
constexpr char kDeviceDirectoryApiId[] = "iwa_test_directory_id";
constexpr char kDeviceHostname[] = "iwa_test_hostname";
constexpr char kDeviceSerialNumber[] = "iwa_test_serial_number";

constexpr std::array<const char*, 5> kDeviceAttributeNames = {
    "AnnotatedAssetId", "AnnotatedLocation", "DirectoryId", "Hostname",
    "SerialNumber"};

constexpr std::array<const char*, 5> kExpectedDeviceAttributeValues = {
    kDeviceAnnotatedAssetId, kDeviceAnnotatedLocation, kDeviceDirectoryApiId,
    kDeviceHostname, kDeviceSerialNumber};

constexpr char kNotAllowedOriginExpectedError[] =
    "a JavaScript error: \"UnknownError: The current origin cannot use this "
    "web API because it is not allowed by the "
    "DeviceAttributesAllowedForOrigins policy.\"\n";

const web_package::SignedWebBundleId kTestWebBundleId =
    web_app::test::GetDefaultEd25519WebBundleId();

KioskMixin::Config GetKioskIwaManualLaunchConfig(
    const GURL& update_manifest_url) {
  KioskMixin::IsolatedWebAppOption iwa_option(
      /*account_id=*/"simple-iwa@localhost",
      /*web_bundle_id=*/kTestWebBundleId,
      /*update_manifest_url=*/update_manifest_url);

  KioskMixin::Config kiosk_iwa_config = {/*name=*/"IsolatedWebApp",
                                         /*auto_launch_account_id=*/{},
                                         {iwa_option}};
  return kiosk_iwa_config;
}

// Waits until a `js_name` is defined. Is used to prevent API calls before the
// test web page has loaded.
void WaitForJsObject(content::WebContents* web_contents,
                     const std::string& js_name) {
  ash::test::TestPredicateWaiter(
      base::BindRepeating(
          [](content::WebContents* web_contents, const std::string& js_name) {
            return content::EvalJs(
                       web_contents,
                       base::ReplaceStringPlaceholders(
                           "typeof $1 !== 'undefined'", {js_name}, nullptr))
                .ExtractBool();
          },
          web_contents, js_name))
      .Wait();
}

content::EvalJsResult CallDeviceAttributesApi(
    content::WebContents* web_contents,
    const std::string& attribute_name) {
  return content::EvalJs(
      web_contents, base::ReplaceStringPlaceholders("navigator.managed.get$1()",
                                                    {attribute_name}, nullptr));
}

}  // namespace

class KioskIwaDeviceAttributesApiTest : public MixinBasedInProcessBrowserTest {
 public:
  KioskIwaDeviceAttributesApiTest() {
    iwa_server_mixin_.AddBundle(
        web_app::IsolatedWebAppBuilder(web_app::ManifestBuilder())
            .BuildBundle(web_app::test::GetDefaultEd25519KeyPair()));
  }

  ~KioskIwaDeviceAttributesApiTest() override = default;
  KioskIwaDeviceAttributesApiTest(const KioskIwaDeviceAttributesApiTest&) =
      delete;
  KioskIwaDeviceAttributesApiTest& operator=(
      const KioskIwaDeviceAttributesApiTest&) = delete;

  void SetUpOnMainThread() override {
    MixinBasedInProcessBrowserTest::SetUpOnMainThread();
    SetDevicePolicies();
    LaunchIwaKiosk();
  }

 protected:
  content::WebContents* web_contents() {
    BrowserView* browser_view =
        BrowserView::GetBrowserViewForBrowser(browser());
    return browser_view ? browser_view->GetActiveWebContents() : nullptr;
  }

  void AllowDeviceAttributesForIwaOrigin() {
    browser()->profile()->GetPrefs()->SetList(
        prefs::kDeviceAttributesAllowedForOrigins,
        base::Value::List().Append(kAppOrigin.Serialize()));
  }

 private:
  void SetDevicePolicies() {
    ScopedDevicePolicyUpdate scoped_update(
        policy_helper_.device_policy(), base::BindLambdaForTesting([this]() {
          policy_helper_.RefreshPolicyAndWaitUntilDeviceSettingsUpdated(
              {ash::kAccountsPrefDeviceLocalAccounts});
        }));
    kiosk_.Configure(
        scoped_update,
        GetKioskIwaManualLaunchConfig(
            iwa_server_mixin_.GetUpdateManifestUrl(kTestWebBundleId)));

    scoped_update.policy_data()->set_annotated_asset_id(
        kDeviceAnnotatedAssetId);
    scoped_update.policy_data()->set_annotated_location(
        kDeviceAnnotatedLocation);
    scoped_update.policy_data()->set_directory_api_id(kDeviceDirectoryApiId);
    scoped_update.policy_payload()
        ->mutable_network_hostname()
        ->set_device_hostname_template(kDeviceHostname);

    fake_statistics_provider_.SetMachineStatistic(ash::system::kSerialNumberKey,
                                                  kDeviceSerialNumber);
  }

  void LaunchIwaKiosk() {
    ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
    ASSERT_TRUE(WaitKioskLaunched());

    SelectFirstBrowser();
    ASSERT_NE(web_contents(), nullptr);
    ASSERT_EQ(web_contents()->GetVisibleURL(), kAppOrigin.GetURL());
  }

  const url::Origin kAppOrigin =
      url::Origin::CreateFromNormalizedTuple(chrome::kIsolatedAppScheme,
                                             kTestWebBundleId.id(),
                                             /*port=*/0);

  base::test::ScopedFeatureList feature_list_{
      ash::features::kIsolatedWebAppKiosk};
  web_app::IsolatedWebAppUpdateServerMixin iwa_server_mixin_{&mixin_host_};
  KioskMixin kiosk_{&mixin_host_};
  policy::DevicePolicyCrosTestHelper policy_helper_;
  ash::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
};

IN_PROC_BROWSER_TEST_F(KioskIwaDeviceAttributesApiTest,
                       AllowedOriginWithAttributesSet) {
  AllowDeviceAttributesForIwaOrigin();

  WaitForJsObject(web_contents(), "navigator.managed");

  ASSERT_EQ(kDeviceAttributeNames.size(),
            kExpectedDeviceAttributeValues.size());
  for (size_t i = 0; i < kDeviceAttributeNames.size(); ++i) {
    EXPECT_EQ(kExpectedDeviceAttributeValues[i],
              CallDeviceAttributesApi(web_contents(), kDeviceAttributeNames[i])
                  .ExtractString());
  }
}

IN_PROC_BROWSER_TEST_F(KioskIwaDeviceAttributesApiTest,
                       DisallowedOriginCantAccessAPI) {
  WaitForJsObject(web_contents(), "navigator.managed");

  // All of the methods should be defined, but return the same error.
  for (const std::string& attribute : kDeviceAttributeNames) {
    content::EvalJsResult result =
        CallDeviceAttributesApi(web_contents(), attribute);
    EXPECT_EQ(result.error, kNotAllowedOriginExpectedError);
    EXPECT_TRUE(result.value.is_none());
  }
}

}  // namespace ash