summaryrefslogtreecommitdiff
path: root/server/BandwidthControllerTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'server/BandwidthControllerTest.cpp')
-rw-r--r--server/BandwidthControllerTest.cpp80
1 files changed, 39 insertions, 41 deletions
diff --git a/server/BandwidthControllerTest.cpp b/server/BandwidthControllerTest.cpp
index 2b2ce6e4..959ad9a3 100644
--- a/server/BandwidthControllerTest.cpp
+++ b/server/BandwidthControllerTest.cpp
@@ -41,23 +41,15 @@ public:
}
BandwidthController mBw;
- void addPopenContents(std::string contents) {
- sPopenContents.push_back(contents);
- }
-
- void addPopenContents(std::string contents1, std::string contents2) {
- sPopenContents.push_back(contents1);
- sPopenContents.push_back(contents2);
- }
-
- void clearPopenContents() {
- sPopenContents.clear();
- }
-
void addIptablesRestoreOutput(std::string contents) {
sIptablesRestoreOutput.push_back(contents);
}
+ void addIptablesRestoreOutput(std::string contents1, std::string contents2) {
+ sIptablesRestoreOutput.push_back(contents1);
+ sIptablesRestoreOutput.push_back(contents2);
+ }
+
void clearIptablesRestoreOutput() {
sIptablesRestoreOutput.clear();
}
@@ -229,17 +221,17 @@ TEST_F(BandwidthControllerTest, TestGetTetherStats) {
BandwidthController::TetherStats filter;
// If no filter is specified, both IPv4 and IPv6 counters must have at least one interface pair.
- addPopenContents(kIPv4TetherCounters, "");
+ addIptablesRestoreOutput(kIPv4TetherCounters);
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
- addPopenContents("", kIPv6TetherCounters);
+ addIptablesRestoreOutput(kIPv6TetherCounters);
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
- clearPopenContents();
+ clearIptablesRestoreOutput();
// IPv4 and IPv6 counters are properly added together.
- addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
+ addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
filter = BandwidthController::TetherStats();
std::string expected =
"114 wlan0 rmnet0 10002373 10026 20002002 20027\n"
@@ -248,83 +240,89 @@ TEST_F(BandwidthControllerTest, TestGetTetherStats) {
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
// Test filtering.
- addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
+ addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
filter = BandwidthController::TetherStats("bt-pan", "rmnet0", -1, -1, -1, -1);
expected = "221 bt-pan rmnet0 107471 1040 1708806 1450\n";
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
- addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
+ addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
filter = BandwidthController::TetherStats("wlan0", "rmnet0", -1, -1, -1, -1);
expected = "221 wlan0 rmnet0 10002373 10026 20002002 20027\n";
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
- clearPopenContents();
+ clearIptablesRestoreOutput();
// Select nonexistent interfaces.
- addPopenContents(kIPv4TetherCounters, kIPv6TetherCounters);
+ addIptablesRestoreOutput(kIPv4TetherCounters, kIPv6TetherCounters);
filter = BandwidthController::TetherStats("rmnet0", "foo0", -1, -1, -1, -1);
expected = "200 Tethering stats list completed\n";
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
- clearPopenContents();
+ clearIptablesRestoreOutput();
// No stats with a filter: no error.
- addPopenContents("", "");
+ addIptablesRestoreOutput("", "");
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
- clearPopenContents();
+ clearIptablesRestoreOutput();
- addPopenContents("foo", "foo");
+ addIptablesRestoreOutput("foo", "foo");
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ("200 Tethering stats list completed\n", readSocketClientResponse(socketPair[1]));
- clearPopenContents();
+ clearIptablesRestoreOutput();
// No stats and empty filter: error.
filter = BandwidthController::TetherStats();
- addPopenContents("", kIPv6TetherCounters);
+ addIptablesRestoreOutput("", kIPv6TetherCounters);
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
- addPopenContents(kIPv4TetherCounters, "");
+ addIptablesRestoreOutput(kIPv4TetherCounters, "");
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
// Include only one pair of interfaces and things are fine.
std::vector<std::string> counterLines = android::base::Split(kIPv4TetherCounters, "\n");
std::vector<std::string> brokenCounterLines = counterLines;
counterLines.resize(4);
std::string counters = android::base::Join(counterLines, "\n") + "\n";
- addPopenContents(counters, counters);
+ addIptablesRestoreOutput(counters, counters);
expected =
"114 wlan0 rmnet0 4746 52 4004 54\n"
"200 Tethering stats list completed\n";
ASSERT_EQ(0, mBw.getTetherStats(&cli, filter, err));
ASSERT_EQ(expected, readSocketClientResponse(socketPair[1]));
- clearPopenContents();
+ clearIptablesRestoreOutput();
// But if interfaces aren't paired, it's always an error.
+ err = "";
counterLines.resize(3);
counters = android::base::Join(counterLines, "\n") + "\n";
- addPopenContents(counters, counters);
+ addIptablesRestoreOutput(counters, counters);
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
+
+ // Token unit test of the fact that we return the stats in the error message which the caller
+ // ignores.
+ std::string expectedError = counters;
+ EXPECT_EQ(expectedError, err);
// popen() failing is always an error.
- addPopenContents(kIPv4TetherCounters);
+ addIptablesRestoreOutput(kIPv4TetherCounters);
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
- addPopenContents(kIPv6TetherCounters);
+ clearIptablesRestoreOutput();
+ addIptablesRestoreOutput(kIPv6TetherCounters);
ASSERT_EQ(-1, mBw.getTetherStats(&cli, filter, err));
expectNoSocketClientResponse(socketPair[1]);
- clearPopenContents();
+ clearIptablesRestoreOutput();
}