From: Lorenzo Colitti Date: Fri, 13 May 2016 07:57:15 +0000 (+0900) Subject: Don't crash the test if expecting more commands than were run. X-Git-Tag: android-x86-7.1-r1~39^2~4^2~2 X-Git-Url: http://git.osdn.net/view?p=android-x86%2Fsystem-netd.git;a=commitdiff_plain;h=54ecf16d8effb5feedb7138254c880bd9f7a26b3 Don't crash the test if expecting more commands than were run. Bug: 26675191 Change-Id: I54860c7cf7b79bb6ace89c3130467ba7c0473e03 --- diff --git a/server/IptablesBaseTest.cpp b/server/IptablesBaseTest.cpp index 5ce6667..1502c4b 100644 --- a/server/IptablesBaseTest.cpp +++ b/server/IptablesBaseTest.cpp @@ -70,12 +70,20 @@ int IptablesBaseTest::fakeExecIptablesRestore(IptablesTarget target, const std:: int IptablesBaseTest::expectIptablesCommand(IptablesTarget target, int pos, const std::string& cmd) { + + if ((unsigned) pos >= sCmds.size()) { + ADD_FAILURE() << "Expected too many iptables commands, want command " + << pos + 1 << "/" << sCmds.size(); + return -1; + } + if (target == V4 || target == V4V6) { EXPECT_EQ("/system/bin/iptables -w " + cmd, sCmds[pos++]); } if (target == V6 || target == V4V6) { EXPECT_EQ("/system/bin/ip6tables -w " + cmd, sCmds[pos++]); } + return target == V4V6 ? 2 : 1; } @@ -92,7 +100,12 @@ void IptablesBaseTest::expectIptablesCommands(const ExpectedIptablesCommands& ex for (size_t i = 0; i < expectedCmds.size(); i ++) { auto target = expectedCmds[i].first; auto cmd = expectedCmds[i].second; - pos += expectIptablesCommand(target, pos, cmd); + int numConsumed = expectIptablesCommand(target, pos, cmd); + if (numConsumed < 0) { + // Read past the end of the array. + break; + } + pos += numConsumed; } EXPECT_EQ(pos, sCmds.size());