blob: 1d6317ff771899b27d1806c83a296016dcf7e3dd [file] [log] [blame]
Sergio Giro7987b832015-08-18 17:36:50 +01001/*
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
Sergio Girod95e47f2015-08-18 17:36:50 +010017#define __STDC_LIMIT_MACROS
Sergio Giro7987b832015-08-18 17:36:50 +010018
19#include <gtest/gtest.h>
20
21#include <memory>
22#include <stdint.h>
23
Sergio Giro8dba9a72015-09-23 21:22:14 +010024#include "SharedBuffer.h"
25
Elliott Hughesf2f016e2020-10-19 13:18:45 -070026extern "C" void __hwasan_init() __attribute__((weak));
27#define SKIP_WITH_HWASAN \
28 if (&__hwasan_init != 0) GTEST_SKIP()
Sergio Giro7987b832015-08-18 17:36:50 +010029
Elliott Hughesf2f016e2020-10-19 13:18:45 -070030TEST(SharedBufferTest, alloc_death) {
31 EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX), "");
32 EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer)), "");
Sergio Giro7987b832015-08-18 17:36:50 +010033}
34
Steven Moreland128826c2021-04-27 21:36:50 +000035TEST(SharedBufferTest, alloc_max) {
Elliott Hughesf2f016e2020-10-19 13:18:45 -070036 SKIP_WITH_HWASAN; // hwasan has a 2GiB allocation limit.
Steven Moreland128826c2021-04-27 21:36:50 +000037
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
46TEST(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 }
Elliott Hughesf2f016e2020-10-19 13:18:45 -070054}
Sergio Giro7987b832015-08-18 17:36:50 +010055
Elliott Hughesf2f016e2020-10-19 13:18:45 -070056TEST(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}
Sergio Giro7987b832015-08-18 17:36:50 +010062
Elliott Hughesf2f016e2020-10-19 13:18:45 -070063TEST(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
70TEST(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);
Steven Moreland128826c2021-04-27 21:36:50 +000074 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 }
Elliott Hughesf2f016e2020-10-19 13:18:45 -070081}
82
83TEST(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();
Sergio Giro7987b832015-08-18 17:36:50 +010088}