blob: d38fb80264360f371ce7adbc3718db3c09ba83c1 [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
Eric Caruso721d82e2017-08-21 15:57:30 -07005#include "mtpd/device_manager.h"
Lei Zhang0d66f922012-08-07 15:22:05 -07006
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
Eric Caruso721d82e2017-08-21 15:57:30 -070014#include "mtpd/device_event_delegate.h"
Lei Zhang12f55c22012-09-05 14:54:48 -070015
Lei Zhang0d66f922012-08-07 15:22:05 -070016namespace mtpd {
17namespace {
18
19TEST(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 Zhang1c82c532017-04-17 22:09:12 -070026 {"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 Zhang0d66f922012-08-07 15:22:05 -070031 };
32
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080033 for (size_t i = 0; i < arraysize(test_cases); ++i) {
Lei Zhang0d66f922012-08-07 15:22:05 -070034 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 Zhang12f55c22012-09-05 14:54:48 -070046class TestDeviceEventDelegate : public DeviceEventDelegate {
47 public:
48 TestDeviceEventDelegate() {}
49 ~TestDeviceEventDelegate() {}
50
51 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080052 void StorageAttached(const std::string& storage_name) override {}
53 void StorageDetached(const std::string& storage_name) override {}
Lei Zhang12f55c22012-09-05 14:54:48 -070054
55 private:
56 DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate);
57};
58
59class TestDeviceManager : public DeviceManager {
60 public:
61 explicit TestDeviceManager(DeviceEventDelegate* delegate)
Lei Zhang1c82c532017-04-17 22:09:12 -070062 : DeviceManager(delegate) {}
Lei Zhang12f55c22012-09-05 14:54:48 -070063 ~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 Zhang12f55c22012-09-05 14:54:48 -070074// Devices do not actually have a root node, so one is synthesized.
75TEST(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 Zhang692c4e32014-08-04 13:12:05 -070083 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 Zhang12f55c22012-09-05 14:54:48 -070087 EXPECT_TRUE(ret);
Lei Zhang692c4e32014-08-04 13:12:05 -070088 ASSERT_EQ(1U, file_entries.size());
89 const FileEntry& file_entry = file_entries[0];
Lei Zhang12f55c22012-09-05 14:54:48 -070090
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.
101TEST(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 Zhang692c4e32014-08-04 13:12:05 -0700110 ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */,
111 0 /* offset */, 1 /* byte */, &data);
Lei Zhang12f55c22012-09-05 14:54:48 -0700112 EXPECT_FALSE(ret);
113 EXPECT_TRUE(data.empty());
114}
115
Lei Zhang0d66f922012-08-07 15:22:05 -0700116} // namespace
Ben Chan81757a02018-01-23 14:06:08 -0800117} // namespace mtpd