File: consent_provider_unittest.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 (266 lines) | stat: -rw-r--r-- 10,354 bytes parent folder | download | duplicates (2)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/extensions/api/file_system/consent_provider_impl.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/mojom/dialog_button.mojom.h"

using extensions::file_system_api::ConsentProviderImpl;
using extensions::mojom::ManifestLocation;

namespace extensions {
namespace {

// Configurations and results for TestingConsentProviderDelegate, with directly
// accessible fields.
struct TestDelegateState {
  // Used to assign a fake dialog response.
  ui::mojom::DialogButton dialog_button = ui::mojom::DialogButton::kNone;

  // Used to assign fake result of detection the auto launch kiosk mode.
  bool is_auto_launched = false;

  // Used to set allowlisted components list with a single id.
  std::string allowlisted_component_id;

  // Counters to record calls.
  int show_dialog_counter = 0;
  int show_notification_counter = 0;
};

// Test implementation of ConsentProviderImpl::DelegateInterface that exposes
// states to a TestDelegateState instance.
class TestingConsentProviderDelegate
    : public ConsentProviderImpl::DelegateInterface {
 public:
  explicit TestingConsentProviderDelegate(TestDelegateState* state)
      : state_(state) {}

  TestingConsentProviderDelegate(const TestingConsentProviderDelegate&) =
      delete;
  TestingConsentProviderDelegate& operator=(
      const TestingConsentProviderDelegate&) = delete;

  ~TestingConsentProviderDelegate() override = default;

 private:
  // ConsentProviderImpl::DelegateInterface:
  void ShowDialog(content::RenderFrameHost* host,
                  const extensions::ExtensionId& extension_id,
                  const std::string& extension_name,
                  const std::string& volume_id,
                  const std::string& volume_label,
                  bool writable,
                  ConsentProviderImpl::ShowDialogCallback callback) override {
    ++state_->show_dialog_counter;
    std::move(callback).Run(state_->dialog_button);
  }

  // ConsentProviderImpl::DelegateInterface:
  void ShowNotification(const extensions::ExtensionId& extension_id,
                        const std::string& extension_name,
                        const std::string& volume_id,
                        const std::string& volume_label,
                        bool writable) override {
    ++state_->show_notification_counter;
  }

  // ConsentProviderImpl::DelegateInterface:
  bool IsAutoLaunched(const extensions::Extension& extension) override {
    return state_->is_auto_launched;
  }

  // ConsentProviderImpl::DelegateInterface:
  bool IsAllowlistedComponent(const extensions::Extension& extension) override {
    return state_->allowlisted_component_id.compare(extension.id()) == 0;
  }

  // Use raw_ptr since |state| is owned by owner.
  raw_ptr<TestDelegateState> state_;
};

// Rewrites result of a consent request from |result| to |log|.
void OnConsentReceived(ConsentProviderImpl::Consent* log,
                       const ConsentProviderImpl::Consent result) {
  *log = result;
}

}  // namespace

class FileSystemApiConsentProviderTest : public testing::Test {
 public:
  FileSystemApiConsentProviderTest() = default;

  void SetUp() override {
    user_manager_ = new ash::FakeChromeUserManager;
    scoped_user_manager_enabler_ =
        std::make_unique<user_manager::ScopedUserManager>(
            base::WrapUnique(user_manager_.get()));
  }

  void TearDown() override {
    scoped_user_manager_enabler_.reset();
    user_manager_ = nullptr;
  }

 protected:
  ScopedTestingLocalState scoped_testing_local_state_{
      TestingBrowserProcess::GetGlobal()};
  raw_ptr<ash::FakeChromeUserManager, DanglingUntriaged>
      user_manager_;  // Owned by the scope enabler.
  std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_enabler_;
  content::BrowserTaskEnvironment task_environment_;
};

TEST_F(FileSystemApiConsentProviderTest, ForNonKioskApps) {
  // Component apps are not granted unless they are allowlisted.
  {
    scoped_refptr<const Extension> component_extension(
        ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
            .SetLocation(ManifestLocation::kComponent)
            .Build());
    TestDelegateState state;
    ConsentProviderImpl provider(
        std::make_unique<TestingConsentProviderDelegate>(&state));
    EXPECT_FALSE(provider.IsGrantable(*component_extension));
  }

  // Allowlisted component apps are instantly granted access without asking
  // user.
  {
    scoped_refptr<const Extension> allowlisted_component_extension(
        ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
            .SetLocation(ManifestLocation::kComponent)
            .Build());
    TestDelegateState state;
    state.allowlisted_component_id = allowlisted_component_extension->id();
    ConsentProviderImpl provider(
        std::make_unique<TestingConsentProviderDelegate>(&state));
    EXPECT_TRUE(provider.IsGrantable(*allowlisted_component_extension));

    ConsentProviderImpl::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
    provider.RequestConsent(nullptr, *allowlisted_component_extension.get(),
                            "Volume ID 1", "Volume Label 1",
                            true /* writable */,
                            base::BindOnce(&OnConsentReceived, &result));
    base::RunLoop().RunUntilIdle();

    EXPECT_EQ(0, state.show_dialog_counter);
    EXPECT_EQ(0, state.show_notification_counter);
    EXPECT_EQ(ConsentProvider::CONSENT_GRANTED, result);
  }

  // Non-component apps in non-kiosk mode will be rejected instantly, without
  // asking for user consent.
  {
    scoped_refptr<const Extension> non_component_extension(
        ExtensionBuilder("Test").Build());
    TestDelegateState state;
    ConsentProviderImpl provider(
        std::make_unique<TestingConsentProviderDelegate>(&state));
    EXPECT_FALSE(provider.IsGrantable(*non_component_extension));
  }
}

TEST_F(FileSystemApiConsentProviderTest, ForKioskApps) {
  // Non-component apps in auto-launch kiosk mode will be granted access
  // instantly without asking for user consent, but with a notification.
  {
    scoped_refptr<const Extension> auto_launch_kiosk_app(
        ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
            .SetManifestKey("kiosk_enabled", true)
            .SetManifestKey("kiosk_only", true)
            .Build());
    auto* auto_user = user_manager_->AddKioskAppUser(
        AccountId::FromUserEmail(auto_launch_kiosk_app->id()));
    user_manager_->LoginUser(auto_user->GetAccountId());

    TestDelegateState state;
    state.is_auto_launched = true;
    ConsentProviderImpl provider(
        std::make_unique<TestingConsentProviderDelegate>(&state));
    EXPECT_TRUE(provider.IsGrantable(*auto_launch_kiosk_app));

    ConsentProviderImpl::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
    provider.RequestConsent(
        nullptr, *auto_launch_kiosk_app.get(), "Volume ID 2", "Volume Label 2",
        true /* writable */, base::BindOnce(&OnConsentReceived, &result));
    base::RunLoop().RunUntilIdle();

    EXPECT_EQ(0, state.show_dialog_counter);
    EXPECT_EQ(1, state.show_notification_counter);
    EXPECT_EQ(ConsentProvider::CONSENT_GRANTED, result);
  }

  // Non-component apps in manual-launch kiosk mode will be granted access after
  // receiving approval from the user.
  scoped_refptr<const Extension> manual_launch_kiosk_app(
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .SetManifestKey("kiosk_only", true)
          .Build());
  auto* manual_user = user_manager_->AddKioskAppUser(
      AccountId::FromUserEmail(manual_launch_kiosk_app->id()));
  user_manager_->LoginUser(manual_user->GetAccountId());
  {
    TestDelegateState state;
    state.dialog_button = ui::mojom::DialogButton::kOk;
    ConsentProviderImpl provider(
        std::make_unique<TestingConsentProviderDelegate>(&state));
    EXPECT_TRUE(provider.IsGrantable(*manual_launch_kiosk_app));

    ConsentProviderImpl::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
    provider.RequestConsent(nullptr, *manual_launch_kiosk_app.get(),
                            "Volume ID 3", "Volume Label 3",
                            true /* writable */,
                            base::BindOnce(&OnConsentReceived, &result));
    base::RunLoop().RunUntilIdle();

    EXPECT_EQ(1, state.show_dialog_counter);
    EXPECT_EQ(0, state.show_notification_counter);
    EXPECT_EQ(ConsentProvider::CONSENT_GRANTED, result);
  }

  // Non-component apps in manual-launch kiosk mode will be rejected access
  // after rejecting by a user.
  {
    TestDelegateState state;
    state.dialog_button = ui::mojom::DialogButton::kCancel;
    ConsentProviderImpl provider(
        std::make_unique<TestingConsentProviderDelegate>(&state));
    EXPECT_TRUE(provider.IsGrantable(*manual_launch_kiosk_app));

    ConsentProviderImpl::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
    provider.RequestConsent(nullptr, *manual_launch_kiosk_app.get(),
                            "Volume ID 4", "Volume Label 4",
                            true /* writable */,
                            base::BindOnce(&OnConsentReceived, &result));
    base::RunLoop().RunUntilIdle();

    EXPECT_EQ(1, state.show_dialog_counter);
    EXPECT_EQ(0, state.show_notification_counter);
    EXPECT_EQ(ConsentProvider::CONSENT_REJECTED, result);
  }
}

}  // namespace extensions