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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <array>
// StorageMonitorLinux unit tests.
#include "components/storage_monitor/storage_monitor_linux.h"
#include <mntent.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <memory>
#include <string>
#include "base/check.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/task_environment.h"
#include "components/storage_monitor/mock_removable_storage_observer.h"
#include "components/storage_monitor/removable_device_constants.h"
#include "components/storage_monitor/storage_info.h"
#include "components/storage_monitor/storage_monitor.h"
#include "components/storage_monitor/test_storage_monitor.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace storage_monitor {
namespace {
const char kValidFS[] = "vfat";
const char kInvalidFS[] = "invalidfs";
const char kInvalidPath[] = "invalid path does not exist";
const char kDeviceDCIM1[] = "d1";
const char kDeviceDCIM2[] = "d2";
const char kDeviceDCIM3[] = "d3";
const char kDeviceNoDCIM[] = "d4";
const char kDeviceFixed[] = "d5";
const char kInvalidDevice[] = "invalid_device";
const char kMountPointA[] = "mnt_a";
const char kMountPointB[] = "mnt_b";
const char kMountPointC[] = "mnt_c";
struct TestDeviceData {
const char* device_path;
const char* unique_id;
StorageInfo::Type type;
uint64_t partition_size_in_bytes;
};
constexpr auto kTestDeviceData = std::to_array<TestDeviceData>({
{kDeviceDCIM1, "UUID:FFF0-000F",
StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, 88788},
{kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989",
StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, 8773},
{kDeviceDCIM3, "VendorModelSerial:::WEM319X792",
StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, 22837},
{kDeviceNoDCIM, "UUID:ABCD-1234",
StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM, 512},
{kDeviceFixed, "UUID:743A-2349", StorageInfo::FIXED_MASS_STORAGE, 17282},
});
std::unique_ptr<StorageInfo> GetDeviceInfo(const base::FilePath& device_path,
const base::FilePath& mount_point) {
bool device_found = false;
size_t i = 0;
for (; i < std::size(kTestDeviceData); i++) {
if (device_path.value() == kTestDeviceData[i].device_path) {
device_found = true;
break;
}
}
DCHECK(device_found);
StorageInfo::Type type = kTestDeviceData[i].type;
auto storage_info = std::make_unique<StorageInfo>(
StorageInfo::MakeDeviceId(type, kTestDeviceData[i].unique_id),
mount_point.value(), u"volume label", u"vendor name", u"model name",
kTestDeviceData[i].partition_size_in_bytes);
return storage_info;
}
uint64_t GetDevicePartitionSize(const std::string& device) {
for (const auto& data : kTestDeviceData) {
if (device == data.device_path)
return data.partition_size_in_bytes;
}
return 0;
}
std::string GetDeviceId(const std::string& device) {
for (const auto& data : kTestDeviceData) {
if (device == data.device_path)
return StorageInfo::MakeDeviceId(data.type, data.unique_id);
}
if (device == kInvalidDevice) {
return StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
kInvalidDevice);
}
return std::string();
}
class TestStorageMonitorLinux : public StorageMonitorLinux {
public:
explicit TestStorageMonitorLinux(const base::FilePath& path)
: StorageMonitorLinux(path) {
SetGetDeviceInfoCallbackForTest(base::BindRepeating(&GetDeviceInfo));
}
TestStorageMonitorLinux(const TestStorageMonitorLinux&) = delete;
TestStorageMonitorLinux& operator=(const TestStorageMonitorLinux&) = delete;
~TestStorageMonitorLinux() override = default;
void SetOnMtabUpdateCallback(base::OnceClosure on_mtab_update_callback) {
EXPECT_FALSE(on_mtab_update_callback_);
on_mtab_update_callback_ = std::move(on_mtab_update_callback);
}
private:
void UpdateMtab(
const MtabWatcherLinux::MountPointDeviceMap& new_mtab) override {
StorageMonitorLinux::UpdateMtab(new_mtab);
if (on_mtab_update_callback_)
std::move(on_mtab_update_callback_).Run();
}
base::OnceClosure on_mtab_update_callback_;
};
class StorageMonitorLinuxTest : public testing::Test {
public:
struct MtabTestData {
MtabTestData(const std::string& mount_device,
const std::string& mount_point,
const std::string& mount_type)
: mount_device(mount_device),
mount_point(mount_point),
mount_type(mount_type) {
}
const std::string mount_device;
const std::string mount_point;
const std::string mount_type;
};
StorageMonitorLinuxTest() = default;
StorageMonitorLinuxTest(const StorageMonitorLinuxTest&) = delete;
StorageMonitorLinuxTest& operator=(const StorageMonitorLinuxTest&) = delete;
~StorageMonitorLinuxTest() override = default;
protected:
void SetUp() override {
// Create and set up a temp dir with files for the test.
ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
base::FilePath test_dir =
scoped_temp_dir_.GetPath().AppendASCII("test_etc");
ASSERT_TRUE(base::CreateDirectory(test_dir));
mtab_file_ = test_dir.AppendASCII("test_mtab");
MtabTestData initial_test_data[] = {
MtabTestData("dummydevice", "dummydir", kInvalidFS),
};
WriteToMtab(initial_test_data, std::size(initial_test_data),
/*overwrite=*/true);
monitor_ = std::make_unique<TestStorageMonitorLinux>(mtab_file_);
mock_storage_observer_ = std::make_unique<MockRemovableStorageObserver>();
monitor_->AddObserver(mock_storage_observer_.get());
monitor_->Init();
task_environment_.RunUntilIdle();
}
void TearDown() override {
task_environment_.RunUntilIdle();
monitor_->RemoveObserver(mock_storage_observer_.get());
task_environment_.RunUntilIdle();
// Linux storage monitor must be destroyed on the UI thread, so do it here.
monitor_.reset();
}
// Append mtab entries from the |data| array of size |data_size| to the mtab
// file, and run the message loop.
void AppendToMtabAndRunLoop(const MtabTestData* data, size_t data_size) {
WriteToMtab(data, data_size, /*overwrite=*/false);
WaitForMtabUpdate();
}
// Overwrite the mtab file with mtab entries from the |data| array of size
// |data_size|, and run the message loop.
void OverwriteMtabAndRunLoop(const MtabTestData* data, size_t data_size) {
WriteToMtab(data, data_size, /*overwrite=*/true);
WaitForMtabUpdate();
}
// Simplied version of OverwriteMtabAndRunLoop() that just deletes all the
// entries in the mtab file.
void WriteEmptyMtabAndRunLoop() {
OverwriteMtabAndRunLoop(/*data=*/nullptr, /*data_size=*/0);
}
// Create a directory named |dir| relative to the test directory.
// It has a DCIM directory, so StorageMonitorLinux recognizes it as a media
// directory.
base::FilePath CreateMountPointWithDCIMDir(const std::string& dir) {
return CreateMountPoint(dir, /*with_dcim_dir=*/true);
}
// Create a directory named |dir| relative to the test directory.
// It does not have a DCIM directory, so StorageMonitorLinux does not
// recognize it as a media directory.
base::FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) {
return CreateMountPoint(dir, /*with_dcim_dir=*/false);
}
void RemoveDCIMDirFromMountPoint(const std::string& dir) {
base::FilePath dcim =
scoped_temp_dir_.GetPath().AppendASCII(dir).Append(kDCIMDirectoryName);
base::DeleteFile(dcim);
}
MockRemovableStorageObserver& observer() {
return *mock_storage_observer_;
}
StorageMonitor* notifier() {
return monitor_.get();
}
uint64_t GetStorageSize(const base::FilePath& path) {
StorageInfo info;
if (!notifier()->GetStorageInfoForPath(path, &info))
return 0;
return info.total_size_in_bytes();
}
private:
// Invoked after making an mtab update. Blocks (in an active RunLoop) until
// the mtab changes are detected by the file watcher and side-effects of
// UpdateMtab() propagate.
void WaitForMtabUpdate() {
base::RunLoop run_loop;
monitor_->SetOnMtabUpdateCallback(run_loop.QuitClosure());
// Wait until the UpdateMtab() notification comes in from the system
// (cannot use RunUntilIdle right away as that would racily return early
// per being idle until the system notification comes in).
run_loop.Run();
// UpdateMtab() causes asynchronous work on internal task runners, flush
// everything to make sure `mock_storage_observer_` gets to observe the
// change.
task_environment_.RunUntilIdle();
}
// Create a directory named |dir| relative to the test directory.
// Set |with_dcim_dir| to true if the created directory will have a "DCIM"
// subdirectory.
// Returns the full path to the created directory on success, or an empty
// path on failure.
base::FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) {
base::FilePath return_path(scoped_temp_dir_.GetPath());
return_path = return_path.AppendASCII(dir);
base::FilePath path(return_path);
if (with_dcim_dir)
path = path.Append(kDCIMDirectoryName);
if (!base::CreateDirectory(path))
return base::FilePath();
return return_path;
}
// Write the test mtab data to |mtab_file_|.
// |data| is an array of mtab entries.
// |data_size| is the array size of |data|.
// |overwrite| specifies whether to overwrite |mtab_file_|.
void WriteToMtab(const MtabTestData* data,
size_t data_size,
bool overwrite) {
FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a");
ASSERT_TRUE(file);
// Due to the glibc *mntent() interface design, which is out of our
// control, the mtnent struct has several char* fields, even though
// addmntent() does not write to them in the calls below. To make the
// compiler happy while avoiding making additional copies of strings,
// we just const_cast() the strings' c_str()s.
// Assuming addmntent() does not write to the char* fields, this is safe.
// It is unlikely the platforms this test suite runs on will have an
// addmntent() implementation that does change the char* fields. If that
// was ever the case, the test suite will start crashing or failing.
mntent entry;
static const char kMountOpts[] = "rw";
entry.mnt_opts = const_cast<char*>(kMountOpts);
entry.mnt_freq = 0;
entry.mnt_passno = 0;
for (size_t i = 0; i < data_size; ++i) {
entry.mnt_fsname = const_cast<char*>(data[i].mount_device.c_str());
entry.mnt_dir = const_cast<char*>(data[i].mount_point.c_str());
entry.mnt_type = const_cast<char*>(data[i].mount_type.c_str());
ASSERT_EQ(0, addmntent(file, &entry));
}
ASSERT_EQ(1, endmntent(file));
}
base::test::TaskEnvironment task_environment_;
std::unique_ptr<MockRemovableStorageObserver> mock_storage_observer_;
// Temporary directory for created test data.
base::ScopedTempDir scoped_temp_dir_;
// Path to the test mtab file.
base::FilePath mtab_file_;
std::unique_ptr<TestStorageMonitorLinux> monitor_;
};
// TODO(crbug.com/40822314): This test is flaky.
// Simple test case where we attach and detach a media device.
TEST_F(StorageMonitorLinuxTest, DISABLED_BasicAttachDetach) {
base::FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA);
ASSERT_FALSE(test_path.empty());
MtabTestData test_data[] = {
MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS),
MtabTestData(kDeviceFixed, kInvalidPath, kValidFS),
};
// Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad
// path.
AppendToMtabAndRunLoop(test_data, std::size(test_data));
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
EXPECT_EQ(GetDeviceId(kDeviceDCIM2), observer().last_attached().device_id());
EXPECT_EQ(test_path.value(), observer().last_attached().location());
// |kDeviceDCIM2| should be detached here.
WriteEmptyMtabAndRunLoop();
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
EXPECT_EQ(GetDeviceId(kDeviceDCIM2), observer().last_detached().device_id());
}
// Only removable devices are recognized.
// This test is flaky, see https://crbug.com/1012211
TEST_F(StorageMonitorLinuxTest, Removable) {
base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
ASSERT_FALSE(test_path_a.empty());
MtabTestData test_data1[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
};
// |kDeviceDCIM1| should be attached as expected.
AppendToMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
EXPECT_EQ(GetDeviceId(kDeviceDCIM1), observer().last_attached().device_id());
EXPECT_EQ(test_path_a.value(), observer().last_attached().location());
// This should do nothing, since |kDeviceFixed| is not removable.
base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
ASSERT_FALSE(test_path_b.empty());
MtabTestData test_data2[] = {
MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data2, std::size(test_data2));
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
// |kDeviceDCIM1| should be detached as expected.
WriteEmptyMtabAndRunLoop();
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
EXPECT_EQ(GetDeviceId(kDeviceDCIM1), observer().last_detached().device_id());
// |kDeviceNoDCIM| should be attached as expected.
MtabTestData test_data3[] = {
MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data3, std::size(test_data3));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), observer().last_attached().device_id());
EXPECT_EQ(test_path_b.value(), observer().last_attached().location());
// |kDeviceNoDCIM| should be detached as expected.
WriteEmptyMtabAndRunLoop();
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(2, observer().detach_calls());
EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), observer().last_detached().device_id());
}
// More complicated test case with multiple devices on multiple mount points.
TEST_F(StorageMonitorLinuxTest, SwapMountPoints) {
base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
ASSERT_FALSE(test_path_a.empty());
ASSERT_FALSE(test_path_b.empty());
// Attach two devices.
// (*'d mounts are those StorageMonitor knows about.)
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM2 -> kMountPointB *
MtabTestData test_data1[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
// Detach two devices from old mount points and attach the devices at new
// mount points.
// kDeviceDCIM1 -> kMountPointB *
// kDeviceDCIM2 -> kMountPointA *
MtabTestData test_data2[] = {
MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
MtabTestData(kDeviceDCIM2, test_path_a.value(), kValidFS),
};
OverwriteMtabAndRunLoop(test_data2, std::size(test_data2));
EXPECT_EQ(4, observer().attach_calls());
EXPECT_EQ(2, observer().detach_calls());
// Detach all devices.
WriteEmptyMtabAndRunLoop();
EXPECT_EQ(4, observer().attach_calls());
EXPECT_EQ(4, observer().detach_calls());
}
// More complicated test case with multiple devices on multiple mount points.
TEST_F(StorageMonitorLinuxTest, MultiDevicesMultiMountPoints) {
base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
ASSERT_FALSE(test_path_a.empty());
ASSERT_FALSE(test_path_b.empty());
// Attach two devices.
// (*'d mounts are those StorageMonitor knows about.)
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM2 -> kMountPointB *
MtabTestData test_data1[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
// Attach |kDeviceDCIM1| to |kMountPointB|.
// |kDeviceDCIM2| is inaccessible, so it is detached. |kDeviceDCIM1| has been
// attached at |kMountPointB|, but is still accessible from |kMountPointA|.
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM2 -> kMountPointB
// kDeviceDCIM1 -> kMountPointB
MtabTestData test_data2[] = {
MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data2, std::size(test_data2));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
// Detach |kDeviceDCIM1| from |kMountPointA|, causing a detach and attach
// event.
// kDeviceDCIM2 -> kMountPointB
// kDeviceDCIM1 -> kMountPointB *
MtabTestData test_data3[] = {
MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
};
OverwriteMtabAndRunLoop(test_data3, std::size(test_data3));
EXPECT_EQ(3, observer().attach_calls());
EXPECT_EQ(2, observer().detach_calls());
// Attach |kDeviceDCIM1| to |kMountPointA|.
// kDeviceDCIM2 -> kMountPointB
// kDeviceDCIM1 -> kMountPointB *
// kDeviceDCIM1 -> kMountPointA
MtabTestData test_data4[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data4, std::size(test_data4));
EXPECT_EQ(3, observer().attach_calls());
EXPECT_EQ(2, observer().detach_calls());
// Detach |kDeviceDCIM1| from |kMountPointB|.
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM2 -> kMountPointB *
OverwriteMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(5, observer().attach_calls());
EXPECT_EQ(3, observer().detach_calls());
// Detach all devices.
WriteEmptyMtabAndRunLoop();
EXPECT_EQ(5, observer().attach_calls());
EXPECT_EQ(5, observer().detach_calls());
}
TEST_F(StorageMonitorLinuxTest, MultipleMountPointsWithNonDCIMDevices) {
base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
ASSERT_FALSE(test_path_a.empty());
ASSERT_FALSE(test_path_b.empty());
// Attach to one first.
// (*'d mounts are those StorageMonitor knows about.)
// kDeviceDCIM1 -> kMountPointA *
MtabTestData test_data1[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
// Attach |kDeviceDCIM1| to |kMountPointB|.
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM1 -> kMountPointB
MtabTestData test_data2[] = {
MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data2, std::size(test_data2));
EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
// Attach |kDeviceFixed| (a non-removable device) to |kMountPointA|.
// kDeviceDCIM1 -> kMountPointA
// kDeviceDCIM1 -> kMountPointB *
// kDeviceFixed -> kMountPointA
MtabTestData test_data3[] = {
MtabTestData(kDeviceFixed, test_path_a.value(), kValidFS),
};
RemoveDCIMDirFromMountPoint(kMountPointA);
AppendToMtabAndRunLoop(test_data3, std::size(test_data3));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
// Detach |kDeviceFixed|.
// kDeviceDCIM1 -> kMountPointA
// kDeviceDCIM1 -> kMountPointB *
MtabTestData test_data4[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
};
CreateMountPointWithDCIMDir(kMountPointA);
OverwriteMtabAndRunLoop(test_data4, std::size(test_data4));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
// Attach |kDeviceNoDCIM| (a non-DCIM device) to |kMountPointB|.
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM1 -> kMountPointB
// kDeviceNoDCIM -> kMountPointB *
MtabTestData test_data5[] = {
MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
};
base::DeleteFile(test_path_b.Append(kDCIMDirectoryName));
AppendToMtabAndRunLoop(test_data5, std::size(test_data5));
EXPECT_EQ(4, observer().attach_calls());
EXPECT_EQ(2, observer().detach_calls());
// Detach |kDeviceNoDCIM|.
// kDeviceDCIM1 -> kMountPointA *
// kDeviceDCIM1 -> kMountPointB
MtabTestData test_data6[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
};
CreateMountPointWithDCIMDir(kMountPointB);
OverwriteMtabAndRunLoop(test_data6, std::size(test_data6));
EXPECT_EQ(4, observer().attach_calls());
EXPECT_EQ(3, observer().detach_calls());
// Detach |kDeviceDCIM1| from |kMountPointB|.
// kDeviceDCIM1 -> kMountPointA *
OverwriteMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(4, observer().attach_calls());
EXPECT_EQ(3, observer().detach_calls());
// Detach all devices.
WriteEmptyMtabAndRunLoop();
EXPECT_EQ(4, observer().attach_calls());
EXPECT_EQ(4, observer().detach_calls());
}
TEST_F(StorageMonitorLinuxTest, DeviceLookUp) {
base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
base::FilePath test_path_c = CreateMountPointWithoutDCIMDir(kMountPointC);
ASSERT_FALSE(test_path_a.empty());
ASSERT_FALSE(test_path_b.empty());
ASSERT_FALSE(test_path_c.empty());
// Attach to one first.
// (starred mounts are those StorageMonitor knows about.)
// kDeviceDCIM1 -> kMountPointA *
// kDeviceNoDCIM -> kMountPointB *
// kDeviceFixed -> kMountPointC
MtabTestData test_data1[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
MtabTestData(kDeviceFixed, test_path_c.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
StorageInfo device_info;
EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_a, &device_info));
EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id());
EXPECT_EQ(test_path_a.value(), device_info.location());
EXPECT_EQ(88788ULL, device_info.total_size_in_bytes());
EXPECT_EQ(u"volume label", device_info.storage_label());
EXPECT_EQ(u"vendor name", device_info.vendor_name());
EXPECT_EQ(u"model name", device_info.model_name());
EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_b, &device_info));
EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), device_info.device_id());
EXPECT_EQ(test_path_b.value(), device_info.location());
EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_c, &device_info));
EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id());
EXPECT_EQ(test_path_c.value(), device_info.location());
// An invalid path.
EXPECT_FALSE(notifier()->GetStorageInfoForPath(base::FilePath(kInvalidPath),
&device_info));
// Test filling in of the mount point.
EXPECT_TRUE(
notifier()->GetStorageInfoForPath(test_path_a.Append("some/other/path"),
&device_info));
EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id());
EXPECT_EQ(test_path_a.value(), device_info.location());
// One device attached at multiple points.
// kDeviceDCIM1 -> kMountPointA *
// kDeviceFixed -> kMountPointB
// kDeviceFixed -> kMountPointC
MtabTestData test_data2[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS),
MtabTestData(kDeviceFixed, test_path_c.value(), kValidFS),
};
AppendToMtabAndRunLoop(test_data2, std::size(test_data2));
EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_a, &device_info));
EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id());
EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_b, &device_info));
EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id());
EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_c, &device_info));
EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id());
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls());
}
TEST_F(StorageMonitorLinuxTest, DevicePartitionSize) {
base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
ASSERT_FALSE(test_path_a.empty());
ASSERT_FALSE(test_path_b.empty());
MtabTestData test_data1[] = {
MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
MtabTestData(kDeviceFixed, kInvalidPath, kInvalidFS),
};
AppendToMtabAndRunLoop(test_data1, std::size(test_data1));
EXPECT_EQ(2, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls());
EXPECT_EQ(GetDevicePartitionSize(kDeviceDCIM1),
GetStorageSize(test_path_a));
EXPECT_EQ(GetDevicePartitionSize(kDeviceNoDCIM),
GetStorageSize(test_path_b));
EXPECT_EQ(GetDevicePartitionSize(kInvalidPath),
GetStorageSize(base::FilePath(kInvalidPath)));
}
} // namespace
} // namespace storage_monitor
|