Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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. |
| 4 | |
Eric Caruso | 721d82e | 2017-08-21 15:57:30 -0700 | [diff] [blame] | 5 | #include "mtpd/device_manager.h" |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 12 | #include <base/compiler_specific.h> |
Qijiang Fan | e19d67d | 2020-04-01 08:18:39 +0900 | [diff] [blame] | 13 | #include <base/stl_util.h> |
hscham | 92fa34f | 2020-09-01 17:36:56 +0900 | [diff] [blame^] | 14 | #include <base/test/task_environment.h> |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 15 | |
Eric Caruso | 721d82e | 2017-08-21 15:57:30 -0700 | [diff] [blame] | 16 | #include "mtpd/device_event_delegate.h" |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 17 | |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 18 | namespace mtpd { |
| 19 | namespace { |
| 20 | |
Anand K Mistry | 8baa56b | 2018-08-28 15:15:06 +1000 | [diff] [blame] | 21 | class DeviceManagerTest : public testing::Test { |
| 22 | public: |
| 23 | DeviceManagerTest() = default; |
| 24 | |
| 25 | private: |
| 26 | // DeviceManager needs the current thread to have a task runner. |
hscham | 92fa34f | 2020-09-01 17:36:56 +0900 | [diff] [blame^] | 27 | base::test::TaskEnvironment task_environment_{ |
| 28 | base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY}; |
Anand K Mistry | 8baa56b | 2018-08-28 15:15:06 +1000 | [diff] [blame] | 29 | |
| 30 | DISALLOW_COPY_AND_ASSIGN(DeviceManagerTest); |
| 31 | }; |
| 32 | |
| 33 | TEST_F(DeviceManagerTest, ParseStorageName) { |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 34 | struct ParseStorageNameTestCase { |
| 35 | const char* input; |
| 36 | bool expected_result; |
| 37 | const char* expected_bus; |
| 38 | uint32_t expected_storage_id; |
| 39 | } test_cases[] = { |
Lei Zhang | 1c82c53 | 2017-04-17 22:09:12 -0700 | [diff] [blame] | 40 | {"usb:123:4", true, "usb:123", 4}, |
| 41 | {"usb:1,2,3:4", true, "usb:1,2,3", 4}, |
| 42 | {"notusb:123:4", false, "", 0}, |
| 43 | {"usb:123:4:badfield", false, "", 0}, |
| 44 | {"usb:123:not_number", false, "", 0}, |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Qijiang Fan | e19d67d | 2020-04-01 08:18:39 +0900 | [diff] [blame] | 47 | for (size_t i = 0; i < base::size(test_cases); ++i) { |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 48 | std::string bus; |
| 49 | uint32_t storage_id = static_cast<uint32_t>(-1); |
| 50 | bool result = |
| 51 | DeviceManager::ParseStorageName(test_cases[i].input, &bus, &storage_id); |
| 52 | EXPECT_EQ(test_cases[i].expected_result, result); |
| 53 | if (test_cases[i].expected_result) { |
| 54 | EXPECT_EQ(test_cases[i].expected_bus, bus); |
| 55 | EXPECT_EQ(test_cases[i].expected_storage_id, storage_id); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 60 | class TestDeviceEventDelegate : public DeviceEventDelegate { |
| 61 | public: |
| 62 | TestDeviceEventDelegate() {} |
| 63 | ~TestDeviceEventDelegate() {} |
| 64 | |
| 65 | // DeviceEventDelegate implementation. |
Alex Vakulenko | 3a09a2b | 2014-12-11 07:53:44 -0800 | [diff] [blame] | 66 | void StorageAttached(const std::string& storage_name) override {} |
| 67 | void StorageDetached(const std::string& storage_name) override {} |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate); |
| 71 | }; |
| 72 | |
| 73 | class TestDeviceManager : public DeviceManager { |
| 74 | public: |
| 75 | explicit TestDeviceManager(DeviceEventDelegate* delegate) |
Lei Zhang | 1c82c53 | 2017-04-17 22:09:12 -0700 | [diff] [blame] | 76 | : DeviceManager(delegate) {} |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 77 | ~TestDeviceManager() {} |
| 78 | |
| 79 | bool AddStorage(const std::string& storage_name, |
| 80 | const StorageInfo& storage_info) { |
| 81 | return AddStorageForTest(storage_name, storage_info); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | DISALLOW_COPY_AND_ASSIGN(TestDeviceManager); |
| 86 | }; |
| 87 | |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 88 | // Devices do not actually have a root node, so one is synthesized. |
Anand K Mistry | 8baa56b | 2018-08-28 15:15:06 +1000 | [diff] [blame] | 89 | TEST_F(DeviceManagerTest, GetFileInfoForSynthesizedRootNode) { |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 90 | const std::string kDummyStorageName = "usb:1,2:65432"; |
| 91 | StorageInfo dummy_storage_info; |
| 92 | TestDeviceEventDelegate dummy_device_event_delegate; |
| 93 | TestDeviceManager device_manager(&dummy_device_event_delegate); |
| 94 | bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info); |
| 95 | EXPECT_TRUE(ret); |
| 96 | |
Lei Zhang | 692c4e3 | 2014-08-04 13:12:05 -0700 | [diff] [blame] | 97 | std::vector<FileEntry> file_entries; |
| 98 | std::vector<uint32_t> file_ids; |
| 99 | file_ids.push_back(0); |
| 100 | ret = device_manager.GetFileInfo(kDummyStorageName, file_ids, &file_entries); |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 101 | EXPECT_TRUE(ret); |
Lei Zhang | 692c4e3 | 2014-08-04 13:12:05 -0700 | [diff] [blame] | 102 | ASSERT_EQ(1U, file_entries.size()); |
| 103 | const FileEntry& file_entry = file_entries[0]; |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 104 | |
| 105 | EXPECT_EQ(0, file_entry.item_id()); |
| 106 | EXPECT_EQ(0, file_entry.parent_id()); |
| 107 | EXPECT_EQ("/", file_entry.file_name()); |
| 108 | EXPECT_EQ(0, file_entry.file_size()); |
| 109 | EXPECT_EQ(0, file_entry.modification_time()); |
| 110 | EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type()); |
| 111 | } |
| 112 | |
| 113 | // Devices do not actually have a root node, and it is not possible to read |
| 114 | // from the synthesized one. |
Anand K Mistry | 8baa56b | 2018-08-28 15:15:06 +1000 | [diff] [blame] | 115 | TEST_F(DeviceManagerTest, ReadFileFromSynthesizedRootNodeFails) { |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 116 | const std::string kDummyStorageName = "usb:1,2:65432"; |
| 117 | StorageInfo dummy_storage_info; |
| 118 | TestDeviceEventDelegate dummy_device_event_delegate; |
| 119 | TestDeviceManager device_manager(&dummy_device_event_delegate); |
| 120 | bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info); |
| 121 | EXPECT_TRUE(ret); |
| 122 | |
| 123 | std::vector<uint8_t> data; |
Lei Zhang | 692c4e3 | 2014-08-04 13:12:05 -0700 | [diff] [blame] | 124 | ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */, |
| 125 | 0 /* offset */, 1 /* byte */, &data); |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 126 | EXPECT_FALSE(ret); |
| 127 | EXPECT_TRUE(data.empty()); |
| 128 | } |
| 129 | |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 130 | } // namespace |
Ben Chan | 81757a0 | 2018-01-23 14:06:08 -0800 | [diff] [blame] | 131 | } // namespace mtpd |