-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotobuf_test.cpp
162 lines (148 loc) · 4.64 KB
/
protobuf_test.cpp
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
#include <google/protobuf/text_format.h>
#include <pomagma/io/protobuf.hpp>
#include <pomagma/io/protobuf_test.pb.h>
#include <string>
#include <vector>
using google::protobuf::TextFormat;
using namespace pomagma;
using pomagma::protobuf::TestMessage;
TestMessage parse(const std::string& text) {
TestMessage message;
POMAGMA_ASSERT(TextFormat::ParseFromString(text, &message), "parse error");
return message;
}
TestMessage make_big_message(size_t depth = 12) {
TestMessage message = parse(R"(
optional_string: 'test'
repeated_string: 'test1'
repeated_string: 'test2'
repeated_message: {
repeated_string: 'sub 2'
}
)");
*message.mutable_optional_message() = TestMessage(message);
for (size_t i = 0; i < depth; ++i) {
*message.add_repeated_message() = TestMessage(message);
}
// this should be a big message to test Next and Backup
POMAGMA_ASSERT_LT(65536, message.ByteSize());
return message;
}
const std::vector<TestMessage> g_examples = {
// The empty message may actually fail for gzip streams, which need at
// least 6 bytes. see google/protobuf/io/gzip_stream.h:171 about
// GzipOutputStream::Flush
parse(""), parse(R"(
optional_string: 'test'
)"),
parse(R"(
repeated_string: 'test1'
repeated_string: 'test2'
)"),
parse(R"(
optional_string: 'test'
repeated_string: 'test1'
repeated_string: 'test2'
optional_message: {
repeated_message: {}
repeated_message: {
optional_string: 'sub sub 1'
repeated_string: 'sub'
}
repeated_message: {
optional_string: 'sub 1'
}
repeated_message: {
repeated_string: 'sub 2'
}
}
)"),
make_big_message()};
std::string get_digest(const std::string& filename) {
Hasher hasher;
hasher.add_file(filename);
hasher.finish();
return hasher.str();
}
std::string get_digest_and_delete(protobuf::OutFile* file) {
std::string filename = file->filename();
delete file;
return get_digest(filename);
}
std::string get_digest_and_delete(protobuf::Sha1OutFile* file) {
std::string digest = file->hexdigest();
delete file;
return digest;
}
void discard_digest(protobuf::OutFile&) {}
void discard_digest(protobuf::Sha1OutFile& file) {
POMAGMA_ASSERT(not file.hexdigest().empty(), "no digest found");
}
template <class OutFile>
void test_write_read(const TestMessage& expected) {
POMAGMA_INFO("Testing read o write");
TestMessage actual;
in_temp_dir([&]() {
const std::string filename = "test.pb";
{
OutFile file(filename);
file.write(expected);
discard_digest(file);
}
{
protobuf::InFile file(filename);
file.read(actual);
}
});
POMAGMA_ASSERT_EQ(actual.ShortDebugString(), expected.ShortDebugString());
}
template <class OutFile>
void test_write_read_chunks(const TestMessage& expected) {
POMAGMA_INFO("Testing chunked read o write");
TestMessage actual;
in_temp_dir([&]() {
const std::string filename = "test.pb";
{
OutFile file(filename);
file.write(expected);
discard_digest(file);
}
{
protobuf::InFile file(filename);
while (file.try_read_chunk(actual)) {
POMAGMA_INFO("cumulative: " << actual.ShortDebugString());
}
}
});
POMAGMA_ASSERT_EQ(actual.ShortDebugString(), expected.ShortDebugString());
}
template <class OutFile>
void test_digest(const TestMessage& message) {
POMAGMA_INFO("Testing digest");
std::string actual;
std::string expected;
in_temp_dir([&]() {
const std::string filename = "test.pb";
{
OutFile* file = new OutFile(filename);
file->write(message);
actual = get_digest_and_delete(file);
}
expected = get_digest(filename);
});
POMAGMA_ASSERT_EQ(actual, expected);
}
int main() {
Log::Context log_context("Util Protobuf Test");
for (const auto& message : g_examples) {
POMAGMA_INFO("Testing with "
<< message.ShortDebugString().substr(0, 256));
test_write_read<protobuf::OutFile>(message);
test_write_read_chunks<protobuf::OutFile>(message);
test_write_read<protobuf::Sha1OutFile>(message);
test_write_read_chunks<protobuf::Sha1OutFile>(message);
test_digest<protobuf::OutFile>(message);
test_digest<protobuf::Sha1OutFile>(message);
}
return 0;
}