blob: f15aae146d2efd3c3dee43864773f8e5f06fd1d2 [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>
Lei Zhang12f55c22012-09-05 14:54:48 -070014
Eric Caruso721d82e2017-08-21 15:57:30 -070015#include "mtpd/device_event_delegate.h"
Lei Zhang12f55c22012-09-05 14:54:48 -070016
Lei Zhang0d66f922012-08-07 15:22:05 -070017namespace mtpd {
18namespace {
19
Anand K Mistry8baa56b2018-08-28 15:15:06 +100020class DeviceManagerTest : public testing::Test {
21 public:
22 DeviceManagerTest() = default;
23
24 private:
25 // DeviceManager needs the current thread to have a task runner.
26 base::MessageLoop message_loop_;
27
28 DISALLOW_COPY_AND_ASSIGN(DeviceManagerTest);
29};
30
31TEST_F(DeviceManagerTest, ParseStorageName) {
Lei Zhang0d66f922012-08-07 15:22:05 -070032 struct ParseStorageNameTestCase {
33 const char* input;
34 bool expected_result;
35 const char* expected_bus;
36 uint32_t expected_storage_id;
37 } test_cases[] = {
Lei Zhang1c82c532017-04-17 22:09:12 -070038 {"usb:123:4", true, "usb:123", 4},
39 {"usb:1,2,3:4", true, "usb:1,2,3", 4},
40 {"notusb:123:4", false, "", 0},
41 {"usb:123:4:badfield", false, "", 0},
42 {"usb:123:not_number", false, "", 0},
Lei Zhang0d66f922012-08-07 15:22:05 -070043 };
44
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080045 for (size_t i = 0; i < arraysize(test_cases); ++i) {
Lei Zhang0d66f922012-08-07 15:22:05 -070046 std::string bus;
47 uint32_t storage_id = static_cast<uint32_t>(-1);
48 bool result =
49 DeviceManager::ParseStorageName(test_cases[i].input, &bus, &storage_id);
50 EXPECT_EQ(test_cases[i].expected_result, result);
51 if (test_cases[i].expected_result) {
52 EXPECT_EQ(test_cases[i].expected_bus, bus);
53 EXPECT_EQ(test_cases[i].expected_storage_id, storage_id);
54 }
55 }
56}
57
Lei Zhang12f55c22012-09-05 14:54:48 -070058class TestDeviceEventDelegate : public DeviceEventDelegate {
59 public:
60 TestDeviceEventDelegate() {}
61 ~TestDeviceEventDelegate() {}
62
63 // DeviceEventDelegate implementation.
Alex Vakulenko3a09a2b2014-12-11 07:53:44 -080064 void StorageAttached(const std::string& storage_name) override {}
65 void StorageDetached(const std::string& storage_name) override {}
Lei Zhang12f55c22012-09-05 14:54:48 -070066
67 private:
68 DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate);
69};
70
71class TestDeviceManager : public DeviceManager {
72 public:
73 explicit TestDeviceManager(DeviceEventDelegate* delegate)
Lei Zhang1c82c532017-04-17 22:09:12 -070074 : DeviceManager(delegate) {}
Lei Zhang12f55c22012-09-05 14:54:48 -070075 ~TestDeviceManager() {}
76
77 bool AddStorage(const std::string& storage_name,
78 const StorageInfo& storage_info) {
79 return AddStorageForTest(storage_name, storage_info);
80 }
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(TestDeviceManager);
84};
85
Lei Zhang12f55c22012-09-05 14:54:48 -070086// Devices do not actually have a root node, so one is synthesized.
Anand K Mistry8baa56b2018-08-28 15:15:06 +100087TEST_F(DeviceManagerTest, GetFileInfoForSynthesizedRootNode) {
Lei Zhang12f55c22012-09-05 14:54:48 -070088 const std::string kDummyStorageName = "usb:1,2:65432";
89 StorageInfo dummy_storage_info;
90 TestDeviceEventDelegate dummy_device_event_delegate;
91 TestDeviceManager device_manager(&dummy_device_event_delegate);
92 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
93 EXPECT_TRUE(ret);
94
Lei Zhang692c4e32014-08-04 13:12:05 -070095 std::vector<FileEntry> file_entries;
96 std::vector<uint32_t> file_ids;
97 file_ids.push_back(0);
98 ret = device_manager.GetFileInfo(kDummyStorageName, file_ids, &file_entries);
Lei Zhang12f55c22012-09-05 14:54:48 -070099 EXPECT_TRUE(ret);
Lei Zhang692c4e32014-08-04 13:12:05 -0700100 ASSERT_EQ(1U, file_entries.size());
101 const FileEntry& file_entry = file_entries[0];
Lei Zhang12f55c22012-09-05 14:54:48 -0700102
103 EXPECT_EQ(0, file_entry.item_id());
104 EXPECT_EQ(0, file_entry.parent_id());
105 EXPECT_EQ("/", file_entry.file_name());
106 EXPECT_EQ(0, file_entry.file_size());
107 EXPECT_EQ(0, file_entry.modification_time());
108 EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type());
109}
110
111// Devices do not actually have a root node, and it is not possible to read
112// from the synthesized one.
Anand K Mistry8baa56b2018-08-28 15:15:06 +1000113TEST_F(DeviceManagerTest, ReadFileFromSynthesizedRootNodeFails) {
Lei Zhang12f55c22012-09-05 14:54:48 -0700114 const std::string kDummyStorageName = "usb:1,2:65432";
115 StorageInfo dummy_storage_info;
116 TestDeviceEventDelegate dummy_device_event_delegate;
117 TestDeviceManager device_manager(&dummy_device_event_delegate);
118 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
119 EXPECT_TRUE(ret);
120
121 std::vector<uint8_t> data;
Lei Zhang692c4e32014-08-04 13:12:05 -0700122 ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */,
123 0 /* offset */, 1 /* byte */, &data);
Lei Zhang12f55c22012-09-05 14:54:48 -0700124 EXPECT_FALSE(ret);
125 EXPECT_TRUE(data.empty());
126}
127
Lei Zhang0d66f922012-08-07 15:22:05 -0700128} // namespace
Ben Chan81757a02018-01-23 14:06:08 -0800129} // namespace mtpd