blob: 3e4f61e542dbd70bcc6cf6dd4f5b33d5ceaa0169 [file] [log] [blame]
Alexei Frolovf9ae1892021-04-01 18:24:27 -07001// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "gtest/gtest.h"
16#include "pw_bytes/array.h"
Armando Montaneza095fc22021-05-21 12:27:11 -070017#include "pw_protobuf/encoder.h"
Alexei Frolovf9ae1892021-04-01 18:24:27 -070018
19namespace pw::protobuf {
20namespace {
21
22TEST(Encoder, SizeTypeIsConfigured) {
Armando Montanez328a0512021-05-21 13:37:58 -070023 static_assert(config::kMaxVarintSize == sizeof(uint8_t));
Alexei Frolovf9ae1892021-04-01 18:24:27 -070024}
25
26TEST(Encoder, NestedWriteSmallerThanVarintSize) {
27 std::array<std::byte, 256> buffer;
Alexei Frolovf9ae1892021-04-01 18:24:27 -070028
Armando Montanez328a0512021-05-21 13:37:58 -070029 MemoryEncoder encoder(buffer);
30
Ewout van Bekkum011a4d52021-08-20 20:19:52 -070031 {
32 StreamEncoder nested = encoder.GetNestedEncoder(1);
33 // 1 byte key + 1 byte size + 125 byte value = 127 byte nested length.
34 EXPECT_EQ(nested.WriteBytes(2, bytes::Initialized<125>(0xaa)), OkStatus());
35 }
Alexei Frolovf9ae1892021-04-01 18:24:27 -070036
Armando Montanez328a0512021-05-21 13:37:58 -070037 EXPECT_EQ(encoder.status(), OkStatus());
Alexei Frolovf9ae1892021-04-01 18:24:27 -070038}
39
Armando Montanez328a0512021-05-21 13:37:58 -070040TEST(Encoder, NestedWriteLargerThanVarintSizeReturnsResourceExhausted) {
Alexei Frolovf9ae1892021-04-01 18:24:27 -070041 std::array<std::byte, 256> buffer;
Armando Montanez328a0512021-05-21 13:37:58 -070042
Armando Montanez328a0512021-05-21 13:37:58 -070043 MemoryEncoder encoder(buffer);
Alexei Frolovf9ae1892021-04-01 18:24:27 -070044
Ewout van Bekkum011a4d52021-08-20 20:19:52 -070045 {
46 // Try to write a larger nested message than the max nested varint value.
47 StreamEncoder nested = encoder.GetNestedEncoder(1);
48 // 1 byte key + 1 byte size + 126 byte value = 128 byte nested length.
49 EXPECT_EQ(nested.WriteBytes(2, bytes::Initialized<126>(0xaa)),
50 Status::ResourceExhausted());
51 EXPECT_EQ(nested.WriteUint32(3, 42), Status::ResourceExhausted());
52 }
Alexei Frolovf9ae1892021-04-01 18:24:27 -070053
Armando Montanez328a0512021-05-21 13:37:58 -070054 EXPECT_EQ(encoder.status(), Status::ResourceExhausted());
Alexei Frolovf9ae1892021-04-01 18:24:27 -070055}
56
Armando Montanez328a0512021-05-21 13:37:58 -070057TEST(Encoder, NestedMessageLargerThanVarintSizeReturnsResourceExhausted) {
Alexei Frolovf9ae1892021-04-01 18:24:27 -070058 std::array<std::byte, 256> buffer;
Armando Montanez328a0512021-05-21 13:37:58 -070059
Armando Montanez328a0512021-05-21 13:37:58 -070060 MemoryEncoder encoder(buffer);
Alexei Frolovf9ae1892021-04-01 18:24:27 -070061
Ewout van Bekkum011a4d52021-08-20 20:19:52 -070062 {
63 // Try to write a larger nested message than the max nested varint value as
64 // multiple smaller writes.
65 StreamEncoder nested = encoder.GetNestedEncoder(1);
66 EXPECT_EQ(nested.WriteBytes(2, bytes::Initialized<60>(0xaa)), OkStatus());
67 EXPECT_EQ(nested.WriteBytes(3, bytes::Initialized<60>(0xaa)), OkStatus());
68 EXPECT_EQ(nested.WriteBytes(4, bytes::Initialized<60>(0xaa)),
69 Status::ResourceExhausted());
70 }
Alexei Frolovf9ae1892021-04-01 18:24:27 -070071
Armando Montanez328a0512021-05-21 13:37:58 -070072 EXPECT_EQ(encoder.status(), Status::ResourceExhausted());
Alexei Frolovf9ae1892021-04-01 18:24:27 -070073}
74
75} // namespace
76} // namespace pw::protobuf