forked from starwing/lua-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
1691 lines (1515 loc) · 49.5 KB
/
test.lua
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
local lu = require "luaunit"
local pb = require "pb"
local pbio = require "pb.io"
local buffer = require "pb.buffer"
local slice = require "pb.slice"
local conv = require "pb.conv"
local protoc = require "protoc"
-- local assert_not = lu.assertEvalToFalse
local eq = lu.assertEquals
local table_eq = lu.assertItemsEquals
local fail = lu.assertErrorMsgContains
local is_true = lu.assertIsTrue
local types = 0
for _ in pb.types() do
types = types + 1
end
pbio.write("pb predefined types: ", tostring(types), "\n")
local function check_load(chunk, name)
local pbdata = protoc.new():compile(chunk, name)
local ret, offset = pb.load(pbdata)
if not ret then
error("load error at "..offset..
"\nproto: "..chunk..
"\ndata: "..buffer(pbdata):tohex())
end
end
local function check_msg(name, data, r)
local chunk2, _ = pb.encode(name, data)
local data2 = pb.decode(name, chunk2)
--print("msg: ", name, "<"..chunk2:gsub(".", function(s)
--return ("%02X "):format(s:byte())
--end)..">")
eq(data2, r or data)
end
local function withstate(f)
local old = pb.state(nil)
local ok, res = pcall(f, old)
pb.state(old)
assert(ok, res)
end
_G.test_io = {} do
function _G.test_io.setup()
pbio.dump("address.proto", [[
message Phone {
optional string name = 1;
optional int64 phonenumber = 2;
}
message Person {
optional string name = 1;
optional int32 age = 2;
optional string address = 3;
repeated Phone contacts = 4;
} ]])
end
function _G.test_io.teardown()
os.remove "address.proto"
os.remove "address.pb"
end
function _G.test_io.test()
local code = "assert(io.write(require 'pb.io'.read()))"
assert(pbio.dump("t.lua", code))
local fh = assert(io.popen(arg[-1].." t.lua < t.lua", "r"))
eq(fh:read "*a", code)
fh:close()
assert(os.remove "t.lua")
fail("-not-exists-", function() assert(pbio.read "-not-exists-") end)
local chunk = protoc.new():compile(pbio.read "address.proto",
"address.proto")
assert(pbio.dump("address.pb", chunk))
assert(pb.loadfile "address.pb")
assert(pb.type "Person")
eq(pb.type "-not-exists-", nil)
local ft = {}
for name in pb.fields "Person" do
ft[name] = true
end
table_eq(ft, { name=true, age=true,address=true,contacts=true })
eq(pb.decode("Person", "\240\255\255\255\255\255\255\255\255\1\1"), {})
eq(pb.decode("Person", "\240\255\255\255\255\255\255\255\255\255"), {})
eq(pb.decode("Person", "\240\255\255\255\255"), {})
eq(pb.decode("Person", "\242\255\255\255\255\1\255"), {})
eq(pb.decode("Person", "\x71"), {})
eq(pb.decode("Person", "\x33\1\2\3\4\x34"), {})
eq(pb.decode("Person", "\x33\1\2\3\4\x44"), {})
eq(pb.decode("Person", "\x33\1\2\3\4"), {})
eq(pb.decode("Person", "\x75"), {})
eq(pb.decode("Person", "\x75\1\1\1\1"), {})
fail("type 'Foo' does not exists", function() pb.encode("Foo", {}) end)
fail("type 'Foo' does not exists", function() pb.decode("Foo", "") end)
fail("string expected for field 'name', got boolean",
function() pb.encode("Person", { name = true }) end)
fail("type mismatch for field 'name' at offset 2, bytes expected for type string, got varint",
function() pb.decode("Person", "\8\1") end)
fail("invalid varint value at offset 2",
function() pb.decode("Person", "\16\255") end)
fail("invalid bytes length: 0 (at offset 2)",
function() pb.decode("Person", "\10\255") end)
fail("unfinished bytes (len 10 at offset 3)",
function() pb.decode("Person", "\10\10") end)
local data = {
name = "ilse",
age = 18,
contacts = {
{ name = "alice", phonenumber = 12312341234 },
{ name = "bob", phonenumber = 45645674567 }
}
}
check_msg(".Person", data)
pb.clear()
protoc.reload()
fail("-not-exists-", function() assert(pb.loadfile "-not-exists-") end)
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
end
_G.test_depend = {} do
function _G.test_depend.setup()
pbio.dump("depend1.proto", [[
syntax = "proto2";
message Depend1Msg {
optional int32 id = 1;
optional string name = 2;
} ]])
end
function _G.test_depend.teardown()
os.remove "depend1.proto"
end
function _G.test_depend.test()
local function load_depend(p)
p:load [[
syntax = "proto2";
import "depend1.proto";
message Depend2Msg {
optional Depend1Msg dep1 = 1;
optional int32 other = 2;
} ]]
end
load_depend(protoc.new())
local t = { dep1 = { id = 1, name = "foo" }, other = 2 }
check_msg("Depend2Msg", t, { dep1 = {}, other = 2 })
eq(protoc.new():loadfile "depend1.proto", true)
local chunk = pb.encode("Depend2Msg", t)
check_msg("Depend2Msg", t)
pb.clear "Depend1Msg"
check_msg("Depend2Msg", t, { other = 2 })
table_eq(pb.decode("Depend2Msg", chunk), { other = 2 })
eq(protoc.new():loadfile "depend1.proto", true)
check_msg("Depend2Msg", t)
pb.clear "Depend1Msg"
pb.clear "Depend2Msg"
local p = protoc.new()
p.include_imports = true
load_depend(p)
check_msg("Depend2Msg", t)
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
end
function _G.test_extend()
local P = protoc.new()
P.unknown_import = true
P.unknown_type = true
assert(P:load([[
syntax = "proto2";
import "descriptor.proto";
extend google.protobuf.EnumValueOptions {
optional string name = 51000;
}
message Extendable {
extend NestExtend {
optional string ext_name = 100;
}
optional int32 id = 1;
extensions 100 to max;
} ]], "extend1.proto"))
eq(pb.type "Extendable", ".Extendable")
P.unknown_import = nil
P.unknown_type = nil
local chunk = assert(P:compile [[
syntax = "proto2";
import "extend1.proto"
enum MyEnum {
First = 1 [(name) = "first"];
Second = 2 [(name) = "second"];
Third = 3 [(name) = "third"];
}
message NestExtend {
optional uint32 id = 1;
extensions 100 to max;
}
extend Extendable {
optional string ext_name = 100;
} ]])
assert(pb.load(chunk))
assert(pb.type "MyEnum")
local t = { ext_name = "foo", id = 10 }
check_msg("Extendable", t)
local t2 = { ext_name = "foo", id = 10 }
check_msg("NestExtend", t2)
local data = pb.decode("google.protobuf.FileDescriptorSet", chunk)
eq(data.file[1].enum_type[1].value[1].options.name, "first")
eq(data.file[1].enum_type[1].value[2].options.name, "second")
eq(data.file[1].enum_type[1].value[3].options.name, "third")
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_type()
pb.clear "not-exists"
check_load [[
message TestTypes {
optional double dv = 1;
optional float fv = 2;
optional int64 i64v = 3;
optional uint64 u64v = 4;
optional int32 i32v = 5;
optional uint32 u32v = 13;
optional fixed64 f64v = 6;
optional fixed32 f32v = 7;
optional bool bv = 8;
optional string sv = 9;
optional bytes btv = 12;
optional sfixed32 sf32v = 15;
optional sfixed64 sf64v = 16;
optional sint32 s32v = 17;
optional sint64 s64v = 18;
} ]]
local data = {
dv = 0.125;
fv = 0.5;
i64v = -12345678901234567;
u64v = 12345678901234567;
i32v = -2101112222;
u32v = 2101112222;
f64v = 12345678901234567;
f32v = 1231231234;
bv = true;
sv = "foo";
btv = "bar";
sf32v = -123;
sf64v = -456;
s32v = -1234;
s64v = -4321;
}
check_msg(".TestTypes", data)
pb.clear "TestTypes"
check_load [[
message test_type {
optional uint32 r = 1;
repeated uint64 r64 = 2;
}
message test2 {
optional test_type test_type = 1;
} ]]
pb.option "int64_as_string"
local data = {
r64 = {
1231234123,
"#45645674567",
"#18446744073709551615"
}
}
check_msg(".test_type", data)
pb.option "int64_as_number"
check_msg("test_type", {r = 1})
pb.clear "test_type"
pb.clear "test2"
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_default()
withstate(function()
protoc.reload()
check_load [[
enum TestDefaultColor {
RED = 0;
GREEN = 1;
BLUE = 2;
}
message TestDefault {
// some fields here
optional int32 foo = 1;
optional uint32 defaulted_uint = 18 [ default = 666 ];
optional int32 defaulted_int = 10 [ default = 777 ];
optional bool defaulted_bool = 11 [ default = true ];
optional string defaulted_str = 12 [ default = "foo" ];
optional float defaulted_num = 13 [ default = 0.125 ];
optional TestDefaultColor color = 14 [default = RED];
optional bool bool1 = 15 [default=false];
optional bool bool2 = 16 [default=foo];
repeated int32 array = 17;
} ]]
check_msg("TestDefault", { foo = 1 })
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
pb.option "enum_as_value"
table_eq(copy_no_meta(pb.defaults "TestDefault"), {
defaulted_uint = 666,
defaulted_int = 777,
defaulted_bool = true,
defaulted_str = "foo",
defaulted_num = 0.125,
color = 0,
bool1 = false,
bool2 = nil
})
pb.option "enum_as_name"
pb.defaults("TestDefault", "clear")
table_eq(copy_no_meta(pb.defaults "TestDefault"), {
defaulted_uint = 666,
defaulted_int = 777,
defaulted_bool = true,
defaulted_str = "foo",
defaulted_num = 0.125,
color = "RED",
bool1 = false,
bool2 = nil
})
pb.clear "TestDefault"
pb.clear "TestDefaultColor"
fail("type not found",
function() pb.defaults "-invalid-type-" end)
check_load [[
syntax = "proto3";
enum TestDefaultColor {
RED = 0;
GREEN = 1;
BLUE = 2;
}
message TestNest{}
message TestNest1 {
TestNest nest = 1;
}
message TestNest2 {
TestNest1 nest = 1;
}
message TestNest3 {
TestNest2 nest = 1;
}
message TestDefault {
// some fields here
int32 foo = 1;
int32 defaulted_int = 10;
bool defaulted_bool = 11;
string defaulted_str = 12;
float defaulted_num = 13;
TestDefaultColor color = 14;
bool bool1 = 15;
bool bool2 = 16;
TestNest nest = 17;
repeated int32 array = 18;
} ]]
pb.option "decode_default_message"
local dt = pb.decode("TestNest3", "")
table_eq(dt, {nest={nest={nest={}}}})
pb.option "no_decode_default_message"
local _, _, _, _, rep = pb.field("TestDefault", "foo")
eq(rep, "optional")
table_eq(copy_no_meta(pb.defaults "TestDefault"), {
defaulted_int = 0,
defaulted_bool = false,
defaulted_str = "",
defaulted_num = 0.0,
color = "RED",
bool1 = false,
bool2 = false,
})
pb.option "enum_as_value"
pb.defaults("TestDefault", "clear")
table_eq(copy_no_meta(pb.defaults "TestDefault"), {
defaulted_int = 0,
defaulted_bool = false,
defaulted_str = "",
defaulted_num = 0.0,
color = 0,
bool1 = false,
bool2 = false,
})
pb.option "use_default_metatable"
local dt = pb.decode("TestDefault", "")
eq(getmetatable(dt), pb.defaults "TestDefault")
eq(dt.defaulted_int, 0)
eq(dt.defaulted_bool, false)
eq(dt.defaulted_str, "")
eq(dt.defaulted_num, 0.0)
eq(dt.color, 0)
eq(dt.bool1, false)
eq(dt.bool2, false)
table_eq(dt.array, {})
table_eq(pb.decode "TestDefault", pb.decode("TestDefault", ""))
pb.option "use_default_values"
dt = pb.decode("TestDefault", "")
eq(getmetatable(dt), nil)
table_eq(dt, {
defaulted_int = 0,
defaulted_bool = false,
defaulted_str = "",
defaulted_num = 0.0,
color = 0,
bool1 = false,
bool2 = false,
array = {}
})
eq(dt.defaulted_int, 0)
eq(dt.defaulted_bool, false)
eq(dt.defaulted_str, "")
eq(dt.defaulted_num, 0.0)
eq(dt.color, 0)
eq(dt.bool1, false)
eq(dt.bool2, false)
table_eq(dt.array, {})
pb.option "no_default_values"
pb.option "encode_default_values"
pb.option "decode_default_array"
local dt = pb.decode("TestDefault", "")
eq(getmetatable(dt), nil)
table_eq(dt,{ array = {} })
local chunk2, _ = pb.encode("TestDefault", {defaulted_int = 0,defaulted_bool = true})
local dt = pb.decode("TestDefault", chunk2)
eq(dt.defaulted_int, 0)
eq(dt.defaulted_bool, true)
eq(dt.defaulted_str, nil)
eq(dt.defaulted_num, nil)
eq(dt.color, nil)
eq(dt.bool1, nil)
eq(dt.bool2, nil)
table_eq(dt.array, {})
pb.option "no_encode_default_values"
pb.option "no_decode_default_array"
pb.option "no_default_values"
pb.option "enum_as_name"
pb.clear "TestDefault"
pb.clear "TestNest"
pb.option "auto_default_values"
end)
end
function _G.test_enum()
check_load [[
enum Color {
Red = 0;
Green = 1;
Blue = 2;
}
message TestEnum {
optional Color color = 1;
} ]]
eq(pb.enum("Color", 0), "Red")
eq(pb.enum("Color", "Red"), 0)
eq(pb.enum("Color", 1), "Green")
eq(pb.enum("Color", "Green"), 1)
local t = {}
for name, number in pb.fields "Color" do
t[name] = number
end
table_eq(t, {Red=0, Green=1, Blue=2})
eq({pb.field("TestEnum", "color")}, {"color", 1, ".Color", nil, "optional"})
local data = { color = "Red" }
check_msg("TestEnum", data)
local data2 = { color = 123 }
check_msg("TestEnum", data2)
pb.option "enum_as_value"
check_msg("TestEnum", data, { color = 0 })
pb.option "int64_as_string"
check_msg("TestEnum", { color = "#18446744073709551615" })
pb.option "int64_as_number"
pb.option "enum_as_name"
check_msg("TestEnum", data, { color = "Red" })
fail("invalid varint value at offset 2",
function() pb.decode("TestEnum", "\8\255") end)
fail("number/string expected at field 'color', got boolean",
function() pb.encode("TestEnum", { color = true }) end)
fail("can not encode unknown enum 'foo' at field 'color'",
function() pb.encode("TestEnum", { color = "foo" }) end)
check_load [[
message TestAlias {
enum AliasedEnum {
option allow_alias = true;
ZERO = 0;
ONE = 1;
TWO = 2;
FIRST = 1;
}
repeated AliasedEnum aliased_enumf = 2;
} ]]
check_msg("TestAlias",
{ aliased_enumf = { "ZERO", "FIRST", "TWO", 23, "ONE" } },
{ aliased_enumf = { "ZERO", "FIRST", "TWO", 23, "FIRST" } })
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_packed()
check_load [[
message Empty {}
message TestPacked {
repeated int64 packs = 1 [packed=true];
} ]]
local data = {
packs = { 1,2,3,4,-1,-2,3 }
}
check_msg(".TestPacked", data)
fail("table expected at field 'packs', got boolean",
function() pb.encode("TestPacked", { packs = true }) end)
local data = {packs = {}}
check_msg(".TestPacked", data, {})
local hasEmpty
for _, name in pb.types() do
if name == "Empty" then
hasEmpty = true
break
end
end
assert(hasEmpty)
pb.clear "TestPacked"
pb.clear "Empty"
eq(pb.type("TestPacked"), nil)
for _, name in pb.types() do
assert(name ~= "TestPacked", name)
assert(name ~= "Empty", name)
end
eq(pb.types()(nil, "not-exists"), nil)
check_load [[
syntax="proto3";
message MyMessage
{
repeated int32 intList = 1;
} ]]
local b = pb.encode("MyMessage", { intList = { 1,2,3 } })
eq(pb.tohex(b), "0A 03 01 02 03")
check_load [[
syntax="proto3";
message MessageA
{
int32 intValue = 1;
}
message MessageB
{
repeated MessageA messageValue = 1;
} ]]
pb.option "use_default_values"
check_msg("MessageB", { messageValue = { { intValue = 0 } } })
pb.option "no_default_values"
check_msg("MessageB", { messageValue = { {} } })
check_msg("MessageB", { messageValue = { { intValue = 1 } } })
eq(pb.tohex(pb.encode(
"MessageB", { messageValue = { {} } })), "0A 00")
eq(pb.tohex(pb.encode(
"MessageB", { messageValue = { { intValue = 1 } } })), "0A 02 08 01")
pb.clear "MessageA"
pb.clear "MessageB"
check_load [[
syntax="proto3";
message Message3
{
repeated int32 v1 = 1;
} ]]
check_load [[
message Message2
{
repeated int32 v1 = 1;
} ]]
local t = { v1 = {1,2,3,4,5} }
local bytes = pb.encode("Message2", t)
eq(pb.decode("Message3", bytes), t)
bytes = pb.encode("Message3", t)
eq(pb.decode("Message2", bytes), t)
pb.clear "Message2"
pb.clear "Message3"
pb.option "auto_default_values"
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_map()
check_load [[
syntax = "proto3";
message TestEmpty {}
message TestNum {
int32 f = 1;
}
message TestMap {
map<string, int32> map = 1;
map<string, int32> packed_map = 2 [packed=true];
map<string, TestEmpty> msg_map = 3;
} ]]
check_msg("TestMap", { map = {}, packed_map = {}, msg_map = {} })
check_msg(".TestMap", {
map = { one = 1, two = 2, three = 3 };
packed_map = { one = 1, two = 2, three = 3 }
}, {
map = { one = 1, two = 2, three = 3 };
packed_map = { one = 1, two = 2, three = 3 };
msg_map = {}
})
local data2 = { map = { one = 1, [1]=1 } }
fail("string expected for field 'key', got number", function()
local chunk = pb.encode("TestMap", data2)
table_eq(pb.decode("TestMap", chunk), {
map = {one = 1},
packed_map = {},
msg_map = {},
})
end)
fail("type mismatch for repeated field 'map' at offset 2,"..
" bytes expected for type message, got varint", function()
local chunk = pb.encode("TestNum", {f = 123})
pb.decode("TestMap", chunk)
end)
eq(pb.decode("TestMap", "\10\4\3\10\1\1"), {
map = {["\1"] = 0},
packed_map = {},
msg_map = {},
})
eq(pb.decode("TestMap", "\10\0"), {
map = { [""] = 0 },
packed_map = {},
msg_map = {}
})
eq(pb.decode("TestMap", "\26\0"), {
map = {},
packed_map = {},
msg_map = {[""] = {}}
})
check_load [[
syntax = "proto2";
message TestMap2 {
map<string, int32> map = 1;
} ]]
check_msg("TestMap2", { map = { one = 1, two = 2, three = 3 } })
end
function _G.test_oneof()
check_load [[
syntax = "proto3";
message TO_M1 {
}
message TO_M2 {
}
message TO_M3 {
int32 value = 1;
}
message TestOneof {
oneof body_oneof {
TO_M1 m1 = 100;
TO_M2 m2 = 200;
TO_M3 m3 = 300;
}
} ]]
check_msg("TestOneof", {})
check_msg("TestOneof", { m1 = {}, body_oneof = "m1" })
check_msg("TestOneof", { m2 = {}, body_oneof = "m2" })
check_msg("TestOneof", { m3 = { value = 0 }, body_oneof = "m3" })
check_msg("TestOneof", { m3 = { value = 10 }, body_oneof = "m3" })
pb.clear "TestOneof"
check_load [[
syntax = "proto3";
message TestOneof {
oneof body {
uint32 foo = 1;
string bar = 2;
}
}
message Outter {
TestOneof msg = 1;
}
]]
check_msg("TestOneof", { foo = 0, body = "foo" })
check_msg("TestOneof", { bar = "", body = "bar" })
local chunk = pb.encode("TestOneof", { foo = 0, bar = "" })
local data = pb.decode("TestOneof", chunk)
is_true(data.body == "foo" or data.body == "bar")
check_msg("Outter", { msg = { foo = 0, body = "foo" }})
check_msg("Outter", { msg = { bar = "", body = "bar" }})
local chunk = pb.encode("Outter", {msg = { foo = 0, bar = "" }})
local data = pb.decode("Outter", chunk)
is_true(data.msg.body == "foo" or data.msg.body == "bar")
pb.clear "TestOneof"
pb.clear "Outter"
check_load [[
syntax = "proto3";
message TestOneof {
oneof test_oneof {
string name = 4;
int32 value = 5;
}
} ]]
check_msg("TestOneof", { name = "foo", test_oneof = "name" })
check_msg("TestOneof", { value = 0, test_oneof = "value" })
local chunk = pb.encode("TestOneof", { name = "foo", value = 0 })
local data = pb.decode("TestOneof", chunk)
is_true(data.test_oneof == "name" or data.test_oneof == "value")
eq(pb.field("TestOneof", "name"), "name")
pb.clear("TestOneof", "name")
eq(pb.field("TestOneof", "name"), nil)
eq(pb.type "TestOneof", ".TestOneof")
pb.clear "TestOneof"
eq(pb.type "TestOneof", nil)
pb.option "auto_default_values"
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_conv()
eq(conv.encode_uint32(-1), 0xFFFFFFFF)
eq(conv.decode_uint32(0xFFFFFFFF), 0xFFFFFFFF)
eq(conv.decode_uint32(conv.encode_uint32(-1)), 0xFFFFFFFF)
pb.option "int64_as_string"
eq(conv.encode_int32(-1), "#18446744073709551615")
pb.option "int64_as_number"
eq(conv.encode_int32(0x12300000123), 0x123)
eq(conv.encode_int32(0xFFFFFFFF), -1)
eq(conv.encode_int32(0x123FFFFFFFF), -1)
eq(conv.encode_int32(0x123FFFFFFFE), -2)
eq(conv.decode_int32(0x12300000123), 0x123)
eq(conv.decode_int32(0xFFFFFFFF), -1)
eq(conv.decode_int32(0x123FFFFFFFF), -1)
eq(conv.decode_int32(0x123FFFFFFFE), -2)
eq(conv.decode_int32(conv.encode_int32(-1)), -1)
eq(conv.encode_sint32(0), 0)
eq(conv.encode_sint32(-1), 1)
eq(conv.encode_sint32(1), 2)
eq(conv.encode_sint32(-2), 3)
eq(conv.encode_sint32(2), 4)
eq(conv.encode_sint32(-3), 5)
eq(conv.encode_sint32(-123), 245)
eq(conv.encode_sint32(123), 246)
eq(conv.encode_sint64(-123), 245)
eq(conv.encode_sint64(123), 246)
eq(conv.decode_sint32(0), 0)
eq(conv.decode_sint32(1), -1)
eq(conv.decode_sint32(2), 1)
eq(conv.decode_sint32(3), -2)
eq(conv.decode_sint32(4), 2)
eq(conv.decode_sint32(5), -3)
eq(conv.decode_sint32(245), -123)
eq(conv.decode_sint32(246), 123)
eq(conv.decode_sint32(0xFFFFFFFF), -0x80000000)
eq(conv.decode_sint32(0xFFFFFFFE), 0x7FFFFFFF)
eq(conv.decode_sint64(0xFFFFFFFF), -0x80000000)
eq(conv.decode_sint64(0xFFFFFFFE), 0x7FFFFFFF)
eq(conv.decode_float(conv.encode_float(123.125)), 123.125)
eq(conv.decode_double(conv.encode_double(123.125)), 123.125)
pb.option "int64_as_string"
eq(conv.decode_sint64(conv.encode_sint64("#1311768467294899695")), "#1311768467294899695")
pb.option "int64_as_hexstring"
eq(conv.decode_sint64(conv.encode_sint64("#0x1234567890ABCDEF")), "#0x1234567890ABCDEF")
pb.option "int64_as_number"
if _VERSION == "Lua 5.3" then
eq(conv.decode_sint64(conv.encode_sint64("#0x1234567890ABCDEF")), 0x1234567890ABCDEF)
else
assert(conv.decode_sint64(conv.encode_sint64("#0x1234567890ABCDEF")))
end
eq(conv.encode_sint32('---1'), 1)
fail("number/string expected, got boolean", function() conv.encode_sint64(true) end)
fail("integer format error: '@xyz'", function() conv.encode_sint64('@xyz') end)
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_buffer()
eq(buffer.pack("vvv", 1,2,3), "\1\2\3")
eq(buffer.tohex(buffer.pack("d", 4294967295)), "FF FF FF FF")
if _VERSION == "Lua 5.3" then
eq(buffer.tohex(buffer.pack("q", 9223372036854775807)), "FF FF FF FF FF FF FF 7F")
else
eq(buffer.tohex(buffer.pack("q", "#9223372036854775807")), "FF FF FF FF FF FF FF 7F")
end
eq(buffer.pack("s", "foo"), "\3foo")
eq(buffer.pack("cc", "foo", "bar"), "foobar")
eq(buffer():pack("vvv", 1,2,3):result(), "\1\2\3")
eq(buffer("foo", "bar"):result(), "foobar")
eq(buffer.new("foo", "bar"):result(), "foobar")
eq(pb.fromhex"01 23 456789ABCDEF", "\1\35\69\103\137\171\205\239")
local b = buffer.new()
b:pack("b", true); eq(b:tohex(-1), "01")
b:pack("f", 0.125); eq(b:tohex(-4), "00 00 00 3E")
b:pack("F", 0.125); eq(b:tohex(-8), "00 00 00 00 00 00 C0 3F")
b:pack("i", 4294967295); eq(b:tohex(-10), "FF FF FF FF FF FF FF FF FF 01")
b:pack("j", 4294967295); eq(b:tohex(-1), "01")
b:pack("u", 4294967295); eq(b:tohex(-5), "FF FF FF FF 0F")
b:pack("x", 4294967295); eq(b:tohex(-4), "FF FF FF FF")
b:pack("y", 4294967295); eq(b:tohex(-4), "FF FF FF FF")
if _VERSION == "Lua 5.3" then
b:pack("I", 9223372036854775807); eq(b:tohex(-9), "FF FF FF FF FF FF FF FF 7F")
b:pack("J", 9223372036854775807); eq(b:tohex(-10), "FE FF FF FF FF FF FF FF FF 01")
b:pack("U", 9223372036854775807); eq(b:tohex(-9), "FF FF FF FF FF FF FF FF 7F")
b:pack("X", 9223372036854775807); eq(b:tohex(-8), "FF FF FF FF FF FF FF 7F")
b:pack("Y", 9223372036854775807); eq(b:tohex(-8), "FF FF FF FF FF FF FF 7F")
else
b:pack("I", "#9223372036854775807"); eq(b:tohex(-9), "FF FF FF FF FF FF FF FF 7F")
b:pack("J", "#9223372036854775807"); eq(b:tohex(-10), "FE FF FF FF FF FF FF FF FF 01")
b:pack("U", "#9223372036854775807"); eq(b:tohex(-9), "FF FF FF FF FF FF FF FF 7F")
b:pack("X", "#9223372036854775807"); eq(b:tohex(-8), "FF FF FF FF FF FF FF 7F")
b:pack("Y", "#9223372036854775807"); eq(b:tohex(-8), "FF FF FF FF FF FF FF 7F")
end
assert(#b ~= 0)
assert(#b:reset() == 0)
assert(tostring(b):match 'pb.Buffer')
b = buffer.new "foo"
assert(#b == 3)
b:delete()
assert(#b == 0)
b:pack("vvv", 1,2,3)
assert(#b == 3)
b = buffer.new()
eq(b:pack("(vvv)", 1,2,3):tohex(-4), "03 01 02 03")
eq(b:pack("((vvv))", 1,2,3):tohex(-5), "04 03 01 02 03")
fail("unmatch '(' in format", function() buffer.pack "(" end)
fail("unexpected ')' in format", function() buffer.pack ")" end)
fail("number expected for type 'int32', got string", function() buffer.pack("i", "foo") end)
fail("number expected for type 'int32', got boolean", function() buffer.pack("i", true) end)
fail("invalid formater: '!'", function() buffer.pack '!' end)
b = buffer.new()
eq(b:pack("c", ("a"):rep(1025)):result(), ("a"):rep(1025))
eq(b:pack("c", ("b"):rep(1025)):result(), ("a"):rep(1025)..("b"):rep(1025))
eq(#b, 2050)
b:reset("foo", "bar")
eq(#b, 6)
fail("integer format error: 'foo'", function() buffer.pack("v", "foo") end)
if _VERSION == "Lua 5.3" or _VERSION == "Lua 5.4" then
fail("integer format error", function() buffer.pack("v", 1e308) end)
else
fail("number has no integer representation", function() buffer.pack("v", 1e308) end)
end
b = buffer.new()
fail("encode bytes fail", function() b:pack("#", 10) end)
check_load [[
message Test { optional int32 value = 1 }
]]
local len = #b
eq(#b, 0)
eq(pb.encode("Test", { value = 1 }, b), b)
eq(#b, 2)
b:pack("#", len)
eq(b:tohex(), "02 08 01")
b = buffer.new()
eq(b:pack("i", -1):tohex(), "FF FF FF FF FF FF FF FF FF 01")
assert(pb.type ".google.protobuf.FileDescriptorSet")
end
function _G.test_slice()
local s = slice.new "\3\1\2\3"
eq(#s, 4)
eq(s:level(), 1)
eq(s:level(1), 1)
eq(s:level(2), nil)
eq({s:level(-1)}, {1,1,4})
eq(s:enter(), s)
eq(s:level(), 2)
eq({s:level(-1)}, {2,2,4})
eq({s:level(1)}, {5,1,4})
eq({s:unpack "vvv"}, {1,2,3})
eq(s:unpack "v", nil)
eq(s:leave(), s)
eq(s:unpack("+s", -4), "\1\2\3")
s = slice "\3\1\2\3"
for i = 2, 20 do
s:enter(1, 4); eq(s:level(), i)
end
for i = 19, 1 do
s:leave(); eq(s:level(), i)
end
eq(s:tohex(), "03 01 02 03")
eq(s:result(-3), "\1\2\3")
eq(#s, 4)
eq(#s:reset(), 0)
eq(#s:reset"foo", 3)
eq({slice.unpack("\255\1", "v@")}, { 255, 3 })
eq({slice.unpack("\1", "v*v", 1)}, { 1, 1 })
fail("invalid formater: '!'", function() slice.unpack("\1", '!') end)
table_eq({slice.unpack("\1\2\3", "vvv")}, {1,2,3})
eq(slice.unpack("\255\255\255\255", "d"), 4294967295)
if _VERSION == "Lua 5.3" then
eq(slice.unpack("\255\255\255\255\255\255\255\127", "q"), 9223372036854775807)
else
pb.option 'int64_as_string'
eq(slice.unpack("\255\255\255\255\255\255\255\127", "q"), '#9223372036854775807')
pb.option 'int64_as_number'
end
eq(slice.unpack("\3foo", "s"), "foo")
eq({slice.unpack("foobar", "cc", 3, 3)}, {"foo", "bar"})
eq(slice.unpack("\255\255\255\127\255", "v"), 0xFFFFFFF)
fail("invalid varint value at offset 1", function()
slice.unpack(("\255"):rep(10), "v") end)
fail("invalid varint value at offset 1", function() slice.unpack("\255\255\255", "v") end)
fail("invalid varint value at offset 1", function() slice.unpack("\255\255\255", "v") end)
fail("invalid bytes value at offset 1", function() slice.unpack("\3\1\2", "s") end)
fail("invalid fixed32 value at offset 1", function() slice.unpack("\1\2\3", "d") end)
fail("invalid fixed64 value at offset 1", function() slice.unpack("\1\2\3", "q") end)
fail("invalid sub string at offset 1", function() slice.unpack("\3\1\2", "c", 5) end)