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