blob: d056a2d7b94c34ad34a28fc883f8221d083d8f73 [file] [log] [blame]
Lei Zhang0d66f922012-08-07 15:22:05 -07001// 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
5#include "mtpd/device_manager.h"
6
7#include <string>
8#include <vector>
9
10#include <gtest/gtest.h>
11
Lei Zhang12f55c22012-09-05 14:54:48 -070012#include <base/compiler_specific.h>
13
14#include "mtpd/device_event_delegate.h"
15
Lei Zhang0d66f922012-08-07 15:22:05 -070016namespace mtpd {
17namespace {
18
Lei Zhangb22ea842012-08-22 15:47:19 -070019struct PathToFileIdHelperTestCase {
20 const char* filename;
21 bool is_file;
22};
23
24const PathToFileIdHelperTestCase kFolderPathTestCase[] = {
25 { "/", false },
26 { "valid", false },
27 { "path", false },
28};
29
30const PathToFileIdHelperTestCase kFolderPathWithExtraSlashesTestCase[] = {
31 { "/", false },
32 { "still", false },
33 { "valid", false },
34 { "/", false },
35 { "/", false },
36 { "path", false },
37};
38
39const PathToFileIdHelperTestCase kFilePathTestCase[] = {
40 { "/", false },
41 { "path", false },
42 { "to", false },
43 { "file", true },
44};
45
46const PathToFileIdHelperTestCase kFilePathWithExtraSlashesTestCase[] = {
47 { "/", false },
48 { "path", false },
49 { "/", false },
50 { "/", false },
51 { "to", false },
52 { "file2", true },
53};
54
55const PathToFileIdHelperTestCase kInvalidFilePathTestCase1[] = {
56 { "/", false },
57 { "invalid", false },
58 { "test", true },
59 { "path", false },
60};
61
62const PathToFileIdHelperTestCase kInvalidFilePathTestCase2[] = {
63 { "/", false },
64 { "also", false },
65 { "invalid", false },
66 { "test", true },
67 { "path", true },
68};
69
70bool TestPathToId(DeviceManager::ProcessPathComponentFunc test_func,
71 const PathToFileIdHelperTestCase* test_case,
72 size_t test_case_size) {
73 for (size_t i = 0; i < test_case_size; ++i) {
74 LIBMTP_file_t dummy_file;
75 dummy_file.filename = const_cast<char*>(test_case[i].filename);
76 dummy_file.filetype = test_case[i].is_file ? LIBMTP_FILETYPE_JPEG :
77 LIBMTP_FILETYPE_FOLDER;
78 uint32_t id;
79 if (!test_func(&dummy_file, i, test_case_size, &id))
80 return false;
81 }
82 return true;
83}
84
Lei Zhang0d66f922012-08-07 15:22:05 -070085TEST(DeviceManagerTest, ParseStorageName) {
86 struct ParseStorageNameTestCase {
87 const char* input;
88 bool expected_result;
89 const char* expected_bus;
90 uint32_t expected_storage_id;
91 } test_cases[] = {
92 { "usb:123:4", true, "usb:123", 4 },
93 { "usb:1,2,3:4", true, "usb:1,2,3", 4 },
94 { "notusb:123:4", false, "", 0 },
95 { "usb:123:4:badfield", false, "", 0 },
96 { "usb:123:not_number", false, "", 0 },
97 };
98
99 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
100 std::string bus;
101 uint32_t storage_id = static_cast<uint32_t>(-1);
102 bool result =
103 DeviceManager::ParseStorageName(test_cases[i].input, &bus, &storage_id);
104 EXPECT_EQ(test_cases[i].expected_result, result);
105 if (test_cases[i].expected_result) {
106 EXPECT_EQ(test_cases[i].expected_bus, bus);
107 EXPECT_EQ(test_cases[i].expected_storage_id, storage_id);
108 }
109 }
110}
111
Lei Zhang12f55c22012-09-05 14:54:48 -0700112class TestDeviceEventDelegate : public DeviceEventDelegate {
113 public:
114 TestDeviceEventDelegate() {}
115 ~TestDeviceEventDelegate() {}
116
117 // DeviceEventDelegate implementation.
118 virtual void StorageAttached(const std::string& storage_name) OVERRIDE {}
119 virtual void StorageDetached(const std::string& storage_name) OVERRIDE {}
120
121 private:
122 DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate);
123};
124
125class TestDeviceManager : public DeviceManager {
126 public:
127 explicit TestDeviceManager(DeviceEventDelegate* delegate)
128 : DeviceManager(delegate) {
129 }
130 ~TestDeviceManager() {}
131
132 bool AddStorage(const std::string& storage_name,
133 const StorageInfo& storage_info) {
134 return AddStorageForTest(storage_name, storage_info);
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(TestDeviceManager);
139};
140
Lei Zhangb22ea842012-08-22 15:47:19 -0700141TEST(DeviceManager, IsFolder) {
142 EXPECT_TRUE(TestPathToId(DeviceManager::IsFolder,
143 kFolderPathTestCase,
144 arraysize(kFolderPathTestCase)));
145 EXPECT_TRUE(TestPathToId(DeviceManager::IsFolder,
146 kFolderPathWithExtraSlashesTestCase,
147 arraysize(kFolderPathWithExtraSlashesTestCase)));
148 EXPECT_FALSE(TestPathToId(DeviceManager::IsFolder,
149 kFilePathTestCase,
150 arraysize(kFilePathTestCase)));
151 EXPECT_FALSE(TestPathToId(DeviceManager::IsFolder,
152 kFilePathWithExtraSlashesTestCase,
153 arraysize(kFilePathWithExtraSlashesTestCase)));
154 EXPECT_FALSE(TestPathToId(DeviceManager::IsFolder,
155 kInvalidFilePathTestCase1,
156 arraysize(kInvalidFilePathTestCase1)));
157 EXPECT_FALSE(TestPathToId(DeviceManager::IsFolder,
158 kInvalidFilePathTestCase2,
159 arraysize(kInvalidFilePathTestCase2)));
160}
161
162TEST(DeviceManager, IsValidComponentInFilePath) {
163 EXPECT_FALSE(TestPathToId(DeviceManager::IsValidComponentInFilePath,
164 kFolderPathTestCase,
165 arraysize(kFolderPathTestCase)));
166 EXPECT_FALSE(TestPathToId(DeviceManager::IsValidComponentInFilePath,
167 kFolderPathWithExtraSlashesTestCase,
168 arraysize(kFolderPathWithExtraSlashesTestCase)));
169 EXPECT_TRUE(TestPathToId(DeviceManager::IsValidComponentInFilePath,
170 kFilePathTestCase,
171 arraysize(kFilePathTestCase)));
172 EXPECT_TRUE(TestPathToId(DeviceManager::IsValidComponentInFilePath,
173 kFilePathWithExtraSlashesTestCase,
174 arraysize(kFilePathWithExtraSlashesTestCase)));
175 EXPECT_FALSE(TestPathToId(DeviceManager::IsValidComponentInFilePath,
176 kInvalidFilePathTestCase1,
177 arraysize(kInvalidFilePathTestCase1)));
178 EXPECT_FALSE(TestPathToId(DeviceManager::IsValidComponentInFilePath,
179 kInvalidFilePathTestCase2,
180 arraysize(kInvalidFilePathTestCase2)));
181}
182
183TEST(DeviceManager, IsValidComponentInFileOrFolderPath) {
184 EXPECT_TRUE(TestPathToId(DeviceManager::IsValidComponentInFileOrFolderPath,
185 kFolderPathTestCase,
186 arraysize(kFolderPathTestCase)));
187 EXPECT_TRUE(TestPathToId(DeviceManager::IsValidComponentInFileOrFolderPath,
188 kFolderPathWithExtraSlashesTestCase,
189 arraysize(kFolderPathWithExtraSlashesTestCase)));
190 EXPECT_TRUE(TestPathToId(DeviceManager::IsValidComponentInFileOrFolderPath,
191 kFilePathTestCase,
192 arraysize(kFilePathTestCase)));
193 EXPECT_TRUE(TestPathToId(DeviceManager::IsValidComponentInFileOrFolderPath,
194 kFilePathWithExtraSlashesTestCase,
195 arraysize(kFilePathWithExtraSlashesTestCase)));
196 EXPECT_FALSE(TestPathToId(DeviceManager::IsValidComponentInFileOrFolderPath,
197 kInvalidFilePathTestCase1,
198 arraysize(kInvalidFilePathTestCase1)));
199 EXPECT_FALSE(TestPathToId(DeviceManager::IsValidComponentInFileOrFolderPath,
200 kInvalidFilePathTestCase2,
201 arraysize(kInvalidFilePathTestCase2)));
202}
203
Lei Zhang12f55c22012-09-05 14:54:48 -0700204// Devices do not actually have a root node, so one is synthesized.
205TEST(DeviceManager, GetFileInfoForSynthesizedRootNode) {
206 const std::string kDummyStorageName = "usb:1,2:65432";
207 StorageInfo dummy_storage_info;
208 TestDeviceEventDelegate dummy_device_event_delegate;
209 TestDeviceManager device_manager(&dummy_device_event_delegate);
210 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
211 EXPECT_TRUE(ret);
212
213 FileEntry file_entry;
214 ret = device_manager.GetFileInfoById(kDummyStorageName, 0, &file_entry);
215 EXPECT_TRUE(ret);
216
217 EXPECT_EQ(0, file_entry.item_id());
218 EXPECT_EQ(0, file_entry.parent_id());
219 EXPECT_EQ("/", file_entry.file_name());
220 EXPECT_EQ(0, file_entry.file_size());
221 EXPECT_EQ(0, file_entry.modification_time());
222 EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type());
223}
224
225// Devices do not actually have a root node, and it is not possible to read
226// from the synthesized one.
227TEST(DeviceManager, ReadFileFromSynthesizedRootNodeFails) {
228 const std::string kDummyStorageName = "usb:1,2:65432";
229 StorageInfo dummy_storage_info;
230 TestDeviceEventDelegate dummy_device_event_delegate;
231 TestDeviceManager device_manager(&dummy_device_event_delegate);
232 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
233 EXPECT_TRUE(ret);
234
235 std::vector<uint8_t> data;
236 ret = device_manager.ReadFileById(kDummyStorageName, 0, &data);
237 EXPECT_FALSE(ret);
238 EXPECT_TRUE(data.empty());
239}
240
Lei Zhang0d66f922012-08-07 15:22:05 -0700241} // namespace
242} // namespace mtp