aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/pb_io_ut.cpp
blob: 875d6dc602634eb1aa692accff022bddb7d99f94 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "pb_io.h"

#include "is_equal.h"

#include <library/cpp/protobuf/util/ut/common_ut.pb.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/folder/path.h>
#include <util/folder/tempdir.h>
#include <util/stream/file.h>
#include <util/stream/str.h>

static NProtobufUtilUt::TTextTest GetCorrectMessage() {
    NProtobufUtilUt::TTextTest m;
    m.SetFoo(42);
    return m;
}

static NProtobufUtilUt::TTextEnumTest GetCorrectEnumMessage() {
    NProtobufUtilUt::TTextEnumTest m;
    m.SetSlot(NProtobufUtilUt::TTextEnumTest::EET_SLOT_1);
    return m;
}

static const TString CORRECT_MESSAGE =
    R"(Foo: 42
)";
static const TString CORRECT_ENUM_NAME_MESSAGE =
    R"(Slot: EET_SLOT_1
)";
static const TString CORRECT_ENUM_ID_MESSAGE =
    R"(Slot: 1
)";

static const TString INCORRECT_MESSAGE =
    R"(Bar: 1
)";
static const TString INCORRECT_ENUM_NAME_MESSAGE =
    R"(Slot: EET_SLOT_3
)";
static const TString INCORRECT_ENUM_ID_MESSAGE =
    R"(Slot: 3
)";

static const TString CORRECT_BASE64_MESSAGE = "CCo,";

static const TString CORRECT_UNEVEN_BASE64_MESSAGE = "CCo";

static const TString INCORRECT_BASE64_MESSAGE = "CC";

Y_UNIT_TEST_SUITE(TTestProtoBufIO) {
    Y_UNIT_TEST(TestBase64) {
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(NProtoBuf::TryParseFromBase64String(CORRECT_BASE64_MESSAGE, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(!NProtoBuf::TryParseFromBase64String(INCORRECT_BASE64_MESSAGE, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(NProtoBuf::TryParseFromBase64String(CORRECT_UNEVEN_BASE64_MESSAGE , message, true));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(!NProtoBuf::TryParseFromBase64String(CORRECT_UNEVEN_BASE64_MESSAGE , message, false));
        }
        {
            UNIT_ASSERT_VALUES_EQUAL(CORRECT_BASE64_MESSAGE, NProtoBuf::SerializeToBase64String(GetCorrectMessage()));
        }
        {
            const auto m = NProtoBuf::ParseFromBase64String<NProtobufUtilUt::TTextTest>(CORRECT_BASE64_MESSAGE);
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
    }

    Y_UNIT_TEST(TestParseFromTextFormat) {
        TTempDir tempDir;
        const TFsPath correctFileName = TFsPath{tempDir()} / "correct.pb.txt";
        const TFsPath incorrectFileName = TFsPath{tempDir()} / "incorrect.pb.txt";

        TFileOutput{correctFileName}.Write(CORRECT_MESSAGE);
        TFileOutput{incorrectFileName}.Write(INCORRECT_MESSAGE);

        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(TryParseFromTextFormat(correctFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(!TryParseFromTextFormat(incorrectFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{CORRECT_MESSAGE};
            UNIT_ASSERT(TryParseFromTextFormat(in, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{INCORRECT_MESSAGE};
            UNIT_ASSERT(!TryParseFromTextFormat(in, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_NO_EXCEPTION(TryParseFromTextFormat(incorrectFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(!TryParseFromTextFormat("this_file_doesnt_exists", message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_NO_EXCEPTION(TryParseFromTextFormat("this_file_doesnt_exists", message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat("this_file_doesnt_exists", message), TFileError);
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(correctFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(incorrectFileName, message), yexception);
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{CORRECT_MESSAGE};
            UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(in, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{INCORRECT_MESSAGE};
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(in, message), yexception);
        }
        {
            NProtobufUtilUt::TTextTest m;
            const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
                mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(correctFileName);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
        {
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextTest>(incorrectFileName), yexception);
        }
        {
            NProtobufUtilUt::TTextTest m;
            TStringInput in{CORRECT_MESSAGE};
            const auto f = [&in](NProtobufUtilUt::TTextTest& mm) {
                mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(in);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
        {
            TStringInput in{INCORRECT_MESSAGE};
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextTest>(in), yexception);
        }
        {
            const TFsPath correctFileName2 = TFsPath{tempDir()} / "serialized.pb.txt";
            const auto original = GetCorrectMessage();
            UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, correctFileName2));
            const auto serializedStr = TUnbufferedFileInput{correctFileName2}.ReadAll();
            UNIT_ASSERT_VALUES_EQUAL(serializedStr, CORRECT_MESSAGE);
        }
        {
            const auto original = GetCorrectMessage();
            TStringStream out;
            UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out));
            UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_MESSAGE);
        }
        {
            NProtobufUtilUt::TTextTest m;
            const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
                mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(
                    correctFileName,
                    EParseFromTextFormatOption::AllowUnknownField);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
        {
            const NProtobufUtilUt::TTextTest empty;
            NProtobufUtilUt::TTextTest m;
            const auto f = [&incorrectFileName](NProtobufUtilUt::TTextTest& mm) {
                mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(
                    incorrectFileName,
                    EParseFromTextFormatOption::AllowUnknownField);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(empty, m));
        }
    }

    Y_UNIT_TEST(TestSerializeToTextFormatWithEnumId) {
        TTempDir tempDir;
        const TFsPath correctNameFileName = TFsPath{tempDir()} / "correct_name.pb.txt";
        const TFsPath incorrectNameFileName = TFsPath{tempDir()} / "incorrect_name.pb.txt";
        const TFsPath correctIdFileName = TFsPath{tempDir()} / "correct_id.pb.txt";
        const TFsPath incorrectIdFileName = TFsPath{tempDir()} / "incorrect_id.pb.txt";

        TFileOutput{correctNameFileName}.Write(CORRECT_ENUM_NAME_MESSAGE);
        TFileOutput{incorrectNameFileName}.Write(INCORRECT_ENUM_NAME_MESSAGE);
        TFileOutput{correctIdFileName}.Write(CORRECT_ENUM_ID_MESSAGE);
        TFileOutput{incorrectIdFileName}.Write(INCORRECT_ENUM_ID_MESSAGE);

        {
            NProtobufUtilUt::TTextEnumTest message;
            for (auto correct_message: {CORRECT_ENUM_ID_MESSAGE, CORRECT_ENUM_NAME_MESSAGE}) {
                TStringInput in{correct_message};
                UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(in, message));
            }
        }
        {
            NProtobufUtilUt::TTextEnumTest message;
            for (auto incorrect_message: {INCORRECT_ENUM_ID_MESSAGE, INCORRECT_ENUM_NAME_MESSAGE}) {
                TStringInput in{incorrect_message};
                UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(in, message), yexception);
            }
        }
        {
            const auto f = [](NProtobufUtilUt::TTextEnumTest& mm, const TString fileName) {
                mm = ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(fileName);
            };
            for (auto fileName: {correctIdFileName, correctNameFileName}) {
                NProtobufUtilUt::TTextEnumTest m;
                UNIT_ASSERT_NO_EXCEPTION(f(m, fileName));
                UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectEnumMessage(), m));
            }
        }
        {
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(incorrectIdFileName), yexception);
            UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(incorrectNameFileName), yexception);
        }
        {
            const auto original = GetCorrectEnumMessage();
            TStringStream out;
            UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out));
            UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_ENUM_NAME_MESSAGE);
        }
        {
            const auto original = GetCorrectEnumMessage();
            TStringStream out;
            UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormatWithEnumId(original, out));
            UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_ENUM_ID_MESSAGE);
        }
    }

    Y_UNIT_TEST(TestMergeFromTextFormat) {
        //
        // Tests cases below are identical to `Parse` tests
        //
        TTempDir tempDir;
        const TFsPath correctFileName = TFsPath{tempDir()} / "correct.pb.txt";
        const TFsPath incorrectFileName = TFsPath{tempDir()} / "incorrect.pb.txt";

        TFileOutput{correctFileName}.Write(CORRECT_MESSAGE);
        TFileOutput{incorrectFileName}.Write(INCORRECT_MESSAGE);

        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(TryMergeFromTextFormat(correctFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(!TryMergeFromTextFormat(incorrectFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{CORRECT_MESSAGE};
            UNIT_ASSERT(TryMergeFromTextFormat(in, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{INCORRECT_MESSAGE};
            UNIT_ASSERT(!TryMergeFromTextFormat(in, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_NO_EXCEPTION(TryMergeFromTextFormat(incorrectFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT(!TryMergeFromTextFormat("this_file_doesnt_exists", message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_NO_EXCEPTION(TryMergeFromTextFormat("this_file_doesnt_exists", message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_EXCEPTION(MergeFromTextFormat("this_file_doesnt_exists", message), TFileError);
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_NO_EXCEPTION(MergeFromTextFormat(correctFileName, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            UNIT_ASSERT_EXCEPTION(MergeFromTextFormat(incorrectFileName, message), yexception);
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{CORRECT_MESSAGE};
            UNIT_ASSERT_NO_EXCEPTION(MergeFromTextFormat(in, message));
        }
        {
            NProtobufUtilUt::TTextTest message;
            TStringInput in{INCORRECT_MESSAGE};
            UNIT_ASSERT_EXCEPTION(MergeFromTextFormat(in, message), yexception);
        }
        {
            NProtobufUtilUt::TTextTest m;
            const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
                mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(correctFileName);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
        {
            UNIT_ASSERT_EXCEPTION(MergeFromTextFormat<NProtobufUtilUt::TTextTest>(incorrectFileName), yexception);
        }
        {
            NProtobufUtilUt::TTextTest m;
            TStringInput in{CORRECT_MESSAGE};
            const auto f = [&in](NProtobufUtilUt::TTextTest& mm) {
                mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(in);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
        {
            TStringInput in{INCORRECT_MESSAGE};
            UNIT_ASSERT_EXCEPTION(MergeFromTextFormat<NProtobufUtilUt::TTextTest>(in), yexception);
        }
        {
            const TFsPath correctFileName2 = TFsPath{tempDir()} / "serialized.pb.txt";
            const auto original = GetCorrectMessage();
            UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, correctFileName2));
            const auto serializedStr = TUnbufferedFileInput{correctFileName2}.ReadAll();
            UNIT_ASSERT_VALUES_EQUAL(serializedStr, CORRECT_MESSAGE);
        }
        {
            const auto original = GetCorrectMessage();
            TStringStream out;
            UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out));
            UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_MESSAGE);
        }
        {
            NProtobufUtilUt::TTextTest m;
            const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
                mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(
                    correctFileName,
                    EParseFromTextFormatOption::AllowUnknownField);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
        }
        {
            const NProtobufUtilUt::TTextTest empty;
            NProtobufUtilUt::TTextTest m;
            const auto f = [&incorrectFileName](NProtobufUtilUt::TTextTest& mm) {
                mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(
                    incorrectFileName,
                    EParseFromTextFormatOption::AllowUnknownField);
            };
            UNIT_ASSERT_NO_EXCEPTION(f(m));
            UNIT_ASSERT(NProtoBuf::IsEqual(empty, m));
        }

        //
        // Test cases for `Merge`
        //
        {
            NProtobufUtilUt::TTextTest message;
            message.SetFoo(100500);
            TStringInput in{CORRECT_MESSAGE};
            UNIT_ASSERT(TryMergeFromTextFormat(in, message));
            UNIT_ASSERT(NProtoBuf::IsEqual(message, GetCorrectMessage()));
        }
    }

    Y_UNIT_TEST(TestMergeFromString) {
        NProtobufUtilUt::TMergeTest message;
        NProtobufUtilUt::TMergeTest messageFirstHalf;
        NProtobufUtilUt::TMergeTest messageSecondHalf;

        for (ui32 v = ~0; v != 0; v >>= 1) {
            message.AddMergeInt(v);
            (v > 0xffff ? messageFirstHalf : messageSecondHalf).AddMergeInt(v);
        }

        const TString full = message.SerializeAsString();

        {
            NProtobufUtilUt::TMergeTest m1;
            UNIT_ASSERT(NProtoBuf::MergeFromString(m1, full));
            UNIT_ASSERT(NProtoBuf::IsEqual(message, m1));
        }
        {
            NProtobufUtilUt::TMergeTest m2;
            TStringBuf s0 = TStringBuf(full).SubStr(0, 3);
            TStringBuf s1 = TStringBuf(full).SubStr(3);
            // объединение результатов двух MergePartialFromString не эквивалентно вызову MergePartialFromString от объединения строк
            UNIT_ASSERT(!(NProtoBuf::MergePartialFromString(m2, s0) && NProtoBuf::MergePartialFromString(m2, s1)));
        }
        {
            NProtobufUtilUt::TMergeTest m3;
            UNIT_ASSERT(NProtoBuf::MergePartialFromString(m3, messageFirstHalf.SerializeAsString()));
            UNIT_ASSERT(NProtoBuf::MergeFromString(m3, messageSecondHalf.SerializeAsString()));
            UNIT_ASSERT(NProtoBuf::IsEqual(message, m3));
        }
    }
}