File: udev_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (169 lines) | stat: -rw-r--r-- 6,378 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/udev_linux/udev.h"
#include <vector>

#include "base/files/file_path.h"
#include "device/udev_linux/fake_udev_loader.h"
#include "device/udev_linux/udev_loader.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace device {

TEST(UdevTest, DecodeString) {
  ASSERT_EQ("", UdevDecodeString(""));
  ASSERT_EQ("\\", UdevDecodeString("\\x5c"));
  ASSERT_EQ("\\x5", UdevDecodeString("\\x5"));
  ASSERT_EQ("049f", UdevDecodeString("049f"));
  ASSERT_EQ(
      "HD Pro Webcam C920", UdevDecodeString("HD\\x20Pro\\x20Webcam\\x20C920"));
  ASSERT_EQ("E-MU Systems,Inc.", UdevDecodeString("E-MU\\x20Systems\\x2cInc."));
}

TEST(UdevTest, Loader) {
  ASSERT_NE(nullptr, UdevLoader::Get());
}

TEST(UdevTest, GetPropertyWithNone) {
  testing::FakeUdevLoader fake_udev;
  udev_device* device =
      fake_udev.AddFakeDevice(/*name=*/"Foo", /*syspath=*/"/device/foo",
                              /*subsystem=*/"", /*devnode=*/std::nullopt,
                              /*devtype=*/std::nullopt, /*sysattrs=*/{},
                              /*properties=*/{});

  const std::string attr_value = UdevDeviceGetPropertyValue(device, "prop");
  EXPECT_TRUE(attr_value.empty());
}

TEST(UdevTest, GetSysPropSimple) {
  testing::FakeUdevLoader fake_udev;
  std::map<std::string, std::string> props;
  props.emplace("prop", "prop value");
  udev_device* device = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, std::move(props));

  std::string attr_value = UdevDeviceGetPropertyValue(device, "prop");
  EXPECT_EQ("prop value", attr_value);

  attr_value = UdevDeviceGetPropertyValue(device, "unknown prop");
  EXPECT_TRUE(attr_value.empty());
}

TEST(UdevTest, GetFullSysPropertiesList) {
  testing::FakeUdevLoader fake_udev;
  std::map<std::string, std::string> props = {
      {"TAGS", ":powerd:"},
      {"DEVNAME", "/dev/dri/card0"},
      {"DEVTYPE", "drm_minor"},
      {"HOTPLUG", "1"},
      {"USEC_INITIALIZED", "1234567"},
      {"MINOR", "0"},
      {"ID_PATH_TAG", "pci-0000_00_02_0"},
      {"PROPERTY", "456"},
      {"DEVPATH", "/devices/pci0000:00/0000:00:02.0/drm/card0"},
      {"ID_PATH", "pci-0000:00:02.0"},
      {"SUBSYSTEM", "drm"},
      {"CONNECTOR", "123"},
      {"MAJOR", "226"},
      {"SEQNUM", "1234"},
      {"ACTION", "change"}};
  udev_device* device = fake_udev.AddFakeDevice(
      /*name=*/"/dev/dri/card0",
      /*syspath=*/"/devices/pci0000:00/0000:00:02.0/drm/card0",
      /*subsystem=*/"drm", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, std::move(props));

  udev_list_entry* prop_list = udev_device_get_properties_list_entry(device);
  udev_list_entry* entry;
  std::vector<std::string> output_prop_list;
  udev_list_entry_foreach(entry, prop_list) {
    output_prop_list.emplace_back(udev_list_entry_get_name(entry));
  }

  // Expect the properties to be sorted when enumerated.
  EXPECT_THAT(
      output_prop_list,
      testing::UnorderedElementsAre(
          "ACTION", "CONNECTOR", "DEVNAME", "DEVPATH", "DEVTYPE", "HOTPLUG",
          "ID_PATH", "ID_PATH_TAG", "MAJOR", "MINOR", "PROPERTY", "SEQNUM",
          "SUBSYSTEM", "TAGS", "USEC_INITIALIZED"));
}

TEST(UdevTest, GetSysAttrNoAttrs) {
  testing::FakeUdevLoader fake_udev;
  udev_device* device = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, /*properties=*/{});

  const std::string attr_value = UdevDeviceGetSysattrValue(device, "attr");
  EXPECT_TRUE(attr_value.empty());
}

TEST(UdevTest, GetSysAttrSimple) {
  testing::FakeUdevLoader fake_udev;
  std::map<std::string, std::string> attrs;
  attrs.emplace("attr", "attr value");
  udev_device* device = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      std::move(attrs), /*properties=*/{});

  std::string attr_value = UdevDeviceGetSysattrValue(device, "attr");
  EXPECT_EQ("attr value", attr_value);

  attr_value = UdevDeviceGetSysattrValue(device, "unknown attr");
  EXPECT_TRUE(attr_value.empty());
}

TEST(UdevTest, GetParent) {
  testing::FakeUdevLoader fake_udev;
  std::map<std::string, std::string> attrs;
  udev_device* grandparent = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, /*properties=*/{});
  udev_device* parent = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo/bar",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, /*properties=*/{});
  udev_device* device = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo/bar/baz",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, /*properties=*/{});

  EXPECT_EQ(parent, udev_device_get_parent(device));
  EXPECT_EQ(grandparent, udev_device_get_parent(parent));
  EXPECT_EQ(nullptr, udev_device_get_parent(grandparent));
}

TEST(UdevTest, GetSysAttrRecursiveOneLevel) {
  testing::FakeUdevLoader fake_udev;
  std::map<std::string, std::string> attrs;
  attrs.emplace("attr", "attr value");
  fake_udev.AddFakeDevice(/*name=*/"Foo", /*syspath=*/"/device/foo",
                          /*subsystem=*/"", /*devnode=*/std::nullopt,
                          /*devtype=*/std::nullopt, std::move(attrs),
                          /*properties=*/{});
  udev_device* device = fake_udev.AddFakeDevice(
      /*name=*/"Foo", /*syspath=*/"/device/foo/bar",
      /*subsystem=*/"", /*devnode=*/std::nullopt, /*devtype=*/std::nullopt,
      /*sysattrs=*/{}, /*properties=*/{});

  // Don't find the attr on the current device.
  std::string attr_value = UdevDeviceGetSysattrValue(device, "attr");
  EXPECT_TRUE(attr_value.empty());

  // Find it when searching recursive.
  attr_value = UdevDeviceRecursiveGetSysattrValue(device, "attr");
  EXPECT_EQ("attr value", attr_value);
}

}  // namespace device