include $(CLEAR_VARS)
LOCAL_MODULE := netd_unit_test
LOCAL_CFLAGS := -Wall -Werror -Wunused-parameter
-LOCAL_C_INCLUDES := system/netd/server system/netd/server/binder system/core/logwrapper/include
+LOCAL_C_INCLUDES := \
+ system/netd/include \
+ system/netd/server \
+ system/netd/server/binder \
+ system/core/logwrapper/include \
+
LOCAL_SRC_FILES := \
NetdConstants.cpp IptablesBaseTest.cpp \
BandwidthController.cpp BandwidthControllerTest.cpp \
FirewallControllerTest.cpp FirewallController.cpp \
+ NatController.cpp \
SockDiagTest.cpp SockDiag.cpp \
StrictController.cpp StrictControllerTest.cpp \
UidRanges.cpp \
LOCAL_MODULE_TAGS := tests
-LOCAL_SHARED_LIBRARIES := liblog libbase libcutils liblogwrap
+LOCAL_SHARED_LIBRARIES := liblog libbase libcutils liblogwrap libsysutils
include $(BUILD_NATIVE_TEST)
#include <string>
#include <vector>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
#include <gtest/gtest.h>
#include <android-base/strings.h>
#include "BandwidthController.h"
#include "IptablesBaseTest.h"
-FILE *fake_popen(const char *, const char *) {
- return NULL;
-};
-
class BandwidthControllerTest : public IptablesBaseTest {
public:
BandwidthControllerTest() {
BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore;
}
BandwidthController mBw;
+
+ void addPopenContents(std::string contents) {
+ sPopenContents.push_back(contents);
+ }
};
TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) {
};
expectIptablesCommands(expected);
}
+
+std::string kIPv4TetherCounters = android::base::Join(std::vector<std::string> {
+ "Chain natctrl_tether_counters (4 references)",
+ " pkts bytes target prot opt in out source destination",
+ " 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0",
+ " 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0",
+ " 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0",
+ " 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0",
+}, '\n');
+
+std::string readSocketClientResponse(int fd) {
+ char buf[32768];
+ ssize_t bytesRead = read(fd, buf, sizeof(buf));
+ if (bytesRead < 0) {
+ return "";
+ }
+ for (int i = 0; i < bytesRead; i++) {
+ if (buf[i] == '\0') buf[i] = '\n';
+ }
+ return std::string(buf, bytesRead);
+}
+
+TEST_F(BandwidthControllerTest, TestGetTetherStats) {
+ int socketPair[2];
+ ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair));
+ ASSERT_EQ(0, fcntl(socketPair[0], F_SETFL, O_NONBLOCK | fcntl(socketPair[0], F_GETFL)));
+ ASSERT_EQ(0, fcntl(socketPair[1], F_SETFL, O_NONBLOCK | fcntl(socketPair[1], F_GETFL)));
+ SocketClient cli(socketPair[0], false);
+
+ std::string err;
+ BandwidthController::TetherStats filter;
+ addPopenContents(kIPv4TetherCounters);
+ std::string expected =
+ "114 wlan0 rmnet0 2373 26 2002 27\n"
+ "114 bt-pan rmnet0 107471 1040 1708806 1450\n"
+ "200 Tethering stats list completed\n";
+ mBw.getTetherStats(&cli, filter, err);
+ ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
+
+ addPopenContents(kIPv4TetherCounters);
+ filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
+ expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
+ mBw.getTetherStats(&cli, filter, err);
+ ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
+
+
+ addPopenContents(kIPv4TetherCounters);
+ filter = BandwidthController::TetherStats("rmnet0", "wlan0", -1, -1, -1, -1);
+ expected = "221 rmnet0 wlan0 2002 27 2373 26\n";
+ mBw.getTetherStats(&cli, filter, err);
+ ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
+
+ addPopenContents(kIPv4TetherCounters);
+ filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
+ expected = "200 Tethering stats list completed\n";
+ mBw.getTetherStats(&cli, filter, err);
+ ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
+}
* IptablesBaseTest.cpp - utility class for tests that use iptables
*/
+#include <deque>
#include <string>
#include <vector>
#include <gtest/gtest.h>
+#include <android-base/stringprintf.h>
+
#include "IptablesBaseTest.h"
#include "NetdConstants.h"
+#define LOG_TAG "IptablesBaseTest"
+#include <cutils/log.h>
+
IptablesBaseTest::IptablesBaseTest() {
sCmds.clear();
sRestoreCmds.clear();
return 0;
}
+FILE *IptablesBaseTest::fake_popen(const char * /* cmd */, const char *type) {
+ if (sPopenContents.empty() || strcmp(type, "r") != 0) {
+ return NULL;
+ }
+
+ std::string realCmd = android::base::StringPrintf("echo '%s'", sPopenContents.front().c_str());
+ sPopenContents.pop_front();
+ return popen(realCmd.c_str(), "r");
+}
+
int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std::string& commands) {
sRestoreCmds.push_back({ target, commands });
return 0;
std::vector<std::string> IptablesBaseTest::sCmds = {};
IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
+std::deque<std::string> IptablesBaseTest::sPopenContents = {};
* IptablesBaseTest.h - utility class for tests that use iptables
*/
+#include <deque>
+
#include "NetdConstants.h"
class IptablesBaseTest : public ::testing::Test {
static int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool);
static int fakeExecIptables(IptablesTarget target, ...);
static int fakeExecIptablesRestore(IptablesTarget target, const std::string& commands);
+ static FILE *fake_popen(const char *cmd, const char *type);
void expectIptablesCommands(const std::vector<std::string>& expectedCmds);
void expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds);
void expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds);
protected:
static std::vector<std::string> sCmds;
static ExpectedIptablesCommands sRestoreCmds;
+ static std::deque<std::string> sPopenContents;
int expectIptablesCommand(IptablesTarget target, int pos, const std::string& cmd);
};