aboutsummaryrefslogtreecommitdiff
path: root/pw_log/proto_utils_test.cc
blob: 94ba4578697e200e35746d6f3cf3b2cb83d4a25f (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright 2021 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 "pw_log/proto_utils.h"

#include "gtest/gtest.h"
#include "pw_bytes/span.h"
#include "pw_containers/algorithm.h"
#include "pw_log/levels.h"
#include "pw_log/proto/log.pwpb.h"
#include "pw_protobuf/bytes_utils.h"
#include "pw_protobuf/decoder.h"

namespace pw::log {

void VerifyTokenizedLogEntry(pw::protobuf::Decoder& entry_decoder,
                             pw::log_tokenized::Metadata expected_metadata,
                             ConstByteSpan expected_tokenized_data,
                             const int64_t expected_timestamp,
                             ConstByteSpan expected_thread_name) {
  ConstByteSpan tokenized_data;
  EXPECT_TRUE(entry_decoder.Next().ok());  // message [tokenized]
  EXPECT_EQ(entry_decoder.FieldNumber(),
            static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kMessage));
  EXPECT_TRUE(entry_decoder.ReadBytes(&tokenized_data).ok());
  EXPECT_TRUE(std::memcmp(tokenized_data.data(),
                          expected_tokenized_data.data(),
                          expected_tokenized_data.size()) == 0);

  uint32_t line_level;
  EXPECT_TRUE(entry_decoder.Next().ok());  // line_level
  EXPECT_EQ(entry_decoder.FieldNumber(),
            static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kLineLevel));
  EXPECT_TRUE(entry_decoder.ReadUint32(&line_level).ok());

  uint32_t line_number;
  uint8_t level;
  std::tie(line_number, level) = UnpackLineLevel(line_level);
  EXPECT_EQ(expected_metadata.level(), level);
  EXPECT_EQ(expected_metadata.line_number(), line_number);

  if (expected_metadata.flags() != 0) {
    uint32_t flags;
    EXPECT_TRUE(entry_decoder.Next().ok());  // flags
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kFlags));
    EXPECT_TRUE(entry_decoder.ReadUint32(&flags).ok());
    EXPECT_EQ(expected_metadata.flags(), flags);
  }

  int64_t timestamp;
  EXPECT_TRUE(entry_decoder.Next().ok());  // timestamp
  EXPECT_TRUE(
      entry_decoder.FieldNumber() ==
          static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kTimestamp) ||
      entry_decoder.FieldNumber() ==
          static_cast<uint32_t>(
              log::pwpb::LogEntry::Fields::kTimeSinceLastEntry));
  EXPECT_TRUE(entry_decoder.ReadInt64(&timestamp).ok());
  EXPECT_EQ(expected_timestamp, timestamp);

  if (expected_metadata.module() != 0) {
    EXPECT_TRUE(entry_decoder.Next().ok());  // module name
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kModule));
    const Result<uint32_t> module =
        protobuf::DecodeBytesToUint32(entry_decoder);
    ASSERT_TRUE(module.ok());
    EXPECT_EQ(expected_metadata.module(), module.value());
  }

  if (!expected_thread_name.empty()) {
    ConstByteSpan tokenized_thread_name;
    EXPECT_TRUE(entry_decoder.Next().ok());  // thread [tokenized]
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kThread));
    EXPECT_TRUE(entry_decoder.ReadBytes(&tokenized_thread_name).ok());
    EXPECT_TRUE(std::memcmp(tokenized_thread_name.data(),
                            expected_thread_name.data(),
                            expected_thread_name.size()) == 0);
  }
}

void VerifyLogEntry(pw::protobuf::Decoder& entry_decoder,
                    int expected_level,
                    unsigned int expected_flags,
                    std::string_view expected_module,
                    std::string_view expected_thread_name,
                    std::string_view expected_file_name,
                    int expected_line_number,
                    int64_t expected_ticks_since_epoch,
                    std::string_view expected_message) {
  std::string_view message;
  EXPECT_TRUE(entry_decoder.Next().ok());  // message
  EXPECT_EQ(entry_decoder.FieldNumber(),
            static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kMessage));
  EXPECT_TRUE(entry_decoder.ReadString(&message).ok());
  EXPECT_TRUE(pw::containers::Equal(message, expected_message));

  uint32_t line_level;
  EXPECT_TRUE(entry_decoder.Next().ok());  // line_level
  EXPECT_EQ(entry_decoder.FieldNumber(),
            static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kLineLevel));
  EXPECT_TRUE(entry_decoder.ReadUint32(&line_level).ok());
  uint32_t line_number;
  uint8_t level;
  std::tie(line_number, level) = UnpackLineLevel(line_level);
  EXPECT_EQ(static_cast<unsigned int>(expected_line_number), line_number);
  EXPECT_EQ(expected_level, level);

  if (expected_flags != 0) {
    uint32_t flags;
    EXPECT_TRUE(entry_decoder.Next().ok());  // flags
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kFlags));
    EXPECT_TRUE(entry_decoder.ReadUint32(&flags).ok());
    EXPECT_EQ(expected_flags, flags);
  }

  int64_t timestamp;
  EXPECT_TRUE(entry_decoder.Next().ok());  // timestamp
  EXPECT_TRUE(
      entry_decoder.FieldNumber() ==
          static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kTimestamp) ||
      entry_decoder.FieldNumber() ==
          static_cast<uint32_t>(
              log::pwpb::LogEntry::Fields::kTimeSinceLastEntry));
  EXPECT_TRUE(entry_decoder.ReadInt64(&timestamp).ok());
  EXPECT_EQ(expected_ticks_since_epoch, timestamp);

  if (!expected_module.empty()) {
    std::string_view module_name;
    EXPECT_TRUE(entry_decoder.Next().ok());  // module
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kModule));
    EXPECT_TRUE(entry_decoder.ReadString(&module_name).ok());
    EXPECT_TRUE(pw::containers::Equal(module_name, expected_module));
  }

  if (!expected_file_name.empty()) {
    std::string_view file_name;
    EXPECT_TRUE(entry_decoder.Next().ok());  // file
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kFile));
    EXPECT_TRUE(entry_decoder.ReadString(&file_name).ok());
    EXPECT_TRUE(pw::containers::Equal(file_name, expected_file_name));
  }

  if (!expected_thread_name.empty()) {
    std::string_view thread_name;
    EXPECT_TRUE(entry_decoder.Next().ok());  // file
    EXPECT_EQ(entry_decoder.FieldNumber(),
              static_cast<uint32_t>(log::pwpb::LogEntry::Fields::kThread));
    EXPECT_TRUE(entry_decoder.ReadString(&thread_name).ok());
    EXPECT_TRUE(pw::containers::Equal(thread_name, expected_thread_name));
  }
}

TEST(UtilsTest, LineLevelPacking) {
  constexpr uint8_t kExpectedLevel = PW_LOG_LEVEL_ERROR;
  constexpr uint32_t kExpectedLine = 1234567;
  constexpr uint32_t kExpectedLineLevel =
      (kExpectedLine << PW_LOG_LEVEL_BITS) |
      (kExpectedLevel & PW_LOG_LEVEL_BITMASK);

  EXPECT_EQ(kExpectedLineLevel, PackLineLevel(kExpectedLine, kExpectedLevel));
}

TEST(UtilsTest, LineLevelUnpacking) {
  constexpr uint8_t kExpectedLevel = PW_LOG_LEVEL_ERROR;
  constexpr uint32_t kExpectedLine = 1234567;
  constexpr uint32_t kExpectedLineLevel =
      (kExpectedLine << PW_LOG_LEVEL_BITS) |
      (kExpectedLevel & PW_LOG_LEVEL_BITMASK);

  uint32_t line_number;
  uint8_t level;
  std::tie(line_number, level) = UnpackLineLevel(kExpectedLineLevel);

  EXPECT_EQ(kExpectedLine, line_number);
  EXPECT_EQ(kExpectedLevel, level);
}

TEST(UtilsTest, LineLevelPackAndUnpack) {
  constexpr uint8_t kExpectedLevel = PW_LOG_LEVEL_ERROR;
  constexpr uint32_t kExpectedLine = 1234567;

  uint32_t line_number;
  uint8_t level;
  std::tie(line_number, level) =
      UnpackLineLevel(PackLineLevel(kExpectedLine, kExpectedLevel));

  EXPECT_EQ(kExpectedLine, line_number);
  EXPECT_EQ(kExpectedLevel, level);
}

TEST(UtilsTest, EncodeTokenizedLog) {
  constexpr std::byte kTokenizedData[1] = {std::byte(0x01)};
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::byte kExpectedThreadName[1] = {std::byte(0x02)};
  std::byte encode_buffer[32];

  pw::log_tokenized::Metadata metadata =
      pw::log_tokenized::Metadata::Set<1, 2, 3, 4>();

  Result<ConstByteSpan> result = EncodeTokenizedLog(metadata,
                                                    kTokenizedData,
                                                    kExpectedTimestamp,
                                                    kExpectedThreadName,
                                                    encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyTokenizedLogEntry(log_decoder,
                          metadata,
                          kTokenizedData,
                          kExpectedTimestamp,
                          kExpectedThreadName);

  result = EncodeTokenizedLog(metadata,
                              reinterpret_cast<const uint8_t*>(kTokenizedData),
                              sizeof(kTokenizedData),
                              kExpectedTimestamp,
                              kExpectedThreadName,
                              encode_buffer);
  EXPECT_TRUE(result.ok());

  log_decoder.Reset(result.value());
  VerifyTokenizedLogEntry(log_decoder,
                          metadata,
                          kTokenizedData,
                          kExpectedTimestamp,
                          kExpectedThreadName);
}

TEST(UtilsTest, EncodeTokenizedLog_EmptyFlags) {
  constexpr std::byte kTokenizedData[1] = {std::byte(0x01)};
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::byte kExpectedThreadName[1] = {std::byte(0x02)};
  std::byte encode_buffer[32];

  // Create an empty flags set.
  pw::log_tokenized::Metadata metadata =
      pw::log_tokenized::Metadata::Set<1, 2, 0, 4>();

  Result<ConstByteSpan> result = EncodeTokenizedLog(metadata,
                                                    kTokenizedData,
                                                    kExpectedTimestamp,
                                                    kExpectedThreadName,
                                                    encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyTokenizedLogEntry(log_decoder,
                          metadata,
                          kTokenizedData,
                          kExpectedTimestamp,
                          kExpectedThreadName);
}

TEST(UtilsTest, EncodeTokenizedLog_InsufficientSpace) {
  constexpr std::byte kTokenizedData[1] = {std::byte(0x01)};
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::byte kExpectedThreadName[1] = {std::byte(0x02)};
  std::byte encode_buffer[1];

  pw::log_tokenized::Metadata metadata =
      pw::log_tokenized::Metadata::Set<1, 2, 3, 4>();

  Result<ConstByteSpan> result = EncodeTokenizedLog(metadata,
                                                    kTokenizedData,
                                                    kExpectedTimestamp,
                                                    kExpectedThreadName,
                                                    encode_buffer);
  EXPECT_TRUE(result.status().IsResourceExhausted());
}

TEST(UtilsTest, EncodeLog) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 2;
  constexpr std::string_view kExpectedModule("TST");
  constexpr std::string_view kExpectedThread("thread");
  constexpr std::string_view kExpectedFile("proto_test.cc");
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage("msg");
  std::byte encode_buffer[64];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyLogEntry(log_decoder,
                 kExpectedLevel,
                 kExpectedFlags,
                 kExpectedModule,
                 kExpectedThread,
                 kExpectedFile,
                 kExpectedLine,
                 kExpectedTimestamp,
                 kExpectedMessage);
}

TEST(UtilsTest, EncodeLog_EmptyFlags) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 0;
  constexpr std::string_view kExpectedModule("TST");
  constexpr std::string_view kExpectedThread("thread");
  constexpr std::string_view kExpectedFile("proto_test.cc");
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage("msg");
  std::byte encode_buffer[64];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyLogEntry(log_decoder,
                 kExpectedLevel,
                 kExpectedFlags,
                 kExpectedModule,
                 kExpectedThread,
                 kExpectedFile,
                 kExpectedLine,
                 kExpectedTimestamp,
                 kExpectedMessage);
}

TEST(UtilsTest, EncodeLog_EmptyFile) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 0;
  constexpr std::string_view kExpectedModule("TST");
  constexpr std::string_view kExpectedThread("thread");
  constexpr std::string_view kExpectedFile;
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage("msg");
  std::byte encode_buffer[64];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyLogEntry(log_decoder,
                 kExpectedLevel,
                 kExpectedFlags,
                 kExpectedModule,
                 kExpectedThread,
                 kExpectedFile,
                 kExpectedLine,
                 kExpectedTimestamp,
                 kExpectedMessage);
}

TEST(UtilsTest, EncodeLog_EmptyModule) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 3;
  constexpr std::string_view kExpectedModule;
  constexpr std::string_view kExpectedThread("thread");
  constexpr std::string_view kExpectedFile("test.cc");
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage("msg");
  std::byte encode_buffer[64];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyLogEntry(log_decoder,
                 kExpectedLevel,
                 kExpectedFlags,
                 kExpectedModule,
                 kExpectedThread,
                 kExpectedFile,
                 kExpectedLine,
                 kExpectedTimestamp,
                 kExpectedMessage);
}

TEST(UtilsTest, EncodeLog_EmptyThread) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 2;
  constexpr std::string_view kExpectedModule("TST");
  constexpr std::string_view kExpectedThread;
  constexpr std::string_view kExpectedFile("proto_test.cc");
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage("msg");
  std::byte encode_buffer[64];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);
  EXPECT_TRUE(result.ok());

  pw::protobuf::Decoder log_decoder(result.value());
  VerifyLogEntry(log_decoder,
                 kExpectedLevel,
                 kExpectedFlags,
                 kExpectedModule,
                 kExpectedThread,
                 kExpectedFile,
                 kExpectedLine,
                 kExpectedTimestamp,
                 kExpectedMessage);
}

TEST(UtilsTest, EncodeLog_EmptyMessage) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 0;
  constexpr std::string_view kExpectedModule;
  constexpr std::string_view kExpectedThread;
  constexpr std::string_view kExpectedFile;
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage;
  std::byte encode_buffer[64];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);

  EXPECT_TRUE(result.status().IsInvalidArgument());
}

TEST(UtilsTest, EncodeLog_InsufficientSpace) {
  constexpr int kExpectedLevel = PW_LOG_LEVEL_INFO;
  constexpr unsigned int kExpectedFlags = 0;
  constexpr std::string_view kExpectedModule;
  constexpr std::string_view kExpectedThread;
  constexpr std::string_view kExpectedFile;
  constexpr int kExpectedLine = 14;
  constexpr int64_t kExpectedTimestamp = 1;
  constexpr std::string_view kExpectedMessage("msg");
  std::byte encode_buffer[1];

  Result<ConstByteSpan> result = EncodeLog(kExpectedLevel,
                                           kExpectedFlags,
                                           kExpectedModule,
                                           kExpectedThread,
                                           kExpectedFile,
                                           kExpectedLine,
                                           kExpectedTimestamp,
                                           kExpectedMessage,
                                           encode_buffer);

  EXPECT_TRUE(result.status().IsResourceExhausted());
}

}  // namespace pw::log