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