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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/file_system/file_system_api.h"
#include <stddef.h>
#include <vector>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#if defined(OS_CHROMEOS)
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/file_manager/volume_manager.h"
#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/user.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest.h"
#include "extensions/common/test_util.h"
#include "extensions/common/value_builder.h"
#endif
using extensions::FileSystemChooseEntryFunction;
using extensions::api::file_system::AcceptOption;
#if defined(OS_CHROMEOS)
using extensions::file_system_api::ConsentProvider;
using file_manager::Volume;
#endif
namespace extensions {
namespace {
void CheckExtensions(const std::vector<base::FilePath::StringType>& expected,
const std::vector<base::FilePath::StringType>& actual) {
EXPECT_EQ(expected.size(), actual.size());
if (expected.size() != actual.size())
return;
for (size_t i = 0; i < expected.size(); ++i) {
EXPECT_EQ(expected[i], actual[i]);
}
}
AcceptOption BuildAcceptOption(const std::string& description,
const std::string& mime_types,
const std::string& extensions) {
AcceptOption option;
if (!description.empty())
option.description.reset(new std::string(description));
if (!mime_types.empty()) {
option.mime_types.reset(new std::vector<std::string>(base::SplitString(
mime_types, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)));
}
if (!extensions.empty()) {
option.extensions.reset(new std::vector<std::string>(base::SplitString(
extensions, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)));
}
return option;
}
#if defined(OS_WIN)
#define ToStringType base::UTF8ToWide
#else
#define ToStringType
#endif
#if defined(OS_CHROMEOS)
class TestingConsentProviderDelegate
: public ConsentProvider::DelegateInterface {
public:
TestingConsentProviderDelegate()
: show_dialog_counter_(0),
show_notification_counter_(0),
dialog_button_(ui::DIALOG_BUTTON_NONE),
is_auto_launched_(false) {}
~TestingConsentProviderDelegate() {}
// Sets a fake dialog response.
void SetDialogButton(ui::DialogButton button) { dialog_button_ = button; }
// Sets a fake result of detection the auto launch kiosk mode.
void SetIsAutoLaunched(bool is_auto_launched) {
is_auto_launched_ = is_auto_launched;
}
// Sets a whitelisted components list with a single id.
void SetComponentWhitelist(const std::string& extension_id) {
whitelisted_component_id_ = extension_id;
}
int show_dialog_counter() const { return show_dialog_counter_; }
int show_notification_counter() const { return show_notification_counter_; }
private:
// ConsentProvider::DelegateInterface overrides:
void ShowDialog(
const extensions::Extension& extension,
const base::WeakPtr<Volume>& volume,
bool writable,
const ConsentProvider::ShowDialogCallback& callback) override {
++show_dialog_counter_;
callback.Run(dialog_button_);
}
void ShowNotification(const extensions::Extension& extension,
const base::WeakPtr<Volume>& volume,
bool writable) override {
++show_notification_counter_;
}
bool IsAutoLaunched(const extensions::Extension& extension) override {
return is_auto_launched_;
}
bool IsWhitelistedComponent(const extensions::Extension& extension) override {
return whitelisted_component_id_.compare(extension.id()) == 0;
}
int show_dialog_counter_;
int show_notification_counter_;
ui::DialogButton dialog_button_;
bool is_auto_launched_;
std::string whitelisted_component_id_;
DISALLOW_COPY_AND_ASSIGN(TestingConsentProviderDelegate);
};
// Rewrites result of a consent request from |result| to |log|.
void OnConsentReceived(ConsentProvider::Consent* log,
const ConsentProvider::Consent result) {
*log = result;
}
#endif
} // namespace
#if defined(OS_CHROMEOS)
class FileSystemApiConsentProviderTest : public testing::Test {
public:
FileSystemApiConsentProviderTest() {}
void SetUp() override {
testing_pref_service_.reset(new TestingPrefServiceSimple);
TestingBrowserProcess::GetGlobal()->SetLocalState(
testing_pref_service_.get());
user_manager_ = new chromeos::FakeChromeUserManager;
scoped_user_manager_enabler_.reset(
new chromeos::ScopedUserManagerEnabler(user_manager_));
}
void TearDown() override {
scoped_user_manager_enabler_.reset();
user_manager_ = nullptr;
testing_pref_service_.reset();
TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr);
}
protected:
base::WeakPtr<Volume> volume_;
std::unique_ptr<TestingPrefServiceSimple> testing_pref_service_;
chromeos::FakeChromeUserManager*
user_manager_; // Owned by the scope enabler.
std::unique_ptr<chromeos::ScopedUserManagerEnabler>
scoped_user_manager_enabler_;
content::TestBrowserThreadBundle thread_bundle_;
};
#endif
TEST(FileSystemApiUnitTest, FileSystemChooseEntryFunctionFileTypeInfoTest) {
// AcceptsAllTypes is ignored when no other extensions are available.
ui::SelectFileDialog::FileTypeInfo file_type_info;
bool acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
base::FilePath::StringType(), NULL, &acceptsAllTypes);
EXPECT_TRUE(file_type_info.include_all_files);
EXPECT_TRUE(file_type_info.extensions.empty());
// Test grouping of multiple types.
file_type_info = ui::SelectFileDialog::FileTypeInfo();
std::vector<AcceptOption> options;
options.push_back(BuildAcceptOption(std::string(),
"application/x-chrome-extension", "jso"));
acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
base::FilePath::StringType(), &options, &acceptsAllTypes);
EXPECT_FALSE(file_type_info.include_all_files);
ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1);
EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) <<
"No override must be specified for boring accept types";
// Note here (and below) that the expectedTypes are sorted, because we use a
// set internally to generate the output: thus, the output is sorted.
std::vector<base::FilePath::StringType> expectedTypes;
expectedTypes.push_back(ToStringType("crx"));
expectedTypes.push_back(ToStringType("jso"));
CheckExtensions(expectedTypes, file_type_info.extensions[0]);
// Test that not satisfying the extension will force all types.
file_type_info = ui::SelectFileDialog::FileTypeInfo();
options.clear();
options.push_back(
BuildAcceptOption(std::string(), std::string(), "unrelated"));
acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
ToStringType(".jso"), &options, &acceptsAllTypes);
EXPECT_TRUE(file_type_info.include_all_files);
// Test multiple list entries, all containing their own types.
file_type_info = ui::SelectFileDialog::FileTypeInfo();
options.clear();
options.push_back(BuildAcceptOption(std::string(), std::string(), "jso,js"));
options.push_back(BuildAcceptOption(std::string(), std::string(), "cpp,cc"));
acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
base::FilePath::StringType(), &options, &acceptsAllTypes);
ASSERT_EQ(file_type_info.extensions.size(), options.size());
expectedTypes.clear();
expectedTypes.push_back(ToStringType("js"));
expectedTypes.push_back(ToStringType("jso"));
CheckExtensions(expectedTypes, file_type_info.extensions[0]);
expectedTypes.clear();
expectedTypes.push_back(ToStringType("cc"));
expectedTypes.push_back(ToStringType("cpp"));
CheckExtensions(expectedTypes, file_type_info.extensions[1]);
// Test accept type that causes description override.
file_type_info = ui::SelectFileDialog::FileTypeInfo();
options.clear();
options.push_back(BuildAcceptOption(std::string(), "image/*", "html"));
acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
base::FilePath::StringType(), &options, &acceptsAllTypes);
ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1);
EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) <<
"Accept type \"image/*\" must generate description override";
// Test multiple accept types that cause description override causes us to
// still present the default.
file_type_info = ui::SelectFileDialog::FileTypeInfo();
options.clear();
options.push_back(BuildAcceptOption(std::string(), "image/*,audio/*,video/*",
std::string()));
acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
base::FilePath::StringType(), &options, &acceptsAllTypes);
ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1);
EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty());
// Test explicit description override.
file_type_info = ui::SelectFileDialog::FileTypeInfo();
options.clear();
options.push_back(
BuildAcceptOption("File Types 101", "image/jpeg", std::string()));
acceptsAllTypes = false;
FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
base::FilePath::StringType(), &options, &acceptsAllTypes);
EXPECT_EQ(file_type_info.extension_description_overrides[0],
base::UTF8ToUTF16("File Types 101"));
}
TEST(FileSystemApiUnitTest, FileSystemChooseEntryFunctionSuggestionTest) {
std::string opt_name;
base::FilePath suggested_name;
base::FilePath::StringType suggested_extension;
opt_name = std::string("normal_path.txt");
FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
&suggested_extension);
EXPECT_FALSE(suggested_name.IsAbsolute());
EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt");
EXPECT_EQ(suggested_extension, ToStringType("txt"));
// We should provide just the basename, i.e., "path".
opt_name = std::string("/a/bad/path");
FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
&suggested_extension);
EXPECT_FALSE(suggested_name.IsAbsolute());
EXPECT_EQ(suggested_name.MaybeAsASCII(), "path");
EXPECT_TRUE(suggested_extension.empty());
#if !defined(OS_WIN)
// TODO(thorogood): Fix this test on Windows.
// Filter out absolute paths with no basename.
opt_name = std::string("/");
FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
&suggested_extension);
EXPECT_FALSE(suggested_name.IsAbsolute());
EXPECT_TRUE(suggested_name.MaybeAsASCII().empty());
EXPECT_TRUE(suggested_extension.empty());
#endif
}
#if defined(OS_CHROMEOS)
TEST_F(FileSystemApiConsentProviderTest, ForNonKioskApps) {
// Component apps are not granted unless they are whitelisted.
{
scoped_refptr<Extension> component_extension(
test_util::BuildApp(
std::move(ExtensionBuilder().SetLocation(Manifest::COMPONENT)))
.Build());
TestingConsentProviderDelegate delegate;
ConsentProvider provider(&delegate);
EXPECT_FALSE(provider.IsGrantable(*component_extension));
}
// Whitelitsed component apps are instantly granted access without asking
// user.
{
scoped_refptr<Extension> whitelisted_component_extension(
test_util::BuildApp(
std::move(ExtensionBuilder().SetLocation(Manifest::COMPONENT)))
.Build());
TestingConsentProviderDelegate delegate;
delegate.SetComponentWhitelist(whitelisted_component_extension->id());
ConsentProvider provider(&delegate);
EXPECT_TRUE(provider.IsGrantable(*whitelisted_component_extension));
ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
provider.RequestConsent(*whitelisted_component_extension.get(), volume_,
true /* writable */,
base::Bind(&OnConsentReceived, &result));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, delegate.show_dialog_counter());
EXPECT_EQ(0, delegate.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<Extension> non_component_extension(
test_util::CreateEmptyExtension());
TestingConsentProviderDelegate delegate;
ConsentProvider provider(&delegate);
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<Extension> auto_launch_kiosk_app(
test_util::BuildApp(ExtensionBuilder())
.MergeManifest(DictionaryBuilder()
.SetBoolean("kiosk_enabled", true)
.SetBoolean("kiosk_only", true)
.Build())
.Build());
user_manager_->AddKioskAppUser(
AccountId::FromUserEmail(auto_launch_kiosk_app->id()));
user_manager_->LoginUser(
AccountId::FromUserEmail(auto_launch_kiosk_app->id()));
TestingConsentProviderDelegate delegate;
delegate.SetIsAutoLaunched(true);
ConsentProvider provider(&delegate);
EXPECT_TRUE(provider.IsGrantable(*auto_launch_kiosk_app));
ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
provider.RequestConsent(*auto_launch_kiosk_app.get(), volume_,
true /* writable */,
base::Bind(&OnConsentReceived, &result));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, delegate.show_dialog_counter());
EXPECT_EQ(1, delegate.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<Extension> manual_launch_kiosk_app(
test_util::BuildApp(ExtensionBuilder())
.MergeManifest(DictionaryBuilder()
.SetBoolean("kiosk_enabled", true)
.SetBoolean("kiosk_only", true)
.Build())
.Build());
user_manager::User* const manual_kiosk_app_user =
user_manager_->AddKioskAppUser(
AccountId::FromUserEmail(manual_launch_kiosk_app->id()));
user_manager_->KioskAppLoggedIn(manual_kiosk_app_user);
{
TestingConsentProviderDelegate delegate;
delegate.SetDialogButton(ui::DIALOG_BUTTON_OK);
ConsentProvider provider(&delegate);
EXPECT_TRUE(provider.IsGrantable(*manual_launch_kiosk_app));
ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
provider.RequestConsent(*manual_launch_kiosk_app.get(), volume_,
true /* writable */,
base::Bind(&OnConsentReceived, &result));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, delegate.show_dialog_counter());
EXPECT_EQ(0, delegate.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.
{
TestingConsentProviderDelegate delegate;
ConsentProvider provider(&delegate);
delegate.SetDialogButton(ui::DIALOG_BUTTON_CANCEL);
EXPECT_TRUE(provider.IsGrantable(*manual_launch_kiosk_app));
ConsentProvider::Consent result = ConsentProvider::CONSENT_IMPOSSIBLE;
provider.RequestConsent(*manual_launch_kiosk_app.get(), volume_,
true /* writable */,
base::Bind(&OnConsentReceived, &result));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, delegate.show_dialog_counter());
EXPECT_EQ(0, delegate.show_notification_counter());
EXPECT_EQ(ConsentProvider::CONSENT_REJECTED, result);
}
}
#endif
} // namespace extensions
|