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 using namespace android::util;
19 using android::sp;
20 
21 constexpr size_t TEST_CHUNK_SIZE = 16UL;
22 constexpr size_t TEST_CHUNK_HALF_SIZE = TEST_CHUNK_SIZE / 2;
23 constexpr size_t TEST_CHUNK_3X_SIZE = 3 * TEST_CHUNK_SIZE;
24 
expectPointer(EncodedBuffer::Pointer * p,size_t pos)25 static void expectPointer(EncodedBuffer::Pointer* p, size_t pos) {
26     EXPECT_EQ(p->pos(), pos);
27     EXPECT_EQ(p->index(), pos / TEST_CHUNK_SIZE);
28     EXPECT_EQ(p->offset(), pos % TEST_CHUNK_SIZE);
29 }
30 
TEST(EncodedBufferTest,WriteSimple)31 TEST(EncodedBufferTest, WriteSimple) {
32     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
33     EXPECT_EQ(buffer->size(), 0UL);
34     expectPointer(buffer->wp(), 0);
35     EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_SIZE);
36     for (size_t i = 0; i < TEST_CHUNK_HALF_SIZE; i++) {
37         buffer->writeRawByte(50 + i);
38     }
39     EXPECT_EQ(buffer->size(), TEST_CHUNK_HALF_SIZE);
40     expectPointer(buffer->wp(), TEST_CHUNK_HALF_SIZE);
41     EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_HALF_SIZE);
42     for (size_t i = 0; i < TEST_CHUNK_SIZE; i++) {
43         buffer->writeRawByte(80 + i);
44     }
45     EXPECT_EQ(buffer->size(), TEST_CHUNK_SIZE + TEST_CHUNK_HALF_SIZE);
46     expectPointer(buffer->wp(), TEST_CHUNK_SIZE + TEST_CHUNK_HALF_SIZE);
47     EXPECT_EQ(buffer->currentToWrite(), TEST_CHUNK_HALF_SIZE);
48 
49     // verifies the buffer's data
50     expectPointer(buffer->ep(), 0);
51     for (size_t i = 0; i < TEST_CHUNK_HALF_SIZE; i++) {
52         EXPECT_EQ(buffer->readRawByte(), 50 + i);
53     }
54     for (size_t i = 0; i < TEST_CHUNK_SIZE; i++) {
55         EXPECT_EQ(buffer->readRawByte(), 80 + i);
56     }
57 
58     // clears the buffer
59     buffer->clear();
60     EXPECT_EQ(buffer->size(), 0UL);
61     expectPointer(buffer->wp(), 0);
62 }
63 
TEST(EncodedBufferTest,WriteVarint)64 TEST(EncodedBufferTest, WriteVarint) {
65     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
66     size_t expected_buffer_size = 0;
67     EXPECT_EQ(buffer->writeRawVarint32(13), 1);
68     expected_buffer_size += 1;
69     EXPECT_EQ(buffer->size(), expected_buffer_size);
70     EXPECT_EQ(buffer->writeRawVarint32(UINT32_C(-1)), 5);
71     expected_buffer_size += 5;
72     EXPECT_EQ(buffer->size(), expected_buffer_size);
73 
74     EXPECT_EQ(buffer->writeRawVarint64(200), 2);
75     expected_buffer_size += 2;
76     EXPECT_EQ(buffer->size(), expected_buffer_size);
77     EXPECT_EQ(buffer->writeRawVarint64(UINT64_C(-1)), 10);
78     expected_buffer_size += 10;
79     EXPECT_EQ(buffer->size(), expected_buffer_size);
80 
81     buffer->writeRawFixed32(UINT32_C(-1));
82     expected_buffer_size += 4;
83     EXPECT_EQ(buffer->size(), expected_buffer_size);
84     buffer->writeRawFixed64(UINT64_C(-1));
85     expected_buffer_size += 8;
86     EXPECT_EQ(buffer->size(), expected_buffer_size);
87 
88     EXPECT_EQ(buffer->writeHeader(32, 2), 2);
89     expected_buffer_size += 2;
90     EXPECT_EQ(buffer->size(), expected_buffer_size);
91 
92     // verify data are correctly written to the buffer.
93     expectPointer(buffer->ep(), 0);
94     EXPECT_EQ(buffer->readRawVarint(), UINT32_C(13));
95     EXPECT_EQ(buffer->readRawVarint(), UINT32_C(-1));
96     EXPECT_EQ(buffer->readRawVarint(), UINT64_C(200));
97     EXPECT_EQ(buffer->readRawVarint(), UINT64_C(-1));
98     EXPECT_EQ(buffer->readRawFixed32(), UINT32_C(-1));
99     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(-1));
100     EXPECT_EQ(buffer->readRawVarint(), UINT64_C((32 << 3) + 2));
101     expectPointer(buffer->ep(), expected_buffer_size);
102 }
103 
TEST(EncodedBufferTest,Edit)104 TEST(EncodedBufferTest, Edit) {
105     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
106     buffer->writeRawFixed64(0xdeadbeefdeadbeef);
107     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(0xdeadbeefdeadbeef));
108 
109     buffer->editRawFixed32(4, 0x12345678);
110     // fixed 64 is little endian order.
111     buffer->ep()->rewind(); // rewind ep for readRawFixed64 from 0
112     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(0x12345678deadbeef));
113 
114     buffer->wp()->rewind();
115     expectPointer(buffer->wp(), 0);
116     buffer->copy(4, 3);
117     buffer->ep()->rewind(); // rewind ep for readRawFixed64 from 0
118     EXPECT_EQ(buffer->readRawFixed64(), UINT64_C(0x12345678de345678));
119 }
120 
TEST(EncodedBufferTest,ReadSimple)121 TEST(EncodedBufferTest, ReadSimple) {
122     sp<EncodedBuffer> buffer = new EncodedBuffer(TEST_CHUNK_SIZE);
123     for (size_t i = 0; i < TEST_CHUNK_3X_SIZE; i++) {
124         buffer->writeRawByte(i);
125     }
126     sp<ProtoReader> reader1 = buffer->read();
127     EXPECT_EQ(reader1->size(), TEST_CHUNK_3X_SIZE);
128     EXPECT_EQ(reader1->bytesRead(), 0);
129 
130     while (reader1->readBuffer() != NULL) {
131         reader1->move(reader1->currentToRead());
132     }
133     EXPECT_EQ(reader1->bytesRead(), TEST_CHUNK_3X_SIZE);
134 
135     sp<ProtoReader> reader2 = buffer->read();
136     uint8_t val = 0;
137     while (reader2->hasNext()) {
138         EXPECT_EQ(reader2->next(), val);
139         val++;
140     }
141     EXPECT_EQ(reader2->bytesRead(), TEST_CHUNK_3X_SIZE);
142     EXPECT_EQ(reader1->bytesRead(), TEST_CHUNK_3X_SIZE);
143 }
144 
TEST(EncodedBufferTest,ReadVarint)145 TEST(EncodedBufferTest, ReadVarint) {
146     sp<EncodedBuffer> buffer = new EncodedBuffer();
147     uint64_t val = UINT64_C(1522865904593);
148     size_t len = buffer->writeRawVarint64(val);
149     sp<ProtoReader> reader = buffer->read();
150     EXPECT_EQ(reader->size(), len);
151     EXPECT_EQ(reader->readRawVarint(), val);
152 }
153