Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "common/vsoc/shm/managed_e2e_test_region_layout.h" |
| 18 | #include "guest/vsoc/lib/e2e_test_common.h" |
| 19 | #include "guest/vsoc/lib/manager_region_view.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | using vsoc::layout::e2e_test::E2EManagedTestRegionLayout; |
| 25 | using vsoc::layout::e2e_test::E2EManagerTestRegionLayout; |
| 26 | |
| 27 | // Region view classes to allow calling the Open() function from the test. |
| 28 | class E2EManagedTestRegionView |
| 29 | : public vsoc::TypedRegionView< |
| 30 | E2EManagedTestRegionView, |
| 31 | E2EManagedTestRegionLayout> { |
| 32 | public: |
| 33 | using vsoc::TypedRegionView< |
| 34 | E2EManagedTestRegionView, E2EManagedTestRegionLayout>::Open; |
| 35 | }; |
| 36 | class E2EManagerTestRegionView |
| 37 | : public vsoc::ManagerRegionView< |
| 38 | E2EManagerTestRegionView, |
| 39 | E2EManagerTestRegionLayout> { |
| 40 | public: |
| 41 | using vsoc::ManagerRegionView< |
| 42 | E2EManagerTestRegionView, E2EManagerTestRegionLayout>::Open; |
| 43 | }; |
| 44 | |
| 45 | class ManagedRegionTest { |
| 46 | public: |
| 47 | void testManagedRegionFailMap() { |
| 48 | E2EManagedTestRegionView managed_region; |
| 49 | disable_tombstones(); |
| 50 | // managed_region.Open should never return. |
| 51 | EXPECT_FALSE(managed_region.Open()); |
| 52 | } |
| 53 | |
| 54 | void testManagedRegionMap() { |
| 55 | EXPECT_TRUE(manager_region_.Open()); |
| 56 | |
| 57 | // Maps correctly with permission |
| 58 | const uint32_t owned_value = 65, begin_offset = 4096, end_offset = 8192; |
| 59 | int perm_fd = manager_region_.CreateFdScopedPermission( |
| 60 | &manager_region_.data()->data[0], owned_value, begin_offset, |
| 61 | end_offset); |
| 62 | EXPECT_TRUE(perm_fd >= 0); |
| 63 | fd_scoped_permission perm; |
| 64 | ASSERT_TRUE(ioctl(perm_fd, VSOC_GET_FD_SCOPED_PERMISSION, &perm) == 0); |
| 65 | void* mapped_ptr = mmap(NULL, perm.end_offset - perm.begin_offset, |
| 66 | PROT_WRITE | PROT_READ, MAP_SHARED, perm_fd, 0); |
| 67 | EXPECT_FALSE(mapped_ptr == MAP_FAILED); |
| 68 | |
| 69 | // Owned value gets written |
| 70 | EXPECT_TRUE(manager_region_.data()->data[0] == owned_value); |
| 71 | |
| 72 | // Data written to the mapped memory stays there after unmap |
| 73 | std::string str = "managed by e2e_manager"; |
| 74 | strcpy(reinterpret_cast<char*>(mapped_ptr), str.c_str()); |
| 75 | EXPECT_TRUE(munmap(mapped_ptr, end_offset - begin_offset) == 0); |
| 76 | mapped_ptr = mmap(NULL, end_offset - begin_offset, PROT_WRITE | PROT_READ, |
| 77 | MAP_SHARED, perm_fd, 0); |
| 78 | EXPECT_FALSE(mapped_ptr == MAP_FAILED); |
| 79 | EXPECT_TRUE(strcmp(reinterpret_cast<char*>(mapped_ptr), str.c_str()) == 0); |
| 80 | |
| 81 | // Create permission elsewhere in the region, map same offset and length, |
| 82 | // ensure data isn't there |
| 83 | EXPECT_TRUE(munmap(mapped_ptr, end_offset - begin_offset) == 0); |
| 84 | close(perm_fd); |
| 85 | EXPECT_TRUE(manager_region_.data()->data[0] == 0); |
| 86 | perm_fd = manager_region_.CreateFdScopedPermission( |
| 87 | &manager_region_.data()->data[1], owned_value, begin_offset + 4096, |
| 88 | end_offset + 4096); |
| 89 | EXPECT_TRUE(perm_fd >= 0); |
| 90 | mapped_ptr = mmap(NULL, end_offset - begin_offset, PROT_WRITE | PROT_READ, |
| 91 | MAP_SHARED, perm_fd, 0); |
| 92 | EXPECT_FALSE(mapped_ptr == MAP_FAILED); |
| 93 | EXPECT_FALSE(strcmp(reinterpret_cast<char*>(mapped_ptr), str.c_str()) == 0); |
| 94 | } |
| 95 | ManagedRegionTest() {} |
| 96 | |
| 97 | private: |
| 98 | E2EManagerTestRegionView manager_region_; |
| 99 | }; |
| 100 | |
| 101 | TEST(ManagedRegionTest, ManagedRegionFailMap) { |
| 102 | ManagedRegionTest test; |
| 103 | EXPECT_EXIT(test.testManagedRegionFailMap(), testing::ExitedWithCode(2), |
| 104 | ".*" DEATH_TEST_MESSAGE ".*"); |
| 105 | } |
| 106 | |
| 107 | TEST(ManagedRegionTest, ManagedRegionMap) { |
| 108 | ManagedRegionTest test; |
| 109 | test.testManagedRegionMap(); |
| 110 | } |
| 111 | |
| 112 | int main(int argc, char** argv) { |
| 113 | if (argc == 2) { |
| 114 | // gtest tries to leave temporary files in the current directory, so make the |
| 115 | // current directory something that we control. |
| 116 | if (chdir(argv[1]) != 0) { |
| 117 | abort(); |
| 118 | } |
| 119 | } |
| 120 | android::base::InitLogging(argv); |
| 121 | testing::InitGoogleTest(&argc, argv); |
| 122 | return RUN_ALL_TESTS(); |
| 123 | } |