OSDN Git Service

Add a test for getTetherStats.
authorLorenzo Colitti <lorenzo@google.com>
Fri, 8 Jul 2016 09:24:26 +0000 (18:24 +0900)
committerLorenzo Colitti <lorenzo@google.com>
Fri, 8 Jul 2016 10:52:35 +0000 (19:52 +0900)
Bug: 9580643
Change-Id: I26f7adb9639f1ddf4eda0c98bcc6cd3a83d3ba0b

server/Android.mk
server/BandwidthControllerTest.cpp
server/IptablesBaseTest.cpp
server/IptablesBaseTest.h

index 71a1087..352d1e0 100644 (file)
@@ -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)
 
index aa24583..5af9749 100644 (file)
 #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() {
@@ -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]));
+}
index 1502c4b..9e75cb6 100644 (file)
  * 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 = {};
index a8521b1..a354ef2 100644 (file)
@@ -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);
 };