1 /*
2  * Copyright 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 #define LOG_TAG "GraphicBufferAllocatorTest"
18 
19 #include <ui/GraphicBuffer.h>
20 #include <ui/GraphicBufferAllocator.h>
21 #include <ui/PixelFormat.h>
22 
23 #include <gtest/gtest.h>
24 
25 #include "mock/MockGrallocAllocator.h"
26 
27 #include <algorithm>
28 #include <limits>
29 
30 namespace android {
31 
32 namespace {
33 
34 constexpr uint32_t kTestWidth = 1024;
35 constexpr uint32_t kTestHeight = 1;
36 constexpr uint32_t kTestLayerCount = 1;
37 constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN;
38 
39 } // namespace
40 
41 using ::testing::DoAll;
42 using ::testing::Return;
43 using ::testing::SetArgPointee;
44 
45 class TestableGraphicBufferAllocator : public GraphicBufferAllocator {
46 public:
TestableGraphicBufferAllocator()47     TestableGraphicBufferAllocator() {
48         mAllocator = std::make_unique<const mock::MockGrallocAllocator>();
49     }
setUpAllocateExpectations(status_t err,uint32_t stride)50     void setUpAllocateExpectations(status_t err, uint32_t stride) {
51         std::cout << "Setting expected stride to " << stride << std::endl;
52         EXPECT_CALL(*(reinterpret_cast<const mock::MockGrallocAllocator*>(mAllocator.get())),
53                     allocate)
54                 .WillOnce(DoAll(SetArgPointee<6>(stride), Return(err)));
55     }
getAllocator()56     std::unique_ptr<const GrallocAllocator>& getAllocator() { return mAllocator; }
57 };
58 
59 class GraphicBufferAllocatorTest : public testing::Test {
60 public:
GraphicBufferAllocatorTest()61     GraphicBufferAllocatorTest() : mAllocator() {}
getAllocator()62     const TestableGraphicBufferAllocator& getAllocator() { return mAllocator; }
63 
64 protected:
65     TestableGraphicBufferAllocator mAllocator;
66 };
67 
TEST_F(GraphicBufferAllocatorTest,AllocateNoError)68 TEST_F(GraphicBufferAllocatorTest, AllocateNoError) {
69     mAllocator.setUpAllocateExpectations(NO_ERROR, kTestWidth);
70     android::PixelFormat format = PIXEL_FORMAT_RGBA_8888;
71     uint32_t stride = 0;
72     buffer_handle_t handle;
73     status_t err = mAllocator.allocate(kTestWidth, kTestHeight, format, kTestLayerCount, kTestUsage,
74                                        &handle, &stride, 0, "GraphicBufferAllocatorTest");
75     ASSERT_EQ(NO_ERROR, err);
76     ASSERT_EQ(kTestWidth, stride);
77 }
78 
TEST_F(GraphicBufferAllocatorTest,AllocateZeroStride)79 TEST_F(GraphicBufferAllocatorTest, AllocateZeroStride) {
80     android::PixelFormat format = PIXEL_FORMAT_RGBA_8888;
81     uint32_t expectedStride = 0;
82 
83     mAllocator.setUpAllocateExpectations(NO_ERROR, expectedStride);
84     uint32_t stride = 0;
85     buffer_handle_t handle;
86     // a divide by zero would cause a crash
87     status_t err = mAllocator.allocate(kTestWidth, kTestHeight, format, kTestLayerCount, kTestUsage,
88                                        &handle, &stride, 0, "GraphicBufferAllocatorTest");
89     ASSERT_EQ(NO_ERROR, err);
90     ASSERT_EQ(expectedStride, stride);
91 }
92 
TEST_F(GraphicBufferAllocatorTest,AllocateLargeStride)93 TEST_F(GraphicBufferAllocatorTest, AllocateLargeStride) {
94     uint32_t height = std::numeric_limits<uint32_t>::max();
95     uint32_t bpp = 4;
96     android::PixelFormat format = PIXEL_FORMAT_RGBA_8888;
97 
98     if (std::numeric_limits<size_t>::max() / height / bpp >= std::numeric_limits<uint32_t>::max()) {
99         std::cout << "stride cannot cause overflow" << std::endl;
100         GTEST_SUCCEED() << "stride cannot cause overflow";
101         return;
102     }
103     uint32_t width = std::numeric_limits<size_t>::max() / height / bpp;
104 
105     uint32_t expectedStride = std::numeric_limits<uint32_t>::max();
106 
107     mAllocator.setUpAllocateExpectations(NO_ERROR, expectedStride);
108     uint32_t stride = 0;
109     buffer_handle_t handle;
110     // an overflow would cause a crash
111     status_t err = mAllocator.allocate(width, height, format, kTestLayerCount, kTestUsage, &handle,
112                                        &stride, 0, "GraphicBufferAllocatorTest");
113     ASSERT_EQ(NO_ERROR, err);
114     ASSERT_EQ(expectedStride, stride);
115 }
116 } // namespace android
117