Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_api_unittest.cc

Issue 2924943003: Break ConsentProvider classes into own file (Closed)
Patch Set: rebase Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
17 #include "build/build_config.h" 15 #include "build/build_config.h"
18 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/shell_dialogs/select_file_dialog.h" 17 #include "ui/shell_dialogs/select_file_dialog.h"
20 18
21 #if defined(OS_CHROMEOS)
22 #include "base/memory/ref_counted.h"
23 #include "base/memory/weak_ptr.h"
24 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
25 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
26 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
27 #include "chrome/test/base/testing_browser_process.h"
28 #include "components/prefs/testing_pref_service.h"
29 #include "components/user_manager/user.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "extensions/common/extension.h"
32 #include "extensions/common/extension_builder.h"
33 #include "extensions/common/manifest.h"
34 #include "extensions/common/test_util.h"
35 #include "extensions/common/value_builder.h"
36 #endif
37
38 using extensions::FileSystemChooseEntryFunction; 19 using extensions::FileSystemChooseEntryFunction;
39 using extensions::api::file_system::AcceptOption; 20 using extensions::api::file_system::AcceptOption;
40 21
41 #if defined(OS_CHROMEOS)
42 using extensions::file_system_api::ConsentProvider;
43 using file_manager::Volume;
44 #endif
45
46 namespace extensions { 22 namespace extensions {
47 namespace { 23 namespace {
48 24
49 void CheckExtensions(const std::vector<base::FilePath::StringType>& expected, 25 void CheckExtensions(const std::vector<base::FilePath::StringType>& expected,
50 const std::vector<base::FilePath::StringType>& actual) { 26 const std::vector<base::FilePath::StringType>& actual) {
51 EXPECT_EQ(expected.size(), actual.size()); 27 EXPECT_EQ(expected.size(), actual.size());
52 if (expected.size() != actual.size()) 28 if (expected.size() != actual.size())
53 return; 29 return;
54 30
55 for (size_t i = 0; i < expected.size(); ++i) { 31 for (size_t i = 0; i < expected.size(); ++i) {
(...skipping 21 matching lines...) Expand all
77 53
78 return option; 54 return option;
79 } 55 }
80 56
81 #if defined(OS_WIN) 57 #if defined(OS_WIN)
82 #define ToStringType base::UTF8ToWide 58 #define ToStringType base::UTF8ToWide
83 #else 59 #else
84 #define ToStringType 60 #define ToStringType
85 #endif 61 #endif
86 62
87 #if defined(OS_CHROMEOS)
88 class TestingConsentProviderDelegate
89 : public ConsentProvider::DelegateInterface {
90 public:
91 TestingConsentProviderDelegate()
92 : show_dialog_counter_(0),
93 show_notification_counter_(0),
94 dialog_button_(ui::DIALOG_BUTTON_NONE),
95 is_auto_launched_(false) {}
96
97 ~TestingConsentProviderDelegate() {}
98
99 // Sets a fake dialog response.
100 void SetDialogButton(ui::DialogButton button) { dialog_button_ = button; }
101
102 // Sets a fake result of detection the auto launch kiosk mode.
103 void SetIsAutoLaunched(bool is_auto_launched) {
104 is_auto_launched_ = is_auto_launched;
105 }
106
107 // Sets a whitelisted components list with a single id.
108 void SetComponentWhitelist(const std::string& extension_id) {
109 whitelisted_component_id_ = extension_id;
110 }
111
112 int show_dialog_counter() const { return show_dialog_counter_; }
113 int show_notification_counter() const { return show_notification_counter_; }
114
115 private:
116 // ConsentProvider::DelegateInterface overrides:
117 void ShowDialog(
118 const extensions::Extension& extension,
119 const base::WeakPtr<Volume>& volume,
120 bool writable,
121 const ConsentProvider::ShowDialogCallback& callback) override {
122 ++show_dialog_counter_;
123 callback.Run(dialog_button_);
124 }
125
126 void ShowNotification(const extensions::Extension& extension,
127 const base::WeakPtr<Volume>& volume,
128 bool writable) override {
129 ++show_notification_counter_;
130 }
131
132 bool IsAutoLaunched(const extensions::Extension& extension) override {
133 return is_auto_launched_;
134 }
135
136 bool IsWhitelistedComponent(const extensions::Extension& extension) override {
137 return whitelisted_component_id_.compare(extension.id()) == 0;
138 }
139
140 int show_dialog_counter_;
141 int show_notification_counter_;
142 ui::DialogButton dialog_button_;
143 bool is_auto_launched_;
144 std::string whitelisted_component_id_;
145
146 DISALLOW_COPY_AND_ASSIGN(TestingConsentProviderDelegate);
147 };
148
149 // Rewrites result of a consent request from |result| to |log|.
150 void OnConsentReceived(ConsentProvider::Consent* log,
151 const ConsentProvider::Consent result) {
152 *log = result;
153 }
154 #endif
155
156 } // namespace 63 } // namespace
157 64
158 #if defined(OS_CHROMEOS)
159 class FileSystemApiConsentProviderTest : public testing::Test {
160 public:
161 FileSystemApiConsentProviderTest() {}
162
163 void SetUp() override {
164 testing_pref_service_.reset(new TestingPrefServiceSimple);
165 TestingBrowserProcess::GetGlobal()->SetLocalState(
166 testing_pref_service_.get());
167 user_manager_ = new chromeos::FakeChromeUserManager;
168 scoped_user_manager_enabler_.reset(
169 new chromeos::ScopedUserManagerEnabler(user_manager_));
170 }
171
172 void TearDown() override {
173 scoped_user_manager_enabler_.reset();
174 user_manager_ = nullptr;
175 testing_pref_service_.reset();
176 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr);
177 }
178
179 protected:
180 base::WeakPtr<Volume> volume_;
181 std::unique_ptr<TestingPrefServiceSimple> testing_pref_service_;
182 chromeos::FakeChromeUserManager*
183 user_manager_; // Owned by the scope enabler.
184 std::unique_ptr<chromeos::ScopedUserManagerEnabler>
185 scoped_user_manager_enabler_;
186 content::TestBrowserThreadBundle thread_bundle_;
187 };
188 #endif
189
190 TEST(FileSystemApiUnitTest, FileSystemChooseEntryFunctionFileTypeInfoTest) { 65 TEST(FileSystemApiUnitTest, FileSystemChooseEntryFunctionFileTypeInfoTest) {
191 // AcceptsAllTypes is ignored when no other extensions are available. 66 // AcceptsAllTypes is ignored when no other extensions are available.
192 ui::SelectFileDialog::FileTypeInfo file_type_info; 67 ui::SelectFileDialog::FileTypeInfo file_type_info;
193 bool acceptsAllTypes = false; 68 bool acceptsAllTypes = false;
194 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, 69 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
195 base::FilePath::StringType(), NULL, &acceptsAllTypes); 70 base::FilePath::StringType(), NULL, &acceptsAllTypes);
196 EXPECT_TRUE(file_type_info.include_all_files); 71 EXPECT_TRUE(file_type_info.include_all_files);
197 EXPECT_TRUE(file_type_info.extensions.empty()); 72 EXPECT_TRUE(file_type_info.extensions.empty());
198 73
199 // Test grouping of multiple types. 74 // Test grouping of multiple types.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 // Filter out absolute paths with no basename. 180 // Filter out absolute paths with no basename.
306 opt_name = std::string("/"); 181 opt_name = std::string("/");
307 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name, 182 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
308 &suggested_extension); 183 &suggested_extension);
309 EXPECT_FALSE(suggested_name.IsAbsolute()); 184 EXPECT_FALSE(suggested_name.IsAbsolute());
310 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty()); 185 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty());
311 EXPECT_TRUE(suggested_extension.empty()); 186 EXPECT_TRUE(suggested_extension.empty());
312 #endif 187 #endif
313 } 188 }
314 189
315 #if defined(OS_CHROMEOS)
316 TEST_F(FileSystemApiConsentProviderTest, ForNonKioskApps) {
317 // Component apps are not granted unless they are whitelisted.
318 {
319 scoped_refptr<Extension> component_extension(
320 test_util::BuildApp(
321 std::move(ExtensionBuilder().SetLocation(Manifest::COMPONENT)))
322 .Build());
323 TestingConsentProviderDelegate delegate;
324 ConsentProvider provider(&delegate);
325 EXPECT_FALSE(provider.IsGrantable(*component_extension));
326 }
327
328 // Whitelitsed component apps are instantly granted access without asking
329 // user.
330 {
331 scoped_refptr<Extension> whitelisted_component_extension(
332 test_util::BuildApp(
333 std::move(ExtensionBuilder().SetLocation(Manifest::COMPONENT)))
334 .Build());
335 TestingConsentProviderDelegate delegate;
336 delegate.SetComponentWhitelist(whitelisted_component_extension->id());
337 ConsentProvider provider(&delegate);
338 EXPECT_TRUE(provider.IsGrantable(*whitelisted_component_extension));
339
340 ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
341 provider.RequestConsent(*whitelisted_component_extension.get(), volume_,
342 true /* writable */,
343 base::Bind(&OnConsentReceived, &result));
344 base::RunLoop().RunUntilIdle();
345
346 EXPECT_EQ(0, delegate.show_dialog_counter());
347 EXPECT_EQ(0, delegate.show_notification_counter());
348 EXPECT_EQ(ConsentProvider::CONSENT_GRANTED, result);
349 }
350
351 // Non-component apps in non-kiosk mode will be rejected instantly, without
352 // asking for user consent.
353 {
354 scoped_refptr<Extension> non_component_extension(
355 test_util::CreateEmptyExtension());
356 TestingConsentProviderDelegate delegate;
357 ConsentProvider provider(&delegate);
358 EXPECT_FALSE(provider.IsGrantable(*non_component_extension));
359 }
360 }
361
362 TEST_F(FileSystemApiConsentProviderTest, ForKioskApps) {
363 // Non-component apps in auto-launch kiosk mode will be granted access
364 // instantly without asking for user consent, but with a notification.
365 {
366 scoped_refptr<Extension> auto_launch_kiosk_app(
367 test_util::BuildApp(ExtensionBuilder())
368 .MergeManifest(DictionaryBuilder()
369 .SetBoolean("kiosk_enabled", true)
370 .SetBoolean("kiosk_only", true)
371 .Build())
372 .Build());
373 user_manager_->AddKioskAppUser(
374 AccountId::FromUserEmail(auto_launch_kiosk_app->id()));
375 user_manager_->LoginUser(
376 AccountId::FromUserEmail(auto_launch_kiosk_app->id()));
377
378 TestingConsentProviderDelegate delegate;
379 delegate.SetIsAutoLaunched(true);
380 ConsentProvider provider(&delegate);
381 EXPECT_TRUE(provider.IsGrantable(*auto_launch_kiosk_app));
382
383 ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
384 provider.RequestConsent(*auto_launch_kiosk_app.get(), volume_,
385 true /* writable */,
386 base::Bind(&OnConsentReceived, &result));
387 base::RunLoop().RunUntilIdle();
388
389 EXPECT_EQ(0, delegate.show_dialog_counter());
390 EXPECT_EQ(1, delegate.show_notification_counter());
391 EXPECT_EQ(ConsentProvider::CONSENT_GRANTED, result);
392 }
393
394 // Non-component apps in manual-launch kiosk mode will be granted access after
395 // receiving approval from the user.
396 scoped_refptr<Extension> manual_launch_kiosk_app(
397 test_util::BuildApp(ExtensionBuilder())
398 .MergeManifest(DictionaryBuilder()
399 .SetBoolean("kiosk_enabled", true)
400 .SetBoolean("kiosk_only", true)
401 .Build())
402 .Build());
403 user_manager::User* const manual_kiosk_app_user =
404 user_manager_->AddKioskAppUser(
405 AccountId::FromUserEmail(manual_launch_kiosk_app->id()));
406 user_manager_->KioskAppLoggedIn(manual_kiosk_app_user);
407 {
408 TestingConsentProviderDelegate delegate;
409 delegate.SetDialogButton(ui::DIALOG_BUTTON_OK);
410 ConsentProvider provider(&delegate);
411 EXPECT_TRUE(provider.IsGrantable(*manual_launch_kiosk_app));
412
413 ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
414 provider.RequestConsent(*manual_launch_kiosk_app.get(), volume_,
415 true /* writable */,
416 base::Bind(&OnConsentReceived, &result));
417 base::RunLoop().RunUntilIdle();
418
419 EXPECT_EQ(1, delegate.show_dialog_counter());
420 EXPECT_EQ(0, delegate.show_notification_counter());
421 EXPECT_EQ(ConsentProvider::CONSENT_GRANTED, result);
422 }
423
424 // Non-component apps in manual-launch kiosk mode will be rejected access
425 // after rejecting by a user.
426 {
427 TestingConsentProviderDelegate delegate;
428 ConsentProvider provider(&delegate);
429 delegate.SetDialogButton(ui::DIALOG_BUTTON_CANCEL);
430 EXPECT_TRUE(provider.IsGrantable(*manual_launch_kiosk_app));
431
432 ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
433 provider.RequestConsent(*manual_launch_kiosk_app.get(), volume_,
434 true /* writable */,
435 base::Bind(&OnConsentReceived, &result));
436 base::RunLoop().RunUntilIdle();
437
438 EXPECT_EQ(1, delegate.show_dialog_counter());
439 EXPECT_EQ(0, delegate.show_notification_counter());
440 EXPECT_EQ(ConsentProvider::CONSENT_REJECTED, result);
441 }
442 }
443 #endif
444
445 } // namespace extensions 190 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698