blob: fca92ce48f8903705c1452608f7d1cdc2de10933 [file] [log] [blame]
Josh Gao97271922019-11-06 13:15:00 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dirent.h>
20#include <err.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdlib.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#if defined(__BIONIC__)
Josh Gao75962502020-01-28 13:24:33 -080028#include "platform/bionic/fdtrack.h"
Josh Gao97271922019-11-06 13:15:00 -080029#endif
30
31#include <vector>
32
33#include <android-base/unique_fd.h>
34
35#if defined(__BIONIC__)
36std::vector<android_fdtrack_event> FdtrackRun(void (*func)()) {
37 // Each bionic test is run in separate process, so we can safely use a static here.
38 static std::vector<android_fdtrack_event> events;
39 events.clear();
40
41 android_fdtrack_hook_t previous = nullptr;
42 android_fdtrack_hook_t hook = [](android_fdtrack_event* event) {
43 events.push_back(*event);
44 };
45
46 if (!android_fdtrack_compare_exchange_hook(&previous, hook)) {
47 errx(1, "failed to exchange hook: previous hook was %p", previous);
48 }
49
50 if (previous) {
51 errx(1, "hook was already registered?");
52 abort();
53 }
54
55 func();
56
57 if (!android_fdtrack_compare_exchange_hook(&hook, nullptr)) {
58 errx(1, "failed to reset hook");
59 }
60
61 return std::move(events);
62}
63#endif
64
65TEST(fdtrack, open) {
66#if defined(__BIONIC__)
67 static int fd = -1;
68 auto events = FdtrackRun([]() { fd = open("/dev/null", O_WRONLY | O_CLOEXEC); });
69 ASSERT_NE(-1, fd);
70 ASSERT_EQ(1U, events.size());
71 ASSERT_EQ(fd, events[0].fd);
72 ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[0].type);
73 ASSERT_STREQ("open", events[0].data.create.function_name);
74#endif
75}
76
77TEST(fdtrack, close) {
78#if defined(__BIONIC__)
79 static int fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
80 ASSERT_NE(-1, fd);
81
82 auto events = FdtrackRun([]() { close(fd); });
83 ASSERT_EQ(1U, events.size());
84 ASSERT_EQ(fd, events[0].fd);
85 ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CLOSE, events[0].type);
86#endif
87}
88
89TEST(fdtrack, enable_disable) {
90#if defined(__BIONIC__)
91 static int fd1 = -1;
92 static int fd2 = -1;
93 static int fd3 = -1;
94
95 auto events = FdtrackRun([]() {
96 if (!android_fdtrack_get_enabled()) {
97 errx(1, "fdtrack is disabled");
98 }
99 fd1 = open("/dev/null", O_WRONLY | O_CLOEXEC);
100 android_fdtrack_set_enabled(false);
101 fd2 = open("/dev/null", O_WRONLY | O_CLOEXEC);
102 android_fdtrack_set_enabled(true);
103 fd3 = open("/dev/null", O_WRONLY | O_CLOEXEC);
104 });
105
106 if (fd1 == -1 || fd2 == -1 || fd3 == -1) {
107 errx(1, "failed to open /dev/null");
108 }
109
110 ASSERT_EQ(2U, events.size());
111
112 ASSERT_EQ(fd1, events[0].fd);
113 ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[0].type);
114 ASSERT_STREQ("open", events[0].data.create.function_name);
115
116 ASSERT_EQ(fd3, events[1].fd);
117 ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[1].type);
118 ASSERT_STREQ("open", events[1].data.create.function_name);
119#endif
120}