blob: b62ce14cd7c67157b1e0770638e4c9ff3d92eba0 [file] [log] [blame]
Alexei Frolove19ebb82020-05-14 17:21:20 -07001// Copyright 2020 The Pigweed Authors
Alexei Frolovbbf164c2019-12-16 12:51:59 -08002//
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.
Nathaniel Brough06fe47b2020-06-19 17:40:49 +080014#include <span>
Alexei Frolovbbf164c2019-12-16 12:51:59 -080015
16#include "gtest/gtest.h"
17#include "pw_protobuf/encoder.h"
18
19// These header files contain the code generated by the pw_protobuf plugin.
20// They are re-generated every time the tests are built and are used by the
21// tests to ensure that the interface remains consistent.
22//
23// The purpose of the tests in this file is primarily to verify that the
24// generated C++ interface is valid rather than the correctness of the
25// low-level encoder.
Wyatt Hepler91741472021-02-03 08:45:10 -080026#include "pw_protobuf_test_protos/full_test.pwpb.h"
27#include "pw_protobuf_test_protos/importer.pwpb.h"
28#include "pw_protobuf_test_protos/non_pw_package.pwpb.h"
29#include "pw_protobuf_test_protos/proto2.pwpb.h"
30#include "pw_protobuf_test_protos/repeated.pwpb.h"
Alexei Frolovbbf164c2019-12-16 12:51:59 -080031
32namespace pw::protobuf {
33namespace {
34
35using namespace pw::protobuf::test;
36
37TEST(Codegen, Codegen) {
Alexei Frolov311c6202020-01-06 11:16:20 -080038 std::byte encode_buffer[512];
Alexei Frolovbbf164c2019-12-16 12:51:59 -080039 NestedEncoder<20, 20> encoder(encode_buffer);
40
41 Pigweed::Encoder pigweed(&encoder);
42 pigweed.WriteMagicNumber(73);
43 pigweed.WriteZiggy(-111);
44 pigweed.WriteErrorMessage("not a typewriter");
Alexei Frolovd54975b2020-05-19 15:13:27 -070045 pigweed.WriteBin(Pigweed::Protobuf::Binary::ZERO);
Alexei Frolovbbf164c2019-12-16 12:51:59 -080046
47 {
48 Pigweed::Pigweed::Encoder pigweed_pigweed = pigweed.GetPigweedEncoder();
Alexei Frolovd54975b2020-05-19 15:13:27 -070049 pigweed_pigweed.WriteStatus(Bool::FILE_NOT_FOUND);
Alexei Frolovbbf164c2019-12-16 12:51:59 -080050 }
51
52 {
53 Proto::Encoder proto = pigweed.GetProtoEncoder();
Alexei Frolovd54975b2020-05-19 15:13:27 -070054 proto.WriteBin(Proto::Binary::OFF);
55 proto.WritePigweedPigweedBin(Pigweed::Pigweed::Binary::ZERO);
56 proto.WritePigweedProtobufBin(Pigweed::Protobuf::Binary::ZERO);
Alexei Frolovbbf164c2019-12-16 12:51:59 -080057
58 {
59 Pigweed::Protobuf::Compiler::Encoder meta = proto.GetMetaEncoder();
60 meta.WriteFileName("/etc/passwd");
Alexei Frolovd54975b2020-05-19 15:13:27 -070061 meta.WriteStatus(Pigweed::Protobuf::Compiler::Status::FUBAR);
Alexei Frolovbbf164c2019-12-16 12:51:59 -080062 }
63
64 {
65 Pigweed::Encoder nested_pigweed = proto.GetPigweedEncoder();
66 pigweed.WriteErrorMessage("here we go again");
67 pigweed.WriteMagicNumber(616);
68
69 {
70 DeviceInfo::Encoder device_info = nested_pigweed.GetDeviceInfoEncoder();
71
72 {
73 KeyValuePair::Encoder attributes = device_info.GetAttributesEncoder();
74 attributes.WriteKey("version");
75 attributes.WriteValue("5.3.1");
76 }
77
78 {
79 KeyValuePair::Encoder attributes = device_info.GetAttributesEncoder();
80 attributes.WriteKey("chip");
81 attributes.WriteValue("left-soc");
82 }
83
Alexei Frolovd54975b2020-05-19 15:13:27 -070084 device_info.WriteStatus(DeviceInfo::DeviceStatus::PANIC);
Alexei Frolovbbf164c2019-12-16 12:51:59 -080085 }
86 }
87 }
88
89 for (int i = 0; i < 5; ++i) {
90 Proto::ID::Encoder id = pigweed.GetIdEncoder();
91 id.WriteId(5 * i * i + 3 * i + 49);
92 }
93
94 // clang-format off
95 constexpr uint8_t expected_proto[] = {
96 // pigweed.magic_number
97 0x08, 0x49,
98 // pigweed.ziggy
99 0x10, 0xdd, 0x01,
100 // pigweed.error_message
101 0x2a, 0x10, 'n', 'o', 't', ' ', 'a', ' ',
102 't', 'y', 'p', 'e', 'w', 'r', 'i', 't', 'e', 'r',
103 // pigweed.bin
104 0x40, 0x01,
105 // pigweed.pigweed
106 0x3a, 0x02,
107 // pigweed.pigweed.status
108 0x08, 0x02,
109 // pigweed.proto
110 0x4a, 0x56,
111 // pigweed.proto.bin
112 0x10, 0x00,
113 // pigweed.proto.pigweed_pigweed_bin
114 0x18, 0x00,
115 // pigweed.proto.pigweed_protobuf_bin
116 0x20, 0x01,
117 // pigweed.proto.meta
118 0x2a, 0x0f,
119 // pigweed.proto.meta.file_name
120 0x0a, 0x0b, '/', 'e', 't', 'c', '/', 'p', 'a', 's', 's', 'w', 'd',
121 // pigweed.proto.meta.status
122 0x10, 0x02,
123 // pigweed.proto.nested_pigweed
124 0x0a, 0x3d,
125 // pigweed.proto.nested_pigweed.error_message
126 0x2a, 0x10, 'h', 'e', 'r', 'e', ' ', 'w', 'e', ' ',
127 'g', 'o', ' ', 'a', 'g', 'a', 'i', 'n',
128 // pigweed.proto.nested_pigweed.magic_number
129 0x08, 0xe8, 0x04,
130 // pigweed.proto.nested_pigweed.device_info
131 0x32, 0x26,
132 // pigweed.proto.nested_pigweed.device_info.attributes[0]
133 0x22, 0x10,
134 // pigweed.proto.nested_pigweed.device_info.attributes[0].key
135 0x0a, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',
136 // pigweed.proto.nested_pigweed.device_info.attributes[0].value
137 0x12, 0x05, '5', '.', '3', '.', '1',
138 // pigweed.proto.nested_pigweed.device_info.attributes[1]
139 0x22, 0x10,
140 // pigweed.proto.nested_pigweed.device_info.attributes[1].key
141 0x0a, 0x04, 'c', 'h', 'i', 'p',
142 // pigweed.proto.nested_pigweed.device_info.attributes[1].value
143 0x12, 0x08, 'l', 'e', 'f', 't', '-', 's', 'o', 'c',
144 // pigweed.proto.nested_pigweed.device_info.status
145 0x18, 0x03,
146 // pigweed.id[0]
147 0x52, 0x02,
148 // pigweed.id[0].id
149 0x08, 0x31,
150 // pigweed.id[1]
151 0x52, 0x02,
152 // pigweed.id[1].id
153 0x08, 0x39,
154 // pigweed.id[2]
155 0x52, 0x02,
156 // pigweed.id[2].id
157 0x08, 0x4b,
158 // pigweed.id[3]
159 0x52, 0x02,
160 // pigweed.id[3].id
161 0x08, 0x67,
162 // pigweed.id[4]
163 0x52, 0x03,
164 // pigweed.id[4].id
165 0x08, 0x8d, 0x01
166 };
167 // clang-format on
168
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700169 Result result = encoder.Encode();
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800170 ASSERT_EQ(result.status(), OkStatus());
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700171 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
172 EXPECT_EQ(std::memcmp(
173 result.value().data(), expected_proto, sizeof(expected_proto)),
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800174 0);
175}
176
Armando Montanez1b5d6cb2021-04-20 18:43:15 -0700177TEST(Codegen, RecursiveSubmessage) {
178 std::byte encode_buffer[512];
179 NestedEncoder<20, 20> encoder(encode_buffer);
180
181 Crate::Encoder biggest_crate(&encoder);
182 biggest_crate.WriteName("Huge crate");
183
184 {
185 Crate::Encoder medium_crate = biggest_crate.GetSmallerCratesEncoder();
186 medium_crate.WriteName("Medium crate");
187 {
188 Crate::Encoder small_crate = medium_crate.GetSmallerCratesEncoder();
189 small_crate.WriteName("Small crate");
190 }
191 {
192 Crate::Encoder tiny_crate = medium_crate.GetSmallerCratesEncoder();
193 tiny_crate.WriteName("Tiny crate");
194 }
195 }
196
197 // clang-format off
198 constexpr uint8_t expected_proto[] = {
199 // crate.name
200 0x0a, 0x0a, 'H', 'u', 'g', 'e', ' ', 'c', 'r', 'a', 't', 'e',
201 // crate.smaller_crate[0]
202 0x12, 0x2b,
203 // crate.smaller_crate[0].name
204 0x0a, 0x0c, 'M', 'e', 'd', 'i', 'u', 'm', ' ', 'c', 'r', 'a', 't', 'e',
205 // crate.smaller_crate[0].smaller_crate[0]
206 0x12, 0x0d,
207 // crate.smaller_crate[0].smaller_crate[0].name
208 0x0a, 0x0b, 'S', 'm', 'a', 'l', 'l', ' ', 'c', 'r', 'a', 't', 'e',
209 // crate.smaller_crate[0].smaller_crate[1]
210 0x12, 0x0c,
211 // crate.smaller_crate[0].smaller_crate[1].name
212 0x0a, 0x0a, 'T', 'i', 'n', 'y', ' ', 'c', 'r', 'a', 't', 'e',
213 };
214 // clang-format on
215
216 Result result = encoder.Encode();
217 ASSERT_EQ(result.status(), OkStatus());
218 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
219 EXPECT_EQ(std::memcmp(
220 result.value().data(), expected_proto, sizeof(expected_proto)),
221 0);
222}
223
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800224TEST(CodegenRepeated, NonPackedScalar) {
Alexei Frolov311c6202020-01-06 11:16:20 -0800225 std::byte encode_buffer[32];
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800226 NestedEncoder encoder(encode_buffer);
227
228 RepeatedTest::Encoder repeated_test(&encoder);
229 for (int i = 0; i < 4; ++i) {
230 repeated_test.WriteUint32s(i * 16);
231 }
232
233 constexpr uint8_t expected_proto[] = {
234 0x08, 0x00, 0x08, 0x10, 0x08, 0x20, 0x08, 0x30};
235
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700236 Result result = encoder.Encode();
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800237 ASSERT_EQ(result.status(), OkStatus());
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700238 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
239 EXPECT_EQ(std::memcmp(
240 result.value().data(), expected_proto, sizeof(expected_proto)),
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800241 0);
242}
243
244TEST(CodegenRepeated, PackedScalar) {
Alexei Frolov311c6202020-01-06 11:16:20 -0800245 std::byte encode_buffer[32];
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800246 NestedEncoder encoder(encode_buffer);
247
248 RepeatedTest::Encoder repeated_test(&encoder);
249 constexpr uint32_t values[] = {0, 16, 32, 48};
250 repeated_test.WriteUint32s(values);
251
252 constexpr uint8_t expected_proto[] = {0x0a, 0x04, 0x00, 0x10, 0x20, 0x30};
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700253 Result result = encoder.Encode();
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800254 ASSERT_EQ(result.status(), OkStatus());
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700255 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
256 EXPECT_EQ(std::memcmp(
257 result.value().data(), expected_proto, sizeof(expected_proto)),
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800258 0);
259}
260
261TEST(CodegenRepeated, NonScalar) {
Alexei Frolov311c6202020-01-06 11:16:20 -0800262 std::byte encode_buffer[32];
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800263 NestedEncoder encoder(encode_buffer);
264
265 RepeatedTest::Encoder repeated_test(&encoder);
266 constexpr const char* strings[] = {"the", "quick", "brown", "fox"};
267 for (const char* s : strings) {
268 repeated_test.WriteStrings(s);
269 }
270
271 constexpr uint8_t expected_proto[] = {
272 0x1a, 0x03, 't', 'h', 'e', 0x1a, 0x5, 'q', 'u', 'i', 'c', 'k',
273 0x1a, 0x5, 'b', 'r', 'o', 'w', 'n', 0x1a, 0x3, 'f', 'o', 'x'};
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700274 Result result = encoder.Encode();
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800275 ASSERT_EQ(result.status(), OkStatus());
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700276 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
277 EXPECT_EQ(std::memcmp(
278 result.value().data(), expected_proto, sizeof(expected_proto)),
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800279 0);
280}
281
282TEST(CodegenRepeated, Message) {
Alexei Frolov311c6202020-01-06 11:16:20 -0800283 std::byte encode_buffer[64];
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800284 NestedEncoder<1, 3> encoder(encode_buffer);
285
286 RepeatedTest::Encoder repeated_test(&encoder);
287 for (int i = 0; i < 3; ++i) {
288 auto structs = repeated_test.GetStructsEncoder();
289 structs.WriteOne(i * 1);
290 structs.WriteTwo(i * 2);
291 }
292
293 // clang-format off
294 constexpr uint8_t expected_proto[] = {
295 0x2a, 0x04, 0x08, 0x00, 0x10, 0x00, 0x2a, 0x04, 0x08,
296 0x01, 0x10, 0x02, 0x2a, 0x04, 0x08, 0x02, 0x10, 0x04};
297 // clang-format on
298
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700299 Result result = encoder.Encode();
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800300 ASSERT_EQ(result.status(), OkStatus());
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700301 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
302 EXPECT_EQ(std::memcmp(
303 result.value().data(), expected_proto, sizeof(expected_proto)),
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800304 0);
305}
306
307TEST(Codegen, Proto2) {
Alexei Frolov311c6202020-01-06 11:16:20 -0800308 std::byte encode_buffer[64];
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800309 NestedEncoder<1, 3> encoder(encode_buffer);
310
311 Foo::Encoder foo(&encoder);
312 foo.WriteInt(3);
313
314 {
315 constexpr std::byte data[] = {
316 std::byte(0xde), std::byte(0xad), std::byte(0xbe), std::byte(0xef)};
317 Bar::Encoder bar = foo.GetBarEncoder();
318 bar.WriteData(data);
319 }
320
321 constexpr uint8_t expected_proto[] = {
322 0x08, 0x03, 0x1a, 0x06, 0x0a, 0x04, 0xde, 0xad, 0xbe, 0xef};
323
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700324 Result result = encoder.Encode();
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800325 ASSERT_EQ(result.status(), OkStatus());
Alexei Frolov13c7c4f2020-10-02 09:41:01 -0700326 EXPECT_EQ(result.value().size(), sizeof(expected_proto));
327 EXPECT_EQ(std::memcmp(
328 result.value().data(), expected_proto, sizeof(expected_proto)),
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800329 0);
330}
331
Alexei Frolove19ebb82020-05-14 17:21:20 -0700332TEST(Codegen, Import) {
333 std::byte encode_buffer[64];
334 NestedEncoder<1, 3> encoder(encode_buffer);
335
336 Period::Encoder period(&encoder);
337 {
338 imported::Timestamp::Encoder start = period.GetStartEncoder();
339 start.WriteSeconds(1589501793);
340 start.WriteNanoseconds(511613110);
341 }
342
343 {
344 imported::Timestamp::Encoder end = period.GetEndEncoder();
345 end.WriteSeconds(1589501841);
346 end.WriteNanoseconds(490367432);
347 }
348
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800349 EXPECT_EQ(encoder.Encode().status(), OkStatus());
Alexei Frolove19ebb82020-05-14 17:21:20 -0700350}
351
Nathaniel Brough06fe47b2020-06-19 17:40:49 +0800352TEST(Codegen, NonPigweedPackage) {
353 using namespace non::pigweed::package::name;
354 std::byte encode_buffer[64];
355 std::array<const int64_t, 2> repeated = {0, 1};
356 NestedEncoder<1, 2> encoder(encode_buffer);
357 Packed::Encoder packed(&encoder);
358 packed.WriteRep(std::span<const int64_t>(repeated));
359 packed.WritePacked("packed");
360
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800361 EXPECT_EQ(encoder.Encode().status(), OkStatus());
Nathaniel Brough06fe47b2020-06-19 17:40:49 +0800362}
363
Alexei Frolovbbf164c2019-12-16 12:51:59 -0800364} // namespace
365} // namespace pw::protobuf