Repository navigation
Expand file tree
/
Copy pathprotoc_test.lua
More file actions
1388 lines (1305 loc) · 44.6 KB
/
Copy pathprotoc_test.lua
File metadata and controls
1388 lines (1305 loc) · 44.6 KB
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
-- protoc_test.lua -- unit tests for protoc.lua, the pure-Lua .proto compiler.
--
-- Run: lua protoc_test.lua
--
-- Tests whose name ends in `_known_issue` assert behaviour the compiler does
-- not implement yet (currently the schema validation protoc performs, such as
-- rejecting `required` in proto3 or out-of-range field numbers). They are
-- expected to fail until implemented, and are kept separate so the healthy
-- part of the suite can be run on its own:
--
-- lua protoc_test.lua -x known_issue
--
-- When an issue is fixed, drop the suffix from its test name.
local lu = require "luaunit"
local pb = require "pb"
local protoc = require "protoc"
local eq = lu.assertEquals
local fail = lu.assertErrorMsgContains
local error_any = lu.assertError
local is_true = lu.assertIsTrue
local is_nil = lu.assertIsNil
-- every test runs against a fresh pb state with the standard descriptors
-- reloaded, so tests do not leak types/options into one another
local function withstate(f)
local old = pb.state(nil)
protoc.reload()
local ok, err = pcall(f, old)
pb.state(old)
assert(ok, err)
end
local function compile_set(p, src, name)
local bytes = p:compile(src, name)
return pb.decode(".google.protobuf.FileDescriptorSet", bytes), bytes
end
local function first_message(p, src, name)
local set = compile_set(p, src, name)
return set.file[1].message_type[1], set.file[1]
end
local function check_msg(name, data, expected)
local bytes = pb.encode(name, data)
eq(pb.decode(name, bytes), expected or data)
end
local function copy_no_meta(t)
local r = {}
for k, v in pairs(t) do
if k ~= "__index" then r[k] = v end
end
return r
end
local function withfiles(files, f)
local names = {}
for name, content in pairs(files) do
local fh = assert(io.open(name, "w"))
assert(fh:write(content))
fh:close()
names[#names + 1] = name
end
local ok, err = pcall(f)
for _, name in ipairs(names) do os.remove(name) end
assert(ok, err)
end
-- ------------------------------------------------------------------ lexer
function _G.test_protoc_lexer_comments()
withstate(function()
local m = first_message(protoc.new(), [[
// line comment before syntax
syntax = "proto2"; /* block
comment */
message M { // trailing comment
optional int32 x = 1; /* inline */ optional string y = 2;
} ]], "lexer_comments.proto")
eq(m.name, "M")
eq(#m.field, 2)
eq(m.field[1].name, "x")
eq(m.field[2].name, "y")
end)
end
function _G.test_protoc_lexer_integer_literals()
withstate(function()
local set = compile_set(protoc.new(), [[
syntax = "proto2";
enum E { OCT = 010; HEX = 0x11; NEG = -1; }
message M { optional int32 a = 1; optional int32 b = 16; }
]], "lexer_int.proto")
local e = set.file[1].enum_type[1]
eq(e.value[1].number, 8)
eq(e.value[2].number, 17)
eq(e.value[3].number, -1)
local m = set.file[1].message_type[1]
eq(m.field[1].number, 1)
eq(m.field[2].number, 16)
end)
end
function _G.test_protoc_lexer_float_literals()
withstate(function()
local m = first_message(protoc.new(), [[
syntax = "proto2";
message M {
optional float a = 1 [default = 0.125];
optional float b = 2 [default = -2.5];
optional double c = 3 [default = 1000.25];
}
]], "lexer_float.proto")
eq(m.field[1].default_value, "0.125")
eq(m.field[2].default_value, "-2.5")
eq(tonumber(m.field[3].default_value), 1000.25)
end)
end
-- BUG: the exponent sub-pattern starts with `^` inside a capture, so it
-- matches a literal '^' instead of anchoring: any float written with an
-- exponent (1e3, 1.5e-3) fails to parse.
function _G.test_protoc_lexer_float_exponent()
withstate(function()
local m = first_message(protoc.new(), [[
syntax = "proto2";
message M {
optional double a = 1 [default = 1e3];
optional double b = 2 [default = 1.5e-3];
}
]], "lexer_float_exp.proto")
eq(tonumber(m.field[1].default_value), 1000)
eq(tonumber(m.field[2].default_value), 0.0015)
end)
end
function _G.test_protoc_lexer_string_escapes()
withstate(function()
local m = first_message(protoc.new(), [[
syntax = "proto2";
message M { optional string s = 1 [default = "tab\there\nnl \\ \'q"]; }
]], "lexer_str.proto")
eq(m.field[1].default_value, "tab\there\nnl \\ 'q")
end)
end
-- BUG: \xNN and \NNN escapes are decoded with string.byte(number), which
-- stringifies the number first, so "\x41" becomes the text "54", not "A".
function _G.test_protoc_lexer_hex_octal_escapes()
withstate(function()
local m = first_message(protoc.new(), [[
syntax = "proto2";
message M { optional string s = 1 [default = "\x41\101"]; }
]], "lexer_hex.proto")
eq(m.field[1].default_value, "AA")
end)
end
-- BUG: the escape patterns are greedy (`%x+`, `%d+`) but protoc reads at most
-- two hex digits and three octal digits: "\x4142" is "A42", "\1011" is "A1".
function _G.test_protoc_lexer_escape_digit_limit()
withstate(function()
local m = first_message(protoc.new(), [[
syntax = "proto2";
message M {
optional string a = 1 [default = "\x4142"];
optional string b = 2 [default = "\1011"];
}
]], "lexer_hex2.proto")
eq(m.field[1].default_value, "A42")
eq(m.field[2].default_value, "A1")
end)
end
-- three octal digits may exceed 0xff: protoc keeps the low byte ("\400" is
-- a NUL, "\777" is 0xff), not an out-of-range string.char() error
function _G.test_protoc_lexer_octal_overflow()
withstate(function()
local m = first_message(protoc.new(), [[
syntax = "proto2";
message M {
optional string a = 1 [default = "\400"];
optional string b = 2 [default = "\777"];
}
]], "lexer_oct.proto")
eq(m.field[1].default_value, "\0")
eq(m.field[2].default_value, "\255")
end)
end
function _G.test_protoc_lexer_error_location()
withstate(function()
local p = protoc.new()
fail("lexer_err.proto:3:", function()
p:parse('syntax = "proto2";\nmessage M {\n optional int32 x = ;\n}',
"lexer_err.proto")
end)
end)
end
-- ------------------------------------------------------- parser structure
function _G.test_protoc_parse_returns_file_descriptor()
withstate(function()
local info = protoc.new():parse([[
syntax = "proto2";
package demo.pkg;
message M { optional int32 i = 1; }
]], "parse_shape.proto")
eq(info.name, "parse_shape.proto")
eq(info.syntax, "proto2")
eq(info.package, "demo.pkg")
eq(info.message_type[1].name, "M")
end)
end
function _G.test_protoc_package_and_type_names()
withstate(function()
local p = protoc.new()
local set, bytes = compile_set(p, [[
syntax = "proto3";
package foo.bar;
message M { int32 x = 1; }
message N { M m = 1; }
enum E { ZERO = 0; }
]], "pkg.proto")
local f = set.file[1]
eq(f.package, "foo.bar")
eq(f.message_type[1].name, "M")
eq(f.message_type[2].field[1].type_name, ".foo.bar.M")
eq(f.enum_type[1].name, "E")
assert(pb.load(bytes))
eq(pb.type(".foo.bar.M"), ".foo.bar.M")
eq(pb.field(".foo.bar.M", "x"), "x")
check_msg(".foo.bar.N", { m = { x = 1 } })
end)
end
function _G.test_protoc_nested_types()
withstate(function()
local p = protoc.new()
local set, bytes = compile_set(p, [[
syntax = "proto2";
message Outer {
message Inner { optional int32 i = 1; }
enum Kind { A = 0; B = 1; }
optional Inner inner = 1;
optional Kind kind = 2;
}
]], "nested.proto")
local outer = set.file[1].message_type[1]
eq(outer.nested_type[1].name, "Inner")
eq(outer.enum_type[1].name, "Kind")
eq(outer.field[1].type_name, ".Outer.Inner")
eq(outer.field[2].type_name, ".Outer.Kind")
assert(pb.load(bytes))
eq(pb.type(".Outer.Inner"), ".Outer.Inner")
check_msg("Outer", { inner = { i = 1 }, kind = "B" })
end)
end
function _G.test_protoc_map()
withstate(function()
local p = protoc.new()
local set, bytes = compile_set(p, [[
syntax = "proto3";
message M {
map<string, int32> m = 1;
map<int32, N> n = 2;
}
message N { int32 x = 1; }
]], "map.proto")
local m = set.file[1].message_type[1]
local entry = m.nested_type[1]
eq(entry.name, "MEntry")
eq(entry.options.map_entry, true)
eq(entry.field[1].name, "key")
eq(entry.field[1].type, "TYPE_STRING")
eq(entry.field[2].name, "value")
eq(entry.field[2].type, "TYPE_INT32")
eq(m.field[1].type, "TYPE_MESSAGE")
eq(m.field[1].type_name, ".M.MEntry")
eq(m.field[1].label, "LABEL_REPEATED")
eq(m.field[2].type_name, ".M.NEntry")
eq(m.nested_type[2].field[2].type_name, ".N")
assert(pb.load(bytes))
check_msg("M", { m = { a = 1 }, n = { [7] = { x = 2 } } })
local _, _, kind = pb.type(".M.MEntry")
eq(kind, "map")
end)
end
function _G.test_protoc_oneof()
withstate(function()
local p = protoc.new()
local set, bytes = compile_set(p, [[
syntax = "proto3";
message M {
oneof body { int32 a = 1; string b = 2; }
oneof other { bool c = 3; }
}
]], "oneof.proto")
local m = set.file[1].message_type[1]
eq(#m.oneof_decl, 2)
eq(m.oneof_decl[1].name, "body")
eq(m.oneof_decl[2].name, "other")
eq(m.field[1].oneof_index, 0)
eq(m.field[2].oneof_index, 0)
eq(m.field[3].oneof_index, 1)
assert(pb.load(bytes))
eq(select("#", pb.field("M", "a")), 7)
local _, _, _, _, _, oname, oidx = pb.field("M", "a")
eq(oname, "body")
eq(oidx, 0)
check_msg("M", { a = 1, body = "a" })
local data = pb.decode("M", pb.encode("M", { a = 0 }))
eq(data.a, 0)
eq(data.body, "a")
end)
end
function _G.test_protoc_enum()
withstate(function()
local set, bytes = compile_set(protoc.new(), [[
syntax = "proto2";
enum E {
option allow_alias = true;
A = 0;
B = 1;
ALIAS = 1;
reserved 5;
reserved "OLD";
}
]], "enum.proto")
local e = set.file[1].enum_type[1]
eq(#e.value, 3)
eq(e.value[1].name, "A")
eq(e.value[2].number, 1)
eq(e.value[3].name, "ALIAS")
eq(e.options.allow_alias, true)
eq(e.reserved_range[1].start, 5)
eq(e.reserved_name[1], "OLD")
assert(pb.load(bytes))
eq(pb.type(".E"), ".E")
end)
end
function _G.test_protoc_enum_duplicates()
withstate(function()
local p = protoc.new()
fail("enum name 'A' exists", function()
p:parse([[syntax = "proto2"; enum E { A = 0; A = 1; }]], "enum_dup.proto")
end)
local p2 = protoc.new()
fail("enum number '0' exists", function()
p2:parse([[syntax = "proto2"; enum E { A = 0; B = 0; }]], "enum_dup2.proto")
end)
end)
end
-- BUG: nested enums never reach check_enum(), so duplicate names/numbers
-- inside a message are accepted (protoc rejects both).
function _G.test_protoc_nested_enum_duplicates()
withstate(function()
local p = protoc.new()
error_any(function()
p:parse([[syntax = "proto2"; message M { enum E { A = 0; A = 1; } }]],
"nested_enum_dup.proto")
end)
local p2 = protoc.new()
error_any(function()
p2:parse([[syntax = "proto2"; message M { enum E { A = 0; B = 0; } }]],
"nested_enum_dup2.proto")
end)
end)
end
function _G.test_protoc_extensions()
withstate(function()
local p = protoc.new()
local set, bytes = compile_set(p, [[
syntax = "proto2";
message Base {
optional int32 id = 1;
extensions 100 to 199;
}
extend Base {
optional string foo = 100;
}
]], "ext.proto")
local f = set.file[1]
eq(#f.extension, 1)
eq(f.extension[1].extendee, ".Base")
eq(f.extension[1].name, "foo")
eq(f.extension[1].number, 100)
eq(f.message_type[1].extension_range[1].start, 100)
assert(pb.load(bytes))
check_msg("Base", { id = 1, foo = "bar" })
eq(select("#", pb.field("Base", "foo")), 5)
-- extension presence is explicit: an empty string is still encoded
eq(pb.encode("Base", { foo = "" }), "\162\6\0")
end)
end
function _G.test_protoc_nested_extend()
withstate(function()
local set, bytes = compile_set(protoc.new(), [[
syntax = "proto2";
message Nest {
optional uint32 id = 1;
extensions 100 to max;
}
message Holder {
extend Nest { optional string name = 100; }
}
]], "nested_ext.proto")
local holder = set.file[1].message_type[2]
eq(holder.name, "Holder")
eq(holder.extension[1].extendee, ".Nest")
is_nil(set.file[1].extension)
assert(pb.load(bytes))
check_msg("Nest", { id = 2, name = "x" })
end)
end
-- BUG: ExtensionRange.end is exclusive in descriptor.proto; "100 to 200"
-- must be written as 201, "300" as 301 (protoc 36.x does this).
function _G.test_protoc_extension_range_end()
withstate(function()
local set = compile_set(protoc.new(), [[
syntax = "proto2";
message M {
extensions 100 to 200;
extensions 300;
extensions 400 to max;
}
]], "ext_range.proto")
local r = set.file[1].message_type[1].extension_range
eq(r[1]["end"], 201)
eq(r[2]["end"], 301)
eq(r[3]["end"], 536870912)
end)
end
-- BUG: DescriptorProto.ReservedRange.end is exclusive: "300 to 310" must be
-- 311, "320" 321 and "400 to max" 536870912. EnumReservedRange.end is
-- inclusive instead, and "to max" is INT32_MAX there (2147483647), so the enum
-- case needs a different `max` value than the message case.
function _G.test_protoc_reserved_range_end()
withstate(function()
local set = compile_set(protoc.new(), [[
syntax = "proto2";
message M {
reserved 300 to 310;
reserved 320;
reserved 400 to max;
}
enum E {
A = 0;
reserved 5 to 7;
reserved 400 to max;
}
]], "reserved_range.proto")
local r = set.file[1].message_type[1].reserved_range
eq(r[1]["end"], 311)
eq(r[2]["end"], 321)
eq(r[3]["end"], 536870912)
local er = set.file[1].enum_type[1].reserved_range
eq(er[1]["end"], 7)
eq(er[2]["end"], 2147483647)
end)
end
-- BUG: extension numbers are not checked against the declared extension
-- ranges of the extendee (protoc rejects both cases).
function _G.test_protoc_extension_number_range_known_issue()
withstate(function()
local p = protoc.new()
error_any(function()
p:parse([[
syntax = "proto2";
message Base { extensions 100 to 199; }
extend Base { optional int32 x = 300; }
]], "ext_num.proto")
end)
local p2 = protoc.new()
error_any(function()
p2:parse([[
syntax = "proto2";
message Base { optional int32 id = 1; }
extend Base { optional int32 x = 100; }
]], "ext_num2.proto")
end)
end)
end
function _G.test_protoc_extendee_not_message()
withstate(function()
local p = protoc.new()
fail("message type expected in extension", function()
p:parse([[
syntax = "proto2";
enum E { A = 0; }
extend E { optional int32 x = 1; }
]], "ext_enum.proto")
end)
end)
end
function _G.test_protoc_service()
withstate(function()
local set, bytes = compile_set(protoc.new(), [[
syntax = "proto3";
message In { int32 i = 1; }
message Out { int32 o = 1; }
service S {
rpc Unary(In) returns (Out);
rpc ClientStream(stream In) returns (Out);
rpc ServerStream(In) returns (stream Out);
rpc Bidi(stream In) returns (stream Out) {
option deprecated = true;
}
}
]], "svc.proto")
local s = set.file[1].service[1]
eq(s.name, "S")
eq(#s.method, 4)
eq(s.method[1].name, "Unary")
eq(s.method[1].input_type, ".In")
eq(s.method[1].output_type, ".Out")
is_nil(s.method[1].client_streaming)
is_nil(s.method[1].server_streaming)
eq(s.method[2].client_streaming, true)
eq(s.method[3].server_streaming, true)
eq(s.method[4].client_streaming, true)
eq(s.method[4].server_streaming, true)
eq(s.method[4].options.deprecated, true)
assert(pb.load(bytes))
end)
end
function _G.test_protoc_service_errors()
withstate(function()
local p = protoc.new()
fail("unknown type 'Nope'", function()
p:parse([[syntax = "proto3"; service S { rpc R(Nope) returns (Nope); }]],
"svc_err.proto")
end)
local p2 = protoc.new()
fail("message type expected in parameter", function()
p2:parse([[
syntax = "proto3";
enum E { Z = 0; }
message M { int32 i = 1; }
service S { rpc R(E) returns (M); }
]], "svc_err2.proto")
end)
local p3 = protoc.new()
fail("message type expected in return", function()
p3:parse([[
syntax = "proto3";
enum E { Z = 0; }
message M { int32 i = 1; }
service S { rpc R(M) returns (E); }
]], "svc_err3.proto")
end)
end)
end
function _G.test_protoc_options()
withstate(function()
local set = compile_set(protoc.new(), [[
syntax = "proto2";
option java_package = "com.example";
option optimize_for = SPEED;
option deprecated = true;
message M {
option message_set_wire_format = true;
optional int32 x = 1 [deprecated = true, json_name = "xx"];
repeated int32 r = 2 [packed = true];
oneof body {
option deprecated = true;
int32 y = 3;
}
}
]], "options.proto")
local f = set.file[1]
eq(f.options.java_package, "com.example")
eq(f.options.optimize_for, "SPEED")
eq(f.options.deprecated, true)
local m = f.message_type[1]
eq(m.options.message_set_wire_format, true)
eq(m.field[1].options.deprecated, true)
eq(m.field[1].json_name, "xx")
eq(m.field[2].options.packed, true)
end)
end
function _G.test_protoc_defaults()
withstate(function()
local set, bytes = compile_set(protoc.new(), [[
syntax = "proto2";
enum Color { RED = 0; GREEN = 1; }
message M {
optional int32 i = 1 [default = 42];
optional uint32 u = 2 [default = 7];
optional bool b = 3 [default = true];
optional string s = 4 [default = "hi"];
optional float f = 5 [default = 0.5];
optional Color c = 6 [default = GREEN];
}
]], "defaults.proto")
local m = set.file[1].message_type[1]
is_nil(m.field[1].options)
eq(m.field[1].default_value, "42")
eq(m.field[3].default_value, "true")
eq(m.field[4].default_value, "hi")
eq(m.field[5].default_value, "0.5")
eq(m.field[6].default_value, "GREEN")
assert(pb.load(bytes))
pb.option "enum_as_name"
local d = copy_no_meta(pb.defaults "M")
eq(d.i, 42)
eq(d.u, 7)
eq(d.b, true)
eq(d.s, "hi")
eq(d.f, 0.5)
eq(d.c, "GREEN")
end)
end
-- BUG: any inline option sets default_value to the string "nil", because
-- `tostring(options.default)` runs even when the option is absent. This
-- corrupts the descriptor and makes pb.defaults() return "nil" for strings.
function _G.test_protoc_default_without_default_option()
withstate(function()
local set, bytes = compile_set(protoc.new(), [[
syntax = "proto2";
message M {
optional string s = 1 [deprecated = true];
repeated int32 r = 2 [packed = true];
}
]], "default_pollution.proto")
local m = set.file[1].message_type[1]
is_nil(m.field[1].default_value)
is_nil(m.field[2].default_value)
assert(pb.load(bytes))
local _, _, _, def = pb.field("M", "s")
is_nil(def)
is_nil(pb.defaults("M").s)
end)
end
-- ------------------------------------------------ imports and unknown names
function _G.test_protoc_include_imports()
withfiles({
["protoc_test_dep_lib.proto"] = [[
syntax = "proto2";
message DepLib { optional int32 i = 1; }
]],
["protoc_test_user.proto"] = [[
syntax = "proto2";
import "protoc_test_dep_lib.proto";
message User { optional DepLib dep = 1; }
]],
}, function()
withstate(function()
local p = protoc.new()
p:addpath(".")
p.include_imports = true
local bytes = p:compilefile("protoc_test_user.proto")
local set = pb.decode(".google.protobuf.FileDescriptorSet", bytes)
eq(#set.file, 2)
eq(set.file[1].name, "protoc_test_dep_lib.proto")
eq(set.file[2].name, "protoc_test_user.proto")
eq(set.file[2].dependency[1], "protoc_test_dep_lib.proto")
assert(pb.load(bytes))
check_msg("User", { dep = { i = 1 } })
end)
end)
end
function _G.test_protoc_include_imports_unknown_import()
withstate(function()
local p = protoc.new()
p.include_imports = true
p.unknown_import = true
p.unknown_type = true
local set = pb.decode(".google.protobuf.FileDescriptorSet", p:compile([[
syntax = "proto2";
import "missing.proto";
message M { optional int32 i = 1; }
]], "include_missing.proto"))
eq(#set.file, 1)
eq(set.file[1].dependency[1], "missing.proto")
end)
end
function _G.test_protoc_include_imports_restores_on_import()
withstate(function()
local p = protoc.new()
local orig = function() end
p.on_import = orig
p.include_imports = true
p:compile([[syntax = "proto2"; message M { optional int32 i = 1; }]],
"restore_on_import.proto")
is_true(p.on_import == orig)
p.on_import = orig
error_any(function()
p:compile("message", "restore_on_import_bad.proto")
end)
is_true(p.on_import == orig)
end)
end
function _G.test_protoc_on_import()
withfiles({
["protoc_test_dep_cb.proto"] = [[
syntax = "proto2";
message DepCb { optional int32 i = 1; }
]],
["protoc_test_cb_user.proto"] = [[
syntax = "proto2";
import "protoc_test_dep_cb.proto";
message CbUser { optional DepCb dep = 1; }
]],
}, function()
withstate(function()
local p = protoc.new()
p:addpath(".")
local seen = {}
p.on_import = function(info) seen[#seen + 1] = info.name end
p:compilefile("protoc_test_cb_user.proto")
eq(seen, { "protoc_test_dep_cb.proto" })
eq(p.loaded["protoc_test_dep_cb.proto"].name, "protoc_test_dep_cb.proto")
end)
end)
end
-- BUG: a plain `import` is recorded as a public_dependency; descriptor.proto
-- only lists `import public` there.
function _G.test_protoc_import_plain_not_public()
withfiles({
["protoc_test_imp_a.proto"] = [[
syntax = "proto2";
message ImpA { optional int32 i = 1; }
]],
["protoc_test_imp_b.proto"] = [[
syntax = "proto2";
import "protoc_test_imp_a.proto";
import public "protoc_test_imp_c.proto";
message ImpB { optional ImpA a = 1; }
]],
["protoc_test_imp_c.proto"] = [[
syntax = "proto2";
message ImpC { optional int32 i = 1; }
]],
}, function()
withstate(function()
local p = protoc.new()
p:addpath(".")
local set = pb.decode(".google.protobuf.FileDescriptorSet",
p:compilefile("protoc_test_imp_b.proto"))
local f = set.file[1]
eq(f.dependency, { "protoc_test_imp_a.proto", "protoc_test_imp_c.proto" })
eq(f.public_dependency, { 1 })
end)
end)
end
-- BUG: include_imports emits the same file once per import statement instead
-- of once per file (a diamond dependency duplicates the shared file).
function _G.test_protoc_include_imports_dedup()
withfiles({
["protoc_test_leaf.proto"] = [[
syntax = "proto2";
message Leaf { optional int32 i = 1; }
]],
["protoc_test_mid.proto"] = [[
syntax = "proto2";
import "protoc_test_leaf.proto";
message Mid { optional Leaf leaf = 1; }
]],
["protoc_test_top.proto"] = [[
syntax = "proto2";
import "protoc_test_mid.proto";
import "protoc_test_leaf.proto";
message Top { optional Mid mid = 1; }
]],
}, function()
withstate(function()
local p = protoc.new()
p:addpath(".")
p.include_imports = true
local set = pb.decode(".google.protobuf.FileDescriptorSet",
p:compilefile("protoc_test_top.proto"))
local names = {}
for i, f in ipairs(set.file) do names[i] = f.name end
eq(names, {
"protoc_test_leaf.proto",
"protoc_test_mid.proto",
"protoc_test_top.proto",
})
end)
end)
end
-- BUG: import cycles are silently accepted without include_imports, and blow
-- up with a confusing pb.encode error with include_imports. protoc rejects
-- the cycle at parse time.
function _G.test_protoc_import_cycle()
withfiles({
["protoc_test_cyc_a.proto"] = [[
syntax = "proto2";
import "protoc_test_cyc_b.proto";
message CycA { optional int32 i = 1; }
]],
["protoc_test_cyc_b.proto"] = [[
syntax = "proto2";
import "protoc_test_cyc_a.proto";
message CycB { optional int32 i = 1; }
]],
}, function()
withstate(function()
local p = protoc.new()
p:addpath(".")
error_any(function() p:compilefile("protoc_test_cyc_a.proto") end)
end)
end)
end
-- BUG: a file may import itself; parsefile() returns the in-progress guard
-- value `true` instead of reporting the recursion.
function _G.test_protoc_import_self()
withfiles({
["protoc_test_self.proto"] = [[
syntax = "proto2";
import "protoc_test_self.proto";
message SelfImp { optional int32 i = 1; }
]],
}, function()
withstate(function()
local p = protoc.new()
p:addpath(".")
error_any(function() p:compilefile("protoc_test_self.proto") end)
end)
end)
end
-- BUG: README documents unknown_import as accepting a Lua pattern string, but
-- the string is called as a function.
function _G.test_protoc_unknown_import_string()
withstate(function()
local p = protoc.new()
p.unknown_import = "foo%."
local info = p:parse([[syntax = "proto2"; import "foo.proto";]],
"unknown_import_str.proto")
eq(info.dependency[1], "foo.proto")
local p2 = protoc.new()
p2.unknown_import = "foo%."
fail("module load error", function()
p2:parse([[syntax = "proto2"; import "bar.proto";]],
"unknown_import_str2.proto")
end)
end)
end
function _G.test_protoc_unknown_import_handlers()
withstate(function()
local p = protoc.new()
p.unknown_import = true
local info = p:parse([[
syntax = "proto2";
import "no_such_file.proto";
message M { optional int32 i = 1; }
]], "unknown_import_true.proto")
eq(info.dependency[1], "no_such_file.proto")
local p2 = protoc.new()
local called
p2.unknown_import = function(self, name)
called = name
return p2:parse("syntax = 'proto2'; message Fake { optional int32 i = 1; }",
name)
end
local info2 = p2:parse([[
syntax = "proto2";
import "virtual.proto";
message M { optional int32 i = 1; }
]], "unknown_import_fn.proto")
eq(called, "virtual.proto")
eq(info2.dependency[1], "virtual.proto")
end)
end
function _G.test_protoc_unknown_type_handlers()
withstate(function()
local p = protoc.new()
p.unknown_type = true
local set = pb.decode(".google.protobuf.FileDescriptorSet", p:compile([[
syntax = "proto2";
message M { optional Foo f = 1; }
]], "unknown_type_true.proto"))
local f = set.file[1].message_type[1].field[1]
eq(f.type, "TYPE_MESSAGE")
eq(f.type_name, ".Foo")
local p2 = protoc.new()
p2.unknown_type = "Foo.*"
local bytes = p2:compile([[
syntax = "proto2";
message M { optional FooBar f = 1; }
]], "unknown_type_pat.proto")
is_true(type(bytes) == "string")
local p3 = protoc.new()
p3.unknown_type = "Foo.*"
fail("unknown type 'BarBar'", function()
p3:parse([[syntax = "proto2"; message M { optional BarBar f = 1; }]],
"unknown_type_pat2.proto")
end)
end)
end
-- BUG: the unknown_type callback may return a type name *and* a type
-- ("message"/"enum") per README, but only the first return value is used, so
-- enums fall back to message.
function _G.test_protoc_unknown_type_callback_enum()
withstate(function()
local p = protoc.new()
p.unknown_type = function(self, name) return ".My" .. name, "enum" end
local set = pb.decode(".google.protobuf.FileDescriptorSet", p:compile([[
syntax = "proto2";
message M { optional Foo f = 1; }
]], "unknown_type_cb.proto"))
local f = set.file[1].message_type[1].field[1]
eq(f.type, "TYPE_ENUM")
eq(f.type_name, ".MyFoo")
end)
end
function _G.test_protoc_unknown_type_callback_default()
withstate(function()
local p = protoc.new()
p.unknown_type = function(self, name) return ".One" .. name end
local set = pb.decode(".google.protobuf.FileDescriptorSet", p:compile([[
syntax = "proto2";
message M { optional Foo f = 1; }
]], "unknown_type_cb2.proto"))
local f = set.file[1].message_type[1].field[1]
eq(f.type, "TYPE_MESSAGE")
eq(f.type_name, ".OneFoo")
end)
end
function _G.test_protoc_unknown_type_invalid_kind()
withstate(function()
local p = protoc.new()
p.unknown_type = function(self, name) return ".X" .. name, "bogus" end
fail("invalid type 'bogus'", function()
p:parse([[syntax = "proto2"; message M { optional Foo f = 1; }]],
"unknown_type_bad.proto")
end)
end)
end
-- BUG: the unknown_type handler is bypassed for absolute type names.
function _G.test_protoc_unknown_type_absolute()
withstate(function()
local p = protoc.new()
p.unknown_type = true
local set = pb.decode(".google.protobuf.FileDescriptorSet", p:compile([[
syntax = "proto2";
message M { optional .Foo f = 1; }
]], "unknown_type_abs.proto"))
local f = set.file[1].message_type[1].field[1]
eq(f.type, "TYPE_MESSAGE")
eq(f.type_name, ".Foo")
end)
end
-- ------------------------------------------------ proto3 optional (WIP)
-- REGRESSION: proto3 `optional` no longer gets a synthetic oneof, so pb has no
-- presence flag and drops/zero-fills the field.
function _G.test_protoc_proto3_optional_presence()
withstate(function()
local p = protoc.new()
p.proto3_optional = true
local _, bytes = compile_set(p, [[
syntax = "proto3";
message M {
optional int32 x = 1;
optional string s = 2;
optional bool b = 3;
}
]], "opt_presence.proto")