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 #include "androidfw/BigBuffer.h"
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 using ::testing::NotNull;
23 
24 namespace android {
25 
TEST(BigBufferTest,AllocateSingleBlock)26 TEST(BigBufferTest, AllocateSingleBlock) {
27   BigBuffer buffer(4);
28 
29   EXPECT_THAT(buffer.NextBlock<char>(2), NotNull());
30   EXPECT_EQ(2u, buffer.size());
31 }
32 
TEST(BigBufferTest,ReturnSameBlockIfNextAllocationFits)33 TEST(BigBufferTest, ReturnSameBlockIfNextAllocationFits) {
34   BigBuffer buffer(16);
35 
36   char* b1 = buffer.NextBlock<char>(8);
37   EXPECT_THAT(b1, NotNull());
38 
39   char* b2 = buffer.NextBlock<char>(4);
40   EXPECT_THAT(b2, NotNull());
41 
42   EXPECT_EQ(b1 + 8, b2);
43 }
44 
TEST(BigBufferTest,AllocateExactSizeBlockIfLargerThanBlockSize)45 TEST(BigBufferTest, AllocateExactSizeBlockIfLargerThanBlockSize) {
46   BigBuffer buffer(16);
47 
48   EXPECT_THAT(buffer.NextBlock<char>(32), NotNull());
49   EXPECT_EQ(32u, buffer.size());
50 }
51 
TEST(BigBufferTest,AppendAndMoveBlock)52 TEST(BigBufferTest, AppendAndMoveBlock) {
53   BigBuffer buffer(16);
54 
55   uint32_t* b1 = buffer.NextBlock<uint32_t>();
56   ASSERT_THAT(b1, NotNull());
57   *b1 = 33;
58 
59   {
60     BigBuffer buffer2(16);
61     b1 = buffer2.NextBlock<uint32_t>();
62     ASSERT_THAT(b1, NotNull());
63     *b1 = 44;
64 
65     buffer.AppendBuffer(std::move(buffer2));
66     EXPECT_EQ(0u, buffer2.size());  // NOLINT
67     EXPECT_EQ(buffer2.begin(), buffer2.end());
68   }
69 
70   EXPECT_EQ(2 * sizeof(uint32_t), buffer.size());
71 
72   auto b = buffer.begin();
73   ASSERT_NE(b, buffer.end());
74   ASSERT_EQ(sizeof(uint32_t), b->size);
75   ASSERT_EQ(33u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
76   ++b;
77 
78   ASSERT_NE(b, buffer.end());
79   ASSERT_EQ(sizeof(uint32_t), b->size);
80   ASSERT_EQ(44u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
81   ++b;
82 
83   ASSERT_EQ(b, buffer.end());
84 }
85 
TEST(BigBufferTest,PadAndAlignProperly)86 TEST(BigBufferTest, PadAndAlignProperly) {
87   BigBuffer buffer(16);
88 
89   ASSERT_THAT(buffer.NextBlock<char>(2), NotNull());
90   ASSERT_EQ(2u, buffer.size());
91   buffer.Pad(2);
92   ASSERT_EQ(4u, buffer.size());
93   buffer.Align4();
94   ASSERT_EQ(4u, buffer.size());
95   buffer.Pad(2);
96   ASSERT_EQ(6u, buffer.size());
97   buffer.Align4();
98   ASSERT_EQ(8u, buffer.size());
99 }
100 
101 }  // namespace android
102