-
Notifications
You must be signed in to change notification settings - Fork 377
/
socket.test.lua
921 lines (812 loc) · 22.6 KB
/
socket.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
json = require 'json'
yaml = require 'yaml'
pickle = require 'pickle'
socket = require 'socket'
fiber = require 'fiber'
msgpack = require 'msgpack'
log = require 'log'
errno = require 'errno'
fio = require 'fio'
ffi = require('ffi')
type(socket)
env = require('test_run')
test_run = env.new()
test_run:cmd("push filter '(error: .builtin/.*[.]lua):[0-9]+' to '\\1'")
socket('PF_INET', 'SOCK_STREAM', 'tcp121222');
s = socket('PF_INET', 'SOCK_STREAM', 'tcp')
type(s)
-- Invalid arguments
test_run:cmd("setopt delimiter ';'")
for k in pairs(getmetatable(s).__index) do
local r, msg = pcall(s[k])
if not msg:match('Usage:') then
error("Arguments is not checked for "..k)
end
end;
s:close();
test_run:cmd("setopt delimiter ''");
LISTEN = require('uri').parse(box.cfg.listen)
LISTEN ~= nil
s = socket.tcp_connect(LISTEN.host, LISTEN.service)
s:nonblock(true)
s:nonblock()
s:nonblock(false)
s:nonblock()
s:nonblock(true)
s:readable(.01)
s:wait(.01)
socket.iowait(s:fd(), 'RW')
socket.iowait(s:fd(), 3)
socket.iowait(s:fd(), 'R')
socket.iowait(s:fd(), 'r')
socket.iowait(s:fd(), 1)
socket.iowait(s:fd(), 'W')
socket.iowait(s:fd(), 'w')
socket.iowait(s:fd(), 2)
socket.iowait(s:fd(), '')
socket.iowait(s:fd(), -1)
socket.iowait(s:fd(), 'RW')
socket.iowait(s:fd(), 'RW', -100500)
s:readable(0)
s:errno() > 0
s:error()
s:writable(.00000000000001)
s:writable(0)
s:wait(.01)
socket.iowait(nil, nil, -1)
socket.iowait(nil, nil, 0.0001)
socket.iowait(-1, nil, 0.0001)
socket.iowait(nil, 'RW')
socket.iowait(0, nil)
handshake = ffi.new('char[128]')
-- test sysread with char *
s:sysread(handshake, 128)
ffi.string(handshake, 9)
ping = msgpack.encode({ [0] = 64, [1] = 0 })
ping = msgpack.encode(string.len(ping)) .. ping
-- test syswrite with char *
s:syswrite(ffi.cast('const char *', ping), #ping)
s:readable(1)
s:wait(.01)
pong = s:sysread()
string.len(pong)
msgpack.decode(pong)
function remove_schema_id(t, x) if t[5] then t[5] = 'XXX' end return t, x end
remove_schema_id(msgpack.decode(pong, 6))
s:close()
s = socket('PF_INET', 'SOCK_STREAM', 'tcp')
s:setsockopt('SOL_SOCKET', 'SO_REUSEADDR', true)
s:error()
s:bind('127.0.0.1', 0)
s:error()
s:listen(128)
sevres = {}
type(require('fiber').create(function() s:readable() do local sc = s:accept() table.insert(sevres, sc) sc:syswrite('ok') sc:close() end end))
#sevres
sc = socket('PF_INET', 'SOCK_STREAM', 'tcp')
sc:nonblock(false)
sc:sysconnect('127.0.0.1', s:name().port)
sc:nonblock(true)
sc:readable(.5)
sc:sysread()
string.match(tostring(sc), ', peer') ~= nil
#sevres
sevres[1].host
s:setsockopt('SOL_SOCKET', 'SO_BROADCAST', false)
s:getsockopt('socket', 'SO_TYPE')
s:error()
s:setsockopt('SOL_SOCKET', 'SO_DEBUG', false)
s:getsockopt('socket', 'SO_DEBUG')
s:setsockopt('SOL_SOCKET', 'SO_ACCEPTCONN', 1)
s:getsockopt('SOL_SOCKET', 'SO_RCVBUF') > 32
s:error()
s:setsockopt('IPPROTO_TCP', 'TCP_NODELAY', true)
s:getsockopt('IPPROTO_TCP', 'TCP_NODELAY') > 0
s:setsockopt('SOL_TCP', 'TCP_NODELAY', false)
s:getsockopt('SOL_TCP', 'TCP_NODELAY') == 0
s:setsockopt('tcp', 'TCP_NODELAY', true)
s:getsockopt('tcp', 'TCP_NODELAY') > 0
s:setsockopt(6, 'TCP_NODELAY', false)
s:getsockopt(6, 'TCP_NODELAY') == 0
not s:setsockopt(nil, 'TCP_NODELAY', true) and errno() == errno.EINVAL
not s:getsockopt(nil, 'TCP_NODELAY') and errno() == errno.EINVAL
s:linger()
s:linger(true, 1)
s:linger()
s:linger(false, 1)
s:linger()
s:close()
s = socket('PF_INET', 'SOCK_STREAM', 'tcp')
s:setsockopt('SOL_SOCKET', 'SO_REUSEADDR', true)
s:bind('127.0.0.1', 0)
s:listen(128)
sc = socket('PF_INET', 'SOCK_STREAM', 'tcp')
sc:sysconnect('127.0.0.1', s:name().port) or errno() == errno.EINPROGRESS
sc:writable(10)
sc:write('Hello, world')
sa, addr = s:accept()
addr2 = sa:name()
addr2.host == addr.host
addr2.family == addr.family
sa:nonblock(1)
sa:read(8)
sa:read(3)
sc:writable()
sc:write(', again')
sa:read(8)
sa:error()
string.len(sa:read(0))
type(sa:read(0))
sa:read(1, .01)
sc:writable()
sc:send('abc')
sa:read(3)
sc:send('Hello')
sa:readable()
sa:recv()
sa:recv()
sc:send('Hello')
sc:send(', world')
sc:send("\\nnew line")
sa:read('\\n', 1)
sa:read({ chunk = 1, delimiter = 'ine'}, 1)
sa:read('ine', 1)
sa:read('ine', 0.1)
sc:send('Hello, world')
sa:read(',', 1)
sc:shutdown('W')
sa:close()
sc:close()
s = socket('PF_UNIX', 'SOCK_STREAM', 0)
s:setsockopt('SOL_SOCKET', 'SO_REUSEADDR', true)
s ~= nil
s:nonblock()
s:nonblock(true)
s:nonblock()
os.remove('/tmp/tarantool-test-socket')
s:bind('unix/', '/tmp/tarantool-test-socket')
sc ~= nil
s:listen(1234)
sc = socket('PF_UNIX', 'SOCK_STREAM', 0)
sc:nonblock(true)
sc:sysconnect('unix/', '/tmp/tarantool-test-socket')
sc:error()
s:readable()
sa = s:accept()
sa:nonblock(true)
sa:send('Hello, world')
sc:recv()
sc:close()
sa:close()
s:close()
_ = os.remove('/tmp/tarantool-test-socket')
test_run:cmd("setopt delimiter ';'")
function aexitst(ai, hostnames, port)
for i, a in pairs(ai) do
for j, host in pairs(hostnames) do
if a.host == host and a.port == port then
return true
end
end
end
return ai
end;
aexitst( socket.getaddrinfo('localhost', 'http', { protocol = 'tcp',
type = 'SOCK_STREAM'}), {'127.0.0.1', '::1'}, 80 );
test_run:cmd("setopt delimiter ''");
wrong_addr = socket.getaddrinfo('non-existing-domain-name-12211alklkl.com', 'http', {})
wrong_addr == nil or #wrong_addr == 0
sc = socket('PF_INET', 'SOCK_STREAM', 'tcp')
sc ~= nil
sc:getsockopt('SOL_SOCKET', 'SO_ERROR')
sc:nonblock(true)
sc:sysconnect('127.0.0.1', 3458) or errno() == errno.EINPROGRESS or errno() == errno.ECONNREFUSED
string.match(tostring(sc), ', peer') == nil
sc:writable()
string.match(tostring(sc), ', peer') == nil
socket_error = sc:getsockopt('SOL_SOCKET', 'SO_ERROR')
socket_error == errno.ECONNREFUSED or socket_error == 0
test_run:cmd("setopt delimiter ';'")
inf = socket.getaddrinfo('127.0.0.1', '80', { type = 'SOCK_DGRAM',
flags = { 'AI_NUMERICSERV', 'AI_NUMERICHOST', } });
-- musl libc https://github.com/tarantool/tarantool/issues/1249
inf[1].canonname = nil;
inf;
test_run:cmd("setopt delimiter ''");
sc = socket('AF_INET', 'SOCK_STREAM', 'tcp')
json.encode(sc:name())
sc:name()
sc:nonblock(true)
sc:close()
s = socket('AF_INET', 'SOCK_DGRAM', 'udp')
s:bind('127.0.0.1', 0)
sc = socket('AF_INET', 'SOCK_DGRAM', 'udp')
sc:sendto('127.0.0.1', s:name().port, 'Hello, world')
s:readable(10)
s:recv()
sc:sendto('127.0.0.1', s:name().port, 'Hello, world, 2')
s:readable(10)
d, from = s:recvfrom()
from.port > 0
from.port = 'Random port'
json.encode{d, from}
s:close()
sc:close()
s = socket('AF_INET', 'SOCK_DGRAM', 'udp')
s:nonblock(true)
s:bind('127.0.0.1')
s:name().port > 0
sc = socket('AF_INET', 'SOCK_DGRAM', 'udp')
sc:nonblock(true)
sc:sendto('127.0.0.1', s:name().port)
sc:sendto('127.0.0.1', s:name().port, 'Hello, World!')
s:readable(1)
data, from = s:recvfrom(10)
data
s:sendto(from.host, from.port, 'Hello, hello!')
sc:readable(1)
data_r, from_r = sc:recvfrom()
data_r
from_r.host
from_r.port == s:name().port
s:close()
sc:close()
-- tcp_connect
-- test timeout
socket.tcp_connect('127.0.0.1', 80, 0.00000000001)
-- AF_INET
s = socket('AF_INET', 'SOCK_STREAM', 'tcp')
s:bind('127.0.0.1', 0)
port = s:name().port
s:listen()
sc, e = socket.tcp_connect('127.0.0.1', port), errno()
sc ~= nil
e == 0
sc:close()
s:close()
socket.tcp_connect('127.0.0.1', port), errno() == errno.ECONNREFUSED
-- AF_UNIX
path = '/tmp/tarantool-test-socket'
_ = os.remove(path)
s = socket('AF_UNIX', 'SOCK_STREAM', 0)
s:bind('unix/', path)
socket.tcp_connect('unix/', path), errno() == errno.ECONNREFUSED
s:listen()
sc, e = socket.tcp_connect('unix/', path), errno()
sc ~= nil
e
sc:close()
s:close()
socket.tcp_connect('unix/', path), errno() == errno.ECONNREFUSED
_ = os.remove(path)
socket.tcp_connect('unix/', path), errno() == errno.ENOENT
-- invalid fd / tampering
s = socket('AF_INET', 'SOCK_STREAM', 'tcp')
s:read(9)
s:close()
s._gc_socket.fd = 512
s._gc_socket = nil
tostring(s)
s = nil
-- close
serv = socket('AF_INET', 'SOCK_STREAM', 'tcp')
serv:setsockopt('SOL_SOCKET', 'SO_REUSEADDR', true)
serv:bind('127.0.0.1', port)
port = serv:name().port
serv:listen()
test_run:cmd("setopt delimiter ';'")
f = fiber.create(function(serv)
serv:readable()
sc = serv:accept()
sc:write("Tarantool test server")
sc:shutdown()
sc:close()
serv:close()
end, serv);
test_run:cmd("setopt delimiter ''");
s = socket.tcp_connect('127.0.0.1', port)
ch = fiber.channel()
f = fiber.create(function() s:read(12) ch:put(true) end)
s:close()
ch:get(1)
s:error()
-- random port
master = socket('PF_INET', 'SOCK_STREAM', 'tcp')
master:setsockopt('SOL_SOCKET', 'SO_REUSEADDR', true)
port = 32768 + math.random(32768)
attempt = 0
test_run:cmd("setopt delimiter ';'")
while attempt < 10 do
if not master:bind('127.0.0.1', port) then
port = 32768 + math.random(32768)
attempt = attempt + 1
else
break
end
end;
master:listen();
function gh361()
local s = socket('PF_INET', 'SOCK_STREAM', 'tcp')
s:sysconnect('127.0.0.1', port)
s:wait()
res = s:read(1200)
end;
test_run:cmd("setopt delimiter ''");
f = fiber.create(gh361)
fiber.cancel(f)
while f:status() ~= 'dead' do fiber.sleep(0.001) end
master:close()
f = nil
path = '/tmp/tarantool-test-socket'
s = socket('PF_UNIX', 'SOCK_STREAM', 0)
s:setsockopt('SOL_SOCKET', 'SO_REUSEADDR', true)
s:error()
s:bind('unix/', path)
s:error()
s:listen(128)
test_run:cmd("setopt delimiter ';'")
f = fiber.create(function()
for i=1,2 do
s:readable()
local sc = s:accept()
sc:write('ok!')
sc:shutdown()
sc:close()
end
end);
test_run:cmd("setopt delimiter ''");
c = socket.tcp_connect('unix/', path)
c:error()
x = c:read('!')
x, type(x), #x
x = c:read('!')
c:error()
x, type(x), #x
x = c:read('!')
c:error()
x, type(x), #x
c:close()
c = socket.tcp_connect('unix/', path)
c:error()
x = c:read(3)
c:error()
x, type(x), #x
x = c:read(1)
c:error()
x, type(x), #x
x = c:read(1)
c:error()
x, type(x), #x
x = c:sysread(1)
c:error()
x, type(x), #x
c:close()
s:close()
_ = os.remove(path)
server, addr = socket.tcp_server('unix/', path, function(s) s:write('Hello, world') end)
type(addr)
server ~= nil
fiber.sleep(.1)
client = socket.tcp_connect('unix/', path)
client ~= nil
client:read(123)
server:close()
-- unix socket automatically removed
while fio.stat(path) ~= nil do fiber.sleep(0.001) end
test_run:cmd("setopt delimiter ';'")
server, addr = socket.tcp_server('localhost', 0, { handler = function(s)
s:read(2)
s:write('Hello, world')
end, name = 'testserv'});
test_run:cmd("setopt delimiter ''");
type(addr)
server ~= nil
addr2 = server:name()
addr.host == addr2.host
addr.family == addr2.family
fiber.sleep(.1)
client = socket.tcp_connect(addr2.host, addr2.port)
client ~= nil
-- Check that listen and client fibers have appropriate names
cnt = 0
test_run:cmd("setopt delimiter ';'")
for _, f in pairs(fiber.info()) do
if f.name:match('^testserv/') then
cnt = cnt + 1
end
end;
test_run:cmd("setopt delimiter ''");
cnt
client:write('hi')
client:read(123)
client:close()
server:close()
longstring = string.rep("abc", 65535)
server = socket.tcp_server('unix/', path, function(s) s:write(longstring) end)
client = socket.tcp_connect('unix/', path)
client:read(#longstring) == longstring
client = socket.tcp_connect('unix/', path)
client:read(#longstring + 1) == longstring
client = socket.tcp_connect('unix/', path)
client:read(#longstring - 1) == string.sub(longstring, 1, #longstring - 1)
longstring = "Hello\r\n\r\nworld\n\n"
client = socket.tcp_connect('unix/', path)
client:read{ line = { "\n\n", "\r\n\r\n" } }
server:close()
-- gh-658: socket:read() incorrectly handles size and delimiter together
body = "a 10\nb 15\nabc"
remaining = #body
test_run:cmd("setopt delimiter ';'")
server = socket.tcp_server('unix/', path, function(s)
s:write(body)
s:read(100500)
end);
test_run:cmd("setopt delimiter ''");
client = socket.tcp_connect('unix/', path)
buf = client:read({ size = remaining, delimiter = "\n"})
buf == "a 10\n"
remaining = remaining - #buf
buf = client:read({ size = remaining, delimiter = "\n"})
buf == "b 15\n"
remaining = remaining - #buf
buf = client:read({ size = remaining, delimiter = "\n"})
buf == "abc"
remaining = remaining - #buf
remaining == 0
buf = client:read({ size = remaining, delimiter = "\n"})
buf == ""
buf = client:read({ size = remaining, delimiter = "\n"})
buf == ""
client:close()
server:close()
_ = os.remove(path)
-- Test that socket is closed on GC
s = socket('AF_UNIX', 'SOCK_STREAM', 0)
s:bind('unix/', path)
s:listen()
s = nil
while socket.tcp_connect('unix/', path) do collectgarbage('collect') end
_ = os.remove(path)
-- Test serializers with sockets
s = socket('AF_UNIX', 'SOCK_STREAM', 0)
-- check __serialize hook
json.decode(json.encode(s)).fd == s:fd()
yaml.decode(yaml.encode(s)).fd == s:fd()
s = nil
-- start AF_UNIX server with dead socket exists
path = '/tmp/tarantool-test-socket'
s = socket('AF_UNIX', 'SOCK_STREAM', 0)
s:bind('unix/', path)
s:close()
s = socket('AF_UNIX', 'SOCK_STREAM', 0)
{ s:bind('unix/', path), errno() == errno.EADDRINUSE }
s:close()
s = socket.tcp_server('unix/', path, function() end)
s ~= nil
s:close()
fio.stat(path) == nil
{ socket.tcp_connect('abrakadabra#123') == nil, errno.strerror() }
-- wrong options for getaddrinfo
socket.getaddrinfo('host', 'port', { type = 'WRONG' }) == nil and errno() == errno.EINVAL
socket.getaddrinfo('host', 'port', { family = 'WRONG' }) == nil and errno() == errno.EINVAL
socket.getaddrinfo('host', 'port', { protocol = 'WRONG' }) == nil and errno() == errno.EPROTOTYPE
socket.getaddrinfo('host', 'port', { flags = 'WRONG' }) == nil and errno() == errno.EINVAL
-- gh-574: check that fiber with getaddrinfo can be safely cancelled
test_run:cmd("setopt delimiter ';'")
f = fiber.create(function()
while true do
local result = socket.getaddrinfo('localhost', '80')
fiber.sleep(0)
end
end);
test_run:cmd("setopt delimiter ''");
f:cancel()
-- gh-3168: a stopped tcp server removes the unix socket created
-- and used by a newly started one.
test_run:cmd("setopt delimiter ';'")
err = nil;
path = '/tmp/tarantool-test-socket';
for i = 1, 10 do
local server = socket.tcp_server('unix/', path, function() end)
if not server then
err = 'tcp_server: ' .. errno.strerror()
break
end
local client = socket.tcp_connect("unix/", path)
if not client then
err = 'tcp_connect: ' .. errno.strerror()
break
end
client:close()
server:close()
end;
err == nil or err;
test_run:cmd("setopt delimiter ''");
--------------------------------------------------------------------------------
-- Lua Socket Emulation
--------------------------------------------------------------------------------
-- {{{
test_run:cmd("push filter 'fd=([0-9]+)' to 'fd=<FD>'")
s = socket.tcp()
s
s:close()
-- Sic: incompatible with Lua Socket
s:close()
s = socket.tcp()
host, port, family = s:getsockname()
host == '0.0.0.0', port == '0', family == 'inet'
status, reason = s:getpeername()
status == nil, type(reason) == 'string'
s:settimeout(100500)
s:setoption('keepalive', true)
s:setoption('linger', { on = true })
s:setoption('linger', true)
s:setoption('reuseaddr', true)
s:setoption('tcp-nodelay', true)
s:setoption('unknown', true)
s:bind('127.0.0.1', 0)
s:bind('127.0.0.1', 0) -- error handling
s:listen(10)
s -- transformed to tcp{server} socket
host, port, family = s:getsockname()
host == '127.0.0.1', type(port) == 'string', family == 'inet'
status, reason = s:getpeername()
status == nil, type(reason) == 'string'
s:settimeout(0)
status, reason = s:accept()
status == nil, type(reason) == 'string'
s:settimeout(0.001)
status, reason = s:accept()
status == nil, type(reason) == 'string'
s:settimeout(100500)
rch, wch = fiber.channel(1), fiber.channel(1)
sc = socket.connect(host, port)
test_run:cmd("setopt delimiter ';'")
cfiber = fiber.create(function(sc, rch, wch)
while sc:send(wch:get()) and rch:put(sc:receive("*l")) do end
end, sc, rch, wch);
test_run:cmd("setopt delimiter ''");
c = s:accept()
c
chost, cport, cfamily = c:getsockname()
chost == '127.0.0.1', type(cport) == 'string', cfamily == 'inet'
chost, cport, cfamily = c:getpeername()
chost == '127.0.0.1', type(cport) == 'string', cfamily == 'inet'
wch:put("Ping\n")
c:receive("*l")
c:send("Pong\n")
rch:get()
wch:put("HELO lua\nMAIL FROM: <roman@tarantool.org>\n")
c:receive("*l")
c:receive("*l")
c:send("250 Welcome to Lua Universe\n")
c:send("$$$250 OK\n$$$", 4, 11)
rch:get()
wch:put("RCPT TO: <team@lua.org>\n")
c:receive()
c:send("250")
c:send(" ")
c:send("OK")
c:send("\n")
rch:get()
wch:put("DATA\n")
c:receive(4)
c:receive("*l")
wch:put("Fu")
c:send("354 Please type your message\n")
sc:close()
c:receive("*l", "Line: ")
c:receive()
c:receive(10)
c:receive("*a")
c:close()
-- eof with bytes
sc = socket.connect(host, port)
sc
c = s:accept()
c
_ = fiber.create(function() sc:send("Po") end)
sc:close()
c:receive(100500, "Message:")
c:close()
-- eof with '*l'
sc = socket.connect(host, port)
sc
c = s:accept()
c
_ = fiber.create(function() sc:send("Pong\nPo") end)
sc:close()
c:receive("*l", "Message:")
c:receive("*l", "Message: ")
c:receive("*l", "Message: ")
c:close()
-- eof with '*a'
sc = socket.connect(host, port)
sc
c = s:accept()
c
_ = fiber.create(function() sc:send("Pong\n") end)
sc:close()
c:receive("*a", "Message: ")
c:receive("*a", "Message: ")
c:close()
-- shutdown
sc = socket.connect(host, port)
sc
c = s:accept()
c
_ = fiber.create(function() sc:send("Pong\n") end)
sc:shutdown("send")
c:receive()
c:shutdown("send")
status, reason = c:shutdown("recv")
status == nil, type(reason) == 'string'
status, reason = c:shutdown("recv")
status == nil, type(reason) == 'string'
status, reason = c:shutdown("both")
status == nil, type(reason) == 'string'
c:close()
sc:close()
s:close()
-- socket.bind / socket.connect
s = socket.bind('0.0.0.0', 0)
s
host, port, family = s:getsockname()
sc = socket.connect(host, port)
sc
sc:close()
sc = socket.tcp()
sc:connect(host, port)
sc:close()
s:close()
test_run:cmd("clear filter")
-- }}}
-- s:is_udp() {{{
s = socket('AF_INET', 'SOCK_DGRAM', 'udp')
s:is_udp() -- expected true
s:close()
s = socket('AF_INET', 'SOCK_STREAM', 0)
s:is_udp() -- expected false
s:close()
-- }}}
-- s:dgram_length() {{{
-- case: tcp socket is not supported
s = socket('AF_INET', 'SOCK_STREAM', 0)
s:dgram_length() -- expected an error
s:close()
receiving_socket = socket('AF_INET', 'SOCK_DGRAM', 'udp')
receiving_socket:bind('127.0.0.1', 0)
receiving_socket_port = receiving_socket:name().port
sending_socket = socket('AF_INET', 'SOCK_DGRAM', 'udp')
-- case: no datagram
received_len = receiving_socket:dgram_length()
e = receiving_socket:errno()
received_len == nil -- expected true
received_len
e == errno.EAGAIN -- expected true
e
message_len = 0
message = ''
-- case: zero-length datagram (the same as no datagram)
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_len = receiving_socket:dgram_length()
e = receiving_socket:errno()
received_len == nil -- expected true
received_len
e == errno.EAGAIN -- expected true
e
message_len = 1025
message = string.rep('x', message_len)
-- case: non-zero length datagram
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_len = receiving_socket:dgram_length()
e = receiving_socket:errno()
received_len == message_len -- expected true
received_len
e == 0 -- expected true
e
receiving_socket:close()
sending_socket:close()
-- }}}
-- gh-3619: socket.recvfrom crops UDP packets {{{
receiving_socket = socket('AF_INET', 'SOCK_DGRAM', 'udp')
receiving_socket:bind('127.0.0.1', 0)
receiving_socket_port = receiving_socket:name().port
sending_socket = socket('AF_INET', 'SOCK_DGRAM', 'udp')
-- case: recv, no datagram
received_message = receiving_socket:recv()
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
e == errno.EAGAIN -- expected true
e
-- case: recvfrom, no datagram
received_message, from = receiving_socket:recvfrom()
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
from == nil -- expected true
from
e == errno.EAGAIN -- expected true
e
message_len = 0
message = ''
-- case: recv, zero-length datagram (the same as no datagram)
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message = receiving_socket:recv()
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
e == errno.EAGAIN -- expected true
e
-- case: recvfrom, zero-length datagram (the same as no datagram)
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message, from = receiving_socket:recvfrom()
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
from == nil -- expected true
from
e == errno.EAGAIN -- expected true
e
-- case: recv, zero-length datagram (the same as no datagram), explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message = receiving_socket:recv(512)
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
e == errno.EAGAIN -- expected true
e
-- case: recvfrom, zero-length datagram (the same as no datagram), explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message, from = receiving_socket:recvfrom(512)
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
from == nil -- expected true
from
e == errno.EAGAIN -- expected true
e
message_len = 1025
message = string.rep('x', message_len)
-- case: recv, non-zero length datagram
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message = receiving_socket:recv()
e = receiving_socket:errno()
received_message == message -- expected true
received_message:len()
received_message
e == 0 -- expected true
e
-- case: recvfrom, non-zero length datagram
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message, from = receiving_socket:recvfrom()
e = receiving_socket:errno()
received_message == message -- expected true
received_message:len()
received_message
from ~= nil -- expected true
from.host == '127.0.0.1' -- expected true
e == 0 -- expected true
e
-- case: recv, datagram that is larger then the buffer of an explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message = receiving_socket:recv(512)
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
e == errno.EMSGSIZE -- expected true
e
-- case: recvfrom, datagram that is larger then the buffer of an explicit size
sending_socket:sendto('127.0.0.1', receiving_socket_port, message)
received_message, from = receiving_socket:recvfrom(512)
e = receiving_socket:errno()
received_message == nil -- expected true
received_message
from ~= nil -- expected true
from.host == '127.0.0.1' -- expected true
e == errno.EMSGSIZE -- expected true
e
receiving_socket:close()
sending_socket:close()
-- }}}