VTS tests for allocator HAL

Test: VtsHidlAllocatorV1_0TargetTest --hal_service_instance=android.hidl.allocator@1.0::IAllocator/ashmem
Bug: 35915182

Change-Id: I24d22114e5afbf11fc34402278ea6d97dd8b29ae
diff --git a/transport/allocator/1.0/vts/functional/Android.bp b/transport/allocator/1.0/vts/functional/Android.bp
new file mode 100644
index 0000000..797d3f8
--- /dev/null
+++ b/transport/allocator/1.0/vts/functional/Android.bp
@@ -0,0 +1,28 @@
+//
+// Copyright (C) 2018 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: "VtsHidlAllocatorV1_0TargetTest",
+    defaults: ["VtsHalTargetTestDefaults"],
+    srcs: ["VtsHidlAllocatorV1_0TargetTest.cpp"],
+    shared_libs: [
+        "libhidlmemory",
+    ],
+    static_libs: [
+        "android.hidl.allocator@1.0",
+    ],
+}
+
diff --git a/transport/allocator/1.0/vts/functional/Android.mk b/transport/allocator/1.0/vts/functional/Android.mk
new file mode 100644
index 0000000..62880ed
--- /dev/null
+++ b/transport/allocator/1.0/vts/functional/Android.mk
@@ -0,0 +1,22 @@
+#
+# Copyright (C) 2018 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 $(CLEAR_VARS)
+
+LOCAL_MODULE := VtsHidlAllocatorV1_0Target
+-include test/vts/tools/build/Android.host_config.mk
diff --git a/transport/allocator/1.0/vts/functional/AndroidTest.xml b/transport/allocator/1.0/vts/functional/AndroidTest.xml
new file mode 100644
index 0000000..52364a7
--- /dev/null
+++ b/transport/allocator/1.0/vts/functional/AndroidTest.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2018 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 VtsHidlAllocatorV1_0Target test cases">
+    <option name="config-descriptor:metadata" key="plan" value="vts-treble" />
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.VtsFilePusher">
+        <option name="abort-on-push-failure" value="false"/>
+        <option name="push-group" value="HalHidlTargetTest.push"/>
+    </target_preparer>
+    <multi_target_preparer class="com.android.tradefed.targetprep.VtsPythonVirtualenvPreparer" />
+    <test class="com.android.tradefed.testtype.VtsMultiDeviceTest">
+        <option name="test-module-name" value="VtsHidlAllocatorV1_0Target"/>
+        <option name="binary-test-working-directory" value="_32bit::/data/nativetest/" />
+        <option name="binary-test-working-directory" value="_64bit::/data/nativetest64/" />
+        <option name="binary-test-source" value="_32bit::DATA/nativetest/VtsHidlAllocatorV1_0TargetTest/VtsHidlAllocatorV1_0TargetTest" />
+        <option name="binary-test-source" value="_64bit::DATA/nativetest64/VtsHidlAllocatorV1_0TargetTest/VtsHidlAllocatorV1_0TargetTest" />
+        <option name="binary-test-type" value="gtest"/>
+        <option name="test-timeout" value="5m"/>
+    </test>
+</configuration>
diff --git a/transport/allocator/1.0/vts/functional/VtsHidlAllocatorV1_0TargetTest.cpp b/transport/allocator/1.0/vts/functional/VtsHidlAllocatorV1_0TargetTest.cpp
new file mode 100644
index 0000000..39ce606
--- /dev/null
+++ b/transport/allocator/1.0/vts/functional/VtsHidlAllocatorV1_0TargetTest.cpp
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
+#include <android-base/logging.h>
+#include <android/hidl/allocator/1.0/IAllocator.h>
+#include <android/hidl/memory/1.0/IMemory.h>
+#include <hidlmemory/mapping.h>
+
+using ::android::sp;
+using ::android::hardware::hidl_memory;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hidl::allocator::V1_0::IAllocator;
+using ::android::hidl::memory::V1_0::IMemory;
+
+#define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
+#define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
+
+class AllocatorEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+   public:
+    virtual void registerTestServices() override { registerTestService<IAllocator>(); }
+
+    static AllocatorEnvironment* instance() {
+        static AllocatorEnvironment* instance = new AllocatorEnvironment();
+        return instance;
+    };
+};
+
+class AllocatorHidlTest : public ::testing::VtsHalHidlTargetTestBase {
+   public:
+    virtual void SetUp() override {
+        allocator = getService<IAllocator>(AllocatorEnvironment::instance());
+        ASSERT_NE(allocator, nullptr);
+    }
+
+    sp<IMemory> expectAllocateSuccess(size_t size) {
+        sp<IMemory> memory;
+        EXPECT_OK(allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
+            ASSERT_TRUE(success) << "Allocate failed for size: " << size;
+            EXPECT_EQ(mem.size(), size)
+                << "Allocated " << size << " but got hidl_memory with size " << mem.size();
+            memory = mapMemory(mem);
+        }));
+        EXPECT_NE(nullptr, memory.get());
+        EXPECT_EQ(memory->getSize(), size)
+            << "Allocated " << size << " but got IMemory with size " << memory->getSize();
+        return memory;
+    }
+
+    std::vector<sp<IMemory>> expectBatchAllocateSuccess(size_t size, size_t count) {
+        std::vector<sp<IMemory>> memories;
+        memories.reserve(count);
+        EXPECT_OK(allocator->batchAllocate(
+            size, count, [&](bool success, const hidl_vec<hidl_memory>& mems) {
+                EXPECT_EQ(count, mems.size());
+
+                for (const hidl_memory& mem : mems) {
+                    ASSERT_TRUE(success) << "Allocate failed for size: " << size;
+                    EXPECT_EQ(mem.size(), size)
+                        << "Allocated " << size << " but got hidl_memory with size " << mem.size();
+                    memories.push_back(mapMemory(mem));
+                }
+            }));
+        for (const sp<IMemory>& memory : memories) {
+            EXPECT_NE(nullptr, memory.get());
+            EXPECT_EQ(memory->getSize(), size)
+                << "Allocated " << size << " but got IMemory with size " << memory->getSize();
+        }
+        return memories;
+    }
+
+    sp<IAllocator> allocator;
+};
+
+TEST_F(AllocatorHidlTest, TestAllocateSizes) {
+    for (size_t size : {1, 1023, 1024, 1025, 4096}) {
+        expectAllocateSuccess(size);
+    }
+}
+
+TEST_F(AllocatorHidlTest, TestBatchAllocateSizes) {
+    for (size_t count : {1, 1, 2, 3, 10}) {
+        for (size_t size : {1, 1023, 1024, 1025, 4096}) {
+            expectBatchAllocateSuccess(size, count);
+        }
+    }
+}
+
+TEST_F(AllocatorHidlTest, TestCommit) {
+    constexpr size_t kSize = 1337;
+
+    sp<IMemory> memory = expectAllocateSuccess(kSize);
+    for (int i = 0; i < 100; i++) {
+        EXPECT_OK(memory->read());
+        EXPECT_OK(memory->update());
+        EXPECT_OK(memory->commit());
+
+        EXPECT_OK(memory->read());
+        EXPECT_OK(memory->commit());
+
+        EXPECT_OK(memory->update());
+        EXPECT_OK(memory->commit());
+    }
+
+    for (int i = 0; i < kSize; i++) {
+        EXPECT_OK(memory->readRange(i, kSize));
+        EXPECT_OK(memory->updateRange(i, kSize));
+        EXPECT_OK(memory->commit());
+
+        EXPECT_OK(memory->readRange(i, kSize));
+        EXPECT_OK(memory->commit());
+
+        EXPECT_OK(memory->updateRange(i, kSize));
+        EXPECT_OK(memory->commit());
+    }
+}
+
+int main(int argc, char** argv) {
+    ::testing::AddGlobalTestEnvironment(AllocatorEnvironment::instance());
+    ::testing::InitGoogleTest(&argc, argv);
+    AllocatorEnvironment::instance()->init(&argc, argv);
+    return RUN_ALL_TESTS();
+}