Skip to content

Commit d7836a3

Browse files
committed
Mock only whitelisted libraries.
Also fix bug inside |fake::connect()|.
1 parent 70596e3 commit d7836a3

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

build/config/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ config("base") {
1212
]
1313

1414
cflags_cc = [
15+
"-fdef-sized-delete",
1516
"-std=c++1y",
1617
]
1718

mock-network.config

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define __clang__
2+
#define __cplusplus 20130101L
3+
4+
#define cxx_exceptions 1
5+
#define __has_feature(x) (x != 1)

src/mock_network/functions.cc

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
5454

5555
if (!socket.SetState(Socket::CONNECTING, addr, addrlen)) {
5656
// Assume |errno| is already set by |Socket::SetState()|.
57+
if (Connection::result_ != 0) {
58+
errno = Connection::result_;
59+
}
5760
return -1;
5861
}
5962

src/mock_network/mock.cc

+6-10
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,12 @@ LinkList GetLoadedModules() {
4242
return std::move(modules);
4343
}
4444

45-
const char* blacklist[] = {
46-
// Old libstdc++ doesn't support substring regex matching.
47-
".*ld-linux-x86-64\\.so.*", ".*libc\\.so.*", ".*libpthread\\.so.*",
48-
};
49-
5045
Map<String, void*> handles;
5146

5247
} // namespace
5348

5449
// static
55-
bool Mock::Enable(String* error) {
50+
bool Mock::Enable(const List<String>& whitelist, String* error) {
5651
UniqueLock lock(mutex_);
5752

5853
if (enabled_) {
@@ -67,14 +62,14 @@ bool Mock::Enable(String* error) {
6762
LinkList modules = GetLoadedModules();
6863

6964
for (const auto& handle : modules) {
70-
bool in_blacklist = false;
71-
for (const auto* entry : blacklist) {
65+
bool in_whitelist = false;
66+
for (const auto& entry : whitelist) {
7267
if (std::regex_match(handle->l_name, std::regex(entry))) {
73-
in_blacklist = true;
68+
in_whitelist = true;
7469
break;
7570
}
7671
}
77-
if (in_blacklist) {
72+
if (!in_whitelist) {
7873
continue;
7974
}
8075

@@ -151,6 +146,7 @@ bool Mock::Disable(String* error) {
151146

152147
// NOTE: |elf_hook()| uses the |close()| internally, so we need to be able to
153148
// call the |CallOriginal()|, which asserts the |enabled_|.
149+
// FIXME: looks like deprecated NOTE.
154150
enabled_ = false;
155151

156152
return true;

src/mock_network/mock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Mock {
1717
return reinterpret_cast<R (*)(Args...)>(original->second)(args...);
1818
}
1919

20-
static bool Enable(String* error = nullptr);
20+
static bool Enable(const List<String>& whitelist, String* error = nullptr);
2121
static bool Disable(String* error = nullptr);
2222

2323
private:

src/mock_network/mock_test.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace mock_network {
1212

1313
TEST(MockNetworkTest, Socket) {
1414
String error;
15-
ASSERT_TRUE(Mock::Enable(&error)) << error;
15+
ASSERT_TRUE(Mock::Enable({".*libtest_library\\.so"}, &error)) << error;
1616

1717
int socket = test::socket(AF_UNIX, SOCK_STREAM, 0);
1818
EXPECT_NE(-1, socket);
@@ -43,7 +43,7 @@ TEST(MockNetworkTest, Socket) {
4343

4444
TEST(MockNetworkTest, Connect) {
4545
String error;
46-
ASSERT_TRUE(Mock::Enable(&error)) << error;
46+
ASSERT_TRUE(Mock::Enable({".*libtest_library\\.so"}, &error)) << error;
4747

4848
// Unknown socket.
4949
EXPECT_EQ(-1, test::connect(1, nullptr, 0));
@@ -84,7 +84,7 @@ TEST(MockNetworkTest, Connect) {
8484

8585
TEST(MockNetworkTest, Bind) {
8686
String error;
87-
ASSERT_TRUE(Mock::Enable(&error)) << error;
87+
ASSERT_TRUE(Mock::Enable({".*libtest_library\\.so"}, &error)) << error;
8888

8989
// Unknown socket.
9090
EXPECT_EQ(-1, test::bind(1, nullptr, 0));

0 commit comments

Comments
 (0)