blob: a5d8a7bb0e5503183c17b685077ab6a254a158a2 [file] [log] [blame]
Narayan Kamatha5ace892017-01-06 15:10:02 +00001/*
2 * Copyright 2017 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 <string>
Lorenzo Colitti173da322017-02-05 01:56:40 +090018#include <fcntl.h>
Lorenzo Colittia701afb2017-02-28 01:47:11 +090019#include <sys/socket.h>
20#include <sys/un.h>
Narayan Kamatha5ace892017-01-06 15:10:02 +000021
22#include <gtest/gtest.h>
23
Lorenzo Colitti173da322017-02-05 01:56:40 +090024#define LOG_TAG "IptablesRestoreControllerTest"
25#include <cutils/log.h>
26#include <android-base/stringprintf.h>
Lorenzo Colitticd283772017-01-31 19:00:49 +090027#include <android-base/strings.h>
Lorenzo Colitti173da322017-02-05 01:56:40 +090028
Narayan Kamatha5ace892017-01-06 15:10:02 +000029#include "IptablesRestoreController.h"
30#include "NetdConstants.h"
31
Lorenzo Colittia701afb2017-02-28 01:47:11 +090032#define XTABLES_LOCK "@xtables"
33
Lorenzo Colitticd283772017-01-31 19:00:49 +090034using android::base::Join;
Lorenzo Colitti173da322017-02-05 01:56:40 +090035using android::base::StringPrintf;
Narayan Kamatha5ace892017-01-06 15:10:02 +000036
Lorenzo Colitti173da322017-02-05 01:56:40 +090037class IptablesRestoreControllerTest : public ::testing::Test {
38public:
39 IptablesRestoreController con;
Lorenzo Colittia701afb2017-02-28 01:47:11 +090040 int mDefaultMaxRetries = con.MAX_RETRIES;
41 int mDefaultPollTimeoutMs = con.POLL_TIMEOUT_MS;
42 int mIptablesLock = -1;
43 std::string mChainName;
44
45 void SetUp() {
46 ASSERT_EQ(0, createTestChain());
47 }
48
49 void TearDown() {
50 con.MAX_RETRIES = mDefaultMaxRetries;
51 con.POLL_TIMEOUT_MS = mDefaultPollTimeoutMs;
52 deleteTestChain();
53 }
Lorenzo Colitti173da322017-02-05 01:56:40 +090054
55 pid_t getIpRestorePid(const IptablesRestoreController::IptablesProcessType type) {
56 return con.getIpRestorePid(type);
57 };
58
59 void expectNoIptablesRestoreProcess(pid_t pid) {
60 // We can't readlink /proc/PID/exe, because zombie processes don't have it.
61 // Parse /proc/PID/stat instead.
62 std::string statPath = StringPrintf("/proc/%d/stat", pid);
63 int fd = open(statPath.c_str(), O_RDONLY);
64 if (fd == -1) {
65 // ENOENT means the process is gone (expected).
66 ASSERT_EQ(errno, ENOENT)
67 << "Unexpected error opening " << statPath << ": " << strerror(errno);
68 return;
69 }
70
71 // If the PID exists, it's possible (though very unlikely) that the PID was reused. Check the
72 // binary name as well, to ensure the test isn't flaky.
73 char statBuf[1024];
74 ASSERT_NE(-1, read(fd, statBuf, sizeof(statBuf)))
75 << "Could not read from " << statPath << ": " << strerror(errno);
76 close(fd);
77
78 std::string statString(statBuf);
79 EXPECT_FALSE(statString.find("iptables-restor") || statString.find("ip6tables-resto"))
80 << "Previous iptables-restore pid " << pid << " still alive: " << statString;
81 }
Lorenzo Colittia701afb2017-02-28 01:47:11 +090082
83 int createTestChain() {
84 mChainName = StringPrintf("netd_unit_test_%u", arc4random_uniform(10000)).c_str();
85
86 // Create a chain to list.
87 std::vector<std::string> createCommands = {
88 "*filter",
89 StringPrintf(":%s -", mChainName.c_str()),
90 StringPrintf("-A %s -j RETURN", mChainName.c_str()),
91 "COMMIT",
92 ""
93 };
94
95 int ret = con.execute(V4V6, Join(createCommands, "\n"), nullptr);
96 if (ret) mChainName = "";
97 return ret;
98 }
99
100 void deleteTestChain() {
101 std::vector<std::string> deleteCommands = {
102 "*filter",
103 StringPrintf(":%s -", mChainName.c_str()), // Flush chain (otherwise we can't delete it).
104 StringPrintf("-X %s", mChainName.c_str()), // Delete it.
105 "COMMIT",
106 ""
107 };
108 con.execute(V4V6, Join(deleteCommands, "\n"), nullptr);
109 mChainName = "";
110 }
111
112 int acquireIptablesLock() {
113 mIptablesLock = socket(AF_UNIX, SOCK_STREAM, 0);
114 if (mIptablesLock == -1) {
115 return -errno;
116 }
117 sockaddr_un sun = { AF_UNIX, XTABLES_LOCK };
118 sun.sun_path[0] = '\0';
119 size_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(XTABLES_LOCK) - 1;
120 if (int ret = bind(mIptablesLock, reinterpret_cast<sockaddr *>(&sun), len) == -1) {
121 ret = -errno;
122 close(mIptablesLock);
123 return ret;
124 }
125 return 0;
126 }
127
128 void releaseIptablesLock() {
129 if (mIptablesLock != -1) {
130 close(mIptablesLock);
131 }
132 }
133
134 void setRetryParameters(int maxRetries, int pollTimeoutMs) {
135 con.MAX_RETRIES = maxRetries;
136 con.POLL_TIMEOUT_MS = pollTimeoutMs;
137 }
Narayan Kamatha5ace892017-01-06 15:10:02 +0000138};
139
Lorenzo Colitti173da322017-02-05 01:56:40 +0900140TEST_F(IptablesRestoreControllerTest, TestBasicCommand) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900141 std::string output;
142
143 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr));
Lorenzo Colitti173da322017-02-05 01:56:40 +0900144
145 pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS);
146 pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS);
147
Lorenzo Colitticd283772017-01-31 19:00:49 +0900148 EXPECT_EQ(0, con.execute(IptablesTarget::V6, "#Test\n", nullptr));
149 EXPECT_EQ(0, con.execute(IptablesTarget::V4, "#Test\n", nullptr));
150
151 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output));
152 EXPECT_EQ("#Test\n#Test\n", output); // One for IPv4 and one for IPv6.
Lorenzo Colitti173da322017-02-05 01:56:40 +0900153
154 // Check the PIDs are the same as they were before. If they're not, the child processes were
155 // restarted, which causes a 30-60ms delay.
156 EXPECT_EQ(pid4, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS));
157 EXPECT_EQ(pid6, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS));
Narayan Kamatha5ace892017-01-06 15:10:02 +0000158}
159
Lorenzo Colitti173da322017-02-05 01:56:40 +0900160TEST_F(IptablesRestoreControllerTest, TestRestartOnMalformedCommand) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900161 std::string buffer;
Lorenzo Colitti173da322017-02-05 01:56:40 +0900162 for (int i = 0; i < 50; i++) {
163 IptablesTarget target = (IptablesTarget) (i % 3);
Lorenzo Colitticd283772017-01-31 19:00:49 +0900164 std::string *output = (i % 2) ? &buffer : nullptr;
165 ASSERT_EQ(-1, con.execute(target, "malformed command\n", output)) <<
Lorenzo Colitti173da322017-02-05 01:56:40 +0900166 "Malformed command did not fail at iteration " << i;
Lorenzo Colitticd283772017-01-31 19:00:49 +0900167 ASSERT_EQ(0, con.execute(target, "#Test\n", output)) <<
Lorenzo Colitti173da322017-02-05 01:56:40 +0900168 "No-op command did not succeed at iteration " << i;
169 }
170}
171
172TEST_F(IptablesRestoreControllerTest, TestRestartOnProcessDeath) {
Lorenzo Colitticd283772017-01-31 19:00:49 +0900173 std::string output;
174
Lorenzo Colitti173da322017-02-05 01:56:40 +0900175 // Run a command to ensure that the processes are running.
Lorenzo Colitticd283772017-01-31 19:00:49 +0900176 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", &output));
Lorenzo Colitti173da322017-02-05 01:56:40 +0900177
178 pid_t pid4 = getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS);
179 pid_t pid6 = getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS);
180
181 ASSERT_EQ(0, kill(pid4, 0)) << "iptables-restore pid " << pid4 << " does not exist";
182 ASSERT_EQ(0, kill(pid6, 0)) << "ip6tables-restore pid " << pid6 << " does not exist";
183 ASSERT_EQ(0, kill(pid4, SIGTERM)) << "Failed to send SIGTERM to iptables-restore pid " << pid4;
184 ASSERT_EQ(0, kill(pid6, SIGTERM)) << "Failed to send SIGTERM to ip6tables-restore pid " << pid6;
185
186 // Wait 100ms for processes to terminate.
187 TEMP_FAILURE_RETRY(usleep(100 * 1000));
188
189 // Ensure that running a new command properly restarts the processes.
Lorenzo Colitticd283772017-01-31 19:00:49 +0900190 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, "#Test\n", nullptr));
Lorenzo Colitti173da322017-02-05 01:56:40 +0900191 EXPECT_NE(pid4, getIpRestorePid(IptablesRestoreController::IPTABLES_PROCESS));
192 EXPECT_NE(pid6, getIpRestorePid(IptablesRestoreController::IP6TABLES_PROCESS));
193
194 // Check there are no zombies.
195 expectNoIptablesRestoreProcess(pid4);
196 expectNoIptablesRestoreProcess(pid6);
Narayan Kamatha5ace892017-01-06 15:10:02 +0000197}
Lorenzo Colitticd283772017-01-31 19:00:49 +0900198
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900199TEST_F(IptablesRestoreControllerTest, TestCommandTimeout) {
200 // Don't wait 10 seconds for this test to fail.
201 setRetryParameters(3, 50);
Lorenzo Colitticd283772017-01-31 19:00:49 +0900202
203 // Expected contents of the chain.
204 std::vector<std::string> expectedLines = {
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900205 StringPrintf("Chain %s (0 references)", mChainName.c_str()),
Lorenzo Colitticd283772017-01-31 19:00:49 +0900206 "target prot opt source destination ",
207 "RETURN all -- 0.0.0.0/0 0.0.0.0/0 ",
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900208 StringPrintf("Chain %s (0 references)", mChainName.c_str()),
Lorenzo Colitticd283772017-01-31 19:00:49 +0900209 "target prot opt source destination ",
210 "RETURN all ::/0 ::/0 ",
211 ""
212 };
213 std::string expected = Join(expectedLines, "\n");
214
Lorenzo Colitticd283772017-01-31 19:00:49 +0900215 std::vector<std::string> listCommands = {
216 "*filter",
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900217 StringPrintf("-n -L %s", mChainName.c_str()), // List chain.
Lorenzo Colitticd283772017-01-31 19:00:49 +0900218 "COMMIT",
219 ""
220 };
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900221 std::string commandString = Join(listCommands, "\n");
Lorenzo Colitticd283772017-01-31 19:00:49 +0900222 std::string output;
Lorenzo Colittia701afb2017-02-28 01:47:11 +0900223
224 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, commandString, &output));
225 EXPECT_EQ(expected, output);
226
227 ASSERT_EQ(0, acquireIptablesLock());
228 EXPECT_EQ(-1, con.execute(IptablesTarget::V4V6, commandString, &output));
229 EXPECT_EQ(-1, con.execute(IptablesTarget::V4V6, commandString, &output));
230 releaseIptablesLock();
231
232 EXPECT_EQ(0, con.execute(IptablesTarget::V4V6, commandString, &output));
Lorenzo Colitticd283772017-01-31 19:00:49 +0900233 EXPECT_EQ(expected, output);
234}