Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <dlfcn.h> |
| 18 | |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <linux/ashmem.h> |
| 22 | #include <sys/mman.h> |
| 23 | |
| 24 | #include <android/ashmemd/IAshmemDeviceService.h> |
| 25 | |
| 26 | using android::IBinder; |
| 27 | using android::IServiceManager; |
| 28 | using android::String16; |
| 29 | using android::ashmemd::IAshmemDeviceService; |
| 30 | using android::os::ParcelFileDescriptor; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace ashmemd { |
| 34 | |
| 35 | class AshmemdTest : public ::testing::Test { |
| 36 | public: |
| 37 | virtual void SetUp() override { |
| 38 | sp<IServiceManager> sm = android::defaultServiceManager(); |
| 39 | sp<IBinder> binder = sm->getService(String16("ashmem_device_service")); |
| 40 | ASSERT_NE(binder, nullptr); |
| 41 | |
| 42 | ashmemService = android::interface_cast<IAshmemDeviceService>(binder); |
| 43 | ASSERT_NE(ashmemService, nullptr); |
| 44 | } |
| 45 | |
| 46 | void openFd(ParcelFileDescriptor* fd) { |
| 47 | auto status = ashmemService->open(fd); |
| 48 | ASSERT_TRUE(status.isOk()); |
| 49 | ASSERT_GE(fd->get(), 0); |
| 50 | } |
| 51 | |
| 52 | sp<IAshmemDeviceService> ashmemService; |
| 53 | }; |
| 54 | |
| 55 | TEST_F(AshmemdTest, OpenFd) { |
| 56 | ParcelFileDescriptor fd; |
| 57 | openFd(&fd); |
| 58 | } |
| 59 | |
| 60 | TEST_F(AshmemdTest, OpenMultipleFds) { |
| 61 | ParcelFileDescriptor fd1; |
| 62 | ParcelFileDescriptor fd2; |
| 63 | openFd(&fd1); |
| 64 | openFd(&fd2); |
| 65 | ASSERT_NE(fd1.get(), fd2.get()); |
| 66 | } |
| 67 | |
| 68 | TEST_F(AshmemdTest, MmapFd) { |
| 69 | ParcelFileDescriptor pfd; |
| 70 | openFd(&pfd); |
| 71 | int fd = pfd.get(); |
| 72 | size_t testSize = 2097152; |
| 73 | |
| 74 | ASSERT_EQ(ioctl(fd, ASHMEM_SET_NAME, "AshmemdTest"), 0); |
| 75 | ASSERT_EQ(ioctl(fd, ASHMEM_SET_SIZE, testSize), 0); |
| 76 | |
| 77 | void* data = mmap(NULL, testSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 78 | ASSERT_NE(data, MAP_FAILED) << "Failed to mmap() ashmem fd"; |
| 79 | ASSERT_EQ(munmap(data, testSize), 0) << "Failed to munmap() ashmem fd"; |
| 80 | } |
| 81 | |
| 82 | TEST(LibAshmemdClientTest, OpenFd) { |
| 83 | void* handle = dlopen("libashmemd_client.so", RTLD_NOW); |
| 84 | ASSERT_NE(handle, nullptr) << "Failed to dlopen() libashmemd_client.so: " << dlerror(); |
| 85 | |
| 86 | auto function = (int (*)())dlsym(handle, "openAshmemdFd"); |
| 87 | ASSERT_NE(function, nullptr) << "Failed to dlsym() openAshmemdFd() function: " << dlerror(); |
| 88 | |
| 89 | int fd = function(); |
| 90 | ASSERT_GE(fd, 0) << "Failed to open /dev/ashmem"; |
| 91 | } |
| 92 | |
| 93 | } // namespace ashmemd |
| 94 | } // namespace android |