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 <memory>
20 #include <vector>
21
22 #include <gtest/gtest.h>
23
24 #include <ion/ion.h>
25 #include "ion_test_fixture.h"
26
27 class InvalidValues : public IonTest {};
28
TEST_F(InvalidValues,ion_close)29 TEST_F(InvalidValues, ion_close) {
30 EXPECT_EQ(-EBADF, ion_close(-1));
31 }
32
TEST_F(InvalidValues,ion_alloc_fd)33 TEST_F(InvalidValues, ion_alloc_fd) {
34 int fd;
35 // no heaps
36 EXPECT_EQ(-ENODEV, ion_alloc_fd(ionfd, 4096, 0, 0, 0, &fd));
37 for (const auto& heap : ion_heaps) {
38 // invalid ion_fd
39 EXPECT_EQ(-EBADF, ion_alloc_fd(-1, 4096, 0, (1 << heap.heap_id), 0, &fd));
40 SCOPED_TRACE(::testing::Message()
41 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
42 // zero size
43 EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, 0, 0, (1 << heap.heap_id), 0, &fd));
44 // too large size
45 int ret = ion_alloc_fd(ionfd, -1, 0, (1 << heap.heap_id), 0, &fd);
46 EXPECT_TRUE(ret == -EINVAL || ret == -ENOMEM);
47 // bad alignment
48 // TODO: Current userspace and kernel code completely ignores alignment. So this
49 // test is going to fail. We need to completely remove alignment from the API.
50 // All memory by default is always page aligned. OR actually pass the alignment
51 // down into the kernel and make kernel respect the alignment.
52 // EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, 4096, -1, (1 << heap.heap_id), 0, &fd));
53
54 // NULL fd
55 EXPECT_EQ(-EINVAL, ion_alloc_fd(ionfd, 4096, 0, (1 << heap.heap_id), 0, nullptr));
56 }
57 }
58
TEST_F(InvalidValues,ion_query_heap_cnt)59 TEST_F(InvalidValues, ion_query_heap_cnt) {
60 // NULL count
61 EXPECT_EQ(-EINVAL, ion_query_heap_cnt(ionfd, nullptr));
62
63 int heap_count;
64 // bad fd
65 EXPECT_EQ(-EBADF, ion_query_heap_cnt(-1, &heap_count));
66 }
67
TEST_F(InvalidValues,ion_query_get_heaps)68 TEST_F(InvalidValues, ion_query_get_heaps) {
69 int heap_count;
70 ASSERT_EQ(0, ion_query_heap_cnt(ionfd, &heap_count));
71 ASSERT_GT(heap_count, 0);
72
73 // nullptr buffers, still returns success but without
74 // the ion_heap_data.
75 EXPECT_EQ(0, ion_query_get_heaps(ionfd, heap_count, nullptr));
76
77 std::unique_ptr<struct ion_heap_data[]> heaps =
78 std::make_unique<struct ion_heap_data[]>(heap_count);
79 // bad fd
80 EXPECT_EQ(-EBADF, ion_query_get_heaps(-1, heap_count, heaps.get()));
81
82 // invalid heap data pointer
83 EXPECT_EQ(-EFAULT, ion_query_get_heaps(ionfd, heap_count, reinterpret_cast<void*>(0xdeadf00d)));
84 }
85