blob: d1da109caa9e7dd742a2d6f6a9b54a291cf762df [file] [log] [blame]
Yecheng Zhao931f4d52021-09-02 23:15:13 -07001// Copyright 2021 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
Yecheng Zhao321fb692021-09-06 10:26:04 -070015#include "pw_protobuf/map_utils.h"
16
17#include <string_view>
Yecheng Zhao931f4d52021-09-02 23:15:13 -070018
19#include "gtest/gtest.h"
Yecheng Zhao321fb692021-09-06 10:26:04 -070020#include "pw_stream/memory_stream.h"
21#include "pw_stream/stream.h"
22
23#define ASSERT_OK(status) ASSERT_EQ(OkStatus(), status)
Yecheng Zhao931f4d52021-09-02 23:15:13 -070024
25namespace pw::protobuf {
26
27TEST(ProtoHelper, WriteProtoStringToBytesMapEntry) {
28 // The following defines an instance of the message below:
29 //
30 // message Maps {
31 // map<string, string> map_a = 1;
32 // map<string, string> map_b = 2;
33 // }
34 //
35 // where
36 //
37 // Maps.map_a['key_foo'] = 'foo_a'
38 // Maps.map_a['key_bar'] = 'bar_a'
39 //
40 // Maps.map_b['key_foo'] = 'foo_b'
41 // Maps.map_b['key_bar'] = 'bar_b'
42 //
43 // clang-format off
44 std::uint8_t encoded_proto[] = {
45 // map_a["key_bar"] = "bar_a", key = 1
46 0x0a, 0x10,
47 0x0a, 0x07, 'k', 'e', 'y', '_', 'b', 'a', 'r', // map key
48 0x12, 0x05, 'b', 'a', 'r', '_', 'a', // map value
49
50 // map_a["key_foo"] = "foo_a", key = 1
51 0x0a, 0x10,
52 0x0a, 0x07, 'k', 'e', 'y', '_', 'f', 'o', 'o',
53 0x12, 0x05, 'f', 'o', 'o', '_', 'a',
54
55 // map_b["key_foo"] = "foo_b", key = 2
56 0x12, 0x10,
57 0x0a, 0x07, 'k', 'e', 'y', '_', 'f', 'o', 'o',
58 0x12, 0x05, 'f', 'o', 'o', '_', 'b',
59
60 // map_b["key_bar"] = "bar_b", key = 2
61 0x12, 0x10,
62 0x0a, 0x07, 'k', 'e', 'y', '_', 'b', 'a', 'r',
63 0x12, 0x05, 'b', 'a', 'r', '_', 'b',
64 };
65 // clang-format on
66
67 // Now construct the same message with WriteStringToBytesMapEntry
68 std::byte dst_buffer[sizeof(encoded_proto)];
69 stream::MemoryWriter writer(dst_buffer);
70
71 const struct {
72 uint32_t field_number;
73 std::string_view key;
74 std::string_view value;
75 } kMapData[] = {
76 {1, "key_bar", "bar_a"},
77 {1, "key_foo", "foo_a"},
78 {2, "key_foo", "foo_b"},
79 {2, "key_bar", "bar_b"},
80 };
81
82 std::byte stream_pipe_buffer[1];
83 for (auto ele : kMapData) {
84 stream::MemoryReader key_reader(std::as_bytes(std::span{ele.key}));
85 stream::MemoryReader value_reader(std::as_bytes(std::span{ele.value}));
Yecheng Zhao321fb692021-09-06 10:26:04 -070086 ASSERT_OK(WriteProtoStringToBytesMapEntry(ele.field_number,
87 key_reader,
88 ele.key.size(),
89 value_reader,
90 ele.value.size(),
91 stream_pipe_buffer,
92 writer));
Yecheng Zhao931f4d52021-09-02 23:15:13 -070093 }
94
95 ASSERT_EQ(memcmp(dst_buffer, encoded_proto, sizeof(dst_buffer)), 0);
96}
97
98TEST(ProtoHelper, WriteProtoStringToBytesMapEntryExceedsWriteLimit) {
99 // Construct an instance of the message below:
100 //
101 // message Maps {
102 // map<string, string> map_a = 1;
103 // }
104 //
105 // where
106 //
107 // Maps.map_a['key_bar'] = 'bar_a'. The needed buffer size is 18 in this
108 // case:
109 //
110 // {
111 // 0x0a, 0x10,
112 // 0x0a, 0x07, 'k', 'e', 'y', '_', 'b', 'a', 'r',
113 // 0x12, 0x05, 'b', 'a', 'r', '_', 'a',
114 // }
115 //
116 // Use a smaller buffer.
117 std::byte encode_buffer[17];
118 stream::MemoryWriter writer(encode_buffer);
119 constexpr uint32_t kFieldNumber = 1;
120 std::string_view key = "key_bar";
121 std::string_view value = "bar_a";
122 stream::MemoryReader key_reader(std::as_bytes(std::span{key}));
123 stream::MemoryReader value_reader(std::as_bytes(std::span{value}));
124 std::byte stream_pipe_buffer[1];
125 ASSERT_EQ(
126 WriteProtoStringToBytesMapEntry(kFieldNumber,
127 key_reader,
128 key_reader.ConservativeReadLimit(),
129 value_reader,
130 value_reader.ConservativeReadLimit(),
131 stream_pipe_buffer,
132 writer),
133 Status::ResourceExhausted());
134}
135
136TEST(ProtoHelper, WriteProtoStringToBytesMapEntryInvalidArgument) {
137 std::byte encode_buffer[17];
138 stream::MemoryWriter writer(encode_buffer);
139 std::string_view key = "key_bar";
140 std::string_view value = "bar_a";
141 stream::MemoryReader key_reader(std::as_bytes(std::span{key}));
142 stream::MemoryReader value_reader(std::as_bytes(std::span{value}));
143 std::byte stream_pipe_buffer[1];
144
145 ASSERT_EQ(
146 WriteProtoStringToBytesMapEntry(19091,
147 key_reader,
148 key_reader.ConservativeReadLimit(),
149 value_reader,
150 value_reader.ConservativeReadLimit(),
151 stream_pipe_buffer,
152 writer),
153 Status::InvalidArgument());
154}
155
156} // namespace pw::protobuf