aboutsummaryrefslogtreecommitdiff
path: root/pw_protobuf/find_test.cc
blob: cb78708a1aece87b647e2e58a1fbf38844d2645c (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
// Copyright 2023 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_protobuf/find.h"

#include "gtest/gtest.h"
#include "pw_stream/memory_stream.h"
#include "pw_string/string.h"

namespace pw::protobuf {
namespace {

// clang-format off
constexpr uint8_t _encoded_proto[] = {
  // type=int32, k=1, v=42
  0x08, 0x2a,
  // type=sint32, k=2, v=-13
  0x10, 0x19,
  // type=bool, k=3, v=false
  0x18, 0x00,
  // type=double, k=4, v=3.14159
  0x21, 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40,
  // type=fixed32, k=5, v=0xdeadbeef
  0x2d, 0xef, 0xbe, 0xad, 0xde,
  // type=string, k=6, v="Hello world"
  0x32, 0x0b, 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd',

  // type=message, k=7, len=2
  0x3a, 0x02,
  // (nested) type=uint32, k=1, v=3
  0x08, 0x03
};
ConstByteSpan encoded_proto(as_bytes(span(_encoded_proto)));

TEST(Find, PresentField) {
  EXPECT_EQ(FindInt32(encoded_proto, 1).value(), 42);
  EXPECT_EQ(FindSint32(encoded_proto, 2).value(), -13);
  EXPECT_EQ(FindBool(encoded_proto, 3).value(), false);
  EXPECT_EQ(FindDouble(encoded_proto, 4).value(), 3.14159);
  EXPECT_EQ(FindFixed32(encoded_proto, 5).value(), 0xdeadbeef);

  Result<std::string_view> result = FindString(encoded_proto, 6);
  ASSERT_EQ(result.status(), OkStatus());
  InlineString<32> str(*result);
  EXPECT_STREQ(str.c_str(), "Hello world");
}

TEST(Find, MissingField) {
  EXPECT_EQ(FindUint32(encoded_proto, 8).status(), Status::NotFound());
  EXPECT_EQ(FindUint32(encoded_proto, 66).status(), Status::NotFound());
  EXPECT_EQ(FindUint32(encoded_proto, 123456789).status(), Status::NotFound());
}

TEST(Find, InvalidFieldNumber) {
  EXPECT_EQ(FindUint32(encoded_proto, 0).status(), Status::InvalidArgument());
  EXPECT_EQ(FindUint32(encoded_proto, uint32_t(-1)).status(), Status::InvalidArgument());
}

TEST(Find, WrongWireType) {
  // Field 5 is a fixed32, but we request a uint32 (varint).
  EXPECT_EQ(FindUint32(encoded_proto, 5).status(), Status::FailedPrecondition());
}

TEST(Find, MultiLevel) {
  Result<ConstByteSpan> submessage = FindSubmessage(encoded_proto, 7);
  ASSERT_EQ(submessage.status(), OkStatus());
  EXPECT_EQ(submessage->size(), 2u);

  // Read a field from the submessage.
  EXPECT_EQ(FindUint32(*submessage, 1).value(), 3u);
}

TEST(FindStream, PresentField) {
  stream::MemoryReader reader(encoded_proto);

  EXPECT_EQ(FindInt32(reader, 1).value(), 42);
  EXPECT_EQ(FindSint32(reader, 2).value(), -13);
  EXPECT_EQ(FindBool(reader, 3).value(), false);
  EXPECT_EQ(FindDouble(encoded_proto, 4).value(), 3.14159);

  EXPECT_EQ(FindFixed32(reader, 5).value(), 0xdeadbeef);

  char str[32];
  StatusWithSize sws = FindString(reader, 6, str);
  ASSERT_EQ(sws.status(), OkStatus());
  ASSERT_EQ(sws.size(), 11u);
  str[sws.size()] = '\0';
  EXPECT_STREQ(str, "Hello world");
}

TEST(FindStream, MissingField) {
  stream::MemoryReader reader(encoded_proto);
  EXPECT_EQ(FindUint32(reader, 8).status(), Status::NotFound());

  reader = stream::MemoryReader(encoded_proto);
  EXPECT_EQ(FindUint32(reader, 66).status(), Status::NotFound());

  reader = stream::MemoryReader(encoded_proto);
  EXPECT_EQ(FindUint32(reader, 123456789).status(), Status::NotFound());
}

TEST(FindStream, InvalidFieldNumber) {
  stream::MemoryReader reader(encoded_proto);
  EXPECT_EQ(FindUint32(reader, 0).status(), Status::InvalidArgument());

  reader = stream::MemoryReader(encoded_proto);
  EXPECT_EQ(FindUint32(reader, uint32_t(-1)).status(), Status::InvalidArgument());
}

TEST(FindStream, WrongWireType) {
  stream::MemoryReader reader(encoded_proto);

  // Field 5 is a fixed32, but we request a uint32 (varint).
  EXPECT_EQ(FindUint32(reader, 5).status(), Status::FailedPrecondition());
}

enum class Fields : uint32_t {
  kField1 = 1,
  kField2 = 2,
  kField3 = 3,
  kField4 = 4,
  kField5 = 5,
  kField6 = 6,
  kField7 = 7,
};

TEST(FindEnum, PresentField) {
  EXPECT_EQ(FindInt32(encoded_proto, Fields::kField1).value(), 42);
  EXPECT_EQ(FindSint32(encoded_proto, Fields::kField2).value(), -13);
  EXPECT_EQ(FindBool(encoded_proto, Fields::kField3).value(), false);
  EXPECT_EQ(FindDouble(encoded_proto, Fields::kField4).value(), 3.14159);
  EXPECT_EQ(FindFixed32(encoded_proto, Fields::kField5).value(), 0xdeadbeef);

  stream::MemoryReader reader(encoded_proto);
  InlineString<32> str;
  StatusWithSize result = FindString(reader, Fields::kField6, str);
  ASSERT_EQ(result.status(), OkStatus());
  EXPECT_STREQ(str.c_str(), "Hello world");
}

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