1 /*
2 * Copyright (C) 2013 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 <sys/mman.h>
18
19 #include <gtest/gtest.h>
20
21 #include <ion/ion.h>
22
23 #include "ion_test_fixture.h"
24
25 class InvalidValues : public IonAllHeapsTest {
26 public:
27 virtual void SetUp();
28 virtual void TearDown();
29 ion_user_handle_t m_validHandle;
30 int m_validShareFd;
31 ion_user_handle_t const m_badHandle = -1;
32 };
33
SetUp()34 void InvalidValues::SetUp()
35 {
36 IonAllHeapsTest::SetUp();
37 ASSERT_EQ(0, ion_alloc(m_ionFd, 4096, 0, m_firstHeap, 0, &m_validHandle))
38 << m_ionFd << " " << m_firstHeap;
39 ASSERT_TRUE(m_validHandle != 0);
40 ASSERT_EQ(0, ion_share(m_ionFd, m_validHandle, &m_validShareFd));
41 }
42
TearDown()43 void InvalidValues::TearDown()
44 {
45 ASSERT_EQ(0, ion_free(m_ionFd, m_validHandle));
46 ASSERT_EQ(0, close(m_validShareFd));
47 m_validHandle = 0;
48 IonAllHeapsTest::TearDown();
49 }
50
TEST_F(InvalidValues,ion_close)51 TEST_F(InvalidValues, ion_close)
52 {
53 EXPECT_EQ(-EBADF, ion_close(-1));
54 }
55
TEST_F(InvalidValues,ion_alloc)56 TEST_F(InvalidValues, ion_alloc)
57 {
58 ion_user_handle_t handle;
59 /* invalid ion_fd */
60 int ret = ion_alloc(0, 4096, 0, m_firstHeap, 0, &handle);
61 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
62 /* invalid ion_fd */
63 EXPECT_EQ(-EBADF, ion_alloc(-1, 4096, 0, m_firstHeap, 0, &handle));
64 /* no heaps */
65 EXPECT_EQ(-ENODEV, ion_alloc(m_ionFd, 4096, 0, 0, 0, &handle));
66 for (unsigned int heapMask : m_allHeaps) {
67 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
68 /* zero size */
69 EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, 0, 0, heapMask, 0, &handle));
70 /* too large size */
71 EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, -1, 0, heapMask, 0, &handle));
72 /* bad alignment */
73 EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, 4096, -1, heapMask, 0, &handle));
74 /* NULL handle */
75 EXPECT_EQ(-EINVAL, ion_alloc(m_ionFd, 4096, 0, heapMask, 0, NULL));
76 }
77 }
78
TEST_F(InvalidValues,ion_alloc_fd)79 TEST_F(InvalidValues, ion_alloc_fd)
80 {
81 int fd;
82 /* invalid ion_fd */
83 int ret = ion_alloc_fd(0, 4096, 0, m_firstHeap, 0, &fd);
84 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
85 /* invalid ion_fd */
86 EXPECT_EQ(-EBADF, ion_alloc_fd(-1, 4096, 0, m_firstHeap, 0, &fd));
87 /* no heaps */
88 EXPECT_EQ(-ENODEV, ion_alloc_fd(m_ionFd, 4096, 0, 0, 0, &fd));
89 for (unsigned int heapMask : m_allHeaps) {
90 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
91 /* zero size */
92 EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, 0, 0, heapMask, 0, &fd));
93 /* too large size */
94 EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, -1, 0, heapMask, 0, &fd));
95 /* bad alignment */
96 EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, 4096, -1, heapMask, 0, &fd));
97 /* NULL handle */
98 EXPECT_EQ(-EINVAL, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, 0, NULL));
99 }
100 }
101
TEST_F(InvalidValues,ion_free)102 TEST_F(InvalidValues, ion_free)
103 {
104 /* invalid ion fd */
105 int ret = ion_free(0, m_validHandle);
106 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
107 /* invalid ion fd */
108 EXPECT_EQ(-EBADF, ion_free(-1, m_validHandle));
109 /* zero handle */
110 EXPECT_EQ(-EINVAL, ion_free(m_ionFd, 0));
111 /* bad handle */
112 EXPECT_EQ(-EINVAL, ion_free(m_ionFd, m_badHandle));
113 }
114
TEST_F(InvalidValues,ion_map)115 TEST_F(InvalidValues, ion_map)
116 {
117 int map_fd;
118 unsigned char *ptr;
119
120 /* invalid ion fd */
121 int ret = ion_map(0, m_validHandle, 4096, PROT_READ, 0, 0, &ptr, &map_fd);
122 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
123 /* invalid ion fd */
124 EXPECT_EQ(-EBADF, ion_map(-1, m_validHandle, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
125 /* zero handle */
126 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, 0, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
127 /* bad handle */
128 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_badHandle, 4096, PROT_READ, 0, 0, &ptr, &map_fd));
129 /* zero length */
130 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 0, PROT_READ, 0, 0, &ptr, &map_fd));
131 /* bad prot */
132 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, -1, 0, 0, &ptr, &map_fd));
133 /* bad offset */
134 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, PROT_READ, 0, -1, &ptr, &map_fd));
135 /* NULL ptr */
136 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, PROT_READ, 0, 0, NULL, &map_fd));
137 /* NULL map_fd */
138 EXPECT_EQ(-EINVAL, ion_map(m_ionFd, m_validHandle, 4096, PROT_READ, 0, 0, &ptr, NULL));
139 }
140
TEST_F(InvalidValues,ion_share)141 TEST_F(InvalidValues, ion_share)
142 {
143 int share_fd;
144
145 /* invalid ion fd */
146 int ret = ion_share(0, m_validHandle, &share_fd);
147 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
148 /* invalid ion fd */
149 EXPECT_EQ(-EBADF, ion_share(-1, m_validHandle, &share_fd));
150 /* zero handle */
151 EXPECT_EQ(-EINVAL, ion_share(m_ionFd, 0, &share_fd));
152 /* bad handle */
153 EXPECT_EQ(-EINVAL, ion_share(m_ionFd, m_badHandle, &share_fd));
154 /* NULL share_fd */
155 EXPECT_EQ(-EINVAL, ion_share(m_ionFd, m_validHandle, NULL));
156 }
157
TEST_F(InvalidValues,ion_import)158 TEST_F(InvalidValues, ion_import)
159 {
160 ion_user_handle_t handle;
161
162 /* invalid ion fd */
163 int ret = ion_import(0, m_validShareFd, &handle);
164 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
165 /* invalid ion fd */
166 EXPECT_EQ(-EBADF, ion_import(-1, m_validShareFd, &handle));
167 /* bad share_fd */
168 EXPECT_EQ(-EINVAL, ion_import(m_ionFd, 0, &handle));
169 /* invalid share_fd */
170 EXPECT_EQ(-EBADF, ion_import(m_ionFd, -1, &handle));
171 /* NULL handle */
172 EXPECT_EQ(-EINVAL, ion_import(m_ionFd, m_validShareFd, NULL));
173 }
174
TEST_F(InvalidValues,ion_sync_fd)175 TEST_F(InvalidValues, ion_sync_fd)
176 {
177 /* invalid ion fd */
178 int ret = ion_sync_fd(0, m_validShareFd);
179 EXPECT_TRUE(ret == -EINVAL || ret == -ENOTTY);
180 /* invalid ion fd */
181 EXPECT_EQ(-EBADF, ion_sync_fd(-1, m_validShareFd));
182 /* bad share_fd */
183 EXPECT_EQ(-EINVAL, ion_sync_fd(m_ionFd, 0));
184 /* invalid share_fd */
185 EXPECT_EQ(-EBADF, ion_sync_fd(m_ionFd, -1));
186 }
187