2017-04-06 06:58:31 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2017-04-05 08:53:45 +08:00
|
|
|
template <std::vector<std::string> (*Function)(uevent*)>
|
2017-04-06 06:58:31 +08:00
|
|
|
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()); });
|
|
|
|
|
2017-04-05 08:53:45 +08:00
|
|
|
std::vector<std::string> result = Function(uevent);
|
2017-04-06 06:58:31 +08:00
|
|
|
|
|
|
|
auto expected_size = expected_links.size();
|
2017-04-05 08:53:45 +08:00
|
|
|
ASSERT_EQ(expected_size, result.size());
|
|
|
|
if (expected_size == 0) return;
|
|
|
|
|
|
|
|
// Explicitly iterate so the results are visible if a failure occurs
|
|
|
|
for (unsigned int i = 0; i < expected_size; ++i) {
|
|
|
|
EXPECT_EQ(expected_links[i], result[i]);
|
2017-04-06 06:58:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:53:45 +08:00
|
|
|
TEST(devices, get_block_device_symlinks_pci_bad_format) {
|
|
|
|
const char* platform_device = "/devices/do/not/match";
|
|
|
|
uevent uevent = {
|
|
|
|
.path = "/devices/pci//mmcblk0", .partition_name = nullptr, .partition_num = -1,
|
|
|
|
};
|
|
|
|
std::vector<std::string> expected_result{};
|
|
|
|
|
|
|
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
|
|
|
}
|
|
|
|
|
2017-04-06 06:58:31 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:53:45 +08:00
|
|
|
TEST(devices, get_block_device_symlinks_vbd_bad_format) {
|
|
|
|
const char* platform_device = "/devices/do/not/match";
|
|
|
|
uevent uevent = {
|
|
|
|
.path = "/devices/vbd-/mmcblk0", .partition_name = nullptr, .partition_num = -1,
|
|
|
|
};
|
|
|
|
std::vector<std::string> expected_result{};
|
|
|
|
|
|
|
|
test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
|
|
|
|
}
|
|
|
|
|
2017-04-06 06:58:31 +08:00
|
|
|
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;
|
2017-04-05 08:53:45 +08:00
|
|
|
sanitize_partition_name(&empty);
|
2017-04-06 06:58:31 +08:00
|
|
|
EXPECT_EQ(0u, empty.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(devices, sanitize_allgood) {
|
|
|
|
std::string good =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789"
|
|
|
|
"_-.";
|
|
|
|
std::string good_copy = good;
|
2017-04-05 08:53:45 +08:00
|
|
|
sanitize_partition_name(&good);
|
2017-04-06 06:58:31 +08:00
|
|
|
EXPECT_EQ(good_copy, good);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(devices, sanitize_somebad) {
|
|
|
|
std::string string = "abc!@#$%^&*()";
|
2017-04-05 08:53:45 +08:00
|
|
|
sanitize_partition_name(&string);
|
2017-04-06 06:58:31 +08:00
|
|
|
EXPECT_EQ("abc__________", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(devices, sanitize_allbad) {
|
|
|
|
std::string string = "!@#$%^&*()";
|
2017-04-05 08:53:45 +08:00
|
|
|
sanitize_partition_name(&string);
|
2017-04-06 06:58:31 +08:00
|
|
|
EXPECT_EQ("__________", string);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(devices, sanitize_onebad) {
|
|
|
|
std::string string = ")";
|
2017-04-05 08:53:45 +08:00
|
|
|
sanitize_partition_name(&string);
|
2017-04-06 06:58:31 +08:00
|
|
|
EXPECT_EQ("_", string);
|
|
|
|
}
|