aboutsummaryrefslogtreecommitdiff
path: root/pw_protobuf/codegen_test.cc
blob: 72e11a7e0ee2f17747fd65b05afa0281136b8646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#include <span>

#include "gtest/gtest.h"
#include "pw_protobuf/encoder.h"

// These header files contain the code generated by the pw_protobuf plugin.
// They are re-generated every time the tests are built and are used by the
// tests to ensure that the interface remains consistent.
//
// The purpose of the tests in this file is primarily to verify that the
// generated C++ interface is valid rather than the correctness of the
// low-level encoder.
#include "pw_protobuf_protos/test_protos/full_test.pwpb.h"
#include "pw_protobuf_protos/test_protos/importer.pwpb.h"
#include "pw_protobuf_protos/test_protos/non_pw_package.pwpb.h"
#include "pw_protobuf_protos/test_protos/proto2.pwpb.h"
#include "pw_protobuf_protos/test_protos/repeated.pwpb.h"

namespace pw::protobuf {
namespace {

using namespace pw::protobuf::test;

TEST(Codegen, Codegen) {
  std::byte encode_buffer[512];
  NestedEncoder<20, 20> encoder(encode_buffer);

  Pigweed::Encoder pigweed(&encoder);
  pigweed.WriteMagicNumber(73);
  pigweed.WriteZiggy(-111);
  pigweed.WriteErrorMessage("not a typewriter");
  pigweed.WriteBin(Pigweed::Protobuf::Binary::ZERO);

  {
    Pigweed::Pigweed::Encoder pigweed_pigweed = pigweed.GetPigweedEncoder();
    pigweed_pigweed.WriteStatus(Bool::FILE_NOT_FOUND);
  }

  {
    Proto::Encoder proto = pigweed.GetProtoEncoder();
    proto.WriteBin(Proto::Binary::OFF);
    proto.WritePigweedPigweedBin(Pigweed::Pigweed::Binary::ZERO);
    proto.WritePigweedProtobufBin(Pigweed::Protobuf::Binary::ZERO);

    {
      Pigweed::Protobuf::Compiler::Encoder meta = proto.GetMetaEncoder();
      meta.WriteFileName("/etc/passwd");
      meta.WriteStatus(Pigweed::Protobuf::Compiler::Status::FUBAR);
    }

    {
      Pigweed::Encoder nested_pigweed = proto.GetPigweedEncoder();
      pigweed.WriteErrorMessage("here we go again");
      pigweed.WriteMagicNumber(616);

      {
        DeviceInfo::Encoder device_info = nested_pigweed.GetDeviceInfoEncoder();

        {
          KeyValuePair::Encoder attributes = device_info.GetAttributesEncoder();
          attributes.WriteKey("version");
          attributes.WriteValue("5.3.1");
        }

        {
          KeyValuePair::Encoder attributes = device_info.GetAttributesEncoder();
          attributes.WriteKey("chip");
          attributes.WriteValue("left-soc");
        }

        device_info.WriteStatus(DeviceInfo::DeviceStatus::PANIC);
      }
    }
  }

  for (int i = 0; i < 5; ++i) {
    Proto::ID::Encoder id = pigweed.GetIdEncoder();
    id.WriteId(5 * i * i + 3 * i + 49);
  }

  // clang-format off
  constexpr uint8_t expected_proto[] = {
    // pigweed.magic_number
    0x08, 0x49,
    // pigweed.ziggy
    0x10, 0xdd, 0x01,
    // pigweed.error_message
    0x2a, 0x10, 'n', 'o', 't', ' ', 'a', ' ',
    't', 'y', 'p', 'e', 'w', 'r', 'i', 't', 'e', 'r',
    // pigweed.bin
    0x40, 0x01,
    // pigweed.pigweed
    0x3a, 0x02,
    // pigweed.pigweed.status
    0x08, 0x02,
    // pigweed.proto
    0x4a, 0x56,
    // pigweed.proto.bin
    0x10, 0x00,
    // pigweed.proto.pigweed_pigweed_bin
    0x18, 0x00,
    // pigweed.proto.pigweed_protobuf_bin
    0x20, 0x01,
    // pigweed.proto.meta
    0x2a, 0x0f,
    // pigweed.proto.meta.file_name
    0x0a, 0x0b, '/', 'e', 't', 'c', '/', 'p', 'a', 's', 's', 'w', 'd',
    // pigweed.proto.meta.status
    0x10, 0x02,
    // pigweed.proto.nested_pigweed
    0x0a, 0x3d,
    // pigweed.proto.nested_pigweed.error_message
    0x2a, 0x10, 'h', 'e', 'r', 'e', ' ', 'w', 'e', ' ',
    'g', 'o', ' ', 'a', 'g', 'a', 'i', 'n',
    // pigweed.proto.nested_pigweed.magic_number
    0x08, 0xe8, 0x04,
    // pigweed.proto.nested_pigweed.device_info
    0x32, 0x26,
    // pigweed.proto.nested_pigweed.device_info.attributes[0]
    0x22, 0x10,
    // pigweed.proto.nested_pigweed.device_info.attributes[0].key
    0x0a, 0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n',
    // pigweed.proto.nested_pigweed.device_info.attributes[0].value
    0x12, 0x05, '5', '.', '3', '.', '1',
    // pigweed.proto.nested_pigweed.device_info.attributes[1]
    0x22, 0x10,
    // pigweed.proto.nested_pigweed.device_info.attributes[1].key
    0x0a, 0x04, 'c', 'h', 'i', 'p',
    // pigweed.proto.nested_pigweed.device_info.attributes[1].value
    0x12, 0x08, 'l', 'e', 'f', 't', '-', 's', 'o', 'c',
    // pigweed.proto.nested_pigweed.device_info.status
    0x18, 0x03,
    // pigweed.id[0]
    0x52, 0x02,
    // pigweed.id[0].id
    0x08, 0x31,
    // pigweed.id[1]
    0x52, 0x02,
    // pigweed.id[1].id
    0x08, 0x39,
    // pigweed.id[2]
    0x52, 0x02,
    // pigweed.id[2].id
    0x08, 0x4b,
    // pigweed.id[3]
    0x52, 0x02,
    // pigweed.id[3].id
    0x08, 0x67,
    // pigweed.id[4]
    0x52, 0x03,
    // pigweed.id[4].id
    0x08, 0x8d, 0x01
  };
  // clang-format on

  Result result = encoder.Encode();
  ASSERT_EQ(result.status(), Status::Ok());
  EXPECT_EQ(result.value().size(), sizeof(expected_proto));
  EXPECT_EQ(std::memcmp(
                result.value().data(), expected_proto, sizeof(expected_proto)),
            0);
}

TEST(CodegenRepeated, NonPackedScalar) {
  std::byte encode_buffer[32];
  NestedEncoder encoder(encode_buffer);

  RepeatedTest::Encoder repeated_test(&encoder);
  for (int i = 0; i < 4; ++i) {
    repeated_test.WriteUint32s(i * 16);
  }

  constexpr uint8_t expected_proto[] = {
      0x08, 0x00, 0x08, 0x10, 0x08, 0x20, 0x08, 0x30};

  Result result = encoder.Encode();
  ASSERT_EQ(result.status(), Status::Ok());
  EXPECT_EQ(result.value().size(), sizeof(expected_proto));
  EXPECT_EQ(std::memcmp(
                result.value().data(), expected_proto, sizeof(expected_proto)),
            0);
}

TEST(CodegenRepeated, PackedScalar) {
  std::byte encode_buffer[32];
  NestedEncoder encoder(encode_buffer);

  RepeatedTest::Encoder repeated_test(&encoder);
  constexpr uint32_t values[] = {0, 16, 32, 48};
  repeated_test.WriteUint32s(values);

  constexpr uint8_t expected_proto[] = {0x0a, 0x04, 0x00, 0x10, 0x20, 0x30};
  Result result = encoder.Encode();
  ASSERT_EQ(result.status(), Status::Ok());
  EXPECT_EQ(result.value().size(), sizeof(expected_proto));
  EXPECT_EQ(std::memcmp(
                result.value().data(), expected_proto, sizeof(expected_proto)),
            0);
}

TEST(CodegenRepeated, NonScalar) {
  std::byte encode_buffer[32];
  NestedEncoder encoder(encode_buffer);

  RepeatedTest::Encoder repeated_test(&encoder);
  constexpr const char* strings[] = {"the", "quick", "brown", "fox"};
  for (const char* s : strings) {
    repeated_test.WriteStrings(s);
  }

  constexpr uint8_t expected_proto[] = {
      0x1a, 0x03, 't', 'h', 'e', 0x1a, 0x5, 'q',  'u', 'i', 'c', 'k',
      0x1a, 0x5,  'b', 'r', 'o', 'w',  'n', 0x1a, 0x3, 'f', 'o', 'x'};
  Result result = encoder.Encode();
  ASSERT_EQ(result.status(), Status::Ok());
  EXPECT_EQ(result.value().size(), sizeof(expected_proto));
  EXPECT_EQ(std::memcmp(
                result.value().data(), expected_proto, sizeof(expected_proto)),
            0);
}

TEST(CodegenRepeated, Message) {
  std::byte encode_buffer[64];
  NestedEncoder<1, 3> encoder(encode_buffer);

  RepeatedTest::Encoder repeated_test(&encoder);
  for (int i = 0; i < 3; ++i) {
    auto structs = repeated_test.GetStructsEncoder();
    structs.WriteOne(i * 1);
    structs.WriteTwo(i * 2);
  }

  // clang-format off
  constexpr uint8_t expected_proto[] = {
    0x2a, 0x04, 0x08, 0x00, 0x10, 0x00, 0x2a, 0x04, 0x08,
    0x01, 0x10, 0x02, 0x2a, 0x04, 0x08, 0x02, 0x10, 0x04};
  // clang-format on

  Result result = encoder.Encode();
  ASSERT_EQ(result.status(), Status::Ok());
  EXPECT_EQ(result.value().size(), sizeof(expected_proto));
  EXPECT_EQ(std::memcmp(
                result.value().data(), expected_proto, sizeof(expected_proto)),
            0);
}

TEST(Codegen, Proto2) {
  std::byte encode_buffer[64];
  NestedEncoder<1, 3> encoder(encode_buffer);

  Foo::Encoder foo(&encoder);
  foo.WriteInt(3);

  {
    constexpr std::byte data[] = {
        std::byte(0xde), std::byte(0xad), std::byte(0xbe), std::byte(0xef)};
    Bar::Encoder bar = foo.GetBarEncoder();
    bar.WriteData(data);
  }

  constexpr uint8_t expected_proto[] = {
      0x08, 0x03, 0x1a, 0x06, 0x0a, 0x04, 0xde, 0xad, 0xbe, 0xef};

  Result result = encoder.Encode();
  ASSERT_EQ(result.status(), Status::Ok());
  EXPECT_EQ(result.value().size(), sizeof(expected_proto));
  EXPECT_EQ(std::memcmp(
                result.value().data(), expected_proto, sizeof(expected_proto)),
            0);
}

TEST(Codegen, Import) {
  std::byte encode_buffer[64];
  NestedEncoder<1, 3> encoder(encode_buffer);

  Period::Encoder period(&encoder);
  {
    imported::Timestamp::Encoder start = period.GetStartEncoder();
    start.WriteSeconds(1589501793);
    start.WriteNanoseconds(511613110);
  }

  {
    imported::Timestamp::Encoder end = period.GetEndEncoder();
    end.WriteSeconds(1589501841);
    end.WriteNanoseconds(490367432);
  }

  EXPECT_EQ(encoder.Encode().status(), Status::Ok());
}

TEST(Codegen, NonPigweedPackage) {
  using namespace non::pigweed::package::name;
  std::byte encode_buffer[64];
  std::array<const int64_t, 2> repeated = {0, 1};
  NestedEncoder<1, 2> encoder(encode_buffer);
  Packed::Encoder packed(&encoder);
  packed.WriteRep(std::span<const int64_t>(repeated));
  packed.WritePacked("packed");

  EXPECT_EQ(encoder.Encode().status(), Status::Ok());
}

}  // namespace
}  // namespace pw::protobuf