blob: c8a40e4611f89a6050278495a3b868691b471d04 [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>
Anand K Mistry8baa56b2018-08-28 15:15:06 +100013#include <base/message_loop/message_loop.h>
Qijiang Fane19d67d2020-04-01 08:18:39 +090014#include <base/stl_util.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.
27 base::MessageLoop message_loop_;
28
29 DISALLOW_COPY_AND_ASSIGN(DeviceManagerTest);
30};
31
32TEST_F(DeviceManagerTest, ParseStorageName) {
Lei Zhang0d66f922012-08-07 15:22:05 -070033 struct ParseStorageNameTestCase {
34 const char* input;
35 bool expected_result;
36 const char* expected_bus;
37 uint32_t expected_storage_id;
38 } test_cases[] = {
Lei Zhang1c82c532017-04-17 22:09:12 -070039 {"usb:123:4", true, "usb:123", 4},
40 {"usb:1,2,3:4", true, "usb:1,2,3", 4},
41 {"notusb:123:4", false, "", 0},
42 {"usb:123:4:badfield", false, "", 0},
43 {"usb:123:not_number", false, "", 0},
Lei Zhang0d66f922012-08-07 15:22:05 -070044 };
45
Qijiang Fane19d67d2020-04-01 08:18:39 +090046 for (size_t i = 0; i < base::size(test_cases); ++i) {
Lei Zhang0d66f922012-08-07 15:22:05 -070047 std::string bus;
48 uint32_t storage_id = static_cast<uint32_t>(-1);
49 bool result =
50 DeviceManager::ParseStorageName(test_cases[i].input, &bus, &storage_id);
51 EXPECT_EQ(test_cases[i].expected_result, result);
52 if (test_cases[i].expected_result) {
53 EXPECT_EQ(test_cases[i].expected_bus, bus);
54 EXPECT_EQ(test_cases[i].expected_storage_id, storage_id);
55 }
56 }
57}
58
Lei Zhang12f55c22012-09-05 14:54:48 -070059class TestDeviceEventDelegate : public DeviceEventDelegate {
60 public:
61 TestDeviceEventDelegate() {}
62 ~TestDeviceEventDelegate() {}
63
64 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080065 void StorageAttached(const std::string& storage_name) override {}
66 void StorageDetached(const std::string& storage_name) override {}
Lei Zhang12f55c22012-09-05 14:54:48 -070067
68 private:
69 DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate);
70};
71
72class TestDeviceManager : public DeviceManager {
73 public:
74 explicit TestDeviceManager(DeviceEventDelegate* delegate)
Lei Zhang1c82c532017-04-17 22:09:12 -070075 : DeviceManager(delegate) {}
Lei Zhang12f55c22012-09-05 14:54:48 -070076 ~TestDeviceManager() {}
77
78 bool AddStorage(const std::string& storage_name,
79 const StorageInfo& storage_info) {
80 return AddStorageForTest(storage_name, storage_info);
81 }
82
83 private:
84 DISALLOW_COPY_AND_ASSIGN(TestDeviceManager);
85};
86
Lei Zhang12f55c22012-09-05 14:54:48 -070087// Devices do not actually have a root node, so one is synthesized.
Anand K Mistry8baa56b2018-08-28 15:15:06 +100088TEST_F(DeviceManagerTest, GetFileInfoForSynthesizedRootNode) {
Lei Zhang12f55c22012-09-05 14:54:48 -070089 const std::string kDummyStorageName = "usb:1,2:65432";
90 StorageInfo dummy_storage_info;
91 TestDeviceEventDelegate dummy_device_event_delegate;
92 TestDeviceManager device_manager(&dummy_device_event_delegate);
93 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
94 EXPECT_TRUE(ret);
95
Lei Zhang692c4e32014-08-04 13:12:05 -070096 std::vector<FileEntry> file_entries;
97 std::vector<uint32_t> file_ids;
98 file_ids.push_back(0);
99 ret = device_manager.GetFileInfo(kDummyStorageName, file_ids, &file_entries);
Lei Zhang12f55c22012-09-05 14:54:48 -0700100 EXPECT_TRUE(ret);
Lei Zhang692c4e32014-08-04 13:12:05 -0700101 ASSERT_EQ(1U, file_entries.size());
102 const FileEntry& file_entry = file_entries[0];
Lei Zhang12f55c22012-09-05 14:54:48 -0700103
104 EXPECT_EQ(0, file_entry.item_id());
105 EXPECT_EQ(0, file_entry.parent_id());
106 EXPECT_EQ("/", file_entry.file_name());
107 EXPECT_EQ(0, file_entry.file_size());
108 EXPECT_EQ(0, file_entry.modification_time());
109 EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type());
110}
111
112// Devices do not actually have a root node, and it is not possible to read
113// from the synthesized one.
Anand K Mistry8baa56b2018-08-28 15:15:06 +1000114TEST_F(DeviceManagerTest, ReadFileFromSynthesizedRootNodeFails) {
Lei Zhang12f55c22012-09-05 14:54:48 -0700115 const std::string kDummyStorageName = "usb:1,2:65432";
116 StorageInfo dummy_storage_info;
117 TestDeviceEventDelegate dummy_device_event_delegate;
118 TestDeviceManager device_manager(&dummy_device_event_delegate);
119 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
120 EXPECT_TRUE(ret);
121
122 std::vector<uint8_t> data;
Lei Zhang692c4e32014-08-04 13:12:05 -0700123 ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */,
124 0 /* offset */, 1 /* byte */, &data);
Lei Zhang12f55c22012-09-05 14:54:48 -0700125 EXPECT_FALSE(ret);
126 EXPECT_TRUE(data.empty());
127}
128
Lei Zhang0d66f922012-08-07 15:22:05 -0700129} // namespace
Ben Chan81757a02018-01-23 14:06:08 -0800130} // namespace mtpd