| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-daemon.h" |
| 7 |
#include "sd-event.h" |
| 8 |
|
| 9 |
#include "capability-util.h" |
| 10 |
#include "device-internal.h" |
| 11 |
#include "device-private.h" |
| 12 |
#include "device-util.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "fs-util.h" |
| 16 |
#include "hashmap.h" |
| 17 |
#include "libmount-util.h" |
| 18 |
#include "mkdir.h" |
| 19 |
#include "mount-util.h" |
| 20 |
#include "mountpoint-util.h" |
| 21 |
#include "nulstr-util.h" |
| 22 |
#include "path-util.h" |
| 23 |
#include "process-util.h" |
| 24 |
#include "rm-rf.h" |
| 25 |
#include "set.h" |
| 26 |
#include "stat-util.h" |
| 27 |
#include "string-util.h" |
| 28 |
#include "tests.h" |
| 29 |
#include "tmpfile-util.h" |
| 30 |
#include "udev-util.h" |
| 31 |
#include "virt.h" |
| 32 |
|
| 33 |
TEST(mdio_bus) { |
| 34 |
int r; |
| 35 |
|
| 36 |
/* For issue #37711 */ |
| 37 |
|
| 38 |
if (getuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) |
| 39 |
return (void) log_tests_skipped("Not privileged"); |
| 40 |
if (running_in_chroot() > 0) |
| 41 |
return (void) log_tests_skipped("Running in chroot"); |
| 42 |
|
| 43 |
r = ASSERT_OK(pidref_safe_fork( |
| 44 |
"(mdio_bus)", |
| 45 |
FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, |
| 46 |
NULL)); |
| 47 |
if (r == 0) { |
| 48 |
const char *syspath = "/sys/bus/mdio_bus/drivers/Qualcomm Atheros AR8031!AR8033"; |
| 49 |
const char *id = "+drivers:mdio_bus:Qualcomm Atheros AR8031!AR8033"; |
| 50 |
|
| 51 |
struct { |
| 52 |
int (*getter)(sd_device*, const char**); |
| 53 |
const char *val; |
| 54 |
} table[] = { |
| 55 |
{ sd_device_get_syspath, syspath }, |
| 56 |
{ sd_device_get_device_id, id }, |
| 57 |
{ sd_device_get_subsystem, "drivers" }, |
| 58 |
{ sd_device_get_driver_subsystem, "mdio_bus" }, |
| 59 |
{ sd_device_get_sysname, "Qualcomm Atheros AR8031/AR8033" }, |
| 60 |
}; |
| 61 |
|
| 62 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_DEVICE_VERIFY_SYSFS", "0", /* overwrite= */ false)); |
| 63 |
ASSERT_OK(mount_nofollow_verbose(LOG_ERR, "tmpfs", "/sys/bus/", "tmpfs", 0, NULL)); |
| 64 |
r = mkdir_p(syspath, 0755); |
| 65 |
if (ERRNO_IS_NEG_PRIVILEGE(r)) { |
| 66 |
log_tests_skipped("Lacking privileges to create %s", syspath); |
| 67 |
_exit(EXIT_SUCCESS); |
| 68 |
} |
| 69 |
ASSERT_OK(r); |
| 70 |
|
| 71 |
_cleanup_free_ char *uevent = path_join(syspath, "uevent"); |
| 72 |
ASSERT_NOT_NULL(uevent); |
| 73 |
ASSERT_OK(touch(uevent)); |
| 74 |
|
| 75 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 76 |
ASSERT_OK(sd_device_new_from_syspath(&dev, syspath)); |
| 77 |
|
| 78 |
FOREACH_ELEMENT(t, table) { |
| 79 |
const char *v; |
| 80 |
|
| 81 |
ASSERT_OK(t->getter(dev, &v)); |
| 82 |
ASSERT_STREQ(v, t->val); |
| 83 |
} |
| 84 |
|
| 85 |
dev = sd_device_unref(dev); |
| 86 |
ASSERT_OK(sd_device_new_from_device_id(&dev, id)); |
| 87 |
|
| 88 |
FOREACH_ELEMENT(t, table) { |
| 89 |
const char *v; |
| 90 |
|
| 91 |
ASSERT_OK(t->getter(dev, &v)); |
| 92 |
ASSERT_STREQ(v, t->val); |
| 93 |
} |
| 94 |
|
| 95 |
_exit(EXIT_SUCCESS); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static void test_sd_device_one(sd_device *d) { |
| 100 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 101 |
const char *syspath, *sysname, *subsystem = NULL, *devname, *val; |
| 102 |
bool is_block = false; |
| 103 |
dev_t devnum; |
| 104 |
usec_t usec; |
| 105 |
int ifindex, r; |
| 106 |
|
| 107 |
ASSERT_OK(sd_device_get_syspath(d, &syspath)); |
| 108 |
ASSERT_NOT_NULL(path_startswith(syspath, "/sys")); |
| 109 |
ASSERT_OK(sd_device_get_sysname(d, &sysname)); |
| 110 |
|
| 111 |
log_info("%s(%s)", __func__, syspath); |
| 112 |
|
| 113 |
ASSERT_OK(sd_device_new_from_syspath(&dev, syspath)); |
| 114 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 115 |
ASSERT_STREQ(syspath, val); |
| 116 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 117 |
|
| 118 |
ASSERT_OK(sd_device_new_from_path(&dev, syspath)); |
| 119 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 120 |
ASSERT_STREQ(syspath, val); |
| 121 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 122 |
|
| 123 |
r = sd_device_get_ifindex(d, &ifindex); |
| 124 |
if (r < 0) |
| 125 |
ASSERT_ERROR(r, ENOENT); |
| 126 |
else { |
| 127 |
ASSERT_GT(ifindex, 0); |
| 128 |
|
| 129 |
const char *ifname; |
| 130 |
ASSERT_OK(device_get_ifname(d, &ifname)); |
| 131 |
ASSERT_NOT_NULL(endswith(syspath, ifname)); |
| 132 |
if (strchr(sysname, '/')) |
| 133 |
ASSERT_FALSE(streq(ifname, sysname)); |
| 134 |
else |
| 135 |
ASSERT_STREQ(ifname, sysname); |
| 136 |
|
| 137 |
r = sd_device_new_from_ifindex(&dev, ifindex); |
| 138 |
if (r < 0) { |
| 139 |
ASSERT_ERROR(r, ENODEV); |
| 140 |
log_device_warning_errno(d, r, |
| 141 |
"Failed to create sd-device object from ifindex %i. " |
| 142 |
"Maybe running on a non-host network namespace.", ifindex); |
| 143 |
} else { |
| 144 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 145 |
ASSERT_STREQ(syspath, val); |
| 146 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 147 |
} |
| 148 |
|
| 149 |
/* This does not require the interface really exists on the network namespace. |
| 150 |
* Hence, this should always succeed. */ |
| 151 |
ASSERT_OK(sd_device_new_from_ifname(&dev, sysname)); |
| 152 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 153 |
ASSERT_STREQ(syspath, val); |
| 154 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 155 |
} |
| 156 |
|
| 157 |
r = sd_device_get_subsystem(d, &subsystem); |
| 158 |
if (r < 0) |
| 159 |
ASSERT_ERROR(r, ENOENT); |
| 160 |
else { |
| 161 |
const char *name, *id; |
| 162 |
|
| 163 |
if (streq(subsystem, "drivers")) { |
| 164 |
const char *driver_subsystem; |
| 165 |
ASSERT_OK(sd_device_get_driver_subsystem(d, &driver_subsystem)); |
| 166 |
name = strjoina(driver_subsystem, ":", sysname); |
| 167 |
} else |
| 168 |
name = sysname; |
| 169 |
|
| 170 |
r = sd_device_new_from_subsystem_sysname(&dev, subsystem, name); |
| 171 |
if (r < 0) |
| 172 |
ASSERT_ERROR(r, ETOOMANYREFS); |
| 173 |
else { |
| 174 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 175 |
ASSERT_STREQ(syspath, val); |
| 176 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 177 |
} |
| 178 |
|
| 179 |
/* The device ID depends on subsystem. */ |
| 180 |
ASSERT_OK(sd_device_get_device_id(d, &id)); |
| 181 |
r = sd_device_new_from_device_id(&dev, id); |
| 182 |
if (r < 0) { |
| 183 |
if (r == -ENODEV && ifindex > 0) |
| 184 |
log_device_warning_errno(d, r, |
| 185 |
"Failed to create sd-device object from device ID \"%s\". " |
| 186 |
"Maybe running on a non-host network namespace.", id); |
| 187 |
else |
| 188 |
ASSERT_ERROR(r, ETOOMANYREFS); |
| 189 |
} else { |
| 190 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 191 |
ASSERT_STREQ(syspath, val); |
| 192 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 193 |
} |
| 194 |
|
| 195 |
/* These require udev database, and reading database requires device ID. */ |
| 196 |
ASSERT_OK(r = sd_device_get_is_initialized(d)); |
| 197 |
if (r > 0) { |
| 198 |
r = sd_device_get_usec_since_initialized(d, &usec); |
| 199 |
if (r < 0) |
| 200 |
ASSERT_ERROR(r, ENODATA); |
| 201 |
else |
| 202 |
ASSERT_GT(usec, 0U); |
| 203 |
} |
| 204 |
|
| 205 |
r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val); |
| 206 |
if (r < 0) |
| 207 |
ASSERT_ERROR(r, ENOENT); |
| 208 |
} |
| 209 |
|
| 210 |
if (streq(subsystem, "drm")) { |
| 211 |
const char *edid_content; |
| 212 |
size_t edid_size = 0; |
| 213 |
|
| 214 |
r = sd_device_get_sysattr_value_with_size(d, "edid", &edid_content, &edid_size); |
| 215 |
if (r < 0) |
| 216 |
ASSERT_ERROR(r, ENOENT); |
| 217 |
|
| 218 |
/* at least 128 if monitor is connected, otherwise 0 */ |
| 219 |
ASSERT_TRUE(edid_size == 0 || edid_size >= 128); |
| 220 |
} |
| 221 |
|
| 222 |
is_block = streq_ptr(subsystem, "block"); |
| 223 |
|
| 224 |
r = sd_device_get_devname(d, &devname); |
| 225 |
if (r < 0) |
| 226 |
ASSERT_ERROR(r, ENOENT); |
| 227 |
else { |
| 228 |
r = sd_device_new_from_devname(&dev, devname); |
| 229 |
if (r < 0) |
| 230 |
ASSERT_TRUE(r == -ENODEV || ERRNO_IS_NEG_PRIVILEGE(r)); |
| 231 |
else { |
| 232 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 233 |
ASSERT_STREQ(syspath, val); |
| 234 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 235 |
} |
| 236 |
|
| 237 |
r = sd_device_new_from_path(&dev, devname); |
| 238 |
if (r < 0) |
| 239 |
ASSERT_TRUE(r == -ENODEV || ERRNO_IS_NEG_PRIVILEGE(r)); |
| 240 |
else { |
| 241 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 242 |
ASSERT_STREQ(syspath, val); |
| 243 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 244 |
|
| 245 |
_cleanup_close_ int fd = -EBADF; |
| 246 |
fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH)); |
| 247 |
ASSERT_TRUE(fd >= 0 || ERRNO_IS_NEG_PRIVILEGE(fd)); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
r = sd_device_get_devnum(d, &devnum); |
| 252 |
if (r < 0) |
| 253 |
ASSERT_ERROR(r, ENOENT); |
| 254 |
else { |
| 255 |
_cleanup_free_ char *p = NULL; |
| 256 |
|
| 257 |
ASSERT_GT(major(devnum), 0U); |
| 258 |
|
| 259 |
ASSERT_OK(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum)); |
| 260 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 261 |
ASSERT_STREQ(syspath, val); |
| 262 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 263 |
|
| 264 |
ASSERT_OK(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum))); |
| 265 |
ASSERT_OK(sd_device_new_from_devname(&dev, p)); |
| 266 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 267 |
ASSERT_STREQ(syspath, val); |
| 268 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 269 |
|
| 270 |
ASSERT_OK(sd_device_new_from_path(&dev, p)); |
| 271 |
ASSERT_OK(sd_device_get_syspath(dev, &val)); |
| 272 |
ASSERT_STREQ(syspath, val); |
| 273 |
ASSERT_NULL(dev = sd_device_unref(dev)); |
| 274 |
} |
| 275 |
|
| 276 |
ASSERT_OK(sd_device_get_devpath(d, &val)); |
| 277 |
|
| 278 |
r = sd_device_get_devtype(d, NULL); |
| 279 |
if (r < 0) |
| 280 |
ASSERT_ERROR(r, ENOENT); |
| 281 |
|
| 282 |
r = sd_device_get_driver(d, NULL); |
| 283 |
if (r < 0) |
| 284 |
ASSERT_ERROR(r, ENOENT); |
| 285 |
|
| 286 |
r = sd_device_get_sysnum(d, &val); |
| 287 |
if (r < 0) |
| 288 |
ASSERT_ERROR(r, ENOENT); |
| 289 |
else { |
| 290 |
ASSERT_TRUE(val > sysname); |
| 291 |
ASSERT_TRUE(val < sysname + strlen(sysname)); |
| 292 |
ASSERT_TRUE(in_charset(val, DIGITS)); |
| 293 |
ASSERT_FALSE(ascii_isdigit(val[-1])); |
| 294 |
|
| 295 |
r = device_get_sysnum_unsigned(d, NULL); |
| 296 |
if (r < 0) |
| 297 |
ASSERT_ERROR(r, ERANGE); /* sysnum may be too large. */ |
| 298 |
} |
| 299 |
|
| 300 |
r = sd_device_get_sysattr_value(d, "nsid", NULL); |
| 301 |
if (r < 0) |
| 302 |
ASSERT_TRUE(ERRNO_IS_NEG_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL)); |
| 303 |
else { |
| 304 |
unsigned x; |
| 305 |
ASSERT_OK(r = device_get_sysattr_unsigned(d, "nsid", &x)); |
| 306 |
ASSERT_EQ(x > 0, r > 0); |
| 307 |
} |
| 308 |
|
| 309 |
const char *uevent; |
| 310 |
if (sd_device_get_sysattr_value(d, "uevent", &uevent) >= 0) { |
| 311 |
const char *uevent_safe; |
| 312 |
ASSERT_OK(device_get_sysattr_safe_string(d, "uevent", &uevent_safe)); |
| 313 |
ASSERT_STREQ(uevent, uevent_safe); |
| 314 |
} |
| 315 |
|
| 316 |
if (sd_device_get_ifindex(d, &ifindex) >= 0) { |
| 317 |
int i; |
| 318 |
ASSERT_OK_POSITIVE(device_get_sysattr_int(d, "ifindex", &i)); |
| 319 |
ASSERT_EQ(i, ifindex); |
| 320 |
|
| 321 |
unsigned u; |
| 322 |
ASSERT_OK_POSITIVE(device_get_sysattr_unsigned(d, "ifindex", &u)); |
| 323 |
ASSERT_EQ(u, (unsigned) ifindex); |
| 324 |
|
| 325 |
uint64_t u64; |
| 326 |
ASSERT_OK_POSITIVE(device_get_sysattr_u64(d, "ifindex", &u64)); |
| 327 |
ASSERT_EQ(u64, (uint64_t) ifindex); |
| 328 |
|
| 329 |
uint32_t u32; |
| 330 |
ASSERT_OK_POSITIVE(device_get_sysattr_u32(d, "ifindex", &u32)); |
| 331 |
ASSERT_EQ(u32, (uint32_t) ifindex); |
| 332 |
|
| 333 |
if (ifindex <= UINT16_MAX) { |
| 334 |
uint16_t u16; |
| 335 |
ASSERT_OK_POSITIVE(device_get_sysattr_u16(d, "ifindex", &u16)); |
| 336 |
ASSERT_EQ(u16, (uint16_t) ifindex); |
| 337 |
} |
| 338 |
|
| 339 |
if (ifindex <= UINT8_MAX) { |
| 340 |
uint8_t u8; |
| 341 |
ASSERT_OK_POSITIVE(device_get_sysattr_u8(d, "ifindex", &u8)); |
| 342 |
ASSERT_EQ(u8, (uint8_t) ifindex); |
| 343 |
} |
| 344 |
|
| 345 |
const char *s; |
| 346 |
ASSERT_OK(sd_device_get_sysattr_value(d, "ifindex", &s)); |
| 347 |
ASSERT_OK_POSITIVE(device_get_sysattr_streq(d, "ifindex", s)); |
| 348 |
ASSERT_OK_ZERO(device_get_sysattr_streq(d, "ifindex", "hoge")); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
static void exclude_problematic_devices(sd_device_enumerator *e) { |
| 353 |
/* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes |
| 354 |
* disappear during running this test. Let's exclude them here for stability. */ |
| 355 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "bdi", false)); |
| 356 |
ASSERT_OK(sd_device_enumerator_add_nomatch_sysname(e, "loop*")); |
| 357 |
/* On some CI environments, it seems dm block devices sometimes disappear during running this test. |
| 358 |
* Let's exclude them here for stability. */ |
| 359 |
ASSERT_OK(sd_device_enumerator_add_nomatch_sysname(e, "dm-*")); |
| 360 |
/* Several other unit tests create and remove virtual network interfaces, e.g. test-netlink and |
| 361 |
* test-local-addresses. When one of these tests run in parallel with this unit test, the enumerated |
| 362 |
* device may disappear. Let's exclude them here for stability. */ |
| 363 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "net", false)); |
| 364 |
} |
| 365 |
|
| 366 |
TEST(sd_device_enumerator_devices) { |
| 367 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 368 |
|
| 369 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 370 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 371 |
exclude_problematic_devices(e); |
| 372 |
|
| 373 |
FOREACH_DEVICE(e, d) |
| 374 |
test_sd_device_one(d); |
| 375 |
} |
| 376 |
|
| 377 |
TEST(sd_device_enumerator_subsystems) { |
| 378 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 379 |
|
| 380 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 381 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 382 |
FOREACH_SUBSYSTEM(e, d) |
| 383 |
test_sd_device_one(d); |
| 384 |
} |
| 385 |
|
| 386 |
static void test_sd_device_enumerator_filter_subsystem_one( |
| 387 |
const char *subsystem, |
| 388 |
Hashmap *h, |
| 389 |
unsigned *ret_n_new_dev, |
| 390 |
unsigned *ret_n_removed_dev) { |
| 391 |
|
| 392 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 393 |
unsigned n_new_dev = 0, n_removed_dev = 0; |
| 394 |
sd_device *dev; |
| 395 |
|
| 396 |
assert(ret_n_new_dev); |
| 397 |
assert(ret_n_removed_dev); |
| 398 |
|
| 399 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 400 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, subsystem, true)); |
| 401 |
exclude_problematic_devices(e); |
| 402 |
|
| 403 |
FOREACH_DEVICE(e, d) { |
| 404 |
const char *syspath; |
| 405 |
sd_device *t; |
| 406 |
|
| 407 |
ASSERT_OK(sd_device_get_syspath(d, &syspath)); |
| 408 |
t = hashmap_remove(h, syspath); |
| 409 |
|
| 410 |
if (!t) { |
| 411 |
log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath); |
| 412 |
n_new_dev++; |
| 413 |
} |
| 414 |
|
| 415 |
ASSERT_NULL(sd_device_unref(t)); |
| 416 |
} |
| 417 |
|
| 418 |
HASHMAP_FOREACH(dev, h) { |
| 419 |
const char *syspath; |
| 420 |
|
| 421 |
ASSERT_OK(sd_device_get_syspath(dev, &syspath)); |
| 422 |
log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath); |
| 423 |
n_removed_dev++; |
| 424 |
|
| 425 |
ASSERT_NULL(sd_device_unref(dev)); |
| 426 |
} |
| 427 |
|
| 428 |
hashmap_free(h); |
| 429 |
|
| 430 |
*ret_n_new_dev = n_new_dev; |
| 431 |
*ret_n_removed_dev = n_removed_dev; |
| 432 |
} |
| 433 |
|
| 434 |
static bool test_sd_device_enumerator_filter_subsystem_trial(void) { |
| 435 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 436 |
_cleanup_hashmap_free_ Hashmap *subsystems = NULL; |
| 437 |
unsigned n_new_dev = 0, n_removed_dev = 0; |
| 438 |
Hashmap *h; |
| 439 |
char *s; |
| 440 |
|
| 441 |
ASSERT_NOT_NULL((subsystems = hashmap_new(&string_hash_ops))); |
| 442 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 443 |
exclude_problematic_devices(e); |
| 444 |
|
| 445 |
FOREACH_DEVICE(e, d) { |
| 446 |
const char *syspath, *subsystem; |
| 447 |
int r; |
| 448 |
|
| 449 |
ASSERT_OK(sd_device_get_syspath(d, &syspath)); |
| 450 |
|
| 451 |
r = sd_device_get_subsystem(d, &subsystem); |
| 452 |
if (r < 0) { |
| 453 |
ASSERT_ERROR(r, ENOENT); |
| 454 |
continue; |
| 455 |
} |
| 456 |
|
| 457 |
h = hashmap_get(subsystems, subsystem); |
| 458 |
if (!h) { |
| 459 |
char *str; |
| 460 |
ASSERT_NOT_NULL((str = strdup(subsystem))); |
| 461 |
ASSERT_NOT_NULL((h = hashmap_new(&string_hash_ops))); |
| 462 |
ASSERT_OK(hashmap_put(subsystems, str, h)); |
| 463 |
} |
| 464 |
|
| 465 |
ASSERT_OK(hashmap_put(h, syspath, d)); |
| 466 |
ASSERT_NOT_NULL(sd_device_ref(d)); |
| 467 |
|
| 468 |
log_debug("Added subsystem:%s syspath:%s", subsystem, syspath); |
| 469 |
} |
| 470 |
|
| 471 |
while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) { |
| 472 |
unsigned n, m; |
| 473 |
|
| 474 |
test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m); |
| 475 |
free(s); |
| 476 |
|
| 477 |
n_new_dev += n; |
| 478 |
n_removed_dev += m; |
| 479 |
} |
| 480 |
|
| 481 |
if (n_new_dev > 0) |
| 482 |
log_warning("%u new devices are found in re-scan", n_new_dev); |
| 483 |
if (n_removed_dev > 0) |
| 484 |
log_warning("%u devices removed in re-scan", n_removed_dev); |
| 485 |
|
| 486 |
return n_new_dev + n_removed_dev == 0; |
| 487 |
} |
| 488 |
|
| 489 |
static bool test_sd_device_enumerator_filter_subsystem_trial_many(void) { |
| 490 |
for (unsigned i = 0; i < 20; i++) { |
| 491 |
log_debug("%s(): trial %u", __func__, i); |
| 492 |
if (test_sd_device_enumerator_filter_subsystem_trial()) |
| 493 |
return true; |
| 494 |
} |
| 495 |
|
| 496 |
return false; |
| 497 |
} |
| 498 |
|
| 499 |
static int on_inotify(sd_event_source *s, const struct inotify_event *event, void *userdata) { |
| 500 |
if (test_sd_device_enumerator_filter_subsystem_trial_many()) |
| 501 |
return sd_event_exit(sd_event_source_get_event(s), 0); |
| 502 |
|
| 503 |
return sd_event_exit(sd_event_source_get_event(s), -EBUSY); |
| 504 |
} |
| 505 |
|
| 506 |
TEST(sd_device_enumerator_filter_subsystem) { |
| 507 |
/* The test test_sd_device_enumerator_filter_subsystem_trial() is quite racy. Let's run the function |
| 508 |
* several times after the udev queue becomes empty. */ |
| 509 |
|
| 510 |
if (!udev_available() || (access("/run/udev", F_OK) < 0 && errno == ENOENT)) { |
| 511 |
ASSERT_TRUE(test_sd_device_enumerator_filter_subsystem_trial_many()); |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
/* The rest of this test depends on a full booted system with a working udev and so on */ |
| 516 |
if (!sd_booted()) |
| 517 |
return (void) log_tests_skipped("Test requires fully booted system with udev/etc, skipping to avoid hanging forever."); |
| 518 |
|
| 519 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 520 |
ASSERT_OK(sd_event_default(&event)); |
| 521 |
ASSERT_OK(sd_event_add_inotify(event, NULL, "/run/udev" , IN_DELETE, on_inotify, NULL)); |
| 522 |
|
| 523 |
if (udev_queue_is_empty() == 0) { |
| 524 |
log_debug("udev queue is not empty, waiting for all queued events to be processed."); |
| 525 |
ASSERT_OK(sd_event_loop(event)); |
| 526 |
} else |
| 527 |
ASSERT_TRUE(test_sd_device_enumerator_filter_subsystem_trial_many()); |
| 528 |
} |
| 529 |
|
| 530 |
TEST(sd_device_enumerator_add_match_sysattr) { |
| 531 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 532 |
sd_device *dev; |
| 533 |
int ifindex; |
| 534 |
|
| 535 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 536 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 537 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "net", true)); |
| 538 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true)); |
| 539 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "hoge", true)); |
| 540 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "foo", true)); |
| 541 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "bar", false)); |
| 542 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "baz", false)); |
| 543 |
|
| 544 |
ASSERT_NOT_NULL((dev = sd_device_enumerator_get_device_first(e))); |
| 545 |
ASSERT_OK(sd_device_get_ifindex(dev, &ifindex)); |
| 546 |
ASSERT_EQ(ifindex, 1); |
| 547 |
|
| 548 |
ASSERT_NULL(sd_device_enumerator_get_device_next(e)); |
| 549 |
} |
| 550 |
|
| 551 |
TEST(sd_device_enumerator_add_match_property) { |
| 552 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 553 |
sd_device *dev; |
| 554 |
int ifindex; |
| 555 |
|
| 556 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 557 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 558 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "net", true)); |
| 559 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true)); |
| 560 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "IFINDE*", "1*")); |
| 561 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "IFINDE*", "hoge")); |
| 562 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "IFINDE*", NULL)); |
| 563 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "AAAAA", "BBBB")); |
| 564 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "FOOOO", NULL)); |
| 565 |
|
| 566 |
ASSERT_NOT_NULL((dev = sd_device_enumerator_get_device_first(e))); |
| 567 |
ASSERT_OK(sd_device_get_ifindex(dev, &ifindex)); |
| 568 |
ASSERT_EQ(ifindex, 1); |
| 569 |
} |
| 570 |
|
| 571 |
TEST(sd_device_enumerator_add_match_property_required) { |
| 572 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 573 |
sd_device *dev; |
| 574 |
int ifindex; |
| 575 |
|
| 576 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 577 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 578 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "net", true)); |
| 579 |
ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true)); |
| 580 |
ASSERT_OK(sd_device_enumerator_add_match_property_required(e, "IFINDE*", "1*")); |
| 581 |
|
| 582 |
/* Only one required match which should be satisfied. */ |
| 583 |
ASSERT_NOT_NULL((dev = sd_device_enumerator_get_device_first(e))); |
| 584 |
ASSERT_OK(sd_device_get_ifindex(dev, &ifindex)); |
| 585 |
ASSERT_EQ(ifindex, 1); |
| 586 |
|
| 587 |
/* Now let's add a bunch of garbage properties which should not be satisfied. */ |
| 588 |
ASSERT_OK(sd_device_enumerator_add_match_property_required(e, "IFINDE*", "hoge")); |
| 589 |
ASSERT_OK(sd_device_enumerator_add_match_property_required(e, "IFINDE*", NULL)); |
| 590 |
ASSERT_OK(sd_device_enumerator_add_match_property_required(e, "AAAAA", "BBBB")); |
| 591 |
ASSERT_OK(sd_device_enumerator_add_match_property_required(e, "FOOOO", NULL)); |
| 592 |
|
| 593 |
ASSERT_NULL(sd_device_enumerator_get_device_first(e)); |
| 594 |
} |
| 595 |
|
| 596 |
static void check_parent_match(sd_device_enumerator *e, sd_device *dev) { |
| 597 |
const char *syspath; |
| 598 |
bool found = false; |
| 599 |
|
| 600 |
ASSERT_OK(sd_device_get_syspath(dev, &syspath)); |
| 601 |
|
| 602 |
FOREACH_DEVICE(e, d) { |
| 603 |
const char *s; |
| 604 |
|
| 605 |
ASSERT_OK(sd_device_get_syspath(d, &s)); |
| 606 |
if (streq(s, syspath)) { |
| 607 |
found = true; |
| 608 |
break; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
if (!found) { |
| 613 |
log_device_debug(dev, "not enumerated, already removed??"); |
| 614 |
/* If the original device not found, then the device should be already removed. */ |
| 615 |
ASSERT_FAIL(access(syspath, F_OK)); |
| 616 |
ASSERT_EQ(errno, ENOENT); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
TEST(sd_device_enumerator_add_match_parent) { |
| 621 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 622 |
/* Some devices have thousands of children. Avoid spending too much time in the double loop below. */ |
| 623 |
unsigned iterations = 200; |
| 624 |
int r; |
| 625 |
|
| 626 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 627 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 628 |
exclude_problematic_devices(e); |
| 629 |
|
| 630 |
if (!slow_tests_enabled()) |
| 631 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "block", true)); |
| 632 |
|
| 633 |
FOREACH_DEVICE(e, dev) { |
| 634 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *p = NULL; |
| 635 |
const char *syspath; |
| 636 |
sd_device *parent; |
| 637 |
|
| 638 |
if (iterations-- == 0) |
| 639 |
break; |
| 640 |
|
| 641 |
ASSERT_OK(sd_device_get_syspath(dev, &syspath)); |
| 642 |
|
| 643 |
r = sd_device_get_parent(dev, &parent); |
| 644 |
if (r < 0) { |
| 645 |
ASSERT_TRUE(ERRNO_IS_NEG_DEVICE_ABSENT(r)); |
| 646 |
continue; |
| 647 |
} |
| 648 |
|
| 649 |
log_debug("> %s", syspath); |
| 650 |
|
| 651 |
ASSERT_OK(sd_device_enumerator_new(&p)); |
| 652 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(p)); |
| 653 |
ASSERT_OK(sd_device_enumerator_add_match_parent(p, parent)); |
| 654 |
|
| 655 |
check_parent_match(p, dev); |
| 656 |
|
| 657 |
/* If the device does not have subsystem, then it is not enumerated. */ |
| 658 |
r = sd_device_get_subsystem(parent, NULL); |
| 659 |
if (r < 0) { |
| 660 |
ASSERT_ERROR(r, ENOENT); |
| 661 |
continue; |
| 662 |
} |
| 663 |
check_parent_match(p, parent); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
TEST(sd_device_enumerator_add_all_parents) { |
| 668 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 669 |
int r; |
| 670 |
|
| 671 |
/* STEP 1: enumerate all block devices without all_parents() */ |
| 672 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 673 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 674 |
exclude_problematic_devices(e); |
| 675 |
|
| 676 |
/* filter in only a subsystem */ |
| 677 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "block", true)); |
| 678 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition")); |
| 679 |
|
| 680 |
unsigned devices_count_with_parents = 0; |
| 681 |
unsigned devices_count_without_parents = 0; |
| 682 |
FOREACH_DEVICE(e, dev) { |
| 683 |
ASSERT_OK_POSITIVE(device_is_subsystem_devtype(dev, "block", "partition")); |
| 684 |
devices_count_without_parents++; |
| 685 |
} |
| 686 |
|
| 687 |
log_debug("found %u devices", devices_count_without_parents); |
| 688 |
|
| 689 |
/* STEP 2: enumerate again with all_parents() */ |
| 690 |
ASSERT_OK(sd_device_enumerator_add_all_parents(e)); |
| 691 |
|
| 692 |
unsigned not_filtered_parent_count = 0; |
| 693 |
FOREACH_DEVICE(e, dev) { |
| 694 |
ASSERT_OK(r = device_is_subsystem_devtype(dev, "block", "partition")); |
| 695 |
if (r == 0) |
| 696 |
not_filtered_parent_count++; |
| 697 |
devices_count_with_parents++; |
| 698 |
} |
| 699 |
log_debug("found %u devices out of %u that would have been excluded without all_parents()", |
| 700 |
not_filtered_parent_count, |
| 701 |
devices_count_with_parents); |
| 702 |
ASSERT_EQ(devices_count_with_parents, devices_count_without_parents + not_filtered_parent_count); |
| 703 |
} |
| 704 |
|
| 705 |
TEST(sd_device_get_child) { |
| 706 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 707 |
/* Some devices have thousands of children. Avoid spending too much time in the double loop below. */ |
| 708 |
unsigned iterations = 3000; |
| 709 |
int r; |
| 710 |
|
| 711 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 712 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 713 |
exclude_problematic_devices(e); |
| 714 |
|
| 715 |
if (!slow_tests_enabled()) |
| 716 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "block", true)); |
| 717 |
|
| 718 |
FOREACH_DEVICE(e, dev) { |
| 719 |
const char *syspath, *parent_syspath, *expected_suffix, *suffix; |
| 720 |
sd_device *parent; |
| 721 |
bool found = false; |
| 722 |
|
| 723 |
ASSERT_OK(sd_device_get_syspath(dev, &syspath)); |
| 724 |
|
| 725 |
r = sd_device_get_parent(dev, &parent); |
| 726 |
if (r < 0) { |
| 727 |
ASSERT_TRUE(ERRNO_IS_NEG_DEVICE_ABSENT(r)); |
| 728 |
continue; |
| 729 |
} |
| 730 |
|
| 731 |
ASSERT_OK(sd_device_get_syspath(parent, &parent_syspath)); |
| 732 |
ASSERT_NOT_NULL((expected_suffix = path_startswith(syspath, parent_syspath))); |
| 733 |
|
| 734 |
log_debug("> %s", syspath); |
| 735 |
|
| 736 |
FOREACH_DEVICE_CHILD_WITH_SUFFIX(parent, child, suffix) { |
| 737 |
const char *s; |
| 738 |
|
| 739 |
if (iterations-- == 0) |
| 740 |
return; |
| 741 |
|
| 742 |
ASSERT_NOT_NULL(child); |
| 743 |
ASSERT_NOT_NULL(suffix); |
| 744 |
|
| 745 |
if (!streq(suffix, expected_suffix)) |
| 746 |
continue; |
| 747 |
|
| 748 |
ASSERT_OK(sd_device_get_syspath(child, &s)); |
| 749 |
ASSERT_STREQ(s, syspath); |
| 750 |
found = true; |
| 751 |
break; |
| 752 |
} |
| 753 |
ASSERT_TRUE(found); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
TEST(sd_device_new_from_nulstr) { |
| 758 |
const char *devlinks = |
| 759 |
"/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0" |
| 760 |
"/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0" |
| 761 |
"/dev/disk/by-label/Arch\\x20Linux\0" |
| 762 |
"/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0" |
| 763 |
"/dev/disk/by-partlabel/Arch\\x20Linux\0" |
| 764 |
"\0"; |
| 765 |
|
| 766 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL; |
| 767 |
_cleanup_free_ char *nulstr_copy = NULL; |
| 768 |
const char *nulstr; |
| 769 |
size_t len; |
| 770 |
|
| 771 |
ASSERT_OK(sd_device_new_from_syspath(&device, "/sys/class/net/lo")); |
| 772 |
|
| 773 |
/* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a |
| 774 |
* test for generating and parsing nulstr. For issue #17772. */ |
| 775 |
NULSTR_FOREACH(devlink, devlinks) { |
| 776 |
log_device_info(device, "setting devlink: %s", devlink); |
| 777 |
ASSERT_OK(device_add_devlink(device, devlink)); |
| 778 |
ASSERT_TRUE(set_contains(device->devlinks, devlink)); |
| 779 |
} |
| 780 |
|
| 781 |
/* For issue #23799 */ |
| 782 |
ASSERT_OK(device_add_tag(device, "tag1", false)); |
| 783 |
ASSERT_OK(device_add_tag(device, "tag2", false)); |
| 784 |
ASSERT_OK(device_add_tag(device, "current-tag1", true)); |
| 785 |
ASSERT_OK(device_add_tag(device, "current-tag2", true)); |
| 786 |
|
| 787 |
/* These properties are necessary for device_new_from_nulstr(). See device_verify(). */ |
| 788 |
ASSERT_OK(device_add_property_internal(device, "SEQNUM", "1")); |
| 789 |
ASSERT_OK(device_add_property_internal(device, "ACTION", "change")); |
| 790 |
|
| 791 |
ASSERT_OK(device_get_properties_nulstr(device, &nulstr, &len)); |
| 792 |
ASSERT_NOT_NULL((nulstr_copy = newdup(char, nulstr, len))); |
| 793 |
ASSERT_OK(device_new_from_nulstr(&from_nulstr, nulstr_copy, len)); |
| 794 |
|
| 795 |
ASSERT_OK_POSITIVE(sd_device_has_tag(from_nulstr, "tag1")); |
| 796 |
ASSERT_OK_POSITIVE(sd_device_has_tag(from_nulstr, "tag2")); |
| 797 |
ASSERT_OK_POSITIVE(sd_device_has_tag(from_nulstr, "current-tag1")); |
| 798 |
ASSERT_OK_POSITIVE(sd_device_has_tag(from_nulstr, "current-tag2")); |
| 799 |
ASSERT_OK_ZERO(sd_device_has_current_tag(from_nulstr, "tag1")); |
| 800 |
ASSERT_OK_ZERO(sd_device_has_current_tag(from_nulstr, "tag2")); |
| 801 |
ASSERT_OK_POSITIVE(sd_device_has_current_tag(from_nulstr, "current-tag1")); |
| 802 |
ASSERT_OK_POSITIVE(sd_device_has_current_tag(from_nulstr, "current-tag2")); |
| 803 |
|
| 804 |
NULSTR_FOREACH(devlink, devlinks) { |
| 805 |
log_device_info(from_nulstr, "checking devlink: %s", devlink); |
| 806 |
ASSERT_TRUE(set_contains(from_nulstr->devlinks, devlink)); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
TEST(sd_device_new_from_path) { |
| 811 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 812 |
_cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; |
| 813 |
int r; |
| 814 |
|
| 815 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir)); |
| 816 |
|
| 817 |
ASSERT_OK(sd_device_enumerator_new(&e)); |
| 818 |
ASSERT_OK(sd_device_enumerator_allow_uninitialized(e)); |
| 819 |
exclude_problematic_devices(e); |
| 820 |
|
| 821 |
ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, "block", true)); |
| 822 |
ASSERT_OK(sd_device_enumerator_add_match_property(e, "DEVNAME", "*")); |
| 823 |
|
| 824 |
FOREACH_DEVICE(e, dev) { |
| 825 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 826 |
const char *syspath, *devpath, *sysname, *s; |
| 827 |
_cleanup_free_ char *path = NULL; |
| 828 |
|
| 829 |
ASSERT_OK(sd_device_get_sysname(dev, &sysname)); |
| 830 |
|
| 831 |
log_debug("%s(%s)", __func__, sysname); |
| 832 |
|
| 833 |
ASSERT_OK(sd_device_get_syspath(dev, &syspath)); |
| 834 |
ASSERT_OK(sd_device_new_from_path(&d, syspath)); |
| 835 |
ASSERT_OK(sd_device_get_syspath(d, &s)); |
| 836 |
ASSERT_STREQ(s, syspath); |
| 837 |
ASSERT_NULL(d = sd_device_unref(d)); |
| 838 |
|
| 839 |
ASSERT_OK(sd_device_get_devname(dev, &devpath)); |
| 840 |
r = sd_device_new_from_path(&d, devpath); |
| 841 |
if (r < 0) |
| 842 |
ASSERT_TRUE(r == -ENODEV || ERRNO_IS_NEG_PRIVILEGE(r)); |
| 843 |
else { |
| 844 |
ASSERT_OK(sd_device_get_syspath(d, &s)); |
| 845 |
ASSERT_STREQ(s, syspath); |
| 846 |
ASSERT_NULL(d = sd_device_unref(d)); |
| 847 |
} |
| 848 |
|
| 849 |
ASSERT_NOT_NULL((path = path_join(tmpdir, sysname))); |
| 850 |
ASSERT_OK_ERRNO(symlink(syspath, path)); |
| 851 |
ASSERT_OK(sd_device_new_from_path(&d, path)); |
| 852 |
ASSERT_OK(sd_device_get_syspath(d, &s)); |
| 853 |
ASSERT_STREQ(s, syspath); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
static void test_devname_from_devnum_one(const char *path) { |
| 858 |
_cleanup_free_ char *resolved = NULL; |
| 859 |
struct stat st; |
| 860 |
|
| 861 |
log_debug("> %s", path); |
| 862 |
|
| 863 |
if (stat(path, &st) < 0) { |
| 864 |
log_notice("Path %s not found, skipping test", path); |
| 865 |
return; |
| 866 |
} |
| 867 |
|
| 868 |
ASSERT_OK(devname_from_devnum(st.st_mode, st.st_rdev, &resolved)); |
| 869 |
ASSERT_TRUE(path_equal(path, resolved)); |
| 870 |
ASSERT_NULL(resolved = mfree(resolved)); |
| 871 |
ASSERT_OK(devname_from_stat_rdev(&st, &resolved)); |
| 872 |
ASSERT_TRUE(path_equal(path, resolved)); |
| 873 |
} |
| 874 |
|
| 875 |
TEST(devname_from_devnum) { |
| 876 |
test_devname_from_devnum_one("/dev/null"); |
| 877 |
test_devname_from_devnum_one("/dev/zero"); |
| 878 |
test_devname_from_devnum_one("/dev/full"); |
| 879 |
test_devname_from_devnum_one("/dev/random"); |
| 880 |
test_devname_from_devnum_one("/dev/urandom"); |
| 881 |
test_devname_from_devnum_one("/dev/tty"); |
| 882 |
|
| 883 |
if (is_device_node("/run/systemd/inaccessible/blk") > 0) { |
| 884 |
test_devname_from_devnum_one("/run/systemd/inaccessible/chr"); |
| 885 |
test_devname_from_devnum_one("/run/systemd/inaccessible/blk"); |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
TEST(device_add_property) { |
| 890 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 891 |
const char *val; |
| 892 |
|
| 893 |
ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class/net/lo")); |
| 894 |
|
| 895 |
/* add a property */ |
| 896 |
ASSERT_OK(device_add_property(dev, "hoge", "foo")); |
| 897 |
ASSERT_OK(sd_device_get_property_value(dev, "hoge", &val)); |
| 898 |
ASSERT_STREQ(val, "foo"); |
| 899 |
|
| 900 |
/* update an existing property */ |
| 901 |
ASSERT_OK(device_add_property(dev, "hoge", "bar")); |
| 902 |
ASSERT_OK(sd_device_get_property_value(dev, "hoge", &val)); |
| 903 |
ASSERT_STREQ(val, "bar"); |
| 904 |
|
| 905 |
/* remove an existing property */ |
| 906 |
ASSERT_OK(device_add_property(dev, "hoge", NULL)); |
| 907 |
ASSERT_ERROR(sd_device_get_property_value(dev, "hoge", &val), ENOENT); |
| 908 |
|
| 909 |
/* add a property again */ |
| 910 |
ASSERT_OK(device_add_property(dev, "hoge", "foo")); |
| 911 |
ASSERT_OK(sd_device_get_property_value(dev, "hoge", &val)); |
| 912 |
ASSERT_STREQ(val, "foo"); |
| 913 |
|
| 914 |
/* remove it with an empty string */ |
| 915 |
ASSERT_OK(device_add_property(dev, "hoge", "")); |
| 916 |
ASSERT_ERROR(sd_device_get_property_value(dev, "hoge", &val), ENOENT); |
| 917 |
|
| 918 |
/* check internal property (starting with dot) */ |
| 919 |
ASSERT_OK(device_add_property(dev, ".hoge", "baz")); |
| 920 |
ASSERT_OK(sd_device_get_property_value(dev, ".hoge", &val)); |
| 921 |
ASSERT_STREQ(val, "baz"); |
| 922 |
|
| 923 |
/* refuse invalid property names */ |
| 924 |
ASSERT_ERROR(device_add_property(dev, "hoge-hoge", "aaa"), EINVAL); |
| 925 |
ASSERT_ERROR(device_add_property(dev, "hoge=hoge", "aaa"), EINVAL); |
| 926 |
ASSERT_ERROR(device_add_property(dev, "hoge hoge", "aaa"), EINVAL); |
| 927 |
ASSERT_ERROR(device_add_property(dev, "hoge\nhoge", "aaa"), EINVAL); |
| 928 |
ASSERT_ERROR(device_add_property(dev, "hoge\rhoge", "aaa"), EINVAL); |
| 929 |
ASSERT_ERROR(device_add_property(dev, "hoge\thoge", "aaa"), EINVAL); |
| 930 |
|
| 931 |
/* refuse invalid property values */ |
| 932 |
ASSERT_ERROR(device_add_property(dev, "hoge", "aaa\naaa"), EINVAL); |
| 933 |
ASSERT_ERROR(device_add_property(dev, "hoge", "aaa\raaa"), EINVAL); |
| 934 |
ASSERT_ERROR(device_add_property(dev, "hoge", "aaa\taaa"), EINVAL); |
| 935 |
} |
| 936 |
|
| 937 |
static int intro(void) { |
| 938 |
int r; |
| 939 |
|
| 940 |
if (path_is_mount_point("/sys") <= 0) |
| 941 |
return log_tests_skipped("/sys/ is not mounted"); |
| 942 |
|
| 943 |
r = dlopen_libmount(LOG_DEBUG); |
| 944 |
if (r < 0) |
| 945 |
return log_tests_skipped("libmount not available."); |
| 946 |
|
| 947 |
return EXIT_SUCCESS; |
| 948 |
} |
| 949 |
|
| 950 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |