Lorenzo Colitti | 758bccc | 2019-06-26 22:12:35 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019 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 | * test_utils.cpp - miscellaneous unit test utilities. |
| 17 | */ |
| 18 | |
| 19 | #include <cstdio> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <android-base/stringprintf.h> |
| 24 | |
| 25 | #include "test_utils.h" |
| 26 | |
| 27 | #define IP_PATH "/system/bin/ip" |
| 28 | |
| 29 | using android::base::StringPrintf; |
| 30 | |
| 31 | int randomUid() { |
| 32 | return 100000 * arc4random_uniform(7) + 12000 + arc4random_uniform(3000); |
| 33 | } |
| 34 | |
| 35 | std::vector<std::string> runCommand(const std::string& command) { |
| 36 | std::vector<std::string> lines; |
| 37 | FILE* f = popen(command.c_str(), "r"); // NOLINT(cert-env33-c) |
| 38 | if (f == nullptr) { |
| 39 | perror("popen"); |
| 40 | return lines; |
| 41 | } |
| 42 | |
| 43 | char* line = nullptr; |
| 44 | size_t bufsize = 0; |
| 45 | ssize_t linelen = 0; |
| 46 | while ((linelen = getline(&line, &bufsize, f)) >= 0) { |
| 47 | lines.push_back(std::string(line, linelen)); |
| 48 | free(line); |
| 49 | line = nullptr; |
| 50 | } |
| 51 | |
| 52 | pclose(f); |
| 53 | return lines; |
| 54 | } |
| 55 | |
| 56 | std::vector<std::string> listIpRules(const char* ipVersion) { |
| 57 | std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion); |
| 58 | return runCommand(command); |
| 59 | } |
| 60 | |
| 61 | std::vector<std::string> listIptablesRule(const char* binary, const char* chainName) { |
| 62 | std::string command = StringPrintf("%s -w -n -L %s", binary, chainName); |
| 63 | return runCommand(command); |
| 64 | } |
| 65 | |
| 66 | int iptablesRuleLineLength(const char* binary, const char* chainName) { |
| 67 | return listIptablesRule(binary, chainName).size(); |
| 68 | } |
| 69 | |
| 70 | bool iptablesRuleExists(const char* binary, const char* chainName, |
| 71 | const std::string& expectedRule) { |
| 72 | std::vector<std::string> rules = listIptablesRule(binary, chainName); |
| 73 | for (const auto& rule : rules) { |
| 74 | if (rule.find(expectedRule) != std::string::npos) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | std::vector<std::string> listIpRoutes(const char* ipVersion, const char* table) { |
| 82 | std::string command = StringPrintf("%s %s route ls table %s", IP_PATH, ipVersion, table); |
| 83 | return runCommand(command); |
| 84 | } |
| 85 | |
| 86 | bool ipRouteExists(const char* ipVersion, const char* table, const std::string& ipRoute) { |
| 87 | std::vector<std::string> routes = listIpRoutes(ipVersion, table); |
| 88 | for (const auto& route : routes) { |
| 89 | if (route.find(ipRoute) != std::string::npos) { |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | return false; |
| 94 | } |