From bb88422d9328023aa02f557d5fc4058c841550e4 Mon Sep 17 00:00:00 2001 From: Connor O'Brien Date: Wed, 5 Apr 2017 14:41:34 -0700 Subject: [PATCH] Fix hidl_string copy in boot VTS test hidl_strings returned through a callback need to be copied; the current approach just saves a pointer to the underlying buffer, which is incorrect and sometimes causes the test to fail. Test: the GetSuffix test now passes reliably Change-Id: I4bb143be98b00b20f9da4aebef3b5ab4980b7690 Signed-off-by: Connor O'Brien --- .../functional/VtsHalBootV1_0TargetTest.cpp | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/boot/1.0/vts/functional/VtsHalBootV1_0TargetTest.cpp b/boot/1.0/vts/functional/VtsHalBootV1_0TargetTest.cpp index 9789ee6be2..f48a95d853 100644 --- a/boot/1.0/vts/functional/VtsHalBootV1_0TargetTest.cpp +++ b/boot/1.0/vts/functional/VtsHalBootV1_0TargetTest.cpp @@ -30,6 +30,8 @@ using ::android::hardware::boot::V1_0::Slot; using ::android::hardware::hidl_string; using ::android::hardware::Return; using ::android::sp; +using std::string; +using std::vector; // The main test class for the Boot HIDL HAL. class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase { @@ -83,7 +85,7 @@ TEST_F(BootHidlTest, SetActiveBootSlot) { { // Restore original flags to avoid problems on reboot CommandResult cr; - Return result = boot->markBootSuccessful(generate_callback(&cr)); + Return result = boot->markBootSuccessful(generate_callback(&cr)); EXPECT_TRUE(result.isOk()); EXPECT_TRUE(cr.success); } @@ -151,22 +153,21 @@ TEST_F(BootHidlTest, IsSlotMarkedSuccessful) { // Sanity check Boot::getSuffix() on good and bad inputs. TEST_F(BootHidlTest, GetSuffix) { - const char *suffixPtr; - auto cb = [&](hidl_string suffix) { suffixPtr = suffix.c_str(); }; - for (Slot i = 0; i < 2; i++) { - CommandResult cr; - Return result = boot->getSuffix(i, cb); - EXPECT_TRUE(result.isOk()); - char correctSuffix[3]; - snprintf(correctSuffix, sizeof(correctSuffix), "_%c", 'a' + i); - ASSERT_EQ(0, strcmp(suffixPtr, correctSuffix)); - } - { - char emptySuffix[] = ""; - Return result = boot->getSuffix(boot->getNumberSlots(), cb); - EXPECT_TRUE(result.isOk()); - ASSERT_EQ(0, strcmp(emptySuffix, suffixPtr)); - } + string suffixStr; + vector correctSuffixes = {"_a", "_b"}; + auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); }; + for (Slot i = 0; i < 2; i++) { + CommandResult cr; + Return result = boot->getSuffix(i, cb); + EXPECT_TRUE(result.isOk()); + ASSERT_EQ(0, suffixStr.compare(correctSuffixes[i])); + } + { + string emptySuffix = ""; + Return result = boot->getSuffix(boot->getNumberSlots(), cb); + EXPECT_TRUE(result.isOk()); + ASSERT_EQ(0, suffixStr.compare(emptySuffix)); + } } int main(int argc, char **argv) {