blob: 038c971aad167b0fcab1c9fb296c8ebb5ddc2338 [file] [log] [blame]
Lorenzo Colitti9028d912016-03-28 02:34:54 +09001/*
2 * Copyright 2016 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 * StrictControllerTest.cpp - unit tests for StrictController.cpp
17 */
18
19#include <string>
20#include <vector>
21
22#include <gtest/gtest.h>
23
24#include <android-base/strings.h>
25
26#include "StrictController.h"
27#include "IptablesBaseTest.h"
28
29class StrictControllerTest : public IptablesBaseTest {
30public:
31 StrictControllerTest() {
32 StrictController::execIptables = fakeExecIptables;
33 }
34 StrictController mStrictCtrl;
35};
36
37TEST_F(StrictControllerTest, TestEnableStrict) {
38 mStrictCtrl.enableStrict();
39
40 std::vector<std::pair<IptablesTarget, std::string>> expected = {
41 { V4V6, "-F st_OUTPUT" },
42 { V4V6, "-F st_penalty_log" },
43 { V4V6, "-F st_penalty_reject" },
44 { V4V6, "-F st_clear_caught" },
45 { V4V6, "-F st_clear_detect" },
46 { V4V6, "-X st_penalty_log" },
47 { V4V6, "-X st_penalty_reject" },
48 { V4V6, "-X st_clear_caught" },
49 { V4V6, "-X st_clear_detect" },
50 { V4V6, "-N st_penalty_log" },
51 { V4V6, "-A st_penalty_log -j CONNMARK --or-mark 0x1000000" },
52 { V4V6, "-A st_penalty_log -j NFLOG --nflog-group 0" },
53 { V4V6, "-N st_penalty_reject" },
54 { V4V6, "-A st_penalty_reject -j CONNMARK --or-mark 0x2000000" },
55 { V4V6, "-A st_penalty_reject -j NFLOG --nflog-group 0" },
56 { V4V6, "-A st_penalty_reject -j REJECT" },
57 { V4V6, "-N st_clear_detect" },
58 { V4V6, "-N st_clear_caught" },
59 { V4V6, "-A st_clear_detect -m connmark --mark 0x2000000/0x2000000 -j REJECT" },
60 { V4V6, "-A st_clear_detect -m connmark --mark 0x1000000/0x1000000 -j RETURN" },
61 { V4, "-A st_clear_detect -p tcp -m u32 --u32 "
62 "0>>22&0x3C@ 12>>26&0x3C@ 0&0xFFFF0000=0x16030000 &&"
63 "0>>22&0x3C@ 12>>26&0x3C@ 4&0x00FF0000=0x00010000 "
64 "-j CONNMARK --or-mark 0x1000000" },
65 { V4, "-A st_clear_detect -p udp -m u32 --u32 "
66 "0>>22&0x3C@ 8&0xFFFF0000=0x16FE0000 &&"
67 "0>>22&0x3C@ 20&0x00FF0000=0x00010000"
68 " -j CONNMARK --or-mark 0x1000000" },
69 { V6, "-A st_clear_detect -p tcp -m u32 --u32 "
70 "52>>26&0x3C@ 40&0xFFFF0000=0x16030000 &&"
71 "52>>26&0x3C@ 44&0x00FF0000=0x00010000"
72 " -j CONNMARK --or-mark 0x1000000" },
73 { V6, "-A st_clear_detect -p udp -m u32 --u32 "
74 "48&0xFFFF0000=0x16FE0000 &&60&0x00FF0000=0x00010000"
75 " -j CONNMARK --or-mark 0x1000000" },
76 { V4V6, "-A st_clear_detect -m connmark --mark 0x1000000/0x1000000 -j RETURN" },
77 { V4, "-A st_clear_detect -p tcp -m state --state ESTABLISHED -m u32 --u32 "
78 "0>>22&0x3C@ 12>>26&0x3C@ 0&0x0=0x0"
79 " -j st_clear_caught" },
80 { V6, "-A st_clear_detect -p tcp -m state --state ESTABLISHED -m u32 --u32 "
81 "52>>26&0x3C@ 40&0x0=0x0"
82 " -j st_clear_caught" },
83 { V4V6, "-A st_clear_detect -p udp -j st_clear_caught" },
84 };
85 expectIptablesCommands(expected);
86}
87
88TEST_F(StrictControllerTest, TestDisableStrict) {
89 mStrictCtrl.disableStrict();
90
91 std::vector<std::string> expected = {
92 "-F st_OUTPUT",
93 "-F st_penalty_log",
94 "-F st_penalty_reject",
95 "-F st_clear_caught",
96 "-F st_clear_detect",
97 "-X st_penalty_log",
98 "-X st_penalty_reject",
99 "-X st_clear_caught",
100 "-X st_clear_detect",
101 };
102 expectIptablesCommands(expected);
103}