Repository navigation
Expand file tree
/
Copy pathutils.cpp
More file actions
122 lines (105 loc) · 5.22 KB
/
Copy pathutils.cpp
File metadata and controls
122 lines (105 loc) · 5.22 KB
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
#include "utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
namespace bpftrace {
namespace test {
namespace utils {
TEST(utils, split_string)
{
std::vector<std::string> tokens_empty = {};
std::vector<std::string> tokens_one_empty = {""};
std::vector<std::string> tokens_two_empty = {"", ""};
std::vector<std::string> tokens_f = {"", "f"};
std::vector<std::string> tokens_foo_bar = {"foo", "bar"};
std::vector<std::string> tokens_empty_foo_bar = {"", "foo", "bar"};
std::vector<std::string> tokens_empty_foo_empty_bar = {"", "foo", "", "bar"};
std::vector<std::string> tokens_empty_foo_bar_biz = {"", "foo", "bar", "biz"};
EXPECT_EQ(split_string("", '-'), tokens_empty);
EXPECT_EQ(split_string("-", '-'), tokens_one_empty);
EXPECT_EQ(split_string("--", '-'), tokens_two_empty);
EXPECT_EQ(split_string("-f-", '-'), tokens_f);
EXPECT_EQ(split_string("-foo-bar-", '-'), tokens_empty_foo_bar);
EXPECT_EQ(split_string("-foo--bar-", '-'), tokens_empty_foo_empty_bar);
EXPECT_EQ(split_string("-foo-bar-biz-", '-'), tokens_empty_foo_bar_biz);
EXPECT_EQ(split_string("-foo-bar", '-'), tokens_empty_foo_bar);
EXPECT_EQ(split_string("foo-bar-", '-'), tokens_foo_bar);
EXPECT_EQ(split_string("foo-bar", '-'), tokens_foo_bar);
}
TEST(utils, wildcard_match)
{
std::vector<std::string> tokens_not = {"not"};
std::vector<std::string> tokens_bar = {"bar"};
std::vector<std::string> tokens_bar_not = {"bar", "not"};
std::vector<std::string> tokens_foo = {"foo"};
std::vector<std::string> tokens_biz = {"biz"};
std::vector<std::string> tokens_foo_biz = {"foo", "biz"};
// start: true, end: true
EXPECT_EQ(wildcard_match("foobarbiz", tokens_not, true, true), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar, true, true), true);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar_not, true, true), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo, true, true), true);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_biz, true, true), true);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo_biz, true, true), true);
// start: false, end: true
EXPECT_EQ(wildcard_match("foobarbiz", tokens_not, false, true), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar, false, true), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar_not, false, true), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo, false, true), true);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_biz, false, true), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo_biz, false, true), true);
// start: true, end: false
EXPECT_EQ(wildcard_match("foobarbiz", tokens_not, true, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar, true, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar_not, true, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo, true, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_biz, true, false), true);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo_biz, true, false), true);
// start: false, end: false
EXPECT_EQ(wildcard_match("foobarbiz", tokens_not, false, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar, false, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_bar_not, false, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo, false, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_biz, false, false), false);
EXPECT_EQ(wildcard_match("foobarbiz", tokens_foo_biz, false, false), true);
}
static void symlink_test_binary(const std::string& destination)
{
if (symlink("/proc/self/exe", destination.c_str()))
{
throw std::runtime_error("Couldn't symlink /proc/self/exe to " +
destination + ": " + strerror(errno));
}
}
TEST(utils, resolve_binary_path)
{
std::string path = "/tmp/bpftrace-test-utils-XXXXXX";
if (::mkdtemp(&path[0]) == nullptr) {
throw std::runtime_error("creating temporary path for tests failed");
}
// We need real elf executables, linking test binary allows us to do that
// without additional dependencies.
symlink_test_binary(path + "/executable");
symlink_test_binary(path + "/executable2");
int fd;
fd = open((path + "/nonexecutable").c_str(), O_CREAT, S_IRUSR); close(fd);
fd = open((path + "/nonexecutable2").c_str(), O_CREAT, S_IRUSR); close(fd);
std::vector<std::string> paths_empty = {};
std::vector<std::string> paths_one_executable = {path + "/executable"};
std::vector<std::string> paths_all_executables = {path + "/executable", path + "/executable2"};
EXPECT_EQ(resolve_binary_path(path + "/does/not/exist"), paths_empty);
EXPECT_EQ(resolve_binary_path(path + "/does/not/exist*"), paths_empty);
EXPECT_EQ(resolve_binary_path(path + "/nonexecutable"), paths_empty);
EXPECT_EQ(resolve_binary_path(path + "/nonexecutable*"), paths_empty);
EXPECT_EQ(resolve_binary_path(path + "/executable"), paths_one_executable);
EXPECT_EQ(resolve_binary_path(path + "/executable*"), paths_all_executables);
EXPECT_EQ(resolve_binary_path(path + "/*executable*"), paths_all_executables);
exec_system(("rm -rf " + path).c_str());
}
} // namespace utils
} // namespace test
} // namespace bpftrace