1 //
2 // Copyright 2024 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/log/internal/structured_proto.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 #include <utility>
22 #include <vector>
23
24 #include "gtest/gtest.h"
25 #include "absl/base/config.h"
26 #include "absl/strings/string_view.h"
27 #include "absl/types/span.h"
28 #include "absl/utility/utility.h"
29
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace log_internal {
33 namespace {
34
35 using ::testing::TestWithParam;
36
37 struct StructuredProtoTestCase {
38 std::string test_name;
39 StructuredProtoField field;
40 std::vector<char> expected_encoded_field;
41 };
42
43 using StructuredProtoTest = TestWithParam<StructuredProtoTestCase>;
44
TEST_P(StructuredProtoTest,Encoding)45 TEST_P(StructuredProtoTest, Encoding) {
46 const StructuredProtoTestCase& test_case = GetParam();
47
48 // Greater than or equal to since BufferSizeForStructuredProtoField() is just
49 // an estimate of the data size and not an exact measurement.
50 ASSERT_GE(BufferSizeForStructuredProtoField(test_case.field),
51 test_case.expected_encoded_field.size());
52
53 std::vector<char> buf;
54 buf.resize(1024);
55
56 absl::Span<char> buf_span(buf);
57 EXPECT_TRUE(EncodeStructuredProtoField(test_case.field, buf_span));
58 size_t encoded_field_size = buf.size() - buf_span.size();
59
60 ASSERT_EQ(encoded_field_size, test_case.expected_encoded_field.size());
61 buf.resize(encoded_field_size);
62 EXPECT_EQ(buf, test_case.expected_encoded_field);
63 }
64
65 INSTANTIATE_TEST_SUITE_P(
66 StructuredProtoTestSuiteInstantiation,
67 StructuredProtoTest, // This is the name of your parameterized test
68 testing::ValuesIn<StructuredProtoTestCase>({
69 {
70 "Varint",
71 {
72 42,
73 StructuredProtoField::Value{
74 absl::in_place_type<StructuredProtoField::Varint>,
75 int32_t{23},
76 },
77 },
78 {'\xD0', '\x02', '\x17'},
79 },
80 {
81 "I64",
82 {
83 42,
84 StructuredProtoField::Value{
85 absl::in_place_type<StructuredProtoField::I64>,
86 int64_t{23},
87 },
88 },
89 {'\xD1', '\x02', '\x17', '\x00', '\x00', '\x00', '\x00', '\x00',
90 '\x00', '\x00'},
91 },
92 {
93 "LengthDelimited",
94 {
95 42,
96 // Use a string_view so the terminating NUL is excluded.
97 absl::string_view("Hello"),
98 },
99 {'\xD2', '\x02', '\x05', 'H', 'e', 'l', 'l', 'o'},
100 },
101 {
102 "I32",
103 {
104 42,
105 StructuredProtoField::Value{
106 absl::in_place_type<StructuredProtoField::I32>,
107 int32_t{23},
108 },
109 },
110 {'\xD5', '\x02', '\x17', '\x00', '\x00', '\x00'},
111 },
112 }),
__anondec19b8e0202(const testing::TestParamInfo<StructuredProtoTest::ParamType>& info) 113 [](const testing::TestParamInfo<StructuredProtoTest::ParamType>& info) {
114 return info.param.test_name;
115 });
116
117 } // namespace
118 } // namespace log_internal
119 ABSL_NAMESPACE_END
120 } // namespace absl
121