blob: c2a8a995dcd43dc07f7ea58e2a30a4592e34aab6 [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
Lei Zhange7e0ea52012-10-03 19:27:44 -07005#include "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
Lei Zhange7e0ea52012-10-03 19:27:44 -070014#include "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[] = {
26 { "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 },
31 };
32
33 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
34 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.
52 virtual void StorageAttached(const std::string& storage_name) OVERRIDE {}
53 virtual void StorageDetached(const std::string& storage_name) OVERRIDE {}
54
55 private:
56 DISALLOW_COPY_AND_ASSIGN(TestDeviceEventDelegate);
57};
58
59class TestDeviceManager : public DeviceManager {
60 public:
61 explicit TestDeviceManager(DeviceEventDelegate* delegate)
62 : DeviceManager(delegate) {
63 }
64 ~TestDeviceManager() {}
65
66 bool AddStorage(const std::string& storage_name,
67 const StorageInfo& storage_info) {
68 return AddStorageForTest(storage_name, storage_info);
69 }
70
71 private:
72 DISALLOW_COPY_AND_ASSIGN(TestDeviceManager);
73};
74
Lei Zhang12f55c22012-09-05 14:54:48 -070075// Devices do not actually have a root node, so one is synthesized.
76TEST(DeviceManager, GetFileInfoForSynthesizedRootNode) {
77 const std::string kDummyStorageName = "usb:1,2:65432";
78 StorageInfo dummy_storage_info;
79 TestDeviceEventDelegate dummy_device_event_delegate;
80 TestDeviceManager device_manager(&dummy_device_event_delegate);
81 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
82 EXPECT_TRUE(ret);
83
Lei Zhang692c4e32014-08-04 13:12:05 -070084 std::vector<FileEntry> file_entries;
85 std::vector<uint32_t> file_ids;
86 file_ids.push_back(0);
87 ret = device_manager.GetFileInfo(kDummyStorageName, file_ids, &file_entries);
Lei Zhang12f55c22012-09-05 14:54:48 -070088 EXPECT_TRUE(ret);
Lei Zhang692c4e32014-08-04 13:12:05 -070089 ASSERT_EQ(1U, file_entries.size());
90 const FileEntry& file_entry = file_entries[0];
Lei Zhang12f55c22012-09-05 14:54:48 -070091
92 EXPECT_EQ(0, file_entry.item_id());
93 EXPECT_EQ(0, file_entry.parent_id());
94 EXPECT_EQ("/", file_entry.file_name());
95 EXPECT_EQ(0, file_entry.file_size());
96 EXPECT_EQ(0, file_entry.modification_time());
97 EXPECT_EQ(LIBMTP_FILETYPE_FOLDER, file_entry.file_type());
98}
99
100// Devices do not actually have a root node, and it is not possible to read
101// from the synthesized one.
102TEST(DeviceManager, ReadFileFromSynthesizedRootNodeFails) {
103 const std::string kDummyStorageName = "usb:1,2:65432";
104 StorageInfo dummy_storage_info;
105 TestDeviceEventDelegate dummy_device_event_delegate;
106 TestDeviceManager device_manager(&dummy_device_event_delegate);
107 bool ret = device_manager.AddStorage(kDummyStorageName, dummy_storage_info);
108 EXPECT_TRUE(ret);
109
110 std::vector<uint8_t> data;
Lei Zhang692c4e32014-08-04 13:12:05 -0700111 ret = device_manager.ReadFileChunk(kDummyStorageName, 0 /* node id */,
112 0 /* offset */, 1 /* byte */, &data);
Lei Zhang12f55c22012-09-05 14:54:48 -0700113 EXPECT_FALSE(ret);
114 EXPECT_TRUE(data.empty());
115}
116
Lei Zhang0d66f922012-08-07 15:22:05 -0700117} // namespace
118} // namespace mtp