1 /*
2 * Copyright (C) 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>
18 #include <vector>
19
20 #include <gtest/gtest.h>
21
22 #include <android-base/strings.h>
23
24 #include "NetUtilsWrapper.h"
25
26 #define MAX_ARGS 128
27 #define VALID true
28 #define INVALID false
29
30 struct Command {
31 bool valid;
32 std::string cmdString;
33 };
34
35 std::vector<Command> COMMANDS = {
36 {INVALID, "tc qdisc del dev root"},
37 {VALID, "/system/bin/tc qdisc del dev root"},
38 {VALID, "/system/bin/ip -6 addr add dev r_rmnet_data6 2001:db8::/64"},
39 {INVALID, "/system/bin/ip -6 addr add dev wlan2 2001:db8::/64"},
40 {VALID, "/system/bin/ip6tables -w -A INPUT -j qcom_foo"},
41 {INVALID, "/system/bin/ip6tables -w -A INPUT -j routectrl_MANGLE_INPUT"},
42 {VALID, "/system/bin/ip6tables -w -A INPUT -i rmnet_data9 -j routectrl_MANGLE_INPUT"},
43 {VALID, "/system/bin/ip6tables -w -F nm_pre_ip4"},
44 {INVALID, "/system/bin/ip6tables -w -F INPUT"},
45 {VALID, "/system/bin/ndc network interface add oem10"},
46 {VALID, "/system/bin/ndc network interface add oem10 v_oem9"},
47 {VALID, "/system/bin/ndc network interface add oem10 oem9"},
48 {INVALID, "/system/bin/ndc network interface add 100 v_oem9"},
49 {VALID, "/system/bin/ndc network interface add oem10 r_rmnet_data0"},
50 {VALID, "/system/bin/ndc network interface add handle42966108894 v_oem9"},
51 {VALID, "/system/bin/ndc network interface add handle42966108894 oem9"},
52 {VALID, "/system/bin/ndc network interface add handle42966108894 r_rmnet_data0"},
53 {INVALID, "/system/bin/ndc network interface add handle42966108894"},
54 {VALID, "/system/bin/ip xfrm state"},
55 };
56
TEST(NetUtilsWrapperTest10,TestCommands)57 TEST(NetUtilsWrapperTest10, TestCommands) {
58 // Overwritten by each test case.
59 char *argv[MAX_ARGS];
60
61 for (const Command& cmd : COMMANDS) {
62 std::vector<std::string> pieces = android::base::Split(cmd.cmdString, " ");
63 ASSERT_LE(pieces.size(), ARRAY_SIZE(argv));
64 for (size_t i = 0; i < pieces.size(); i++) {
65 argv[i] = const_cast<char*>(pieces[i].c_str());
66 }
67 EXPECT_EQ(cmd.valid, checkExpectedCommand(pieces.size(), argv)) <<
68 "Expected command to be " <<
69 (cmd.valid ? "valid" : "invalid") << ", but was " <<
70 (cmd.valid ? "invalid" : "valid") << ": '" << cmd.cmdString << "'";
71 }
72 }
73