| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 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 |  | 
| Sergio Giro | d95e47f | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 17 | #define __STDC_LIMIT_MACROS | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 18 |  | 
|  | 19 | #include <gtest/gtest.h> | 
|  | 20 |  | 
|  | 21 | #include <memory> | 
|  | 22 | #include <stdint.h> | 
|  | 23 |  | 
| Sergio Giro | 8dba9a7 | 2015-09-23 21:22:14 +0100 | [diff] [blame] | 24 | #include "SharedBuffer.h" | 
|  | 25 |  | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 26 | extern "C" void __hwasan_init() __attribute__((weak)); | 
|  | 27 | #define SKIP_WITH_HWASAN \ | 
|  | 28 | if (&__hwasan_init != 0) GTEST_SKIP() | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 29 |  | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 30 | TEST(SharedBufferTest, alloc_death) { | 
|  | 31 | EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX), ""); | 
|  | 32 | EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer)), ""); | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 33 | } | 
|  | 34 |  | 
| Steven Moreland | 128826c | 2021-04-27 21:36:50 +0000 | [diff] [blame] | 35 | TEST(SharedBufferTest, alloc_max) { | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 36 | SKIP_WITH_HWASAN;  // hwasan has a 2GiB allocation limit. | 
| Steven Moreland | 128826c | 2021-04-27 21:36:50 +0000 | [diff] [blame] | 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 |  | 
|  | 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 | } | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 54 | } | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 55 |  | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 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 | } | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 62 |  | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 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 |  | 
|  | 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); | 
| Steven Moreland | 128826c | 2021-04-27 21:36:50 +0000 | [diff] [blame] | 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 | } | 
| Elliott Hughes | f2f016e | 2020-10-19 13:18:45 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 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(); | 
| Sergio Giro | 7987b83 | 2015-08-18 17:36:50 +0100 | [diff] [blame] | 88 | } |