From: Lorenzo Colitti Date: Fri, 8 Jul 2016 09:24:26 +0000 (+0900) Subject: Add a test for getTetherStats. X-Git-Tag: android-x86-7.1-r1~37 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=bbeaf9a4d272eefb11748d7d40c6bd117ab468fb;p=android-x86%2Fsystem-netd.git Add a test for getTetherStats. Bug: 9580643 Change-Id: I26f7adb9639f1ddf4eda0c98bcc6cd3a83d3ba0b --- diff --git a/server/Android.mk b/server/Android.mk index 71a1087..352d1e0 100644 --- a/server/Android.mk +++ b/server/Android.mk @@ -130,16 +130,22 @@ include $(BUILD_EXECUTABLE) 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) diff --git a/server/BandwidthControllerTest.cpp b/server/BandwidthControllerTest.cpp index aa24583..5af9749 100644 --- a/server/BandwidthControllerTest.cpp +++ b/server/BandwidthControllerTest.cpp @@ -19,6 +19,11 @@ #include #include +#include +#include +#include +#include + #include #include @@ -26,10 +31,6 @@ #include "BandwidthController.h" #include "IptablesBaseTest.h" -FILE *fake_popen(const char *, const char *) { - return NULL; -}; - class BandwidthControllerTest : public IptablesBaseTest { public: BandwidthControllerTest() { @@ -38,6 +39,10 @@ public: BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore; } BandwidthController mBw; + + void addPopenContents(std::string contents) { + sPopenContents.push_back(contents); + } }; TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { @@ -134,3 +139,61 @@ TEST_F(BandwidthControllerTest, TestEnableDataSaver) { }; expectIptablesCommands(expected); } + +std::string kIPv4TetherCounters = android::base::Join(std::vector { + "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])); +} diff --git a/server/IptablesBaseTest.cpp b/server/IptablesBaseTest.cpp index 1502c4b..9e75cb6 100644 --- a/server/IptablesBaseTest.cpp +++ b/server/IptablesBaseTest.cpp @@ -16,14 +16,20 @@ * IptablesBaseTest.cpp - utility class for tests that use iptables */ +#include #include #include #include +#include + #include "IptablesBaseTest.h" #include "NetdConstants.h" +#define LOG_TAG "IptablesBaseTest" +#include + IptablesBaseTest::IptablesBaseTest() { sCmds.clear(); sRestoreCmds.clear(); @@ -63,6 +69,16 @@ int IptablesBaseTest::fakeExecIptables(IptablesTarget target, ...) { 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; @@ -131,3 +147,4 @@ void IptablesBaseTest::expectIptablesRestoreCommands(const ExpectedIptablesComma std::vector IptablesBaseTest::sCmds = {}; IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {}; +std::deque IptablesBaseTest::sPopenContents = {}; diff --git a/server/IptablesBaseTest.h b/server/IptablesBaseTest.h index a8521b1..a354ef2 100644 --- a/server/IptablesBaseTest.h +++ b/server/IptablesBaseTest.h @@ -16,6 +16,8 @@ * IptablesBaseTest.h - utility class for tests that use iptables */ +#include + #include "NetdConstants.h" class IptablesBaseTest : public ::testing::Test { @@ -27,6 +29,7 @@ public: 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& expectedCmds); void expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds); void expectIptablesRestoreCommands(const std::vector& expectedCmds); @@ -35,5 +38,6 @@ public: protected: static std::vector sCmds; static ExpectedIptablesCommands sRestoreCmds; + static std::deque sPopenContents; int expectIptablesCommand(IptablesTarget target, int pos, const std::string& cmd); };