[Eraser strings] Remove/replace some supervised user strings
[chromium-blink-merge.git] / components / storage_monitor / storage_monitor_linux_unittest.cc
blob710b7c45319de8881b2a2b0b53a4d2ae5848cb51
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // StorageMonitorLinux unit tests.
7 #include "components/storage_monitor/storage_monitor_linux.h"
9 #include <mntent.h>
10 #include <stdio.h>
12 #include <string>
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/run_loop.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "components/storage_monitor/mock_removable_storage_observer.h"
22 #include "components/storage_monitor/removable_device_constants.h"
23 #include "components/storage_monitor/storage_info.h"
24 #include "components/storage_monitor/storage_monitor.h"
25 #include "components/storage_monitor/test_media_transfer_protocol_manager_linux.h"
26 #include "components/storage_monitor/test_storage_monitor.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 namespace storage_monitor {
32 namespace {
34 const char kValidFS[] = "vfat";
35 const char kInvalidFS[] = "invalidfs";
37 const char kInvalidPath[] = "invalid path does not exist";
39 const char kDeviceDCIM1[] = "d1";
40 const char kDeviceDCIM2[] = "d2";
41 const char kDeviceDCIM3[] = "d3";
42 const char kDeviceNoDCIM[] = "d4";
43 const char kDeviceFixed[] = "d5";
45 const char kInvalidDevice[] = "invalid_device";
47 const char kMountPointA[] = "mnt_a";
48 const char kMountPointB[] = "mnt_b";
49 const char kMountPointC[] = "mnt_c";
51 struct TestDeviceData {
52 const char* device_path;
53 const char* unique_id;
54 StorageInfo::Type type;
55 uint64 partition_size_in_bytes;
58 const TestDeviceData kTestDeviceData[] = {
59 { kDeviceDCIM1, "UUID:FFF0-000F",
60 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, 88788 },
61 { kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989",
62 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM,
63 8773 },
64 { kDeviceDCIM3, "VendorModelSerial:::WEM319X792",
65 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, 22837 },
66 { kDeviceNoDCIM, "UUID:ABCD-1234",
67 StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM, 512 },
68 { kDeviceFixed, "UUID:743A-2349",
69 StorageInfo::FIXED_MASS_STORAGE, 17282 },
72 scoped_ptr<StorageInfo> GetDeviceInfo(const base::FilePath& device_path,
73 const base::FilePath& mount_point) {
74 bool device_found = false;
75 size_t i = 0;
76 for (; i < arraysize(kTestDeviceData); i++) {
77 if (device_path.value() == kTestDeviceData[i].device_path) {
78 device_found = true;
79 break;
83 scoped_ptr<StorageInfo> storage_info;
84 if (!device_found) {
85 NOTREACHED();
86 return storage_info.Pass();
89 StorageInfo::Type type = kTestDeviceData[i].type;
90 storage_info.reset(new StorageInfo(
91 StorageInfo::MakeDeviceId(type, kTestDeviceData[i].unique_id),
92 mount_point.value(),
93 base::ASCIIToUTF16("volume label"),
94 base::ASCIIToUTF16("vendor name"),
95 base::ASCIIToUTF16("model name"),
96 kTestDeviceData[i].partition_size_in_bytes));
97 return storage_info.Pass();
100 uint64 GetDevicePartitionSize(const std::string& device) {
101 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) {
102 if (device == kTestDeviceData[i].device_path)
103 return kTestDeviceData[i].partition_size_in_bytes;
105 return 0;
108 std::string GetDeviceId(const std::string& device) {
109 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) {
110 if (device == kTestDeviceData[i].device_path) {
111 return StorageInfo::MakeDeviceId(kTestDeviceData[i].type,
112 kTestDeviceData[i].unique_id);
115 if (device == kInvalidDevice) {
116 return StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
117 kInvalidDevice);
119 return std::string();
122 class TestStorageMonitorLinux : public StorageMonitorLinux {
123 public:
124 explicit TestStorageMonitorLinux(const base::FilePath& path)
125 : StorageMonitorLinux(path) {
126 SetMediaTransferProtocolManagerForTest(
127 new TestMediaTransferProtocolManagerLinux());
128 SetGetDeviceInfoCallbackForTest(base::Bind(&GetDeviceInfo));
130 ~TestStorageMonitorLinux() override {}
132 private:
133 void UpdateMtab(
134 const MtabWatcherLinux::MountPointDeviceMap& new_mtab) override {
135 StorageMonitorLinux::UpdateMtab(new_mtab);
136 base::ThreadTaskRunnerHandle::Get()->PostTask(
137 FROM_HERE, base::MessageLoop::QuitClosure());
140 DISALLOW_COPY_AND_ASSIGN(TestStorageMonitorLinux);
143 class StorageMonitorLinuxTest : public testing::Test {
144 public:
145 struct MtabTestData {
146 MtabTestData(const std::string& mount_device,
147 const std::string& mount_point,
148 const std::string& mount_type)
149 : mount_device(mount_device),
150 mount_point(mount_point),
151 mount_type(mount_type) {
154 const std::string mount_device;
155 const std::string mount_point;
156 const std::string mount_type;
159 StorageMonitorLinuxTest()
160 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
161 ~StorageMonitorLinuxTest() override {}
163 protected:
164 void SetUp() override {
165 // Create and set up a temp dir with files for the test.
166 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
167 base::FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc");
168 ASSERT_TRUE(base::CreateDirectory(test_dir));
169 mtab_file_ = test_dir.AppendASCII("test_mtab");
170 MtabTestData initial_test_data[] = {
171 MtabTestData("dummydevice", "dummydir", kInvalidFS),
173 WriteToMtab(initial_test_data,
174 arraysize(initial_test_data),
175 true /* overwrite */);
177 monitor_.reset(new TestStorageMonitorLinux(mtab_file_));
179 mock_storage_observer_.reset(new MockRemovableStorageObserver);
180 monitor_->AddObserver(mock_storage_observer_.get());
182 monitor_->Init();
183 base::RunLoop().RunUntilIdle();
186 void TearDown() override {
187 base::RunLoop().RunUntilIdle();
188 monitor_->RemoveObserver(mock_storage_observer_.get());
189 base::RunLoop().RunUntilIdle();
191 // Linux storage monitor must be destroyed on the UI thread, so do it here.
192 monitor_.reset();
195 // Append mtab entries from the |data| array of size |data_size| to the mtab
196 // file, and run the message loop.
197 void AppendToMtabAndRunLoop(const MtabTestData* data, size_t data_size) {
198 WriteToMtab(data, data_size, false /* do not overwrite */);
199 base::RunLoop().Run();
202 // Overwrite the mtab file with mtab entries from the |data| array of size
203 // |data_size|, and run the message loop.
204 void OverwriteMtabAndRunLoop(const MtabTestData* data, size_t data_size) {
205 WriteToMtab(data, data_size, true /* overwrite */);
206 base::RunLoop().Run();
209 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the
210 // entries in the mtab file.
211 void WriteEmptyMtabAndRunLoop() {
212 OverwriteMtabAndRunLoop(NULL, // No data.
213 0); // No data length.
216 // Create a directory named |dir| relative to the test directory.
217 // It has a DCIM directory, so StorageMonitorLinux recognizes it as a media
218 // directory.
219 base::FilePath CreateMountPointWithDCIMDir(const std::string& dir) {
220 return CreateMountPoint(dir, true /* create DCIM dir */);
223 // Create a directory named |dir| relative to the test directory.
224 // It does not have a DCIM directory, so StorageMonitorLinux does not
225 // recognize it as a media directory.
226 base::FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) {
227 return CreateMountPoint(dir, false /* do not create DCIM dir */);
230 void RemoveDCIMDirFromMountPoint(const std::string& dir) {
231 base::FilePath dcim =
232 scoped_temp_dir_.path().AppendASCII(dir).Append(kDCIMDirectoryName);
233 base::DeleteFile(dcim, false);
236 MockRemovableStorageObserver& observer() {
237 return *mock_storage_observer_;
240 StorageMonitor* notifier() {
241 return monitor_.get();
244 uint64 GetStorageSize(const base::FilePath& path) {
245 StorageInfo info;
246 if (!notifier()->GetStorageInfoForPath(path, &info))
247 return 0;
249 return info.total_size_in_bytes();
252 private:
253 // Create a directory named |dir| relative to the test directory.
254 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
255 // subdirectory.
256 // Returns the full path to the created directory on success, or an empty
257 // path on failure.
258 base::FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) {
259 base::FilePath return_path(scoped_temp_dir_.path());
260 return_path = return_path.AppendASCII(dir);
261 base::FilePath path(return_path);
262 if (with_dcim_dir)
263 path = path.Append(kDCIMDirectoryName);
264 if (!base::CreateDirectory(path))
265 return base::FilePath();
266 return return_path;
269 // Write the test mtab data to |mtab_file_|.
270 // |data| is an array of mtab entries.
271 // |data_size| is the array size of |data|.
272 // |overwrite| specifies whether to overwrite |mtab_file_|.
273 void WriteToMtab(const MtabTestData* data,
274 size_t data_size,
275 bool overwrite) {
276 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a");
277 ASSERT_TRUE(file);
279 // Due to the glibc *mntent() interface design, which is out of our
280 // control, the mtnent struct has several char* fields, even though
281 // addmntent() does not write to them in the calls below. To make the
282 // compiler happy while avoiding making additional copies of strings,
283 // we just const_cast() the strings' c_str()s.
284 // Assuming addmntent() does not write to the char* fields, this is safe.
285 // It is unlikely the platforms this test suite runs on will have an
286 // addmntent() implementation that does change the char* fields. If that
287 // was ever the case, the test suite will start crashing or failing.
288 mntent entry;
289 static const char kMountOpts[] = "rw";
290 entry.mnt_opts = const_cast<char*>(kMountOpts);
291 entry.mnt_freq = 0;
292 entry.mnt_passno = 0;
293 for (size_t i = 0; i < data_size; ++i) {
294 entry.mnt_fsname = const_cast<char*>(data[i].mount_device.c_str());
295 entry.mnt_dir = const_cast<char*>(data[i].mount_point.c_str());
296 entry.mnt_type = const_cast<char*>(data[i].mount_type.c_str());
297 ASSERT_EQ(0, addmntent(file, &entry));
299 ASSERT_EQ(1, endmntent(file));
302 content::TestBrowserThreadBundle thread_bundle_;
304 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
306 // Temporary directory for created test data.
307 base::ScopedTempDir scoped_temp_dir_;
308 // Path to the test mtab file.
309 base::FilePath mtab_file_;
311 scoped_ptr<TestStorageMonitorLinux> monitor_;
313 DISALLOW_COPY_AND_ASSIGN(StorageMonitorLinuxTest);
316 // Simple test case where we attach and detach a media device.
317 TEST_F(StorageMonitorLinuxTest, BasicAttachDetach) {
318 base::FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA);
319 ASSERT_FALSE(test_path.empty());
320 MtabTestData test_data[] = {
321 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS),
322 MtabTestData(kDeviceFixed, kInvalidPath, kValidFS),
324 // Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad
325 // path.
326 AppendToMtabAndRunLoop(test_data, arraysize(test_data));
328 EXPECT_EQ(1, observer().attach_calls());
329 EXPECT_EQ(0, observer().detach_calls());
330 EXPECT_EQ(GetDeviceId(kDeviceDCIM2), observer().last_attached().device_id());
331 EXPECT_EQ(test_path.value(), observer().last_attached().location());
333 // |kDeviceDCIM2| should be detached here.
334 WriteEmptyMtabAndRunLoop();
335 EXPECT_EQ(1, observer().attach_calls());
336 EXPECT_EQ(1, observer().detach_calls());
337 EXPECT_EQ(GetDeviceId(kDeviceDCIM2), observer().last_detached().device_id());
340 // Only removable devices are recognized.
341 TEST_F(StorageMonitorLinuxTest, Removable) {
342 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
343 ASSERT_FALSE(test_path_a.empty());
344 MtabTestData test_data1[] = {
345 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
347 // |kDeviceDCIM1| should be attached as expected.
348 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
350 EXPECT_EQ(1, observer().attach_calls());
351 EXPECT_EQ(0, observer().detach_calls());
352 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), observer().last_attached().device_id());
353 EXPECT_EQ(test_path_a.value(), observer().last_attached().location());
355 // This should do nothing, since |kDeviceFixed| is not removable.
356 base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
357 ASSERT_FALSE(test_path_b.empty());
358 MtabTestData test_data2[] = {
359 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS),
361 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
362 EXPECT_EQ(1, observer().attach_calls());
363 EXPECT_EQ(0, observer().detach_calls());
365 // |kDeviceDCIM1| should be detached as expected.
366 WriteEmptyMtabAndRunLoop();
367 EXPECT_EQ(1, observer().attach_calls());
368 EXPECT_EQ(1, observer().detach_calls());
369 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), observer().last_detached().device_id());
371 // |kDeviceNoDCIM| should be attached as expected.
372 MtabTestData test_data3[] = {
373 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
375 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3));
376 EXPECT_EQ(2, observer().attach_calls());
377 EXPECT_EQ(1, observer().detach_calls());
378 EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), observer().last_attached().device_id());
379 EXPECT_EQ(test_path_b.value(), observer().last_attached().location());
381 // |kDeviceNoDCIM| should be detached as expected.
382 WriteEmptyMtabAndRunLoop();
383 EXPECT_EQ(2, observer().attach_calls());
384 EXPECT_EQ(2, observer().detach_calls());
385 EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), observer().last_detached().device_id());
388 // More complicated test case with multiple devices on multiple mount points.
389 TEST_F(StorageMonitorLinuxTest, SwapMountPoints) {
390 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
391 base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
392 ASSERT_FALSE(test_path_a.empty());
393 ASSERT_FALSE(test_path_b.empty());
395 // Attach two devices.
396 // (*'d mounts are those StorageMonitor knows about.)
397 // kDeviceDCIM1 -> kMountPointA *
398 // kDeviceDCIM2 -> kMountPointB *
399 MtabTestData test_data1[] = {
400 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
401 MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
403 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
404 EXPECT_EQ(2, observer().attach_calls());
405 EXPECT_EQ(0, observer().detach_calls());
407 // Detach two devices from old mount points and attach the devices at new
408 // mount points.
409 // kDeviceDCIM1 -> kMountPointB *
410 // kDeviceDCIM2 -> kMountPointA *
411 MtabTestData test_data2[] = {
412 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
413 MtabTestData(kDeviceDCIM2, test_path_a.value(), kValidFS),
415 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2));
416 EXPECT_EQ(4, observer().attach_calls());
417 EXPECT_EQ(2, observer().detach_calls());
419 // Detach all devices.
420 WriteEmptyMtabAndRunLoop();
421 EXPECT_EQ(4, observer().attach_calls());
422 EXPECT_EQ(4, observer().detach_calls());
425 // More complicated test case with multiple devices on multiple mount points.
426 TEST_F(StorageMonitorLinuxTest, MultiDevicesMultiMountPoints) {
427 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
428 base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
429 ASSERT_FALSE(test_path_a.empty());
430 ASSERT_FALSE(test_path_b.empty());
432 // Attach two devices.
433 // (*'d mounts are those StorageMonitor knows about.)
434 // kDeviceDCIM1 -> kMountPointA *
435 // kDeviceDCIM2 -> kMountPointB *
436 MtabTestData test_data1[] = {
437 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
438 MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
440 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
441 EXPECT_EQ(2, observer().attach_calls());
442 EXPECT_EQ(0, observer().detach_calls());
444 // Attach |kDeviceDCIM1| to |kMountPointB|.
445 // |kDeviceDCIM2| is inaccessible, so it is detached. |kDeviceDCIM1| has been
446 // attached at |kMountPointB|, but is still accessible from |kMountPointA|.
447 // kDeviceDCIM1 -> kMountPointA *
448 // kDeviceDCIM2 -> kMountPointB
449 // kDeviceDCIM1 -> kMountPointB
450 MtabTestData test_data2[] = {
451 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
453 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
454 EXPECT_EQ(2, observer().attach_calls());
455 EXPECT_EQ(1, observer().detach_calls());
457 // Detach |kDeviceDCIM1| from |kMountPointA|, causing a detach and attach
458 // event.
459 // kDeviceDCIM2 -> kMountPointB
460 // kDeviceDCIM1 -> kMountPointB *
461 MtabTestData test_data3[] = {
462 MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
463 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
465 OverwriteMtabAndRunLoop(test_data3, arraysize(test_data3));
466 EXPECT_EQ(3, observer().attach_calls());
467 EXPECT_EQ(2, observer().detach_calls());
469 // Attach |kDeviceDCIM1| to |kMountPointA|.
470 // kDeviceDCIM2 -> kMountPointB
471 // kDeviceDCIM1 -> kMountPointB *
472 // kDeviceDCIM1 -> kMountPointA
473 MtabTestData test_data4[] = {
474 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
476 AppendToMtabAndRunLoop(test_data4, arraysize(test_data4));
477 EXPECT_EQ(3, observer().attach_calls());
478 EXPECT_EQ(2, observer().detach_calls());
480 // Detach |kDeviceDCIM1| from |kMountPointB|.
481 // kDeviceDCIM1 -> kMountPointA *
482 // kDeviceDCIM2 -> kMountPointB *
483 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1));
484 EXPECT_EQ(5, observer().attach_calls());
485 EXPECT_EQ(3, observer().detach_calls());
487 // Detach all devices.
488 WriteEmptyMtabAndRunLoop();
489 EXPECT_EQ(5, observer().attach_calls());
490 EXPECT_EQ(5, observer().detach_calls());
493 TEST_F(StorageMonitorLinuxTest, MultipleMountPointsWithNonDCIMDevices) {
494 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
495 base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
496 ASSERT_FALSE(test_path_a.empty());
497 ASSERT_FALSE(test_path_b.empty());
499 // Attach to one first.
500 // (*'d mounts are those StorageMonitor knows about.)
501 // kDeviceDCIM1 -> kMountPointA *
502 MtabTestData test_data1[] = {
503 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
505 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
506 EXPECT_EQ(1, observer().attach_calls());
507 EXPECT_EQ(0, observer().detach_calls());
509 // Attach |kDeviceDCIM1| to |kMountPointB|.
510 // kDeviceDCIM1 -> kMountPointA *
511 // kDeviceDCIM1 -> kMountPointB
512 MtabTestData test_data2[] = {
513 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
515 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
516 EXPECT_EQ(1, observer().attach_calls());
517 EXPECT_EQ(0, observer().detach_calls());
519 // Attach |kDeviceFixed| (a non-removable device) to |kMountPointA|.
520 // kDeviceDCIM1 -> kMountPointA
521 // kDeviceDCIM1 -> kMountPointB *
522 // kDeviceFixed -> kMountPointA
523 MtabTestData test_data3[] = {
524 MtabTestData(kDeviceFixed, test_path_a.value(), kValidFS),
526 RemoveDCIMDirFromMountPoint(kMountPointA);
527 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3));
528 EXPECT_EQ(2, observer().attach_calls());
529 EXPECT_EQ(1, observer().detach_calls());
531 // Detach |kDeviceFixed|.
532 // kDeviceDCIM1 -> kMountPointA
533 // kDeviceDCIM1 -> kMountPointB *
534 MtabTestData test_data4[] = {
535 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
536 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
538 CreateMountPointWithDCIMDir(kMountPointA);
539 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4));
540 EXPECT_EQ(2, observer().attach_calls());
541 EXPECT_EQ(1, observer().detach_calls());
543 // Attach |kDeviceNoDCIM| (a non-DCIM device) to |kMountPointB|.
544 // kDeviceDCIM1 -> kMountPointA *
545 // kDeviceDCIM1 -> kMountPointB
546 // kDeviceNoDCIM -> kMountPointB *
547 MtabTestData test_data5[] = {
548 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
550 base::DeleteFile(test_path_b.Append(kDCIMDirectoryName), false);
551 AppendToMtabAndRunLoop(test_data5, arraysize(test_data5));
552 EXPECT_EQ(4, observer().attach_calls());
553 EXPECT_EQ(2, observer().detach_calls());
555 // Detach |kDeviceNoDCIM|.
556 // kDeviceDCIM1 -> kMountPointA *
557 // kDeviceDCIM1 -> kMountPointB
558 MtabTestData test_data6[] = {
559 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
560 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
562 CreateMountPointWithDCIMDir(kMountPointB);
563 OverwriteMtabAndRunLoop(test_data6, arraysize(test_data6));
564 EXPECT_EQ(4, observer().attach_calls());
565 EXPECT_EQ(3, observer().detach_calls());
567 // Detach |kDeviceDCIM1| from |kMountPointB|.
568 // kDeviceDCIM1 -> kMountPointA *
569 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1));
570 EXPECT_EQ(4, observer().attach_calls());
571 EXPECT_EQ(3, observer().detach_calls());
573 // Detach all devices.
574 WriteEmptyMtabAndRunLoop();
575 EXPECT_EQ(4, observer().attach_calls());
576 EXPECT_EQ(4, observer().detach_calls());
579 TEST_F(StorageMonitorLinuxTest, DeviceLookUp) {
580 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
581 base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
582 base::FilePath test_path_c = CreateMountPointWithoutDCIMDir(kMountPointC);
583 ASSERT_FALSE(test_path_a.empty());
584 ASSERT_FALSE(test_path_b.empty());
585 ASSERT_FALSE(test_path_c.empty());
587 // Attach to one first.
588 // (starred mounts are those StorageMonitor knows about.)
589 // kDeviceDCIM1 -> kMountPointA *
590 // kDeviceNoDCIM -> kMountPointB *
591 // kDeviceFixed -> kMountPointC
592 MtabTestData test_data1[] = {
593 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
594 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
595 MtabTestData(kDeviceFixed, test_path_c.value(), kValidFS),
597 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
598 EXPECT_EQ(2, observer().attach_calls());
599 EXPECT_EQ(0, observer().detach_calls());
601 StorageInfo device_info;
602 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_a, &device_info));
603 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id());
604 EXPECT_EQ(test_path_a.value(), device_info.location());
605 EXPECT_EQ(88788ULL, device_info.total_size_in_bytes());
606 EXPECT_EQ(base::ASCIIToUTF16("volume label"), device_info.storage_label());
607 EXPECT_EQ(base::ASCIIToUTF16("vendor name"), device_info.vendor_name());
608 EXPECT_EQ(base::ASCIIToUTF16("model name"), device_info.model_name());
610 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_b, &device_info));
611 EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), device_info.device_id());
612 EXPECT_EQ(test_path_b.value(), device_info.location());
614 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_c, &device_info));
615 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id());
616 EXPECT_EQ(test_path_c.value(), device_info.location());
618 // An invalid path.
619 EXPECT_FALSE(notifier()->GetStorageInfoForPath(base::FilePath(kInvalidPath),
620 &device_info));
622 // Test filling in of the mount point.
623 EXPECT_TRUE(
624 notifier()->GetStorageInfoForPath(test_path_a.Append("some/other/path"),
625 &device_info));
626 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id());
627 EXPECT_EQ(test_path_a.value(), device_info.location());
629 // One device attached at multiple points.
630 // kDeviceDCIM1 -> kMountPointA *
631 // kDeviceFixed -> kMountPointB
632 // kDeviceFixed -> kMountPointC
633 MtabTestData test_data2[] = {
634 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
635 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS),
636 MtabTestData(kDeviceFixed, test_path_c.value(), kValidFS),
638 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
640 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_a, &device_info));
641 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id());
643 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_b, &device_info));
644 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id());
646 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_c, &device_info));
647 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id());
649 EXPECT_EQ(2, observer().attach_calls());
650 EXPECT_EQ(1, observer().detach_calls());
653 TEST_F(StorageMonitorLinuxTest, DevicePartitionSize) {
654 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
655 base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
656 ASSERT_FALSE(test_path_a.empty());
657 ASSERT_FALSE(test_path_b.empty());
659 MtabTestData test_data1[] = {
660 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
661 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
662 MtabTestData(kDeviceFixed, kInvalidPath, kInvalidFS),
664 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
665 EXPECT_EQ(2, observer().attach_calls());
666 EXPECT_EQ(0, observer().detach_calls());
668 EXPECT_EQ(GetDevicePartitionSize(kDeviceDCIM1),
669 GetStorageSize(test_path_a));
670 EXPECT_EQ(GetDevicePartitionSize(kDeviceNoDCIM),
671 GetStorageSize(test_path_b));
672 EXPECT_EQ(GetDevicePartitionSize(kInvalidPath),
673 GetStorageSize(base::FilePath(kInvalidPath)));
676 } // namespace
678 } // namespace storage_monitor