1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <android/util/EncodedBuffer.h>
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 #include <unistd.h>
19 
20 using namespace android::util;
21 using android::sp;
22 
23 constexpr size_t __TEST_CHUNK_SIZE = 16UL;
24 const size_t kPageSize = getpagesize();
25 const size_t TEST_CHUNK_SIZE = (__TEST_CHUNK_SIZE + (kPageSize - 1)) & ~(kPageSize - 1);
26 const size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2;
27 const size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE;
28 
expectPointer(EncodedBuffer::Pointer * p,size_t pos)29 static void expectPointer(EncodedBuffer::Pointer* p, size_t pos) {
30     EXPECT_EQ(p->pos(), pos);
31     EXPECT_EQ(p->index(), pos / TEST_CHUNK_SIZE);
32     EXPECT_EQ(p->offset(), pos % TEST_CHUNK_SIZE);
33 }
34 
TEST(EncodedBufferTest,WriteSimple)35 TEST(EncodedBufferTest, WriteSimple) {
36     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
37     EXPECT_EQ(buffer->size(), 0UL);
38     expectPointer(buffer->wp(), 0);
39     EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_SIZE);
40     for (size_t i = 0; i < TEST_CHUNK_HALF_SIZE; i++) {
41         buffer->writeRawByte(static_cast<uint8_t>(50 + i));
42     }
43     EXPECT_EQ(buffer->size(), TEST_CHUNK_HALF_SIZE);
44     expectPointer(buffer->wp(), TEST_CHUNK_HALF_SIZE);
45     EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_HALF_SIZE);
46     for (size_t i = 0; i < TEST_CHUNK_SIZE; i++) {
47         buffer->writeRawByte(static_cast<uint8_t>(80 + i));
48     }
49     EXPECT_EQ(buffer->size(), TEST_CHUNK_SIZE + TEST_CHUNK_HALF_SIZE);
50     expectPointer(buffer->wp(), TEST_CHUNK_SIZE + TEST_CHUNK_HALF_SIZE);
51     EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_HALF_SIZE);
52 
53     // verifies the buffer's data
54     expectPointer(buffer->ep(), 0);
55     for (size_t i = 0; i < TEST_CHUNK_HALF_SIZE; i++) {
56         EXPECT_EQ(buffer->readRawByte(), static_cast<uint8_t>(50 + i));
57     }
58     for (size_t i = 0; i < TEST_CHUNK_SIZE; i++) {
59         EXPECT_EQ(buffer->readRawByte(), static_cast<uint8_t>(80 + i));
60     }
61 
62     // clears the buffer
63     buffer->clear();
64     EXPECT_EQ(buffer->size(), 0UL);
65     expectPointer(buffer->wp(), 0);
66 }
67 
TEST(EncodedBufferTest,WriteVarint)68 TEST(EncodedBufferTest, WriteVarint) {
69     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
70     size_t expected_buffer_size = 0;
71     EXPECT_EQ(buffer->writeRawVarint32(13), 1);
72     expected_buffer_size += 1;
73     EXPECT_EQ(buffer->size(), expected_buffer_size);
74     EXPECT_EQ(buffer->writeRawVarint32(UINT32_C(-1)), 5);
75     expected_buffer_size += 5;
76     EXPECT_EQ(buffer->size(), expected_buffer_size);
77 
78     EXPECT_EQ(buffer->writeRawVarint64(200), 2);
79     expected_buffer_size += 2;
80     EXPECT_EQ(buffer->size(), expected_buffer_size);
81     EXPECT_EQ(buffer->writeRawVarint64(UINT64_C(-1)), 10);
82     expected_buffer_size += 10;
83     EXPECT_EQ(buffer->size(), expected_buffer_size);
84 
85     buffer->writeRawFixed32(UINT32_C(-1));
86     expected_buffer_size += 4;
87     EXPECT_EQ(buffer->size(), expected_buffer_size);
88     buffer->writeRawFixed64(UINT64_C(-1));
89     expected_buffer_size += 8;
90     EXPECT_EQ(buffer->size(), expected_buffer_size);
91 
92     EXPECT_EQ(buffer->writeHeader(32, 2), 2);
93     expected_buffer_size += 2;
94     EXPECT_EQ(buffer->size(), expected_buffer_size);
95 
96     // verify data are correctly written to the buffer.
97     expectPointer(buffer->ep(), 0);
98     EXPECT_EQ(buffer->readRawVarint(), UINT32_C(13));
99     EXPECT_EQ(buffer->readRawVarint(), UINT32_C(-1));
100     EXPECT_EQ(buffer->readRawVarint(), UINT64_C(200));
101     EXPECT_EQ(buffer->readRawVarint(), UINT64_C(-1));
102     EXPECT_EQ(buffer->readRawFixed32(), UINT32_C(-1));
103     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(-1));
104     EXPECT_EQ(buffer->readRawVarint(), UINT64_C((32 << 3) + 2));
105     expectPointer(buffer->ep(), expected_buffer_size);
106 }
107 
TEST(EncodedBufferTest,Edit)108 TEST(EncodedBufferTest, Edit) {
109     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
110     buffer->writeRawFixed64(0xdeadbeefdeadbeef);
111     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(0xdeadbeefdeadbeef));
112 
113     buffer->editRawFixed32(4, 0x12345678);
114     // fixed 64 is little endian order.
115     buffer->ep()->rewind(); // rewind ep for readRawFixed64 from 0
116     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(0x12345678deadbeef));
117 
118     buffer->wp()->rewind();
119     expectPointer(buffer->wp(), 0);
120     buffer->copy(4, 3);
121     buffer->ep()->rewind(); // rewind ep for readRawFixed64 from 0
122     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(0x12345678de345678));
123 }
124 
TEST(EncodedBufferTest,ReadSimple)125 TEST(EncodedBufferTest, ReadSimple) {
126     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
127     for (size_t i = 0; i < TEST_CHUNK_3X_SIZE; i++) {
128         buffer->writeRawByte(i);
129     }
130     sp<ProtoReader> reader1 = buffer->read();
131     EXPECT_EQ(reader1->size(), TEST_CHUNK_3X_SIZE);
132     EXPECT_EQ(reader1->bytesRead(), 0);
133 
134     while (reader1->readBuffer() != NULL) {
135         reader1->move(reader1->currentToRead());
136     }
137     EXPECT_EQ(reader1->bytesRead(), TEST_CHUNK_3X_SIZE);
138 
139     sp<ProtoReader> reader2 = buffer->read();
140     uint8_t val = 0;
141     while (reader2->hasNext()) {
142         EXPECT_EQ(reader2->next(), val);
143         val++;
144     }
145     EXPECT_EQ(reader2->bytesRead(), TEST_CHUNK_3X_SIZE);
146     EXPECT_EQ(reader1->bytesRead(), TEST_CHUNK_3X_SIZE);
147 }
148 
TEST(EncodedBufferTest,ReadVarint)149 TEST(EncodedBufferTest, ReadVarint) {
150     sp<EncodedBuffer> buffer = new EncodedBuffer();
151     uint64_t val = UINT64_C(1522865904593);
152     size_t len = buffer->writeRawVarint64(val);
153     sp<ProtoReader> reader = buffer->read();
154     EXPECT_EQ(reader->size(), len);
155     EXPECT_EQ(reader->readRawVarint(), val);
156 }
157