Boot HAL target-side test

Test: make vts; vts-tradefed run -m HalBootHidlTargetTest
Bug: 32022514
Change-Id: I65606790cc3b2f93d6706b70fad21c172635e0c7
Signed-off-by: Connor O'Brien <connoro@google.com>
diff --git a/boot/1.0/vts/Android.bp b/boot/1.0/vts/Android.bp
new file mode 100644
index 0000000..7aef46b
--- /dev/null
+++ b/boot/1.0/vts/Android.bp
@@ -0,0 +1,3 @@
+subdirs = [
+    "*"
+]
diff --git a/boot/1.0/vts/Android.mk b/boot/1.0/vts/Android.mk
index 8a56b27..9b30ef1 100644
--- a/boot/1.0/vts/Android.mk
+++ b/boot/1.0/vts/Android.mk
@@ -77,3 +77,5 @@
 LOCAL_PROTOC_OPTIMIZE_TYPE := full
 
 include $(BUILD_SHARED_LIBRARY)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/boot/1.0/vts/functional/Android.bp b/boot/1.0/vts/functional/Android.bp
new file mode 100644
index 0000000..714a18b
--- /dev/null
+++ b/boot/1.0/vts/functional/Android.bp
@@ -0,0 +1,40 @@
+//
+// 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.
+//
+
+cc_test {
+    name: "boot_hidl_hal_test",
+    gtest: true,
+    srcs: ["boot_hidl_hal_test.cpp"],
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libcutils",
+        "libhidlbase",
+        "libhwbinder",
+        "libnativehelper",
+        "libutils",
+        "android.hardware.boot@1.0",
+    ],
+    static_libs: ["libgtest"],
+    cflags: [
+        "--coverage",
+        "-O0",
+        "-g",
+    ],
+    ldflags: [
+        "--coverage"
+    ]
+}
diff --git a/boot/1.0/vts/functional/Android.mk b/boot/1.0/vts/functional/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/1.0/vts/functional/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
diff --git a/boot/1.0/vts/functional/boot_hidl_hal_test.cpp b/boot/1.0/vts/functional/boot_hidl_hal_test.cpp
new file mode 100644
index 0000000..7b002b9
--- /dev/null
+++ b/boot/1.0/vts/functional/boot_hidl_hal_test.cpp
@@ -0,0 +1,171 @@
+/*
+ * 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 <gtest/gtest.h>
+
+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;
+
+// The main test class for the Boot HIDL HAL.
+class BootHidlTest : public ::testing::Test {
+ public:
+  virtual void SetUp() override {
+    // TODO(b/33385836) Delete copied code
+    bool getStub = false;
+    char getsubProperty[PROPERTY_VALUE_MAX];
+    if (property_get("vts.hidl.get_stub", getsubProperty, "") > 0) {
+      if (!strcmp(getsubProperty, "true") || !strcmp(getsubProperty, "True") ||
+          !strcmp(getsubProperty, "1")) {
+        getStub = true;
+      }
+    }
+    boot = IBootControl::getService("bootctrl", getStub);
+    ASSERT_NE(boot, nullptr);
+    ASSERT_EQ(!getStub, boot->isRemote());
+  }
+
+  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.getStatus().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.getStatus().isOk());
+  }
+  {
+    CommandResult cr;
+    uint32_t slots = boot->getNumberSlots();
+    Return<void> result =
+        boot->setActiveBootSlot(slots, generate_callback(&cr));
+    ASSERT_TRUE(result.getStatus().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.getStatus().isOk());
+    if (cr.success) {
+      EXPECT_EQ(BoolResult::FALSE, boot->isSlotBootable(otherSlot));
+      boot->setActiveBootSlot(otherSlot, generate_callback(&cr));
+      EXPECT_TRUE(cr.success);
+    }
+  }
+  {
+    CommandResult cr;
+    uint32_t slots = boot->getNumberSlots();
+    Return<void> result =
+        boot->setSlotAsUnbootable(slots, generate_callback(&cr));
+    EXPECT_TRUE(result.getStatus().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) {
+  const char *suffixPtr;
+  auto cb = [&](hidl_string suffix) { suffixPtr = suffix.c_str(); };
+  for (Slot i = 0; i < 2; i++) {
+    CommandResult cr;
+    Return<void> result = boot->getSuffix(i, cb);
+    EXPECT_TRUE(result.getStatus().isOk());
+    char correctSuffix[3];
+    snprintf(correctSuffix, sizeof(correctSuffix), "_%c", 'a' + i);
+    ASSERT_EQ(0, strcmp(suffixPtr, correctSuffix));
+  }
+  {
+    char emptySuffix[] = "";
+    Return<void> result = boot->getSuffix(boot->getNumberSlots(), cb);
+    EXPECT_TRUE(result.getStatus().isOk());
+    ASSERT_EQ(0, strcmp(emptySuffix, suffixPtr));
+  }
+}
+
+int main(int argc, char **argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  int status = RUN_ALL_TESTS();
+  LOG(INFO) << "Test result = " << status;
+  return status;
+}
diff --git a/boot/1.0/vts/functional/vts/Android.mk b/boot/1.0/vts/functional/vts/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
diff --git a/boot/1.0/vts/functional/vts/testcases/Android.mk b/boot/1.0/vts/functional/vts/testcases/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/testcases/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
diff --git a/boot/1.0/vts/functional/vts/testcases/hal/Android.mk b/boot/1.0/vts/functional/vts/testcases/hal/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/testcases/hal/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
diff --git a/boot/1.0/vts/functional/vts/testcases/hal/boot/Android.mk b/boot/1.0/vts/functional/vts/testcases/hal/boot/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/testcases/hal/boot/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
diff --git a/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/Android.mk b/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
diff --git a/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/target/Android.mk b/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/target/Android.mk
new file mode 100644
index 0000000..844b93b
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/target/Android.mk
@@ -0,0 +1,25 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := HalBootHidlTargetTest
+VTS_CONFIG_SRC_DIR := testcases/hal/boot/hidl/target
+include test/vts/tools/build/Android.host_config.mk
diff --git a/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/target/AndroidTest.xml b/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/target/AndroidTest.xml
new file mode 100644
index 0000000..6c7809c
--- /dev/null
+++ b/boot/1.0/vts/functional/vts/testcases/hal/boot/hidl/target/AndroidTest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<configuration description="Config for VTS Boot HIDL HAL's target-side test cases">
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.VtsFilePusher">
+        <option name="push-group" value="HidlHalTest.push" />
+    </target_preparer>
+    <target_preparer class="com.android.tradefed.targetprep.VtsPythonVirtualenvPreparer" />
+    <test class="com.android.tradefed.testtype.VtsMultiDeviceTest">
+        <option name="test-module-name" value="HalBootHidlTargetTest"/>
+        <option name="binary-test-sources" value="
+            _32bit::DATA/nativetest/boot_hidl_hal_test/boot_hidl_hal_test,
+            _64bit::DATA/nativetest64/boot_hidl_hal_test/boot_hidl_hal_test,
+            "/>
+        <option name="binary-test-type" value="gtest" />
+        <option name="test-timeout" value="1m" />
+    </test>
+</configuration>
diff --git a/boot/Android.bp b/boot/Android.bp
index bbb3e4b..67af5bb 100644
--- a/boot/Android.bp
+++ b/boot/Android.bp
@@ -1,4 +1,6 @@
 // This is an autogenerated file, do not edit.
 subdirs = [
     "1.0",
+    "1.0/vts",
+    "1.0/vts/functional",
 ]
diff --git a/boot/Android.mk b/boot/Android.mk
new file mode 100644
index 0000000..f9e3276
--- /dev/null
+++ b/boot/Android.mk
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(call all-subdir-makefiles)