1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "services/device/serial/serial_device_enumerator_linux.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/test/task_environment.h"
11 #include "device/udev_linux/fake_udev_loader.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 constexpr char kSerialDriverInfo[] =
17 R"(/dev/tty /dev/tty 5 0 system:/dev/tty
18 /dev/console /dev/console 5 1 system:console
19 /dev/ptmx /dev/ptmx 5 2 system
20 /dev/vc/0 /dev/vc/0 4 0 system:vtmaster
21 rfcomm /dev/rfcomm 216 0-255 serial
22 acm /dev/ttyACM 166 0-255 serial
23 ttyAMA /dev/ttyAMA 204 64-77 serial
24 ttyprintk /dev/ttyprintk 5 3 console
25 max310x /dev/ttyMAX 204 209-224 serial
26 serial /dev/ttyS 4 64 serial
27 pty_slave /dev/pts 136 0-1048575 pty:slave
28 pty_master /dev/ptm 128 0-1048575 pty:master
29 pty_slave /dev/ttyp 3 0-255 pty:slave
30 pty_master /dev/pty 2 0-255 pty:master
31 unknown /dev/tty 4 1-63 console)";
33 class SerialDeviceEnumeratorLinuxTest : public testing::Test {
35 void SetUp() override {
36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
37 drivers_file_ = temp_dir_.GetPath().Append("drivers");
38 ASSERT_TRUE(base::WriteFile(drivers_file_, kSerialDriverInfo));
41 std::unique_ptr<SerialDeviceEnumeratorLinux> CreateEnumerator() {
42 return std::make_unique<SerialDeviceEnumeratorLinux>(drivers_file_);
46 base::test::TaskEnvironment task_environment_{
47 base::test::TaskEnvironment::MainThreadType::IO};
48 base::ScopedTempDir temp_dir_;
49 base::FilePath drivers_file_;
52 TEST_F(SerialDeviceEnumeratorLinuxTest, EnumerateUsb) {
53 testing::FakeUdevLoader fake_udev;
54 fake_udev.AddFakeDevice(/*name=*/"ttyACM0",
55 /*syspath=*/"/sys/class/tty/ttyACM0",
56 /*subsystem=*/"tty", /*devnode=*/absl::nullopt,
57 /*devtype=*/absl::nullopt, /*sysattrs=*/{},
60 {"DEVNAME", "/dev/ttyACM0"},
63 {"ID_VENDOR_ID", "2341"},
64 {"ID_MODEL_ID", "0043"},
65 {"ID_MODEL_ENC", "Arduino\\x20Uno"},
66 {"ID_SERIAL_SHORT", "000001"},
69 std::unique_ptr<SerialDeviceEnumeratorLinux> enumerator = CreateEnumerator();
70 std::vector<mojom::SerialPortInfoPtr> devices = enumerator->GetDevices();
71 ASSERT_EQ(devices.size(), 1u);
72 EXPECT_EQ(devices[0]->serial_number, "000001");
73 EXPECT_EQ(devices[0]->path, base::FilePath("/dev/ttyACM0"));
74 EXPECT_TRUE(devices[0]->has_vendor_id);
75 EXPECT_EQ(devices[0]->vendor_id, 0x2341);
76 EXPECT_TRUE(devices[0]->has_product_id);
77 EXPECT_EQ(devices[0]->product_id, 0x0043);
78 EXPECT_EQ(devices[0]->display_name, "Arduino Uno");
81 TEST_F(SerialDeviceEnumeratorLinuxTest, EnumerateRfcomm) {
82 testing::FakeUdevLoader fake_udev;
83 fake_udev.AddFakeDevice(/*name=*/"rfcomm0",
84 /*syspath=*/"/sys/class/tty/rfcomm0",
85 /*subsystem=*/"tty", /*devnode=*/absl::nullopt,
86 /*devtype=*/absl::nullopt, /*sysattrs=*/{},
89 {"DEVNAME", "/dev/rfcomm0"},
94 std::unique_ptr<SerialDeviceEnumeratorLinux> enumerator = CreateEnumerator();
95 std::vector<mojom::SerialPortInfoPtr> devices = enumerator->GetDevices();
96 ASSERT_EQ(devices.size(), 1u);
97 EXPECT_FALSE(devices[0]->serial_number.has_value());
98 EXPECT_EQ(devices[0]->path, base::FilePath("/dev/rfcomm0"));
99 EXPECT_FALSE(devices[0]->has_vendor_id);
100 EXPECT_FALSE(devices[0]->has_product_id);
101 EXPECT_FALSE(devices[0]->display_name.has_value());
104 } // namespace device