Merge "hwcryptohal: Parcelable transfer test" into main am: e6e8eac9e1

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/3530897

Change-Id: Id1f567eea9325a3b7be7c90d681f968169add355
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/security/see/hwcrypto/default/Android.bp b/security/see/hwcrypto/default/Android.bp
index ab23cfd..7a4a7b6 100644
--- a/security/see/hwcrypto/default/Android.bp
+++ b/security/see/hwcrypto/default/Android.bp
@@ -112,6 +112,7 @@
 cc_test {
     name: "HwCryptoHalDelegatorTests",
     enabled: false,
+    require_root: true,
     srcs: [
         "delegatorTest.cpp",
     ],
@@ -124,12 +125,14 @@
     shared_libs: [
         "libbase",
         "liblog",
+        "libutils",
         "libbinder",
         "libbinder_ndk",
     ],
     static_libs: [
         "android.hardware.security.see.hwcrypto-V1-ndk",
         "android.hardware.security.see.hwcrypto-V1-cpp",
+        "hwcryptohallib",
     ],
 
     arch: {
diff --git a/security/see/hwcrypto/default/delegatorTest.cpp b/security/see/hwcrypto/default/delegatorTest.cpp
index a80d6fd..28b177d 100644
--- a/security/see/hwcrypto/default/delegatorTest.cpp
+++ b/security/see/hwcrypto/default/delegatorTest.cpp
@@ -1,11 +1,84 @@
+#include <android/binder_manager.h>
 #include <gtest/gtest.h>
+#include <linux/dma-heap.h>
+#include <sys/auxv.h>
+#include <sys/mman.h>
 #include "hwcryptokeyimpl.h"
 
+static inline bool align_overflow(size_t size, size_t alignment, size_t* aligned) {
+    if (size % alignment == 0) {
+        *aligned = size;
+        return false;
+    }
+    size_t temp = 0;
+    bool overflow = __builtin_add_overflow(size / alignment, 1, &temp);
+    overflow |= __builtin_mul_overflow(temp, alignment, aligned);
+    return overflow;
+}
+
+static int allocate_buffers(size_t size) {
+    const char* device_name = "/dev/dma_heap/system";
+    int dma_heap_fd = open(device_name, O_RDONLY | O_CLOEXEC);
+    if (dma_heap_fd < 0) {
+        LOG(ERROR) << "Cannot open " << device_name;
+        return -1;
+    }
+    size_t aligned = 0;
+    if (align_overflow(size, getauxval(AT_PAGESZ), &aligned)) {
+        LOG(ERROR) << "Rounding up buffer size oveflowed";
+        return -1;
+    }
+    struct dma_heap_allocation_data allocation_request = {
+            .len = aligned,
+            .fd_flags = O_RDWR | O_CLOEXEC,
+    };
+    int rc = ioctl(dma_heap_fd, DMA_HEAP_IOCTL_ALLOC, &allocation_request);
+    if (rc < 0) {
+        LOG(ERROR) << "Buffer allocation request failed  " << rc;
+        return -1;
+    }
+    int fd = allocation_request.fd;
+    if (fd < 0) {
+        LOG(ERROR) << "Allocation request returned bad fd" << fd;
+        return -1;
+    }
+    return fd;
+}
+
 int main(int argc, char** argv) {
     ::testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
 }
 
+TEST(HwCryptoHalDelegator, FdTest) {
+    const std::string instance =
+            std::string() + ndk_hwcrypto::IHwCryptoKey::descriptor + "/default";
+    ndk::SpAIBinder binder(AServiceManager_waitForService(instance.c_str()));
+    ASSERT_NE(binder, nullptr);
+    auto hwCryptoKey = ndk_hwcrypto::IHwCryptoKey::fromBinder(binder);
+    ASSERT_NE(hwCryptoKey, nullptr);
+    auto fd = allocate_buffers(4096);
+    EXPECT_GE(fd, 0);
+    ndk::ScopedFileDescriptor ndkFd(fd);
+    ndk_hwcrypto::MemoryBufferParameter memBuffParam = ndk_hwcrypto::MemoryBufferParameter();
+    memBuffParam.bufferHandle.set<ndk_hwcrypto::MemoryBufferParameter::MemoryBuffer::input>(
+            std::move(ndkFd));
+    memBuffParam.sizeBytes = 4096;
+    auto operation = ndk_hwcrypto::CryptoOperation();
+    operation.set<ndk_hwcrypto::CryptoOperation::setMemoryBuffer>(std::move(memBuffParam));
+    ndk_hwcrypto::CryptoOperationSet operationSet = ndk_hwcrypto::CryptoOperationSet();
+    operationSet.context = nullptr;
+    operationSet.operations.push_back(std::move(operation));
+    std::vector<ndk_hwcrypto::CryptoOperationSet> operationSets;
+    operationSets.push_back(std::move(operationSet));
+    std::vector<ndk_hwcrypto::CryptoOperationResult> aidl_return;
+    std::shared_ptr<ndk_hwcrypto::IHwCryptoOperations> hwCryptoOperations;
+    auto res = hwCryptoKey->getHwCryptoOperations(&hwCryptoOperations);
+    EXPECT_TRUE(res.isOk());
+    res = hwCryptoOperations->processCommandList(&operationSets, &aidl_return);
+    EXPECT_TRUE(res.isOk());
+}
+
 TEST(HwCryptoHalDelegator, keyPolicyCppToNdk) {
     cpp_hwcrypto::KeyPolicy cppPolicy = cpp_hwcrypto::KeyPolicy();
     cppPolicy.keyType = cpp_hwcrypto::types::KeyType::AES_128_CBC_PKCS7_PADDING;
@@ -46,4 +119,4 @@
               cpp_hwcrypto::types::KeyPermissions::ALLOW_EPHEMERAL_KEY_WRAPPING);
     EXPECT_EQ(cppPolicy.keyPermissions[1],
               cpp_hwcrypto::types::KeyPermissions::ALLOW_HARDWARE_KEY_WRAPPING);
-}
\ No newline at end of file
+}