-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
descriptor_unittest.cc
13361 lines (11998 loc) · 461 KB
/
descriptor_unittest.cc
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// This file makes extensive use of RFC 3092. :)
#include "google/protobuf/descriptor.h"
#include <limits.h>
#include <atomic>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "google/protobuf/any.pb.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.pb.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/base/log_severity.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h"
#include "absl/flags/flag.h"
#include "absl/functional/any_invocable.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/log/die_if_null.h"
#include "absl/log/scoped_mock_log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/strings/substitute.h"
#include "absl/synchronization/notification.h"
#include "google/protobuf/compiler/importer.h"
#include "google/protobuf/compiler/parser.h"
#include "google/protobuf/cpp_features.pb.h"
#include "google/protobuf/descriptor_database.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/feature_resolver.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/tokenizer.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/test_textproto.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_custom_options.pb.h"
#include "google/protobuf/unittest_delimited.pb.h"
#include "google/protobuf/unittest_delimited_import.pb.h"
#include "google/protobuf/unittest_features.pb.h"
#include "google/protobuf/unittest_lazy_dependencies.pb.h"
#include "google/protobuf/unittest_lazy_dependencies_custom_option.pb.h"
#include "google/protobuf/unittest_lazy_dependencies_enum.pb.h"
#include "google/protobuf/unittest_proto3_arena.pb.h"
// Must be included last.
#include "google/protobuf/port_def.inc"
using ::google::protobuf::internal::cpp::GetUtf8CheckMode;
using ::google::protobuf::internal::cpp::HasPreservingUnknownEnumSemantics;
using ::google::protobuf::internal::cpp::Utf8CheckMode;
using ::testing::AnyOf;
using ::testing::AtLeast;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
using ::testing::NotNull;
using ::testing::Return;
absl::Status GetStatus(const absl::Status& s) { return s; }
template <typename T>
absl::Status GetStatus(const absl::StatusOr<T>& s) {
return s.status();
}
MATCHER_P2(StatusIs, status, message,
absl::StrCat(".status() is ", testing::PrintToString(status))) {
return GetStatus(arg).code() == status &&
testing::ExplainMatchResult(message, GetStatus(arg).message(),
result_listener);
}
#define EXPECT_OK(x) EXPECT_THAT(x, StatusIs(absl::StatusCode::kOk, testing::_))
#define ASSERT_OK(x) ASSERT_THAT(x, StatusIs(absl::StatusCode::kOk, testing::_))
namespace google {
namespace protobuf {
// Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
namespace descriptor_unittest {
// Some helpers to make assembling descriptors faster.
DescriptorProto* AddMessage(FileDescriptorProto* file,
const std::string& name) {
DescriptorProto* result = file->add_message_type();
result->set_name(name);
return result;
}
DescriptorProto* AddNestedMessage(DescriptorProto* parent,
const std::string& name) {
DescriptorProto* result = parent->add_nested_type();
result->set_name(name);
return result;
}
EnumDescriptorProto* AddEnum(FileDescriptorProto* file,
absl::string_view name) {
EnumDescriptorProto* result = file->add_enum_type();
result->set_name(name);
return result;
}
EnumDescriptorProto* AddNestedEnum(DescriptorProto* parent,
const std::string& name) {
EnumDescriptorProto* result = parent->add_enum_type();
result->set_name(name);
return result;
}
ServiceDescriptorProto* AddService(FileDescriptorProto* file,
const std::string& name) {
ServiceDescriptorProto* result = file->add_service();
result->set_name(name);
return result;
}
FieldDescriptorProto* AddField(DescriptorProto* parent, const std::string& name,
int number, FieldDescriptorProto::Label label,
FieldDescriptorProto::Type type) {
FieldDescriptorProto* result = parent->add_field();
result->set_name(name);
result->set_number(number);
result->set_label(label);
result->set_type(type);
return result;
}
FieldDescriptorProto* AddExtension(FileDescriptorProto* file,
const std::string& extendee,
const std::string& name, int number,
FieldDescriptorProto::Label label,
FieldDescriptorProto::Type type) {
FieldDescriptorProto* result = file->add_extension();
result->set_name(name);
result->set_number(number);
result->set_label(label);
result->set_type(type);
result->set_extendee(extendee);
return result;
}
FieldDescriptorProto* AddNestedExtension(DescriptorProto* parent,
const std::string& extendee,
const std::string& name, int number,
FieldDescriptorProto::Label label,
FieldDescriptorProto::Type type) {
FieldDescriptorProto* result = parent->add_extension();
result->set_name(name);
result->set_number(number);
result->set_label(label);
result->set_type(type);
result->set_extendee(extendee);
return result;
}
DescriptorProto::ExtensionRange* AddExtensionRange(DescriptorProto* parent,
int start, int end) {
DescriptorProto::ExtensionRange* result = parent->add_extension_range();
result->set_start(start);
result->set_end(end);
return result;
}
DescriptorProto::ReservedRange* AddReservedRange(DescriptorProto* parent,
int start, int end) {
DescriptorProto::ReservedRange* result = parent->add_reserved_range();
result->set_start(start);
result->set_end(end);
return result;
}
EnumDescriptorProto::EnumReservedRange* AddReservedRange(
EnumDescriptorProto* parent, int start, int end) {
EnumDescriptorProto::EnumReservedRange* result = parent->add_reserved_range();
result->set_start(start);
result->set_end(end);
return result;
}
EnumValueDescriptorProto* AddEnumValue(EnumDescriptorProto* enum_proto,
const std::string& name, int number) {
EnumValueDescriptorProto* result = enum_proto->add_value();
result->set_name(name);
result->set_number(number);
return result;
}
MethodDescriptorProto* AddMethod(ServiceDescriptorProto* service,
const std::string& name,
const std::string& input_type,
const std::string& output_type) {
MethodDescriptorProto* result = service->add_method();
result->set_name(name);
result->set_input_type(input_type);
result->set_output_type(output_type);
return result;
}
// Empty enums technically aren't allowed. We need to insert a dummy value
// into them.
void AddEmptyEnum(FileDescriptorProto* file, absl::string_view name) {
AddEnumValue(AddEnum(file, name), absl::StrCat(name, "_DUMMY"), 1);
}
class MockErrorCollector : public DescriptorPool::ErrorCollector {
public:
MockErrorCollector() = default;
~MockErrorCollector() override = default;
std::string text_;
std::string warning_text_;
// implements ErrorCollector ---------------------------------------
void RecordError(absl::string_view filename, absl::string_view element_name,
const Message* descriptor, ErrorLocation location,
absl::string_view message) override {
absl::SubstituteAndAppend(&text_, "$0: $1: $2: $3\n", filename,
element_name, ErrorLocationName(location),
message);
}
// implements ErrorCollector ---------------------------------------
void RecordWarning(absl::string_view filename, absl::string_view element_name,
const Message* descriptor, ErrorLocation location,
absl::string_view message) override {
absl::SubstituteAndAppend(&warning_text_, "$0: $1: $2: $3\n", filename,
element_name, ErrorLocationName(location),
message);
}
};
// ===================================================================
// Test simple files.
class FileDescriptorTest : public testing::Test {
protected:
void SetUp() override {
// Build descriptors for the following definitions:
//
// // in "foo.proto"
// message FooMessage { extensions 1; }
// enum FooEnum {FOO_ENUM_VALUE = 1;}
// service FooService {}
// extend FooMessage { optional int32 foo_extension = 1; }
//
// // in "bar.proto"
// package bar_package;
// message BarMessage { extensions 1; }
// enum BarEnum {BAR_ENUM_VALUE = 1;}
// service BarService {}
// extend BarMessage { optional int32 bar_extension = 1; }
//
// Also, we have an empty file "baz.proto". This file's purpose is to
// make sure that even though it has the same package as foo.proto,
// searching it for members of foo.proto won't work.
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
AddExtensionRange(AddMessage(&foo_file, "FooMessage"), 1, 2);
AddEnumValue(AddEnum(&foo_file, "FooEnum"), "FOO_ENUM_VALUE", 1);
AddService(&foo_file, "FooService");
AddExtension(&foo_file, "FooMessage", "foo_extension", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("bar_package");
bar_file.add_dependency("foo.proto");
AddExtensionRange(AddMessage(&bar_file, "BarMessage"), 1, 2);
AddEnumValue(AddEnum(&bar_file, "BarEnum"), "BAR_ENUM_VALUE", 1);
AddService(&bar_file, "BarService");
AddExtension(&bar_file, "bar_package.BarMessage", "bar_extension", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
FileDescriptorProto baz_file;
baz_file.set_name("baz.proto");
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != nullptr);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != nullptr);
baz_file_ = pool_.BuildFile(baz_file);
ASSERT_TRUE(baz_file_ != nullptr);
ASSERT_EQ(1, foo_file_->message_type_count());
foo_message_ = foo_file_->message_type(0);
ASSERT_EQ(1, foo_file_->enum_type_count());
foo_enum_ = foo_file_->enum_type(0);
ASSERT_EQ(1, foo_enum_->value_count());
foo_enum_value_ = foo_enum_->value(0);
ASSERT_EQ(1, foo_file_->service_count());
foo_service_ = foo_file_->service(0);
ASSERT_EQ(1, foo_file_->extension_count());
foo_extension_ = foo_file_->extension(0);
ASSERT_EQ(1, bar_file_->message_type_count());
bar_message_ = bar_file_->message_type(0);
ASSERT_EQ(1, bar_file_->enum_type_count());
bar_enum_ = bar_file_->enum_type(0);
ASSERT_EQ(1, bar_enum_->value_count());
bar_enum_value_ = bar_enum_->value(0);
ASSERT_EQ(1, bar_file_->service_count());
bar_service_ = bar_file_->service(0);
ASSERT_EQ(1, bar_file_->extension_count());
bar_extension_ = bar_file_->extension(0);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const FileDescriptor* baz_file_;
const Descriptor* foo_message_;
const EnumDescriptor* foo_enum_;
const EnumValueDescriptor* foo_enum_value_;
const ServiceDescriptor* foo_service_;
const FieldDescriptor* foo_extension_;
const Descriptor* bar_message_;
const EnumDescriptor* bar_enum_;
const EnumValueDescriptor* bar_enum_value_;
const ServiceDescriptor* bar_service_;
const FieldDescriptor* bar_extension_;
};
TEST_F(FileDescriptorTest, Name) {
EXPECT_EQ("foo.proto", foo_file_->name());
EXPECT_EQ("bar.proto", bar_file_->name());
EXPECT_EQ("baz.proto", baz_file_->name());
}
TEST_F(FileDescriptorTest, Package) {
EXPECT_EQ("", foo_file_->package());
EXPECT_EQ("bar_package", bar_file_->package());
}
TEST_F(FileDescriptorTest, Dependencies) {
EXPECT_EQ(0, foo_file_->dependency_count());
EXPECT_EQ(1, bar_file_->dependency_count());
EXPECT_EQ(foo_file_, bar_file_->dependency(0));
}
TEST_F(FileDescriptorTest, FindMessageTypeByName) {
EXPECT_EQ(foo_message_, foo_file_->FindMessageTypeByName("FooMessage"));
EXPECT_EQ(bar_message_, bar_file_->FindMessageTypeByName("BarMessage"));
EXPECT_TRUE(foo_file_->FindMessageTypeByName("BarMessage") == nullptr);
EXPECT_TRUE(bar_file_->FindMessageTypeByName("FooMessage") == nullptr);
EXPECT_TRUE(baz_file_->FindMessageTypeByName("FooMessage") == nullptr);
EXPECT_TRUE(foo_file_->FindMessageTypeByName("NoSuchMessage") == nullptr);
EXPECT_TRUE(foo_file_->FindMessageTypeByName("FooEnum") == nullptr);
}
TEST_F(FileDescriptorTest, FindEnumTypeByName) {
EXPECT_EQ(foo_enum_, foo_file_->FindEnumTypeByName("FooEnum"));
EXPECT_EQ(bar_enum_, bar_file_->FindEnumTypeByName("BarEnum"));
EXPECT_TRUE(foo_file_->FindEnumTypeByName("BarEnum") == nullptr);
EXPECT_TRUE(bar_file_->FindEnumTypeByName("FooEnum") == nullptr);
EXPECT_TRUE(baz_file_->FindEnumTypeByName("FooEnum") == nullptr);
EXPECT_TRUE(foo_file_->FindEnumTypeByName("NoSuchEnum") == nullptr);
EXPECT_TRUE(foo_file_->FindEnumTypeByName("FooMessage") == nullptr);
}
TEST_F(FileDescriptorTest, FindEnumValueByName) {
EXPECT_EQ(foo_enum_value_, foo_file_->FindEnumValueByName("FOO_ENUM_VALUE"));
EXPECT_EQ(bar_enum_value_, bar_file_->FindEnumValueByName("BAR_ENUM_VALUE"));
EXPECT_TRUE(foo_file_->FindEnumValueByName("BAR_ENUM_VALUE") == nullptr);
EXPECT_TRUE(bar_file_->FindEnumValueByName("FOO_ENUM_VALUE") == nullptr);
EXPECT_TRUE(baz_file_->FindEnumValueByName("FOO_ENUM_VALUE") == nullptr);
EXPECT_TRUE(foo_file_->FindEnumValueByName("NO_SUCH_VALUE") == nullptr);
EXPECT_TRUE(foo_file_->FindEnumValueByName("FooMessage") == nullptr);
}
TEST_F(FileDescriptorTest, FindServiceByName) {
EXPECT_EQ(foo_service_, foo_file_->FindServiceByName("FooService"));
EXPECT_EQ(bar_service_, bar_file_->FindServiceByName("BarService"));
EXPECT_TRUE(foo_file_->FindServiceByName("BarService") == nullptr);
EXPECT_TRUE(bar_file_->FindServiceByName("FooService") == nullptr);
EXPECT_TRUE(baz_file_->FindServiceByName("FooService") == nullptr);
EXPECT_TRUE(foo_file_->FindServiceByName("NoSuchService") == nullptr);
EXPECT_TRUE(foo_file_->FindServiceByName("FooMessage") == nullptr);
}
TEST_F(FileDescriptorTest, FindExtensionByName) {
EXPECT_EQ(foo_extension_, foo_file_->FindExtensionByName("foo_extension"));
EXPECT_EQ(bar_extension_, bar_file_->FindExtensionByName("bar_extension"));
EXPECT_TRUE(foo_file_->FindExtensionByName("bar_extension") == nullptr);
EXPECT_TRUE(bar_file_->FindExtensionByName("foo_extension") == nullptr);
EXPECT_TRUE(baz_file_->FindExtensionByName("foo_extension") == nullptr);
EXPECT_TRUE(foo_file_->FindExtensionByName("no_such_extension") == nullptr);
EXPECT_TRUE(foo_file_->FindExtensionByName("FooMessage") == nullptr);
}
TEST_F(FileDescriptorTest, FindExtensionByNumber) {
EXPECT_EQ(foo_extension_, pool_.FindExtensionByNumber(foo_message_, 1));
EXPECT_EQ(bar_extension_, pool_.FindExtensionByNumber(bar_message_, 1));
EXPECT_TRUE(pool_.FindExtensionByNumber(foo_message_, 2) == nullptr);
}
TEST_F(FileDescriptorTest, BuildAgain) {
// Test that if we call BuildFile again on the same input we get the same
// FileDescriptor back.
FileDescriptorProto file;
foo_file_->CopyTo(&file);
EXPECT_EQ(foo_file_, pool_.BuildFile(file));
// But if we change the file then it won't work.
file.set_package("some.other.package");
EXPECT_TRUE(pool_.BuildFile(file) == nullptr);
}
TEST_F(FileDescriptorTest, BuildAgainWithSyntax) {
// Test that if we call BuildFile again on the same input we get the same
// FileDescriptor back even if syntax param is specified.
FileDescriptorProto proto_syntax2;
proto_syntax2.set_name("foo_syntax2");
proto_syntax2.set_syntax("proto2");
const FileDescriptor* proto2_descriptor = pool_.BuildFile(proto_syntax2);
EXPECT_TRUE(proto2_descriptor != nullptr);
EXPECT_EQ(proto2_descriptor, pool_.BuildFile(proto_syntax2));
FileDescriptorProto implicit_proto2;
implicit_proto2.set_name("foo_implicit_syntax2");
const FileDescriptor* implicit_proto2_descriptor =
pool_.BuildFile(implicit_proto2);
EXPECT_TRUE(implicit_proto2_descriptor != nullptr);
// We get the same FileDescriptor back if syntax param is explicitly
// specified.
implicit_proto2.set_syntax("proto2");
EXPECT_EQ(implicit_proto2_descriptor, pool_.BuildFile(implicit_proto2));
FileDescriptorProto proto_syntax3;
proto_syntax3.set_name("foo_syntax3");
proto_syntax3.set_syntax("proto3");
const FileDescriptor* proto3_descriptor = pool_.BuildFile(proto_syntax3);
EXPECT_TRUE(proto3_descriptor != nullptr);
EXPECT_EQ(proto3_descriptor, pool_.BuildFile(proto_syntax3));
}
TEST_F(FileDescriptorTest, Edition) {
FileDescriptorProto proto;
proto.set_name("foo");
{
proto.set_syntax("proto2");
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
ASSERT_TRUE(file != nullptr);
EXPECT_EQ(FileDescriptorLegacy(file).edition(), Edition::EDITION_PROTO2);
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("", other.syntax());
EXPECT_FALSE(other.has_edition());
}
{
proto.set_syntax("proto3");
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
ASSERT_TRUE(file != nullptr);
EXPECT_EQ(FileDescriptorLegacy(file).edition(), Edition::EDITION_PROTO3);
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("proto3", other.syntax());
EXPECT_FALSE(other.has_edition());
}
{
proto.set_syntax("editions");
proto.set_edition(EDITION_2023);
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
ASSERT_TRUE(file != nullptr);
EXPECT_EQ(FileDescriptorLegacy(file).edition(), Edition::EDITION_2023);
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("editions", other.syntax());
EXPECT_EQ(other.edition(), EDITION_2023);
}
}
TEST_F(FileDescriptorTest, CopyHeadingTo) {
FileDescriptorProto proto;
proto.set_name("foo.proto");
proto.set_package("foo.bar.baz");
proto.set_syntax("proto3");
proto.mutable_options()->set_java_package("foo.bar.baz");
// Won't be copied.
proto.add_message_type()->set_name("Foo");
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
ASSERT_NE(file, nullptr);
FileDescriptorProto other;
file->CopyHeadingTo(&other);
EXPECT_EQ(other.name(), "foo.proto");
EXPECT_EQ(other.package(), "foo.bar.baz");
EXPECT_EQ(other.syntax(), "proto3");
EXPECT_EQ(other.options().java_package(), "foo.bar.baz");
EXPECT_TRUE(other.message_type().empty());
EXPECT_EQ(&other.options().features(), &FeatureSet::default_instance());
{
proto.set_syntax("editions");
proto.set_edition(EDITION_2023);
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
ASSERT_NE(file, nullptr);
FileDescriptorProto other;
file->CopyHeadingTo(&other);
EXPECT_EQ(other.name(), "foo.proto");
EXPECT_EQ(other.package(), "foo.bar.baz");
EXPECT_EQ(other.syntax(), "editions");
EXPECT_EQ(other.edition(), EDITION_2023);
EXPECT_EQ(other.options().java_package(), "foo.bar.baz");
EXPECT_TRUE(other.message_type().empty());
EXPECT_EQ(&other.options().features(), &FeatureSet::default_instance());
}
}
void ExtractDebugString(
const FileDescriptor* file, absl::flat_hash_set<std::string>* visited,
std::vector<std::pair<std::string, std::string>>* debug_strings) {
if (!visited->insert(file->name()).second) {
return;
}
for (int i = 0; i < file->dependency_count(); ++i) {
ExtractDebugString(file->dependency(i), visited, debug_strings);
}
debug_strings->push_back(std::make_pair(file->name(), file->DebugString()));
}
class SimpleErrorCollector : public io::ErrorCollector {
public:
// implements ErrorCollector ---------------------------------------
void RecordError(int line, int column, absl::string_view message) override {
last_error_ = absl::StrFormat("%d:%d:%s", line, column, message);
}
const std::string& last_error() { return last_error_; }
private:
std::string last_error_;
};
// Test that the result of FileDescriptor::DebugString() can be used to create
// the original descriptors.
TEST_F(FileDescriptorTest, DebugStringRoundTrip) {
absl::flat_hash_set<std::string> visited;
std::vector<std::pair<std::string, std::string>> debug_strings;
ExtractDebugString(protobuf_unittest::TestAllTypes::descriptor()->file(),
&visited, &debug_strings);
ExtractDebugString(
protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file(),
&visited, &debug_strings);
ExtractDebugString(proto3_arena_unittest::TestAllTypes::descriptor()->file(),
&visited, &debug_strings);
ASSERT_GE(debug_strings.size(), 3);
DescriptorPool pool;
for (size_t i = 0; i < debug_strings.size(); ++i) {
const std::string& name = debug_strings[i].first;
const std::string& content = debug_strings[i].second;
io::ArrayInputStream input_stream(content.data(), content.size());
SimpleErrorCollector error_collector;
io::Tokenizer tokenizer(&input_stream, &error_collector);
compiler::Parser parser;
parser.RecordErrorsTo(&error_collector);
FileDescriptorProto proto;
ASSERT_TRUE(parser.Parse(&tokenizer, &proto))
<< error_collector.last_error() << "\n"
<< content;
ASSERT_EQ("", error_collector.last_error());
proto.set_name(name);
const FileDescriptor* descriptor = pool.BuildFile(proto);
ASSERT_TRUE(descriptor != nullptr) << error_collector.last_error();
EXPECT_EQ(content, descriptor->DebugString());
}
}
TEST_F(FileDescriptorTest, AbslStringifyWorks) {
std::string s = absl::StrFormat(
"%v",
*protobuf_unittest::TestMessageWithCustomOptions::descriptor()->file());
EXPECT_THAT(s, HasSubstr("TestMessageWithCustomOptions"));
}
// ===================================================================
// Test simple flat messages and fields.
class DescriptorTest : public testing::Test {
protected:
void SetUp() override {
// Build descriptors for the following definitions:
//
// // in "foo.proto"
// message TestForeign {}
// enum TestEnum {}
//
// message TestMessage {
// required string foo = 1;
// optional TestEnum bar = 6;
// repeated TestForeign baz = 500000000;
// optional group moo = 15 {}
// }
//
// // in "bar.proto"
// package corge.grault;
// message TestMessage2 {
// required string foo = 1;
// required string bar = 2;
// required string mooo = 6;
// }
//
// // in "map.proto"
// message TestMessage3 {
// map<int32, int32> map_int32_int32 = 1;
// }
//
// // in "json.proto"
// message TestMessage4 {
// optional int32 field_name1 = 1;
// optional int32 fieldName2 = 2;
// optional int32 FieldName3 = 3;
// optional int32 _field_name4 = 4;
// optional int32 FIELD_NAME5 = 5;
// optional int32 field_name6 = 6 [json_name = "@type"];
// }
//
// We cheat and use TestForeign as the type for moo rather than create
// an actual nested type.
//
// Since all primitive types (including string) use the same building
// code, there's no need to test each one individually.
//
// TestMessage2 is primarily here to test FindFieldByName and friends.
// All messages created from the same DescriptorPool share the same lookup
// table, so we need to insure that they don't interfere.
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
AddMessage(&foo_file, "TestForeign");
AddEmptyEnum(&foo_file, "TestEnum");
DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
AddField(message, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
AddField(message, "bar", 6, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_ENUM)
->set_type_name("TestEnum");
AddField(message, "baz", 500000000, FieldDescriptorProto::LABEL_REPEATED,
FieldDescriptorProto::TYPE_MESSAGE)
->set_type_name("TestForeign");
AddField(message, "moo", 15, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_GROUP)
->set_type_name("TestForeign");
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("corge.grault");
DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
AddField(message2, "foo", 1, FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
AddField(message2, "bar", 2, FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
AddField(message2, "mooo", 6, FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
FileDescriptorProto map_file;
map_file.set_name("map.proto");
DescriptorProto* message3 = AddMessage(&map_file, "TestMessage3");
DescriptorProto* entry = AddNestedMessage(message3, "MapInt32Int32Entry");
AddField(entry, "key", 1, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(entry, "value", 2, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
entry->mutable_options()->set_map_entry(true);
AddField(message3, "map_int32_int32", 1,
FieldDescriptorProto::LABEL_REPEATED,
FieldDescriptorProto::TYPE_MESSAGE)
->set_type_name("MapInt32Int32Entry");
FileDescriptorProto json_file;
json_file.set_name("json.proto");
json_file.set_syntax("proto3");
DescriptorProto* message4 = AddMessage(&json_file, "TestMessage4");
AddField(message4, "field_name1", 1, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "fieldName2", 2, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "FieldName3", 3, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "_field_name4", 4, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "FIELD_NAME5", 5, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "field_name6", 6, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32)
->set_json_name("@type");
AddField(message4, "fieldname7", 7, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != nullptr);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != nullptr);
map_file_ = pool_.BuildFile(map_file);
ASSERT_TRUE(map_file_ != nullptr);
json_file_ = pool_.BuildFile(json_file);
ASSERT_TRUE(json_file_ != nullptr);
ASSERT_EQ(1, foo_file_->enum_type_count());
enum_ = foo_file_->enum_type(0);
ASSERT_EQ(2, foo_file_->message_type_count());
foreign_ = foo_file_->message_type(0);
message_ = foo_file_->message_type(1);
ASSERT_EQ(4, message_->field_count());
foo_ = message_->field(0);
bar_ = message_->field(1);
baz_ = message_->field(2);
moo_ = message_->field(3);
ASSERT_EQ(1, bar_file_->message_type_count());
message2_ = bar_file_->message_type(0);
ASSERT_EQ(3, message2_->field_count());
foo2_ = message2_->field(0);
bar2_ = message2_->field(1);
mooo2_ = message2_->field(2);
ASSERT_EQ(1, map_file_->message_type_count());
message3_ = map_file_->message_type(0);
ASSERT_EQ(1, message3_->field_count());
map_ = message3_->field(0);
ASSERT_EQ(1, json_file_->message_type_count());
message4_ = json_file_->message_type(0);
}
void CopyWithJsonName(const Descriptor* message, DescriptorProto* proto) {
message->CopyTo(proto);
message->CopyJsonNameTo(proto);
}
const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown(
const EnumDescriptor* desc, int number) {
return desc->FindValueByNumberCreatingIfUnknown(number);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const FileDescriptor* map_file_;
const FileDescriptor* json_file_;
const Descriptor* message_;
const Descriptor* message2_;
const Descriptor* message3_;
const Descriptor* message4_;
const Descriptor* foreign_;
const EnumDescriptor* enum_;
const FieldDescriptor* foo_;
const FieldDescriptor* bar_;
const FieldDescriptor* baz_;
const FieldDescriptor* moo_;
const FieldDescriptor* foo2_;
const FieldDescriptor* bar2_;
const FieldDescriptor* mooo2_;
const FieldDescriptor* map_;
};
TEST_F(DescriptorTest, Name) {
EXPECT_EQ("TestMessage", message_->name());
EXPECT_EQ("TestMessage", message_->full_name());
EXPECT_EQ(foo_file_, message_->file());
EXPECT_EQ("TestMessage2", message2_->name());
EXPECT_EQ("corge.grault.TestMessage2", message2_->full_name());
EXPECT_EQ(bar_file_, message2_->file());
}
TEST_F(DescriptorTest, ContainingType) {
EXPECT_TRUE(message_->containing_type() == nullptr);
EXPECT_TRUE(message2_->containing_type() == nullptr);
}
TEST_F(DescriptorTest, FieldNamesDedup) {
const auto collect_unique_names = [](const FieldDescriptor* field) {
absl::btree_set<std::string> names{field->name(), field->lowercase_name(),
field->camelcase_name(),
field->json_name()};
// Verify that we have the same number of string objects as we have string
// values. That is, duplicate names use the same std::string object.
// This is for memory efficiency.
EXPECT_EQ(names.size(), (absl::flat_hash_set<const std::string*>{
&field->name(), &field->lowercase_name(),
&field->camelcase_name(), &field->json_name()}
.size()))
<< testing::PrintToString(names);
return names;
};
// field_name1
EXPECT_THAT(collect_unique_names(message4_->field(0)),
ElementsAre("fieldName1", "field_name1"));
// fieldName2
EXPECT_THAT(collect_unique_names(message4_->field(1)),
ElementsAre("fieldName2", "fieldname2"));
// FieldName3
EXPECT_THAT(collect_unique_names(message4_->field(2)),
ElementsAre("FieldName3", "fieldName3", "fieldname3"));
// _field_name4
EXPECT_THAT(collect_unique_names(message4_->field(3)),
ElementsAre("FieldName4", "_field_name4", "fieldName4"));
// FIELD_NAME5
EXPECT_THAT(
collect_unique_names(message4_->field(4)),
ElementsAre("FIELDNAME5", "FIELD_NAME5", "fIELDNAME5", "field_name5"));
// field_name6, with json name @type
EXPECT_THAT(collect_unique_names(message4_->field(5)),
ElementsAre("@type", "fieldName6", "field_name6"));
// fieldname7
EXPECT_THAT(collect_unique_names(message4_->field(6)),
ElementsAre("fieldname7"));
}
TEST_F(DescriptorTest, FieldNameDedupJsonEqFull) {
// Test a regression where json_name == full_name
FileDescriptorProto proto;
proto.set_name("file");
auto* message = AddMessage(&proto, "Name1");
auto* field =
AddField(message, "Name2", 1, FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
field->set_json_name("Name1.Name2");
auto* file = pool_.BuildFile(proto);
EXPECT_EQ(file->message_type(0)->name(), "Name1");
EXPECT_EQ(file->message_type(0)->field(0)->name(), "Name2");
EXPECT_EQ(file->message_type(0)->field(0)->full_name(), "Name1.Name2");
EXPECT_EQ(file->message_type(0)->field(0)->json_name(), "Name1.Name2");
}
TEST_F(DescriptorTest, FieldsByIndex) {
ASSERT_EQ(4, message_->field_count());
EXPECT_EQ(foo_, message_->field(0));
EXPECT_EQ(bar_, message_->field(1));
EXPECT_EQ(baz_, message_->field(2));
EXPECT_EQ(moo_, message_->field(3));
}
TEST_F(DescriptorTest, FindFieldByName) {
// All messages in the same DescriptorPool share a single lookup table for
// fields. So, in addition to testing that FindFieldByName finds the fields
// of the message, we need to test that it does *not* find the fields of
// *other* messages.
EXPECT_EQ(foo_, message_->FindFieldByName("foo"));
EXPECT_EQ(bar_, message_->FindFieldByName("bar"));
EXPECT_EQ(baz_, message_->FindFieldByName("baz"));
EXPECT_EQ(moo_, message_->FindFieldByName("moo"));
EXPECT_TRUE(message_->FindFieldByName("no_such_field") == nullptr);
EXPECT_TRUE(message_->FindFieldByName("mooo") == nullptr);
EXPECT_EQ(foo2_, message2_->FindFieldByName("foo"));
EXPECT_EQ(bar2_, message2_->FindFieldByName("bar"));
EXPECT_EQ(mooo2_, message2_->FindFieldByName("mooo"));
EXPECT_TRUE(message2_->FindFieldByName("baz") == nullptr);
EXPECT_TRUE(message2_->FindFieldByName("moo") == nullptr);
}
TEST_F(DescriptorTest, FindFieldByNumber) {
EXPECT_EQ(foo_, message_->FindFieldByNumber(1));
EXPECT_EQ(bar_, message_->FindFieldByNumber(6));
EXPECT_EQ(baz_, message_->FindFieldByNumber(500000000));
EXPECT_EQ(moo_, message_->FindFieldByNumber(15));
EXPECT_TRUE(message_->FindFieldByNumber(837592) == nullptr);
EXPECT_TRUE(message_->FindFieldByNumber(2) == nullptr);
EXPECT_EQ(foo2_, message2_->FindFieldByNumber(1));
EXPECT_EQ(bar2_, message2_->FindFieldByNumber(2));
EXPECT_EQ(mooo2_, message2_->FindFieldByNumber(6));
EXPECT_TRUE(message2_->FindFieldByNumber(15) == nullptr);
EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == nullptr);
}
TEST_F(DescriptorTest, FieldName) {
EXPECT_EQ("foo", foo_->name());
EXPECT_EQ("bar", bar_->name());
EXPECT_EQ("baz", baz_->name());
EXPECT_EQ("moo", moo_->name());
}
TEST_F(DescriptorTest, FieldFullName) {
EXPECT_EQ("TestMessage.foo", foo_->full_name());
EXPECT_EQ("TestMessage.bar", bar_->full_name());
EXPECT_EQ("TestMessage.baz", baz_->full_name());
EXPECT_EQ("TestMessage.moo", moo_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.foo", foo2_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.bar", bar2_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.mooo", mooo2_->full_name());
}
TEST_F(DescriptorTest, PrintableNameIsFullNameForNonExtensionFields) {
EXPECT_EQ("TestMessage.foo", foo_->PrintableNameForExtension());
EXPECT_EQ("TestMessage.bar", bar_->PrintableNameForExtension());
EXPECT_EQ("TestMessage.baz", baz_->PrintableNameForExtension());
EXPECT_EQ("TestMessage.moo", moo_->PrintableNameForExtension());
EXPECT_EQ("corge.grault.TestMessage2.foo",
foo2_->PrintableNameForExtension());
EXPECT_EQ("corge.grault.TestMessage2.bar",
bar2_->PrintableNameForExtension());
EXPECT_EQ("corge.grault.TestMessage2.mooo",
mooo2_->PrintableNameForExtension());
}
TEST_F(DescriptorTest, PrintableNameIsFullNameForNonMessageSetExtension) {
EXPECT_EQ("protobuf_unittest.Aggregate.nested",
protobuf_unittest::Aggregate::descriptor()
->FindExtensionByName("nested")
->PrintableNameForExtension());
}
TEST_F(DescriptorTest, PrintableNameIsExtendingTypeForMessageSetExtension) {
EXPECT_EQ("protobuf_unittest.AggregateMessageSetElement",
protobuf_unittest::AggregateMessageSetElement::descriptor()
->FindExtensionByName("message_set_extension")
->PrintableNameForExtension());
}
TEST_F(DescriptorTest, FieldJsonName) {
EXPECT_EQ("fieldName1", message4_->field(0)->json_name());
EXPECT_EQ("fieldName2", message4_->field(1)->json_name());
EXPECT_EQ("FieldName3", message4_->field(2)->json_name());
EXPECT_EQ("FieldName4", message4_->field(3)->json_name());
EXPECT_EQ("FIELDNAME5", message4_->field(4)->json_name());