blob: 3f960d2f593c41aba9bd3d1b8fdb8f481496cc5d [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
Elliott Hughesf2f016e2020-10-19 13:18:45 -070035TEST(SharedBufferTest, alloc_null) {
36 // Big enough to fail, not big enough to abort.
37 SKIP_WITH_HWASAN; // hwasan has a 2GiB allocation limit.
38 ASSERT_EQ(nullptr, android::SharedBuffer::alloc(SIZE_MAX / 2));
39}
Sergio Giro7987b832015-08-18 17:36:50 +010040
Elliott Hughesf2f016e2020-10-19 13:18:45 -070041TEST(SharedBufferTest, alloc_zero_size) {
42 android::SharedBuffer* buf = android::SharedBuffer::alloc(0);
43 ASSERT_NE(nullptr, buf);
44 ASSERT_EQ(0U, buf->size());
45 buf->release();
46}
Sergio Giro7987b832015-08-18 17:36:50 +010047
Elliott Hughesf2f016e2020-10-19 13:18:45 -070048TEST(SharedBufferTest, editResize_death) {
49 android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
50 EXPECT_DEATH(buf->editResize(SIZE_MAX - sizeof(android::SharedBuffer)), "");
51 buf = android::SharedBuffer::alloc(10);
52 EXPECT_DEATH(buf->editResize(SIZE_MAX), "");
53}
54
55TEST(SharedBufferTest, editResize_null) {
56 // Big enough to fail, not big enough to abort.
57 SKIP_WITH_HWASAN; // hwasan has a 2GiB allocation limit.
58 android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
59 ASSERT_EQ(nullptr, buf->editResize(SIZE_MAX / 2));
60}
61
62TEST(SharedBufferTest, editResize_zero_size) {
63 android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
64 buf = buf->editResize(0);
65 ASSERT_EQ(0U, buf->size());
66 buf->release();
Sergio Giro7987b832015-08-18 17:36:50 +010067}