summaryrefslogtreecommitdiffstats
blob: 2f2052c8309797b1700830c3335349124fe0e4d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "boot_hidl_hal_test"
#include <android-base/logging.h>

#include <cutils/properties.h>

#include <android/hardware/boot/1.0/IBootControl.h>

#include <VtsHalHidlTargetTestBase.h>
#include <VtsHalHidlTargetTestEnvBase.h>

#include <unordered_set>

using ::android::hardware::boot::V1_0::IBootControl;
using ::android::hardware::boot::V1_0::CommandResult;
using ::android::hardware::boot::V1_0::BoolResult;
using ::android::hardware::boot::V1_0::Slot;
using ::android::hardware::hidl_string;
using ::android::hardware::Return;
using ::android::sp;
using std::string;
using std::unordered_set;
using std::vector;

// Test environment for Boot HIDL HAL.
class BootHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
   public:
    // get the test environment singleton
    static BootHidlEnvironment* Instance() {
        static BootHidlEnvironment* instance = new BootHidlEnvironment;
        return instance;
    }

    virtual void registerTestServices() override { registerTestService<IBootControl>(); }
};

// The main test class for the Boot HIDL HAL.
class BootHidlTest : public ::testing::VtsHalHidlTargetTestBase {
 public:
  virtual void SetUp() override {
      boot = ::testing::VtsHalHidlTargetTestBase::getService<IBootControl>(
          BootHidlEnvironment::Instance()->getServiceName<IBootControl>());
      ASSERT_NE(boot, nullptr);
  }

  virtual void TearDown() override {}

  sp<IBootControl> boot;
};

auto generate_callback(CommandResult *dest) {
  return [=](CommandResult cr) { *dest = cr; };
}

// Sanity check Boot::getNumberSlots().
TEST_F(BootHidlTest, GetNumberSlots) {
  uint32_t slots = boot->getNumberSlots();
  EXPECT_LE((uint32_t)2, slots);
}

// Sanity check Boot::getCurrentSlot().
TEST_F(BootHidlTest, GetCurrentSlot) {
  Slot curSlot = boot->getCurrentSlot();
  uint32_t slots = boot->getNumberSlots();
  EXPECT_LT(curSlot, slots);
}

// Sanity check Boot::markBootSuccessful().
TEST_F(BootHidlTest, MarkBootSuccessful) {
  CommandResult cr;
  Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
  ASSERT_TRUE(result.isOk());
  if (cr.success) {
    Slot curSlot = boot->getCurrentSlot();
    BoolResult ret = boot->isSlotMarkedSuccessful(curSlot);
    EXPECT_EQ(BoolResult::TRUE, ret);
  }
}

// Sanity check Boot::setActiveBootSlot() on good and bad inputs.
TEST_F(BootHidlTest, SetActiveBootSlot) {
  for (Slot s = 0; s < 2; s++) {
    CommandResult cr;
    Return<void> result = boot->setActiveBootSlot(s, generate_callback(&cr));
    EXPECT_TRUE(result.isOk());
  }
  {
    // Restore original flags to avoid problems on reboot
    CommandResult cr;
    Return<void> result = boot->markBootSuccessful(generate_callback(&cr));
    EXPECT_TRUE(result.isOk());
    EXPECT_TRUE(cr.success);
  }
  {
    CommandResult cr;
    uint32_t slots = boot->getNumberSlots();
    Return<void> result =
        boot->setActiveBootSlot(slots, generate_callback(&cr));
    ASSERT_TRUE(result.isOk());
    EXPECT_EQ(false, cr.success);
  }
}

// Sanity check Boot::setSlotAsUnbootable() on good and bad inputs.
TEST_F(BootHidlTest, SetSlotAsUnbootable) {
  {
    CommandResult cr;
    Slot curSlot = boot->getCurrentSlot();
    Slot otherSlot = curSlot ? 0 : 1;
    Return<void> result =
        boot->setSlotAsUnbootable(otherSlot, generate_callback(&cr));
    EXPECT_TRUE(result.isOk());
    if (cr.success) {
      EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));

      // Restore original flags to avoid problems on reboot
      result = boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
      EXPECT_TRUE(result.isOk());
      EXPECT_TRUE(cr.success);
      result = boot->setActiveBootSlot(curSlot, generate_callback(&cr));
      EXPECT_TRUE(result.isOk());
      EXPECT_TRUE(cr.success);
      result = boot->markBootSuccessful(generate_callback(&cr));
      EXPECT_TRUE(result.isOk());
      EXPECT_TRUE(cr.success);
    }
  }
  {
    CommandResult cr;
    uint32_t slots = boot->getNumberSlots();
    Return<void> result =
        boot->setSlotAsUnbootable(slots, generate_callback(&cr));
    EXPECT_TRUE(result.isOk());
    EXPECT_EQ(false, cr.success);
  }
}

// Sanity check Boot::isSlotBootable() on good and bad inputs.
TEST_F(BootHidlTest, IsSlotBootable) {
  for (Slot s = 0; s < 2; s++) {
    EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotBootable(s));
  }
  uint32_t slots = boot->getNumberSlots();
  EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotBootable(slots));
}

// Sanity check Boot::isSlotMarkedSuccessful() on good and bad inputs.
TEST_F(BootHidlTest, IsSlotMarkedSuccessful) {
  for (Slot s = 0; s < 2; s++) {
    EXPECT_NE(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(s));
  }
  uint32_t slots = boot->getNumberSlots();
  EXPECT_EQ(BoolResult::INVALID_SLOT, boot->isSlotMarkedSuccessful(slots));
}

// Sanity check Boot::getSuffix() on good and bad inputs.
TEST_F(BootHidlTest, GetSuffix) {
    string suffixStr;
    unordered_set<string> suffixes;
    auto cb = [&](hidl_string suffix) { suffixStr = suffix.c_str(); };
    for (Slot i = 0; i < boot->getNumberSlots(); i++) {
        CommandResult cr;
        Return<void> result = boot->getSuffix(i, cb);
        EXPECT_TRUE(result.isOk());
        ASSERT_EQ('_', suffixStr[0]);
        ASSERT_LE((unsigned)2, suffixStr.size());
        suffixes.insert(suffixStr);
    }
    // All suffixes should be unique
    ASSERT_EQ(boot->getNumberSlots(), suffixes.size());
    {
        string emptySuffix = "";
        Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb);
        EXPECT_TRUE(result.isOk());
        ASSERT_EQ(0, suffixStr.compare(emptySuffix));
    }
}

int main(int argc, char **argv) {
    ::testing::AddGlobalTestEnvironment(BootHidlEnvironment::Instance());
    ::testing::InitGoogleTest(&argc, argv);
    BootHidlEnvironment::Instance()->init(&argc, argv);
    int status = RUN_ALL_TESTS();
    LOG(INFO) << "Test result = " << status;
    return status;
}