1 /*
2  * Copyright (C) 2015 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 __STDC_LIMIT_MACROS
18 
19 #include <gtest/gtest.h>
20 
21 #include <memory>
22 #include <stdint.h>
23 
24 #include "SharedBuffer.h"
25 
TEST(SharedBufferTest,TestAlloc)26 TEST(SharedBufferTest, TestAlloc) {
27   EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX), "");
28   EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer)), "");
29 
30   // Make sure we don't die here.
31   // Check that null is returned, as we are asking for the whole address space.
32   android::SharedBuffer* buf =
33       android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer) - 1);
34   ASSERT_EQ(nullptr, buf);
35 
36   buf = android::SharedBuffer::alloc(0);
37   ASSERT_NE(nullptr, buf);
38   ASSERT_EQ(0U, buf->size());
39   buf->release();
40 }
41 
TEST(SharedBufferTest,TestEditResize)42 TEST(SharedBufferTest, TestEditResize) {
43   android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
44   EXPECT_DEATH(buf->editResize(SIZE_MAX - sizeof(android::SharedBuffer)), "");
45   buf = android::SharedBuffer::alloc(10);
46   EXPECT_DEATH(buf->editResize(SIZE_MAX), "");
47 
48   buf = android::SharedBuffer::alloc(10);
49   // Make sure we don't die here.
50   // Check that null is returned, as we are asking for the whole address space.
51   buf = buf->editResize(SIZE_MAX - sizeof(android::SharedBuffer) - 1);
52   ASSERT_EQ(nullptr, buf);
53 
54   buf = android::SharedBuffer::alloc(10);
55   buf = buf->editResize(0);
56   ASSERT_EQ(0U, buf->size());
57   buf->release();
58 }
59