• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 extern "C" void __hwasan_init() __attribute__((weak));
27 #define SKIP_WITH_HWASAN \
28     if (&__hwasan_init != 0) GTEST_SKIP()
29 
TEST(SharedBufferTest,alloc_death)30 TEST(SharedBufferTest, alloc_death) {
31     EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX), "");
32     EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer)), "");
33 }
34 
TEST(SharedBufferTest,alloc_max)35 TEST(SharedBufferTest, alloc_max) {
36     SKIP_WITH_HWASAN;  // hwasan has a 2GiB allocation limit.
37 
38     android::SharedBuffer* buf =
39             android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer) - 1);
40     if (buf != nullptr) {
41         EXPECT_NE(nullptr, buf->data());
42         buf->release();
43     }
44 }
45 
TEST(SharedBufferTest,alloc_big)46 TEST(SharedBufferTest, alloc_big) {
47     SKIP_WITH_HWASAN;  // hwasan has a 2GiB allocation limit.
48 
49     android::SharedBuffer* buf = android::SharedBuffer::alloc(SIZE_MAX / 2);
50     if (buf != nullptr) {
51         EXPECT_NE(nullptr, buf->data());
52         buf->release();
53     }
54 }
55 
TEST(SharedBufferTest,alloc_zero_size)56 TEST(SharedBufferTest, alloc_zero_size) {
57     android::SharedBuffer* buf = android::SharedBuffer::alloc(0);
58     ASSERT_NE(nullptr, buf);
59     ASSERT_EQ(0U, buf->size());
60     buf->release();
61 }
62 
TEST(SharedBufferTest,editResize_death)63 TEST(SharedBufferTest, editResize_death) {
64     android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
65     EXPECT_DEATH(buf->editResize(SIZE_MAX - sizeof(android::SharedBuffer)), "");
66     buf = android::SharedBuffer::alloc(10);
67     EXPECT_DEATH(buf->editResize(SIZE_MAX), "");
68 }
69 
TEST(SharedBufferTest,editResize_null)70 TEST(SharedBufferTest, editResize_null) {
71     // Big enough to fail, not big enough to abort.
72     SKIP_WITH_HWASAN;  // hwasan has a 2GiB allocation limit.
73     android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
74     android::SharedBuffer* buf2 = buf->editResize(SIZE_MAX / 2);
75     if (buf2 == nullptr) {
76         buf->release();
77     } else {
78         EXPECT_NE(nullptr, buf2->data());
79         buf2->release();
80     }
81 }
82 
TEST(SharedBufferTest,editResize_zero_size)83 TEST(SharedBufferTest, editResize_zero_size) {
84     android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
85     buf = buf->editResize(0);
86     ASSERT_EQ(0U, buf->size());
87     buf->release();
88 }
89