Merge "Add a VTS to verify the behavior of the DMA-BUF system heap" am: 815badff0d am: 625ce0cac9

Original change: https://android-review.googlesource.com/c/platform/system/memory/libdmabufheap/+/1609614

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I7baa3495319bab941f7133293e451972e8683385
diff --git a/tests/Android.bp b/tests/Android.bp
index bfac9ea..25eec5c 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -36,7 +36,11 @@
     srcs: [
           "dmabuf_heap_test.cpp",
     ],
-    test_suites: ["device-tests"],
+    test_suites: [
+        "general-tests",
+        "vts"
+    ],
+    test_config: "dmabufheap-unit-tests.xml",
 }
 
 cc_test {
diff --git a/tests/dmabuf_heap_test.cpp b/tests/dmabuf_heap_test.cpp
index a80d513..2c390a7 100644
--- a/tests/dmabuf_heap_test.cpp
+++ b/tests/dmabuf_heap_test.cpp
@@ -275,9 +275,10 @@
     }
 }
 
-TEST_F(DmaBufHeapTest, TestHeapQuery) {
+TEST_F(DmaBufHeapTest, TestDmabufSystemHeapCompliance) {
     using android::vintf::KernelVersion;
 
+    static const size_t kAllocSizeInBytes = 4096;
     if (android::base::GetIntProperty("ro.product.first_api_level", 0) < __ANDROID_API_S__) {
         GTEST_SKIP();
     }
@@ -293,5 +294,32 @@
 
     auto heap_list = allocator->GetDmabufHeapList();
     ASSERT_TRUE(heap_list.find("system") != heap_list.end());
-    ASSERT_TRUE(heap_list.find("system-uncached") != heap_list.end());
+
+    /*
+     * Test that system heap can be allocated from.
+     */
+    int map_fd = allocator->Alloc(kDmabufSystemHeapName, kAllocSizeInBytes);
+    ASSERT_GE(map_fd, 0);
+
+    /*
+     * Test that system heap can be mmapped by the CPU.
+     */
+    void* ptr;
+    ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+    ASSERT_TRUE(ptr != NULL);
+
+    /*
+     * Test that the allocated memory is zeroed.
+     */
+    auto zeroes_ptr = std::make_unique<char[]>(kAllocSizeInBytes);
+    int ret = allocator->CpuSyncStart(map_fd);
+    ASSERT_EQ(0, ret);
+
+    ASSERT_EQ(0, memcmp(ptr, zeroes_ptr.get(), kAllocSizeInBytes));
+
+    ret = allocator->CpuSyncEnd(map_fd);
+    ASSERT_EQ(0, ret);
+
+    ASSERT_EQ(0, munmap(ptr, kAllocSizeInBytes));
+    ASSERT_EQ(0, close(map_fd));
 }
diff --git a/tests/dmabufheap-unit-tests.xml b/tests/dmabufheap-unit-tests.xml
new file mode 100644
index 0000000..3cfa319
--- /dev/null
+++ b/tests/dmabufheap-unit-tests.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2021 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 dmabufheap-unit-tests">
+    <object type="module_controller" class="com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController">
+        <option name="min-api-level" value="30" />
+    </object>
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer"/>
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push" value="dmabufheap-unit-tests->/data/local/tmp/dmabufheap-unit-tests" />
+    </target_preparer>
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="dmabufheap-unit-tests" />
+        <option name="include-filter" value="*TestDmabufSystemHeapCompliance*" />
+    </test>
+</configuration>