blob: 33a4e0c904cd591f5f16a42e72064c07c1c148d8 [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
Sergio Giro7987b832015-08-18 17:36:50 +010026TEST(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
42TEST(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}