summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2016-07-08 18:24:26 +0900
committerLorenzo Colitti <lorenzo@google.com>2016-07-08 19:52:35 +0900
commitbbeaf9a4d272eefb11748d7d40c6bd117ab468fb (patch)
treeda30ae4543427b009a34257353b7b58a9593264f
parent2c5aaa1876db659556c2e9605beccc670e6b7c0d (diff)
downloadnetd-bbeaf9a4d272eefb11748d7d40c6bd117ab468fb.tar.gz
Add a test for getTetherStats.
Bug: 9580643 Change-Id: I26f7adb9639f1ddf4eda0c98bcc6cd3a83d3ba0b
-rw-r--r--server/Android.mk10
-rw-r--r--server/BandwidthControllerTest.cpp71
-rw-r--r--server/IptablesBaseTest.cpp17
-rw-r--r--server/IptablesBaseTest.h4
4 files changed, 96 insertions, 6 deletions
diff --git a/server/Android.mk b/server/Android.mk
index 71a1087e..352d1e06 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 aa245831..5af97496 100644
--- a/server/BandwidthControllerTest.cpp
+++ b/server/BandwidthControllerTest.cpp
@@ -19,6 +19,11 @@
#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>
@@ -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<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]));
+}
diff --git a/server/IptablesBaseTest.cpp b/server/IptablesBaseTest.cpp
index 1502c4bf..9e75cb63 100644
--- a/server/IptablesBaseTest.cpp
+++ b/server/IptablesBaseTest.cpp
@@ -16,14 +16,20 @@
* 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();
@@ -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<std::string> IptablesBaseTest::sCmds = {};
IptablesBaseTest::ExpectedIptablesCommands IptablesBaseTest::sRestoreCmds = {};
+std::deque<std::string> IptablesBaseTest::sPopenContents = {};
diff --git a/server/IptablesBaseTest.h b/server/IptablesBaseTest.h
index a8521b14..a354ef2b 100644
--- a/server/IptablesBaseTest.h
+++ b/server/IptablesBaseTest.h
@@ -16,6 +16,8 @@
* IptablesBaseTest.h - utility class for tests that use iptables
*/
+#include <deque>
+
#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<std::string>& expectedCmds);
void expectIptablesCommands(const ExpectedIptablesCommands& expectedCmds);
void expectIptablesRestoreCommands(const std::vector<std::string>& expectedCmds);
@@ -35,5 +38,6 @@ public:
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);
};