Merge "ueventd: Write tests for the get_*_symlinks() functions" am: e7fbd6a2b4
am: e6b70f5913
Change-Id: I517d4f966d9d8f743cb17824e78e0cfd63a048f9
This commit is contained in:
commit
9cf2cd358c
6 changed files with 302 additions and 33 deletions
|
|
@ -62,6 +62,7 @@ LOCAL_SRC_FILES:= \
|
||||||
action.cpp \
|
action.cpp \
|
||||||
capabilities.cpp \
|
capabilities.cpp \
|
||||||
descriptors.cpp \
|
descriptors.cpp \
|
||||||
|
devices.cpp \
|
||||||
import_parser.cpp \
|
import_parser.cpp \
|
||||||
init_parser.cpp \
|
init_parser.cpp \
|
||||||
log.cpp \
|
log.cpp \
|
||||||
|
|
@ -81,7 +82,6 @@ LOCAL_CPPFLAGS := $(init_cflags)
|
||||||
LOCAL_SRC_FILES:= \
|
LOCAL_SRC_FILES:= \
|
||||||
bootchart.cpp \
|
bootchart.cpp \
|
||||||
builtins.cpp \
|
builtins.cpp \
|
||||||
devices.cpp \
|
|
||||||
init.cpp \
|
init.cpp \
|
||||||
keychords.cpp \
|
keychords.cpp \
|
||||||
property_service.cpp \
|
property_service.cpp \
|
||||||
|
|
@ -138,6 +138,7 @@ include $(BUILD_EXECUTABLE)
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
LOCAL_MODULE := init_tests
|
LOCAL_MODULE := init_tests
|
||||||
LOCAL_SRC_FILES := \
|
LOCAL_SRC_FILES := \
|
||||||
|
devices_test.cpp \
|
||||||
init_parser_test.cpp \
|
init_parser_test.cpp \
|
||||||
property_service_test.cpp \
|
property_service_test.cpp \
|
||||||
util_test.cpp \
|
util_test.cpp \
|
||||||
|
|
|
||||||
|
|
@ -286,8 +286,7 @@ out:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_platform_device(const char *path)
|
void add_platform_device(const char* path) {
|
||||||
{
|
|
||||||
int path_len = strlen(path);
|
int path_len = strlen(path);
|
||||||
struct platform_node *bus;
|
struct platform_node *bus;
|
||||||
const char *name = path;
|
const char *name = path;
|
||||||
|
|
@ -329,8 +328,7 @@ static struct platform_node *find_platform_device(const char *path)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void remove_platform_device(const char *path)
|
void remove_platform_device(const char* path) {
|
||||||
{
|
|
||||||
struct listnode *node;
|
struct listnode *node;
|
||||||
struct platform_node *bus;
|
struct platform_node *bus;
|
||||||
|
|
||||||
|
|
@ -473,8 +471,7 @@ static void parse_event(const char *msg, struct uevent *uevent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **get_character_device_symlinks(struct uevent *uevent)
|
char** get_character_device_symlinks(struct uevent* uevent) {
|
||||||
{
|
|
||||||
const char *parent;
|
const char *parent;
|
||||||
const char *slash;
|
const char *slash;
|
||||||
char **links;
|
char **links;
|
||||||
|
|
@ -526,8 +523,24 @@ err:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **get_block_device_symlinks(struct uevent *uevent)
|
// replaces any unacceptable characters with '_', the
|
||||||
{
|
// length of the resulting string is equal to the input string
|
||||||
|
void sanitize_partition_name(char* s) {
|
||||||
|
const char* accept =
|
||||||
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
"0123456789"
|
||||||
|
"_-.";
|
||||||
|
|
||||||
|
if (!s) return;
|
||||||
|
|
||||||
|
while (*s) {
|
||||||
|
s += strspn(s, accept);
|
||||||
|
if (*s) *s++ = '_';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char** get_block_device_symlinks(struct uevent* uevent) {
|
||||||
const char *device;
|
const char *device;
|
||||||
struct platform_node *pdev;
|
struct platform_node *pdev;
|
||||||
const char *slash;
|
const char *slash;
|
||||||
|
|
@ -562,7 +575,7 @@ static char **get_block_device_symlinks(struct uevent *uevent)
|
||||||
|
|
||||||
if (uevent->partition_name) {
|
if (uevent->partition_name) {
|
||||||
p = strdup(uevent->partition_name);
|
p = strdup(uevent->partition_name);
|
||||||
sanitize(p);
|
sanitize_partition_name(p);
|
||||||
if (strcmp(uevent->partition_name, p)) {
|
if (strcmp(uevent->partition_name, p)) {
|
||||||
LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
|
LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,4 +55,11 @@ extern int add_dev_perms(const char *name, const char *attr,
|
||||||
unsigned short wildcard);
|
unsigned short wildcard);
|
||||||
int get_device_fd();
|
int get_device_fd();
|
||||||
|
|
||||||
#endif /* _INIT_DEVICES_H */
|
// Exposed for testing
|
||||||
|
void add_platform_device(const char* path);
|
||||||
|
void remove_platform_device(const char* path);
|
||||||
|
char** get_character_device_symlinks(uevent* uevent);
|
||||||
|
char** get_block_device_symlinks(struct uevent* uevent);
|
||||||
|
void sanitize_partition_name(char* s);
|
||||||
|
|
||||||
|
#endif /* _INIT_DEVICES_H */
|
||||||
|
|
|
||||||
270
init/devices_test.cpp
Normal file
270
init/devices_test.cpp
Normal file
|
|
@ -0,0 +1,270 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2017 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "devices.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <android-base/scopeguard.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
template <char** (*Function)(uevent*)>
|
||||||
|
void test_get_symlinks(const std::string& platform_device_name, uevent* uevent,
|
||||||
|
const std::vector<std::string> expected_links) {
|
||||||
|
add_platform_device(platform_device_name.c_str());
|
||||||
|
auto platform_device_remover = android::base::make_scope_guard(
|
||||||
|
[&platform_device_name]() { remove_platform_device(platform_device_name.c_str()); });
|
||||||
|
|
||||||
|
char** result = Function(uevent);
|
||||||
|
auto result_freer = android::base::make_scope_guard([result]() {
|
||||||
|
if (result) {
|
||||||
|
for (int i = 0; result[i]; i++) {
|
||||||
|
free(result[i]);
|
||||||
|
}
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
auto expected_size = expected_links.size();
|
||||||
|
if (expected_size == 0) {
|
||||||
|
ASSERT_EQ(nullptr, result);
|
||||||
|
} else {
|
||||||
|
ASSERT_NE(nullptr, result);
|
||||||
|
// First assert size is equal, so we don't overrun expected_links
|
||||||
|
unsigned int size = 0;
|
||||||
|
while (result[size]) ++size;
|
||||||
|
ASSERT_EQ(expected_size, size);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < size; ++i) {
|
||||||
|
EXPECT_EQ(expected_links[i], result[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_success) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name/usb/usb_device/name/tty2-1:1.0",
|
||||||
|
.subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{"/dev/usb/ttyname"};
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_no_pdev_match) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/device/name/tty2-1:1.0", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_nothing_after_platform_device) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_no_usb_found) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name/bad/bad/", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_no_roothub) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name/usb/", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_no_usb_device) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name/usb/usb_device/", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_no_final_slash) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name/usb/usb_device/name", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_character_device_symlinks_no_final_name) {
|
||||||
|
const char* platform_device = "/devices/platform/some_device_name";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/platform/some_device_name/usb/usb_device//", .subsystem = "tty",
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_success_platform) {
|
||||||
|
// These are actual paths from bullhead
|
||||||
|
const char* platform_device = "/devices/soc.0/f9824900.sdhci";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0",
|
||||||
|
.partition_name = nullptr,
|
||||||
|
.partition_num = -1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0"};
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_success_platform_with_partition) {
|
||||||
|
// These are actual paths from bullhead
|
||||||
|
const char* platform_device = "/devices/soc.0/f9824900.sdhci";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
|
||||||
|
.partition_name = "modem",
|
||||||
|
.partition_num = 1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem",
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
|
||||||
|
};
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_success_platform_with_partition_only_num) {
|
||||||
|
const char* platform_device = "/devices/soc.0/f9824900.sdhci";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
|
||||||
|
.partition_name = nullptr,
|
||||||
|
.partition_num = 1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
|
||||||
|
};
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_success_platform_with_partition_only_name) {
|
||||||
|
const char* platform_device = "/devices/soc.0/f9824900.sdhci";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
|
||||||
|
.partition_name = "modem",
|
||||||
|
.partition_num = -1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem",
|
||||||
|
"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
|
||||||
|
};
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_success_pci) {
|
||||||
|
const char* platform_device = "/devices/do/not/match";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/pci0000:00/0000:00:1f.2/mmcblk0",
|
||||||
|
.partition_name = nullptr,
|
||||||
|
.partition_num = -1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"};
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_success_vbd) {
|
||||||
|
const char* platform_device = "/devices/do/not/match";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/vbd-1234/mmcblk0", .partition_name = nullptr, .partition_num = -1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"};
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, get_block_device_symlinks_no_matches) {
|
||||||
|
const char* platform_device = "/devices/soc.0/f9824900.sdhci";
|
||||||
|
uevent uevent = {
|
||||||
|
.path = "/devices/soc.0/not_the_device/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
|
||||||
|
.partition_name = nullptr,
|
||||||
|
.partition_num = -1,
|
||||||
|
};
|
||||||
|
std::vector<std::string> expected_result;
|
||||||
|
|
||||||
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, sanitize_null) {
|
||||||
|
sanitize_partition_name(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, sanitize_empty) {
|
||||||
|
std::string empty;
|
||||||
|
sanitize_partition_name(&empty[0]);
|
||||||
|
EXPECT_EQ(0u, empty.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, sanitize_allgood) {
|
||||||
|
std::string good =
|
||||||
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
"0123456789"
|
||||||
|
"_-.";
|
||||||
|
std::string good_copy = good;
|
||||||
|
sanitize_partition_name(&good[0]);
|
||||||
|
EXPECT_EQ(good_copy, good);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, sanitize_somebad) {
|
||||||
|
std::string string = "abc!@#$%^&*()";
|
||||||
|
sanitize_partition_name(&string[0]);
|
||||||
|
EXPECT_EQ("abc__________", string);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, sanitize_allbad) {
|
||||||
|
std::string string = "!@#$%^&*()";
|
||||||
|
sanitize_partition_name(&string[0]);
|
||||||
|
EXPECT_EQ("__________", string);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(devices, sanitize_onebad) {
|
||||||
|
std::string string = ")";
|
||||||
|
sanitize_partition_name(&string[0]);
|
||||||
|
EXPECT_EQ("_", string);
|
||||||
|
}
|
||||||
|
|
@ -235,27 +235,6 @@ int mkdir_recursive(const char *pathname, mode_t mode)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* replaces any unacceptable characters with '_', the
|
|
||||||
* length of the resulting string is equal to the input string
|
|
||||||
*/
|
|
||||||
void sanitize(char *s)
|
|
||||||
{
|
|
||||||
const char* accept =
|
|
||||||
"abcdefghijklmnopqrstuvwxyz"
|
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
"0123456789"
|
|
||||||
"_-.";
|
|
||||||
|
|
||||||
if (!s)
|
|
||||||
return;
|
|
||||||
|
|
||||||
while (*s) {
|
|
||||||
s += strspn(s, accept);
|
|
||||||
if (*s) *s++ = '_';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int wait_for_file(const char* filename, std::chrono::nanoseconds timeout) {
|
int wait_for_file(const char* filename, std::chrono::nanoseconds timeout) {
|
||||||
boot_clock::time_point timeout_time = boot_clock::now() + timeout;
|
boot_clock::time_point timeout_time = boot_clock::now() + timeout;
|
||||||
while (boot_clock::now() < timeout_time) {
|
while (boot_clock::now() < timeout_time) {
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,6 @@ std::ostream& operator<<(std::ostream& os, const Timer& t);
|
||||||
unsigned int decode_uid(const char *s);
|
unsigned int decode_uid(const char *s);
|
||||||
|
|
||||||
int mkdir_recursive(const char *pathname, mode_t mode);
|
int mkdir_recursive(const char *pathname, mode_t mode);
|
||||||
void sanitize(char *p);
|
|
||||||
int wait_for_file(const char *filename, std::chrono::nanoseconds timeout);
|
int wait_for_file(const char *filename, std::chrono::nanoseconds timeout);
|
||||||
void import_kernel_cmdline(bool in_qemu,
|
void import_kernel_cmdline(bool in_qemu,
|
||||||
const std::function<void(const std::string&, const std::string&, bool)>&);
|
const std::function<void(const std::string&, const std::string&, bool)>&);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue