blob: 278dd2bf81b8b7a2ad3d41263e9623d38485a60d [file] [log] [blame]
Atneya Nair7ade4f42022-02-07 18:16:48 -05001/*
2 * Copyright (C) 2021 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 <binder/MemoryHeapBase.h>
18#include <cutils/ashmem.h>
19#include <fcntl.h>
20
21#include <gtest/gtest.h>
22using namespace android;
23#ifdef __BIONIC__
24TEST(MemoryHeapBase, ForceMemfdRespected) {
25 auto mHeap = sp<MemoryHeapBase>::make(10, MemoryHeapBase::FORCE_MEMFD, "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040026 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050027 int fd = mHeap->getHeapID();
28 EXPECT_NE(fd, -1);
29 EXPECT_FALSE(ashmem_valid(fd));
30 EXPECT_NE(fcntl(fd, F_GET_SEALS), -1);
31}
32
33TEST(MemoryHeapBase, MemfdSealed) {
34 auto mHeap = sp<MemoryHeapBase>::make(8192,
35 MemoryHeapBase::FORCE_MEMFD,
36 "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040037 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050038 int fd = mHeap->getHeapID();
39 EXPECT_NE(fd, -1);
40 EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_SEAL);
41}
42
43TEST(MemoryHeapBase, MemfdUnsealed) {
44 auto mHeap = sp<MemoryHeapBase>::make(8192,
45 MemoryHeapBase::FORCE_MEMFD |
46 MemoryHeapBase::MEMFD_ALLOW_SEALING,
47 "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040048 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050049 int fd = mHeap->getHeapID();
50 EXPECT_NE(fd, -1);
51 EXPECT_EQ(fcntl(fd, F_GET_SEALS), 0);
52}
53
54TEST(MemoryHeapBase, MemfdSealedProtected) {
55 auto mHeap = sp<MemoryHeapBase>::make(8192,
56 MemoryHeapBase::FORCE_MEMFD |
57 MemoryHeapBase::READ_ONLY,
58 "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040059 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050060 int fd = mHeap->getHeapID();
61 EXPECT_NE(fd, -1);
62 EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_SEAL | F_SEAL_FUTURE_WRITE);
63}
64
65TEST(MemoryHeapBase, MemfdUnsealedProtected) {
66 auto mHeap = sp<MemoryHeapBase>::make(8192,
67 MemoryHeapBase::FORCE_MEMFD |
68 MemoryHeapBase::READ_ONLY |
69 MemoryHeapBase::MEMFD_ALLOW_SEALING,
70 "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040071 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050072 int fd = mHeap->getHeapID();
73 EXPECT_NE(fd, -1);
74 EXPECT_EQ(fcntl(fd, F_GET_SEALS), F_SEAL_FUTURE_WRITE);
75}
76
77#else
78TEST(MemoryHeapBase, HostMemfdExpected) {
79 auto mHeap = sp<MemoryHeapBase>::make(8192,
80 MemoryHeapBase::READ_ONLY,
81 "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040082 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050083 int fd = mHeap->getHeapID();
84 void* ptr = mHeap->getBase();
85 EXPECT_NE(ptr, MAP_FAILED);
86 EXPECT_TRUE(ashmem_valid(fd));
87 EXPECT_EQ(mHeap->getFlags(), MemoryHeapBase::READ_ONLY);
88}
89
90TEST(MemoryHeapBase,HostMemfdException) {
91 auto mHeap = sp<MemoryHeapBase>::make(8192,
92 MemoryHeapBase::FORCE_MEMFD |
93 MemoryHeapBase::READ_ONLY |
94 MemoryHeapBase::MEMFD_ALLOW_SEALING,
95 "Test mapping");
Atneya Nair3fd092e2022-05-24 16:50:12 -040096 ASSERT_NE(mHeap.get(), nullptr);
Atneya Nair7ade4f42022-02-07 18:16:48 -050097 int fd = mHeap->getHeapID();
98 void* ptr = mHeap->getBase();
99 EXPECT_EQ(mHeap->getFlags(), MemoryHeapBase::READ_ONLY);
100 EXPECT_TRUE(ashmem_valid(fd));
101 EXPECT_NE(ptr, MAP_FAILED);
102}
103
104#endif