blob: 2e668dc40b9940c0bbd48cf865d80e18bb9fabbc [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>
Qijiang Fane19d67d2020-04-01 08:18:39 +090013#include <base/stl_util.h>
hscham92fa34f2020-09-01 17:36:56 +090014#include <base/test/task_environment.h>
Lei Zhang12f55c22012-09-05 14:54:48 -070015
Eric Caruso721d82e2017-08-21 15:57:30 -070016#include "mtpd/device_event_delegate.h"
Lei Zhang12f55c22012-09-05 14:54:48 -070017
Lei Zhang0d66f922012-08-07 15:22:05 -070018namespace mtpd {
19namespace {
20
Anand K Mistry8baa56b2018-08-28 15:15:06 +100021class DeviceManagerTest : public testing::Test {
22 public:
23 DeviceManagerTest() = default;
24
25 private:
26 // DeviceManager needs the current thread to have a task runner.
hscham92fa34f2020-09-01 17:36:56 +090027 base::test::TaskEnvironment task_environment_{
28 base::test::TaskEnvironment::ThreadingMode::MAIN_THREAD_ONLY};
Anand K Mistry8baa56b2018-08-28 15:15:06 +100029
30 DISALLOW_COPY_AND_ASSIGN(DeviceManagerTest);
31};
32
33TEST_F(DeviceManagerTest, ParseStorageName) {
Lei Zhang0d66f922012-08-07 15:22:05 -070034 struct ParseStorageNameTestCase {
35 const char* input;
36 bool expected_result;
37 const char* expected_bus;
38 uint32_t expected_storage_id;
39 } test_cases[] = {
Lei Zhang1c82c532017-04-17 22:09:12 -070040 {"usb:123:4", true, "usb:123", 4},
41 {"usb:1,2,3:4", true, "usb:1,2,3", 4},
42 {"notusb:123:4", false, "", 0},
43 {"usb:123:4:badfield", false, "", 0},
44 {"usb:123:not_number", false, "", 0},
Lei Zhang0d66f922012-08-07 15:22:05 -070045 };
46
Qijiang Fane19d67d2020-04-01 08:18:39 +090047 for (size_t i = 0; i < base::size(test_cases); ++i) {
Lei Zhang0d66f922012-08-07 15:22:05 -070048 std::string bus;
49 uint32_t storage_id = static_cast<uint32_t>(-1);
50 bool result =
51 DeviceManager::ParseStorageName(test_cases[i].input, &bus, &storage_id);
52 EXPECT_EQ(test_cases[i].expected_result, result);
53 if (test_cases[i].expected_result) {
54 EXPECT_EQ(test_cases[i].expected_bus, bus);
55 EXPECT_EQ(test_cases[i].expected_storage_id, storage_id);
56 }
57 }
58}
59
Lei Zhang12f55c22012-09-05 14:54:48 -070060class TestDeviceEventDelegate : public DeviceEventDelegate {
61 public:
62 TestDeviceEventDelegate() {}
63 ~TestDeviceEventDelegate() {}
64
65 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080066 void StorageAttached(const std::string& storage_name) override {}
67 void StorageDetached(const std::string& storage_name) override {}
Lei Zhang12f55c22012-09-05 14:54:48 -070068
69 private:
70 DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate);
71};
72
73class TestDeviceManager : public DeviceManager {
74 public:
75 explicit TestDeviceManager(DeviceEventDelegate* delegate)
Lei Zhang1c82c532017-04-17 22:09:12 -070076 : DeviceManager(delegate) {}
Lei Zhang12f55c22012-09-05 14:54:48 -070077 ~TestDeviceManager() {}
78
79 bool AddStorage(const std::string& storage_name,
80 const StorageInfo& storage_info) {
81 return AddStorageForTest(storage_name, storage_info);
82 }
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(TestDeviceManager);
86};
87
Lei Zhang12f55c22012-09-05 14:54:48 -070088// Devices do not actually have a root node, so one is synthesized.
Anand K Mistry8baa56b2018-08-28 15:15:06 +100089TEST_F(DeviceManagerTest, GetFileInfoForSynthesizedRootNode) {
Lei Zhang12f55c22012-09-05 14:54:48 -070090 const std::string kDummyStorageName = "usb:1,2:65432";
91 StorageInfo dummy_storage_info;
92 TestDeviceEventDelegate dummy_device_event_delegate;
93 TestDeviceManager device_manager(&dummy_device_event_delegate);
94 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
95 EXPECT_TRUE(ret);
96
Lei Zhang692c4e32014-08-04 13:12:05 -070097 std::vector<FileEntry> file_entries;
98 std::vector<uint32_t> file_ids;
99 file_ids.push_back(0);
100 ret = device_manager.GetFileInfo(kDummyStorageName, file_ids, &file_entries);
Lei Zhang12f55c22012-09-05 14:54:48 -0700101 EXPECT_TRUE(ret);
Lei Zhang692c4e32014-08-04 13:12:05 -0700102 ASSERT_EQ(1U, file_entries.size());
103 const FileEntry& file_entry = file_entries[0];
Lei Zhang12f55c22012-09-05 14:54:48 -0700104
105 EXPECT_EQ(0, file_entry.item_id());
106 EXPECT_EQ(0, file_entry.parent_id());
107 EXPECT_EQ("/", file_entry.file_name());
108 EXPECT_EQ(0, file_entry.file_size());
109 EXPECT_EQ(0, file_entry.modification_time());
110 EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type());
111}
112
113// Devices do not actually have a root node, and it is not possible to read
114// from the synthesized one.
Anand K Mistry8baa56b2018-08-28 15:15:06 +1000115TEST_F(DeviceManagerTest, ReadFileFromSynthesizedRootNodeFails) {
Lei Zhang12f55c22012-09-05 14:54:48 -0700116 const std::string kDummyStorageName = "usb:1,2:65432";
117 StorageInfo dummy_storage_info;
118 TestDeviceEventDelegate dummy_device_event_delegate;
119 TestDeviceManager device_manager(&dummy_device_event_delegate);
120 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
121 EXPECT_TRUE(ret);
122
123 std::vector<uint8_t> data;
Lei Zhang692c4e32014-08-04 13:12:05 -0700124 ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */,
125 0 /* offset */, 1 /* byte */, &data);
Lei Zhang12f55c22012-09-05 14:54:48 -0700126 EXPECT_FALSE(ret);
127 EXPECT_TRUE(data.empty());
128}
129
Lei Zhang0d66f922012-08-07 15:22:05 -0700130} // namespace
Ben Chan81757a02018-01-23 14:06:08 -0800131} // namespace mtpd