Home | History | Annotate | Download | only in functional
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "boot_hidl_hal_test"
     18 #include <android-base/logging.h>
     19 
     20 #include <cutils/properties.h>
     21 
     22 #include <android/hardware/boot/1.0/IBootControl.h>
     23 
     24 #include <VtsHalHidlTargetTestBase.h>
     25 
     26 using ::android::hardware::boot::V1_0::IBootControl;
     27 using ::android::hardware::boot::V1_0::CommandResult;
     28 using ::android::hardware::boot::V1_0::BoolResult;
     29 using ::android::hardware::boot::V1_0::Slot;
     30 using ::android::hardware::hidl_string;
     31 using ::android::hardware::Return;
     32 using ::android::sp;
     33 using std::string;
     34 using std::vector;
     35 
     36 // The main test class for the Boot HIDL HAL.
     37 class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase {
     38  public:
     39   virtual void SetUp() override {
     40     boot = ::testing::VtsHalHidlTargetTestBase::getService<IBootControl>();
     41     ASSERT_NE(boot, nullptr);
     42   }
     43 
     44   virtual void TearDown() override {}
     45 
     46   sp<IBootControl> boot;
     47 };
     48 
     49 auto generate_callback(CommandResult *dest) {
     50   return [=](CommandResult cr) { *dest = cr; };
     51 }
     52 
     53 // Sanity check Boot::getNumberSlots().
     54 TEST_F(BootHidlTest, GetNumberSlots) {
     55   uint32_t slots = boot->getNumberSlots();
     56   EXPECT_LE((uint32_t)2, slots);
     57 }
     58 
     59 // Sanity check Boot::getCurrentSlot().
     60 TEST_F(BootHidlTest, GetCurrentSlot) {
     61   Slot curSlot = boot->getCurrentSlot();
     62   uint32_t slots = boot->getNumberSlots();
     63   EXPECT_LT(curSlot, slots);
     64 }
     65 
     66 // Sanity check Boot::markBootSuccessful().
     67 TEST_F(BootHidlTest, MarkBootSuccessful) {
     68   CommandResult cr;
     69   Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
     70   ASSERT_TRUE(result.isOk());
     71   if (cr.success) {
     72     Slot curSlot = boot->getCurrentSlot();
     73     BoolResult ret = boot->isSlotMarkedSuccessful(curSlot);
     74     EXPECT_EQ(BoolResult::TRUE, ret);
     75   }
     76 }
     77 
     78 // Sanity check Boot::setActiveBootSlot() on good and bad inputs.
     79 TEST_F(BootHidlTest, SetActiveBootSlot) {
     80   for (Slot s = 0; s < 2; s++) {
     81     CommandResult cr;
     82     Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr));
     83     EXPECT_TRUE(result.isOk());
     84   }
     85   {
     86     // Restore original flags to avoid problems on reboot
     87     CommandResult cr;
     88     Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
     89     EXPECT_TRUE(result.isOk());
     90     EXPECT_TRUE(cr.success);
     91   }
     92   {
     93     CommandResult cr;
     94     uint32_t slots = boot->getNumberSlots();
     95     Return<void> result =
     96         boot->setActiveBootSlot(slots, generate_callback(&cr));
     97     ASSERT_TRUE(result.isOk());
     98     EXPECT_EQ(false, cr.success);
     99   }
    100 }
    101 
    102 // Sanity check Boot::setSlotAsUnbootable() on good and bad inputs.
    103 TEST_F(BootHidlTest, SetSlotAsUnbootable) {
    104   {
    105     CommandResult cr;
    106     Slot curSlot = boot->getCurrentSlot();
    107     Slot otherSlot = curSlot ? 0 : 1;
    108     Return<void> result =
    109         boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
    110     EXPECT_TRUE(result.isOk());
    111     if (cr.success) {
    112       EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));
    113 
    114       // Restore original flags to avoid problems on reboot
    115       result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
    116       EXPECT_TRUE(result.isOk());
    117       EXPECT_TRUE(cr.success);
    118       result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
    119       EXPECT_TRUE(result.isOk());
    120       EXPECT_TRUE(cr.success);
    121       result = boot->markBootSuccessful(generate_callback(&cr));
    122       EXPECT_TRUE(result.isOk());
    123       EXPECT_TRUE(cr.success);
    124     }
    125   }
    126   {
    127     CommandResult cr;
    128     uint32_t slots = boot->getNumberSlots();
    129     Return<void> result =
    130         boot->setSlotAsUnbootable(slots, generate_callback(&cr));
    131     EXPECT_TRUE(result.isOk());
    132     EXPECT_EQ(false, cr.success);
    133   }
    134 }
    135 
    136 // Sanity check Boot::isSlotBootable() on good and bad inputs.
    137 TEST_F(BootHidlTest, IsSlotBootable) {
    138   for (Slot s = 0; s < 2; s++) {
    139     EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotBootable(s));
    140   }
    141   uint32_t slots = boot->getNumberSlots();
    142   EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotBootable(slots));
    143 }
    144 
    145 // Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs.
    146 TEST_F(BootHidlTest, IsSlotMarkedSuccessful) {
    147   for (Slot s = 0; s < 2; s++) {
    148     EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(s));
    149   }
    150   uint32_t slots = boot->getNumberSlots();
    151   EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(slots));
    152 }
    153 
    154 // Sanity check Boot::getSuffix() on good and bad inputs.
    155 TEST_F(BootHidlTest, GetSuffix) {
    156     string suffixStr;
    157     vector<string> correctSuffixes = {"_a", "_b"};
    158     auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); };
    159     for (Slot i = 0; i < 2; i++) {
    160         CommandResult cr;
    161         Return<void> result = boot->getSuffix(i, cb);
    162         EXPECT_TRUE(result.isOk());
    163         ASSERT_EQ(0, suffixStr.compare(correctSuffixes[i]));
    164     }
    165     {
    166         string emptySuffix = "";
    167         Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb);
    168         EXPECT_TRUE(result.isOk());
    169         ASSERT_EQ(0, suffixStr.compare(emptySuffix));
    170     }
    171 }
    172 
    173 int main(int argc, char **argv) {
    174   ::testing::InitGoogleTest(&argc, argv);
    175   int status = RUN_ALL_TESTS();
    176   LOG(INFO) << "Test result = " << status;
    177   return status;
    178 }
    179