summaryrefslogtreecommitdiff
path: root/server/ControllersTest.cpp
blob: 9033a1d359d7c9d4ea5641827a4e79a2a1716ba5 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright 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.
 *
 * ControllersTest.cpp - unit tests for Controllers.cpp
 */

#include <set>
#include <string>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <android-base/strings.h>

#include "Controllers.h"
#include "IptablesBaseTest.h"

using testing::ContainerEq;

namespace android {
namespace net {

class ControllersTest : public IptablesBaseTest {
  public:
    ControllersTest() {
        Controllers::execIptablesRestore = fakeExecIptablesRestore;
        Controllers::execIptablesRestoreWithOutput = fakeExecIptablesRestoreWithOutput;
    }

  protected:
    void initChildChains() { Controllers::initChildChains(); };
    std::set<std::string> findExistingChildChains(IptablesTarget a, const char* b, const char*c) {
        return Controllers::findExistingChildChains(a, b, c);
    }
};

TEST_F(ControllersTest, TestFindExistingChildChains) {
    ExpectedIptablesCommands expectedCmds = {
        { V6, "*raw\n-S PREROUTING\nCOMMIT\n" },
    };
    sIptablesRestoreOutput.push_back(
        "-P PREROUTING ACCEPT\n"
        "-A PREROUTING -j bw_raw_PREROUTING\n"
        "-A PREROUTING -j idletimer_raw_PREROUTING\n"
        "-A PREROUTING -j tetherctrl_raw_PREROUTING\n"
    );
    std::set<std::string> expectedChains = {
        "bw_raw_PREROUTING",
        "idletimer_raw_PREROUTING",
        "tetherctrl_raw_PREROUTING",
    };
    std::set<std::string> actual = findExistingChildChains(V6, "raw", "PREROUTING");
    EXPECT_THAT(expectedChains, ContainerEq(actual));
    expectIptablesRestoreCommands(expectedCmds);
}

TEST_F(ControllersTest, TestInitIptablesRules) {
    // Test what happens when we boot and there are no rules.
    ExpectedIptablesCommands expected = {
            {V4V6,
             "*filter\n"
             ":INPUT -\n"
             "-F INPUT\n"
             ":bw_INPUT -\n"
             "-A INPUT -j bw_INPUT\n"
             ":fw_INPUT -\n"
             "-A INPUT -j fw_INPUT\n"
             "COMMIT\n"},
            {V4V6,
             "*filter\n"
             ":FORWARD -\n"
             "-F FORWARD\n"
             ":oem_fwd -\n"
             "-A FORWARD -j oem_fwd\n"
             ":fw_FORWARD -\n"
             "-A FORWARD -j fw_FORWARD\n"
             ":bw_FORWARD -\n"
             "-A FORWARD -j bw_FORWARD\n"
             ":tetherctrl_FORWARD -\n"
             "-A FORWARD -j tetherctrl_FORWARD\n"
             "COMMIT\n"},
            {V4V6,
             "*raw\n"
             ":PREROUTING -\n"
             "-F PREROUTING\n"
             ":idletimer_raw_PREROUTING -\n"
             "-A PREROUTING -j idletimer_raw_PREROUTING\n"
             ":bw_raw_PREROUTING -\n"
             "-A PREROUTING -j bw_raw_PREROUTING\n"
             ":tetherctrl_raw_PREROUTING -\n"
             "-A PREROUTING -j tetherctrl_raw_PREROUTING\n"
             "COMMIT\n"},
            {V4V6,
             "*mangle\n"
             ":FORWARD -\n"
             "-F FORWARD\n"
             ":tetherctrl_mangle_FORWARD -\n"
             "-A FORWARD -j tetherctrl_mangle_FORWARD\n"
             "COMMIT\n"},
            {V4V6,
             "*mangle\n"
             ":INPUT -\n"
             "-F INPUT\n"
             ":wakeupctrl_mangle_INPUT -\n"
             "-A INPUT -j wakeupctrl_mangle_INPUT\n"
             ":routectrl_mangle_INPUT -\n"
             "-A INPUT -j routectrl_mangle_INPUT\n"
             "COMMIT\n"},
            {V4,
             "*nat\n"
             ":PREROUTING -\n"
             "-F PREROUTING\n"
             ":oem_nat_pre -\n"
             "-A PREROUTING -j oem_nat_pre\n"
             "COMMIT\n"},
            {V4,
             "*nat\n"
             ":POSTROUTING -\n"
             "-F POSTROUTING\n"
             ":tetherctrl_nat_POSTROUTING -\n"
             "-A POSTROUTING -j tetherctrl_nat_POSTROUTING\n"
             "COMMIT\n"},
            {V4,
             "*filter\n"
             "-S OUTPUT\n"
             "COMMIT\n"},
            {V4,
             "*filter\n"
             ":oem_out -\n"
             "-A OUTPUT -j oem_out\n"
             ":fw_OUTPUT -\n"
             "-A OUTPUT -j fw_OUTPUT\n"
             ":st_OUTPUT -\n"
             "-A OUTPUT -j st_OUTPUT\n"
             ":bw_OUTPUT -\n"
             "-A OUTPUT -j bw_OUTPUT\n"
             "COMMIT\n"},
            {V6,
             "*filter\n"
             "-S OUTPUT\n"
             "COMMIT\n"},
            {V6,
             "*filter\n"
             ":oem_out -\n"
             "-A OUTPUT -j oem_out\n"
             ":fw_OUTPUT -\n"
             "-A OUTPUT -j fw_OUTPUT\n"
             ":st_OUTPUT -\n"
             "-A OUTPUT -j st_OUTPUT\n"
             ":bw_OUTPUT -\n"
             "-A OUTPUT -j bw_OUTPUT\n"
             "COMMIT\n"},
            {V4,
             "*mangle\n"
             "-S POSTROUTING\n"
             "COMMIT\n"},
            {V4,
             "*mangle\n"
             ":oem_mangle_post -\n"
             "-A POSTROUTING -j oem_mangle_post\n"
             ":bw_mangle_POSTROUTING -\n"
             "-A POSTROUTING -j bw_mangle_POSTROUTING\n"
             ":idletimer_mangle_POSTROUTING -\n"
             "-A POSTROUTING -j idletimer_mangle_POSTROUTING\n"
             "COMMIT\n"},
            {V6,
             "*mangle\n"
             "-S POSTROUTING\n"
             "COMMIT\n"},
            {V6,
             "*mangle\n"
             ":oem_mangle_post -\n"
             "-A POSTROUTING -j oem_mangle_post\n"
             ":bw_mangle_POSTROUTING -\n"
             "-A POSTROUTING -j bw_mangle_POSTROUTING\n"
             ":idletimer_mangle_POSTROUTING -\n"
             "-A POSTROUTING -j idletimer_mangle_POSTROUTING\n"
             "COMMIT\n"},
    };

    // Check that we run these commands and these only.
    initChildChains();
    expectIptablesRestoreCommands(expected);
    expectIptablesRestoreCommands(ExpectedIptablesCommands{});

    // Now test what happens when some rules exist (e.g., if we crash and restart).

    // First, explicitly tell the iptables test code to return empty output to all the commands we
    // send. This allows us to tell it to return non-empty output to particular commands in the
    // following code.
    for (size_t i = 0; i < expected.size(); i++) {
        sIptablesRestoreOutput.push_back("");
    }

    // Define a macro to remove a substring from a string. We use a macro instead of a function so
    // we can assert in it. In the following code, we use ASSERT_* to check for programming errors
    // in the test code, and EXPECT_* to check for errors in the actual code.
#define DELETE_SUBSTRING(substr, str) {                      \
        size_t start = (str).find((substr));                 \
        ASSERT_NE(std::string::npos, start);                 \
        (str).erase(start, strlen((substr)));                \
        ASSERT_EQ(std::string::npos, (str).find((substr)));  \
    }

    // Now set test expectations.

    // 1. Test that if we find rules that we don't create ourselves, we ignore them.
    // First check that command #7 is where we list the OUTPUT chain in the (IPv4) filter table:
    ASSERT_NE(std::string::npos, expected[7].second.find("*filter\n-S OUTPUT\n"));
    // ... and pretend that when we run that command, we find the following rules. Because we don't
    // create any of these rules ourselves, our behaviour is unchanged.
    sIptablesRestoreOutput[7] =
        "-P OUTPUT ACCEPT\n"
        "-A OUTPUT -o r_rmnet_data8 -p udp -m udp --dport 1900 -j DROP\n";

    // 2. Test that rules that we create ourselves are not added if they already exist.
    // Pretend that when we list the OUTPUT chain in the (IPv6) filter table, we find the oem_out
    // and st_OUTPUT chains:
    ASSERT_NE(std::string::npos, expected[9].second.find("*filter\n-S OUTPUT\n"));
    sIptablesRestoreOutput[9] =
        "-A OUTPUT -j oem_out\n"
        "-A OUTPUT -j st_OUTPUT\n";
    // ... and expect that when we populate the OUTPUT chain, we do not re-add them.
    DELETE_SUBSTRING("-A OUTPUT -j oem_out\n", expected[10].second);
    DELETE_SUBSTRING("-A OUTPUT -j st_OUTPUT\n", expected[10].second);

    // 3. Now test that when we list the POSTROUTING chain in the mangle table, we find a mixture of
    // netd-created rules and vendor rules:
    ASSERT_NE(std::string::npos, expected[13].second.find("*mangle\n-S POSTROUTING\n"));
    sIptablesRestoreOutput[13] =
        "-P POSTROUTING ACCEPT\n"
        "-A POSTROUTING -j oem_mangle_post\n"
        "-A POSTROUTING -j bw_mangle_POSTROUTING\n"
        "-A POSTROUTING -j idletimer_mangle_POSTROUTING\n"
        "-A POSTROUTING -j qcom_qos_reset_POSTROUTING\n"
        "-A POSTROUTING -j qcom_qos_filter_POSTROUTING\n";
    // and expect that we don't re-add the netd-created rules that already exist.
    DELETE_SUBSTRING("-A POSTROUTING -j oem_mangle_post\n", expected[14].second);
    DELETE_SUBSTRING("-A POSTROUTING -j bw_mangle_POSTROUTING\n", expected[14].second);
    DELETE_SUBSTRING("-A POSTROUTING -j idletimer_mangle_POSTROUTING\n", expected[14].second);

    // In this last case, also check that our expectations are reasonable.
    std::string expectedCmd14 =
        "*mangle\n"
        ":oem_mangle_post -\n"
        ":bw_mangle_POSTROUTING -\n"
        ":idletimer_mangle_POSTROUTING -\n"
        "COMMIT\n";
    ASSERT_EQ(expectedCmd14, expected[14].second);

    // Finally, actually test that initChildChains runs the expected commands, and nothing more.
    initChildChains();
    expectIptablesRestoreCommands(expected);
    expectIptablesRestoreCommands(ExpectedIptablesCommands{});
}

}  // namespace net
}  // namespace android