blob: e472ef79fa494337b31b64b107a87f3e465bc9d9 [file] [log] [blame]
Ben Chan120c11c2012-09-10 23:10:18 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ben Chan0a389a62011-10-03 15:02:34 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan5ccd9fe2013-11-13 18:28:27 -08005#include "cros-disks/usb_device_info.h"
Ben Chan0a389a62011-10-03 15:02:34 -07006
Ben Chancd8fda42014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Ben Chan0a389a62011-10-03 15:02:34 -07008#include <gtest/gtest.h>
9
Ben Chan0a389a62011-10-03 15:02:34 -070010namespace cros_disks {
11
12class USBDeviceInfoTest : public ::testing::Test {
13 public:
Ben Chan1c6c1942014-08-12 09:48:29 -070014 void SetUp() override {
Ben Chan120c11c2012-09-10 23:10:18 -070015 info_file_ = CreateTestDataFile(
16 "# This is a comment line\n"
17 " \n"
18 "\n"
19 "18d1:4e11 mobile\n"
20 "0bda:0138 sd\n");
21 ASSERT_FALSE(info_file_.empty());
Ben Chan0a389a62011-10-03 15:02:34 -070022
Ben Chan120c11c2012-09-10 23:10:18 -070023 ids_file_ = CreateTestDataFile(
24 "#\n"
25 "#\tList of USB ID's\n"
26 "#\n"
27 "\n"
28 "# Syntax:\n"
29 "# vendor vendor_name\n"
30 "\tdevice device_name\n"
31 "#\t\tinterface interface_name\n"
32 " \n"
33 "0123 Vendor A\n"
34 "\tab01 Product 1\n"
35 "\tab02 Product 2\n"
36 "\tab03 Product 3\n"
37 " \n"
38 "5678 Vendor with no product IDs\n"
39 "abcd Vendor B\n"
40 "\t0004 Product X\n"
41 "\t\n"
42 " \n"
43 "# comment\n"
44 "\t0005 Product Y\n"
45 "\t0006 Product Z\n"
46 "\n"
47 "C 00 Class 0\n");
48 ASSERT_FALSE(ids_file_.empty());
Ben Chan0a389a62011-10-03 15:02:34 -070049 }
50
Ben Chan1c6c1942014-08-12 09:48:29 -070051 void TearDown() override {
hscham53cf73a2020-11-30 15:58:42 +090052 ASSERT_TRUE(base::DeleteFile(base::FilePath(info_file_)));
53 ASSERT_TRUE(base::DeleteFile(base::FilePath(ids_file_)));
Ben Chan0a389a62011-10-03 15:02:34 -070054 }
55
56 protected:
Ben Chan213c6d92019-04-10 16:21:52 -070057 std::string CreateTestDataFile(const std::string& content) const {
58 base::FilePath temp_file;
Ben Chan97e20d42014-02-05 18:38:07 -080059 if (base::CreateTemporaryFile(&temp_file) &&
Ben Chanaf25ddb2014-05-21 18:32:47 -070060 (static_cast<size_t>(base::WriteFile(
61 temp_file, content.c_str(), content.size())) == content.size())) {
Ben Chan120c11c2012-09-10 23:10:18 -070062 return temp_file.value();
63 }
Ben Chan213c6d92019-04-10 16:21:52 -070064 return std::string();
Ben Chan120c11c2012-09-10 23:10:18 -070065 }
66
Ben Chan213c6d92019-04-10 16:21:52 -070067 std::string info_file_;
68 std::string ids_file_;
Ben Chan0a389a62011-10-03 15:02:34 -070069 USBDeviceInfo info_;
70};
71
72TEST_F(USBDeviceInfoTest, GetDeviceMediaType) {
Ben Chan6d0b2722011-11-18 08:24:14 -080073 EXPECT_EQ(DEVICE_MEDIA_USB, info_.GetDeviceMediaType("0bda", "0138"));
Ben Chan0a389a62011-10-03 15:02:34 -070074
75 EXPECT_TRUE(info_.RetrieveFromFile(info_file_));
Ben Chan6d0b2722011-11-18 08:24:14 -080076 EXPECT_EQ(DEVICE_MEDIA_MOBILE, info_.GetDeviceMediaType("18d1", "4e11"));
77 EXPECT_EQ(DEVICE_MEDIA_SD, info_.GetDeviceMediaType("0bda", "0138"));
78 EXPECT_EQ(DEVICE_MEDIA_USB, info_.GetDeviceMediaType("1234", "5678"));
Ben Chan0a389a62011-10-03 15:02:34 -070079}
80
81TEST_F(USBDeviceInfoTest, RetrieveFromFile) {
82 EXPECT_TRUE(info_.RetrieveFromFile(info_file_));
83}
84
Ben Chan120c11c2012-09-10 23:10:18 -070085TEST_F(USBDeviceInfoTest, GetVendorAndProductName) {
Ben Chan213c6d92019-04-10 16:21:52 -070086 std::string vendor_name, product_name;
Ben Chan120c11c2012-09-10 23:10:18 -070087
Ben Chande0e3f62017-09-26 06:28:39 -070088 EXPECT_FALSE(info_.GetVendorAndProductName("nonexistent-path", "0123", "ab01",
89 &vendor_name, &product_name));
90 EXPECT_FALSE(info_.GetVendorAndProductName(ids_file_, "1234", "ab01",
91 &vendor_name, &product_name));
Ben Chan120c11c2012-09-10 23:10:18 -070092
Ben Chande0e3f62017-09-26 06:28:39 -070093 EXPECT_TRUE(info_.GetVendorAndProductName(ids_file_, "0123", "0000",
94 &vendor_name, &product_name));
Ben Chan120c11c2012-09-10 23:10:18 -070095 EXPECT_EQ("Vendor A", vendor_name);
96 EXPECT_EQ("", product_name);
97
Ben Chande0e3f62017-09-26 06:28:39 -070098 EXPECT_TRUE(info_.GetVendorAndProductName(ids_file_, "0123", "ab03",
99 &vendor_name, &product_name));
Ben Chan120c11c2012-09-10 23:10:18 -0700100 EXPECT_EQ("Vendor A", vendor_name);
101 EXPECT_EQ("Product 3", product_name);
102
Ben Chande0e3f62017-09-26 06:28:39 -0700103 EXPECT_TRUE(info_.GetVendorAndProductName(ids_file_, "5678", "0005",
104 &vendor_name, &product_name));
Ben Chan120c11c2012-09-10 23:10:18 -0700105 EXPECT_EQ("Vendor with no product IDs", vendor_name);
106 EXPECT_EQ("", product_name);
107
Ben Chande0e3f62017-09-26 06:28:39 -0700108 EXPECT_TRUE(info_.GetVendorAndProductName(ids_file_, "abcd", "0005",
109 &vendor_name, &product_name));
Ben Chan120c11c2012-09-10 23:10:18 -0700110 EXPECT_EQ("Vendor B", vendor_name);
111 EXPECT_EQ("Product Y", product_name);
112}
113
Ben Chan0a389a62011-10-03 15:02:34 -0700114TEST_F(USBDeviceInfoTest, ConvertToDeviceMediaType) {
Ben Chan6d0b2722011-11-18 08:24:14 -0800115 EXPECT_EQ(DEVICE_MEDIA_MOBILE, info_.ConvertToDeviceMediaType("mobile"));
116 EXPECT_EQ(DEVICE_MEDIA_SD, info_.ConvertToDeviceMediaType("sd"));
117 EXPECT_EQ(DEVICE_MEDIA_USB, info_.ConvertToDeviceMediaType("usb"));
118 EXPECT_EQ(DEVICE_MEDIA_USB, info_.ConvertToDeviceMediaType(""));
119 EXPECT_EQ(DEVICE_MEDIA_USB, info_.ConvertToDeviceMediaType("foo"));
Ben Chan0a389a62011-10-03 15:02:34 -0700120}
121
Ben Chan120c11c2012-09-10 23:10:18 -0700122TEST_F(USBDeviceInfoTest, ExtractIdAndName) {
Ben Chan213c6d92019-04-10 16:21:52 -0700123 std::string id, name;
Ben Chan120c11c2012-09-10 23:10:18 -0700124 EXPECT_FALSE(info_.ExtractIdAndName("", &id, &name));
125 EXPECT_FALSE(info_.ExtractIdAndName("0123 ", &id, &name));
126 EXPECT_FALSE(info_.ExtractIdAndName("012 test device", &id, &name));
127 EXPECT_FALSE(info_.ExtractIdAndName("0123 test device", &id, &name));
128 EXPECT_FALSE(info_.ExtractIdAndName("x123 test device", &id, &name));
129 EXPECT_FALSE(info_.ExtractIdAndName("0x23 test device", &id, &name));
130 EXPECT_FALSE(info_.ExtractIdAndName("01x3 test device", &id, &name));
131 EXPECT_FALSE(info_.ExtractIdAndName("012x test device", &id, &name));
132 EXPECT_FALSE(info_.ExtractIdAndName("01234 test device", &id, &name));
133
134 EXPECT_TRUE(info_.ExtractIdAndName("0123 test device", &id, &name));
135 EXPECT_EQ("0123", id);
136 EXPECT_EQ("test device", name);
137
138 EXPECT_TRUE(info_.ExtractIdAndName("ABCD T", &id, &name));
139 EXPECT_EQ("abcd", id);
140 EXPECT_EQ("T", name);
141}
142
143TEST_F(USBDeviceInfoTest, IsLineSkippable) {
144 EXPECT_TRUE(info_.IsLineSkippable(""));
145 EXPECT_TRUE(info_.IsLineSkippable(" "));
146 EXPECT_TRUE(info_.IsLineSkippable("\t"));
147 EXPECT_TRUE(info_.IsLineSkippable("#"));
148 EXPECT_TRUE(info_.IsLineSkippable("# this is a comment"));
149 EXPECT_TRUE(info_.IsLineSkippable(" # this is a comment"));
150 EXPECT_TRUE(info_.IsLineSkippable("# this is a comment "));
151 EXPECT_TRUE(info_.IsLineSkippable("\t#this is a comment"));
152 EXPECT_FALSE(info_.IsLineSkippable("this is not a comment"));
153}
154
Ben Chan0a389a62011-10-03 15:02:34 -0700155} // namespace cros_disks