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 | |
Lei Zhang | e7e0ea5 | 2012-10-03 19:27:44 -0700 | [diff] [blame] | 5 | #include "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 | |
Lei Zhang | e7e0ea5 | 2012-10-03 19:27:44 -0700 | [diff] [blame] | 14 | #include "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[] = { |
| 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 }, |
| 31 | }; |
| 32 | |
| 33 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 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. |
| 52 | virtual void StorageAttached(const std::string& storage_name) OVERRIDE {} |
| 53 | virtual void StorageDetached(const std::string& storage_name) OVERRIDE {} |
| 54 | |
| 55 | private: |
| 56 | DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate); |
| 57 | }; |
| 58 | |
| 59 | class TestDeviceManager : public DeviceManager { |
| 60 | public: |
| 61 | explicit TestDeviceManager(DeviceEventDelegate* delegate) |
| 62 | : DeviceManager(delegate) { |
| 63 | } |
| 64 | ~TestDeviceManager() {} |
| 65 | |
| 66 | bool AddStorage(const std::string& storage_name, |
| 67 | const StorageInfo& storage_info) { |
| 68 | return AddStorageForTest(storage_name, storage_info); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | DISALLOW_COPY_AND_ASSIGN(TestDeviceManager); |
| 73 | }; |
| 74 | |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 75 | // Devices do not actually have a root node, so one is synthesized. |
| 76 | TEST(DeviceManager, GetFileInfoForSynthesizedRootNode) { |
| 77 | const std::string kDummyStorageName = "usb:1,2:65432"; |
| 78 | StorageInfo dummy_storage_info; |
| 79 | TestDeviceEventDelegate dummy_device_event_delegate; |
| 80 | TestDeviceManager device_manager(&dummy_device_event_delegate); |
| 81 | bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info); |
| 82 | EXPECT_TRUE(ret); |
| 83 | |
Lei Zhang | 692c4e3 | 2014-08-04 13:12:05 -0700 | [diff] [blame^] | 84 | std::vector<FileEntry> file_entries; |
| 85 | std::vector<uint32_t> file_ids; |
| 86 | file_ids.push_back(0); |
| 87 | ret = device_manager.GetFileInfo(kDummyStorageName, file_ids, &file_entries); |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 88 | EXPECT_TRUE(ret); |
Lei Zhang | 692c4e3 | 2014-08-04 13:12:05 -0700 | [diff] [blame^] | 89 | ASSERT_EQ(1U, file_entries.size()); |
| 90 | const FileEntry& file_entry = file_entries[0]; |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 91 | |
| 92 | EXPECT_EQ(0, file_entry.item_id()); |
| 93 | EXPECT_EQ(0, file_entry.parent_id()); |
| 94 | EXPECT_EQ("/", file_entry.file_name()); |
| 95 | EXPECT_EQ(0, file_entry.file_size()); |
| 96 | EXPECT_EQ(0, file_entry.modification_time()); |
| 97 | EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type()); |
| 98 | } |
| 99 | |
| 100 | // Devices do not actually have a root node, and it is not possible to read |
| 101 | // from the synthesized one. |
| 102 | TEST(DeviceManager, ReadFileFromSynthesizedRootNodeFails) { |
| 103 | const std::string kDummyStorageName = "usb:1,2:65432"; |
| 104 | StorageInfo dummy_storage_info; |
| 105 | TestDeviceEventDelegate dummy_device_event_delegate; |
| 106 | TestDeviceManager device_manager(&dummy_device_event_delegate); |
| 107 | bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info); |
| 108 | EXPECT_TRUE(ret); |
| 109 | |
| 110 | std::vector<uint8_t> data; |
Lei Zhang | 692c4e3 | 2014-08-04 13:12:05 -0700 | [diff] [blame^] | 111 | ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */, |
| 112 | 0 /* offset */, 1 /* byte */, &data); |
Lei Zhang | 12f55c2 | 2012-09-05 14:54:48 -0700 | [diff] [blame] | 113 | EXPECT_FALSE(ret); |
| 114 | EXPECT_TRUE(data.empty()); |
| 115 | } |
| 116 | |
Lei Zhang | 0d66f92 | 2012-08-07 15:22:05 -0700 | [diff] [blame] | 117 | } // namespace |
| 118 | } // namespace mtp |