1 // Copyright 2020 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 "pw_protobuf/find.h"
16
17 #include "gtest/gtest.h"
18
19 namespace pw::protobuf {
20 namespace {
21
22 // clang-format off
23 constexpr uint8_t encoded_proto[] = {
24 // type=int32, k=1, v=42
25 0x08, 0x2a,
26 // type=sint32, k=2, v=-13
27 0x10, 0x19,
28 // type=bool, k=3, v=false
29 0x18, 0x00,
30 // type=double, k=4, v=3.14159
31 0x21, 0x6e, 0x86, 0x1b, 0xf0, 0xf9, 0x21, 0x09, 0x40,
32 // type=fixed32, k=5, v=0xdeadbeef
33 0x2d, 0xef, 0xbe, 0xad, 0xde,
34 // type=string, k=6, v="Hello world"
35 0x32, 0x0b, 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd',
36
37 // type=message, k=7, len=2
38 0x3a, 0x02,
39 // (nested) type=uint32, k=1, v=3
40 0x08, 0x03
41 };
42
TEST(FindDecodeHandler,SingleLevel_FindsExistingField)43 TEST(FindDecodeHandler, SingleLevel_FindsExistingField) {
44 CallbackDecoder decoder;
45 FindDecodeHandler finder(3);
46
47 decoder.set_handler(&finder);
48 decoder.Decode(std::as_bytes(std::span(encoded_proto)))
49 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
50
51 EXPECT_TRUE(finder.found());
52 EXPECT_TRUE(decoder.cancelled());
53 }
54
TEST(FindDecodeHandler,SingleLevel_DoesntFindNonExistingField)55 TEST(FindDecodeHandler, SingleLevel_DoesntFindNonExistingField) {
56 CallbackDecoder decoder;
57 FindDecodeHandler finder(8);
58
59 decoder.set_handler(&finder);
60 decoder.Decode(std::as_bytes(std::span(encoded_proto)))
61 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
62
63 EXPECT_FALSE(finder.found());
64 EXPECT_FALSE(decoder.cancelled());
65 }
66
TEST(FindDecodeHandler,MultiLevel_FindsExistingNestedField)67 TEST(FindDecodeHandler, MultiLevel_FindsExistingNestedField) {
68 CallbackDecoder decoder;
69 FindDecodeHandler nested_finder(1);
70 FindDecodeHandler finder(7, &nested_finder);
71
72 decoder.set_handler(&finder);
73 decoder.Decode(std::as_bytes(std::span(encoded_proto)))
74 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
75
76 EXPECT_TRUE(finder.found());
77 EXPECT_TRUE(nested_finder.found());
78 EXPECT_TRUE(decoder.cancelled());
79 }
80
TEST(FindDecodeHandler,MultiLevel_DoesntFindNonExistingNestedField)81 TEST(FindDecodeHandler, MultiLevel_DoesntFindNonExistingNestedField) {
82 CallbackDecoder decoder;
83 FindDecodeHandler nested_finder(3);
84 FindDecodeHandler finder(7, &nested_finder);
85
86 decoder.set_handler(&finder);
87 decoder.Decode(std::as_bytes(std::span(encoded_proto)))
88 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
89
90 EXPECT_TRUE(finder.found());
91 EXPECT_FALSE(nested_finder.found());
92 EXPECT_FALSE(decoder.cancelled());
93 }
94
95 } // namespace
96 } // namespace pw::protobuf
97