comparison src/testdir/test_channel.vim @ 28317:d32dc906dd2c v8.2.4684

patch 8.2.4684: cannot open a channel on a Unix domain socket Commit: https://github.com/vim/vim/commit/cc766a85f460ebb7f8c915508447548b5f5b99bc Author: LemonBoy <thatlemon@gmail.com> Date: Mon Apr 4 15:46:58 2022 +0100 patch 8.2.4684: cannot open a channel on a Unix domain socket Problem: Cannot open a channel on a Unix domain socket. Solution: Add Unix domain socket support. (closes https://github.com/vim/vim/issues/10062)
author Bram Moolenaar <Bram@vim.org>
date Mon, 04 Apr 2022 17:00:04 +0200
parents 85b07a942518
children 3beb14b12bbd
comparison
equal deleted inserted replaced
28316:6ce57599817a 28317:d32dc906dd2c
21 21
22 func SetUp() 22 func SetUp()
23 if g:testfunc =~ '_ipv6()$' 23 if g:testfunc =~ '_ipv6()$'
24 let s:localhost = '[::1]:' 24 let s:localhost = '[::1]:'
25 let s:testscript = 'test_channel_6.py' 25 let s:testscript = 'test_channel_6.py'
26 elseif g:testfunc =~ '_unix()$'
27 let s:localhost = 'unix:Xtestsocket'
28 let s:testscript = 'test_channel_unix.py'
26 else 29 else
27 let s:localhost = 'localhost:' 30 let s:localhost = 'localhost:'
28 let s:testscript = 'test_channel.py' 31 let s:testscript = 'test_channel.py'
29 endif 32 endif
30 let s:chopt = {} 33 let s:chopt = {}
35 endfunc 38 endfunc
36 39
37 " Run "testfunc" after starting the server and stop the server afterwards. 40 " Run "testfunc" after starting the server and stop the server afterwards.
38 func s:run_server(testfunc, ...) 41 func s:run_server(testfunc, ...)
39 call RunServer(s:testscript, a:testfunc, a:000) 42 call RunServer(s:testscript, a:testfunc, a:000)
43 endfunc
44
45 " Returns the address of the test server.
46 func s:address(port)
47 if s:localhost =~ '^unix:'
48 return s:localhost
49 else
50 return s:localhost . a:port
51 end
40 endfunc 52 endfunc
41 53
42 " Return a list of open files. 54 " Return a list of open files.
43 " Can be used to make sure no resources leaked. 55 " Can be used to make sure no resources leaked.
44 " Returns an empty list on systems where this is not supported. 56 " Returns an empty list on systems where this is not supported.
63 func Ch_communicate(port) 75 func Ch_communicate(port)
64 " Avoid dropping messages, since we don't use a callback here. 76 " Avoid dropping messages, since we don't use a callback here.
65 let s:chopt.drop = 'never' 77 let s:chopt.drop = 'never'
66 " Also add the noblock flag to try it out. 78 " Also add the noblock flag to try it out.
67 let s:chopt.noblock = 1 79 let s:chopt.noblock = 1
68 let handle = ch_open(s:localhost . a:port, s:chopt) 80 let handle = ch_open(s:address(a:port), s:chopt)
69 if ch_status(handle) == "fail" 81 if ch_status(handle) == "fail"
70 call assert_report("Can't open channel") 82 call assert_report("Can't open channel")
71 return 83 return
72 endif 84 endif
73 85
75 call assert_equal('no process', string(ch_getjob(handle))) 87 call assert_equal('no process', string(ch_getjob(handle)))
76 88
77 let dict = handle->ch_info() 89 let dict = handle->ch_info()
78 call assert_true(dict.id != 0) 90 call assert_true(dict.id != 0)
79 call assert_equal('open', dict.status) 91 call assert_equal('open', dict.status)
80 call assert_equal(a:port, string(dict.port)) 92 if has_key(dict, 'port')
93 " Channels using Unix sockets have no 'port' entry.
94 call assert_equal(a:port, string(dict.port))
95 end
81 call assert_equal('open', dict.sock_status) 96 call assert_equal('open', dict.sock_status)
82 call assert_equal('socket', dict.sock_io) 97 call assert_equal('socket', dict.sock_io)
83 98
84 " Simple string request and reply. 99 " Simple string request and reply.
85 call assert_equal('got it', ch_evalexpr(handle, 'hello!')) 100 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
250 call s:run_server('Ch_communicate') 265 call s:run_server('Ch_communicate')
251 endfunc 266 endfunc
252 267
253 func Test_communicate_ipv6() 268 func Test_communicate_ipv6()
254 CheckIPv6 269 CheckIPv6
255
256 call Test_communicate() 270 call Test_communicate()
257 endfunc 271 endfunc
272
273 func Test_communicate_unix()
274 CheckUnix
275 call Test_communicate()
276 call delete('Xtestsocket')
277 endfunc
278
258 279
259 " Test that we can open two channels. 280 " Test that we can open two channels.
260 func Ch_two_channels(port) 281 func Ch_two_channels(port)
261 let handle = ch_open(s:localhost . a:port, s:chopt) 282 let handle = ch_open(s:address(a:port), s:chopt)
262 call assert_equal(v:t_channel, type(handle)) 283 call assert_equal(v:t_channel, type(handle))
263 if handle->ch_status() == "fail" 284 if handle->ch_status() == "fail"
264 call assert_report("Can't open channel") 285 call assert_report("Can't open channel")
265 return 286 return
266 endif 287 endif
267 288
268 call assert_equal('got it', ch_evalexpr(handle, 'hello!')) 289 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
269 290
270 let newhandle = ch_open(s:localhost . a:port, s:chopt) 291 let newhandle = ch_open(s:address(a:port), s:chopt)
271 if ch_status(newhandle) == "fail" 292 if ch_status(newhandle) == "fail"
272 call assert_report("Can't open second channel") 293 call assert_report("Can't open second channel")
273 return 294 return
274 endif 295 endif
275 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!')) 296 call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
290 func Test_two_channels_ipv6() 311 func Test_two_channels_ipv6()
291 CheckIPv6 312 CheckIPv6
292 call Test_two_channels() 313 call Test_two_channels()
293 endfunc 314 endfunc
294 315
316 func Test_two_channels_unix()
317 CheckUnix
318 call Test_two_channels()
319 call delete('Xtestsocket')
320 endfunc
321
295 " Test that a server crash is handled gracefully. 322 " Test that a server crash is handled gracefully.
296 func Ch_server_crash(port) 323 func Ch_server_crash(port)
297 let handle = ch_open(s:localhost . a:port, s:chopt) 324 let handle = ch_open(s:address(a:port), s:chopt)
298 if ch_status(handle) == "fail" 325 if ch_status(handle) == "fail"
299 call assert_report("Can't open channel") 326 call assert_report("Can't open channel")
300 return 327 return
301 endif 328 endif
302 329
310 endfunc 337 endfunc
311 338
312 func Test_server_crash_ipv6() 339 func Test_server_crash_ipv6()
313 CheckIPv6 340 CheckIPv6
314 call Test_server_crash() 341 call Test_server_crash()
342 endfunc
343
344 func Test_server_crash_unix()
345 CheckUnix
346 call Test_server_crash()
347 call delete('Xtestsocket')
315 endfunc 348 endfunc
316 349
317 """"""""" 350 """""""""
318 351
319 func Ch_handler(chan, msg) 352 func Ch_handler(chan, msg)
321 unlet g:Ch_reply 354 unlet g:Ch_reply
322 let g:Ch_reply = a:msg 355 let g:Ch_reply = a:msg
323 endfunc 356 endfunc
324 357
325 func Ch_channel_handler(port) 358 func Ch_channel_handler(port)
326 let handle = ch_open(s:localhost . a:port, s:chopt) 359 let handle = ch_open(s:address(a:port), s:chopt)
327 if ch_status(handle) == "fail" 360 if ch_status(handle) == "fail"
328 call assert_report("Can't open channel") 361 call assert_report("Can't open channel")
329 return 362 return
330 endif 363 endif
331 364
350 func Test_channel_handler_ipv6() 383 func Test_channel_handler_ipv6()
351 CheckIPv6 384 CheckIPv6
352 call Test_channel_handler() 385 call Test_channel_handler()
353 endfunc 386 endfunc
354 387
388 func Test_channel_handler_unix()
389 CheckUnix
390 call Test_channel_handler()
391 call delete('Xtestsocket')
392 endfunc
393
355 """"""""" 394 """""""""
356 395
357 let g:Ch_reply = '' 396 let g:Ch_reply = ''
358 func Ch_zeroHandler(chan, msg) 397 func Ch_zeroHandler(chan, msg)
359 unlet g:Ch_reply 398 unlet g:Ch_reply
365 unlet g:Ch_zero_reply 404 unlet g:Ch_zero_reply
366 let g:Ch_zero_reply = a:msg 405 let g:Ch_zero_reply = a:msg
367 endfunc 406 endfunc
368 407
369 func Ch_channel_zero(port) 408 func Ch_channel_zero(port)
370 let handle = (s:localhost .. a:port)->ch_open(s:chopt) 409 let handle = (s:address(a:port))->ch_open(s:chopt)
371 if ch_status(handle) == "fail" 410 if ch_status(handle) == "fail"
372 call assert_report("Can't open channel") 411 call assert_report("Can't open channel")
373 return 412 return
374 endif 413 endif
375 414
413 func Test_zero_reply_ipv6() 452 func Test_zero_reply_ipv6()
414 CheckIPv6 453 CheckIPv6
415 call Test_zero_reply() 454 call Test_zero_reply()
416 endfunc 455 endfunc
417 456
457 func Test_zero_reply_unix()
458 CheckUnix
459 call Test_zero_reply()
460 call delete('Xtestsocket')
461 endfunc
462
463
418 """"""""" 464 """""""""
419 465
420 let g:Ch_reply1 = "" 466 let g:Ch_reply1 = ""
421 func Ch_handleRaw1(chan, msg) 467 func Ch_handleRaw1(chan, msg)
422 unlet g:Ch_reply1 468 unlet g:Ch_reply1
434 unlet g:Ch_reply3 480 unlet g:Ch_reply3
435 let g:Ch_reply3 = a:msg 481 let g:Ch_reply3 = a:msg
436 endfunc 482 endfunc
437 483
438 func Ch_raw_one_time_callback(port) 484 func Ch_raw_one_time_callback(port)
439 let handle = ch_open(s:localhost . a:port, s:chopt) 485 let handle = ch_open(s:address(a:port), s:chopt)
440 if ch_status(handle) == "fail" 486 if ch_status(handle) == "fail"
441 call assert_report("Can't open channel") 487 call assert_report("Can't open channel")
442 return 488 return
443 endif 489 endif
444 call ch_setoptions(handle, {'mode': 'raw'}) 490 call ch_setoptions(handle, {'mode': 'raw'})
458 endfunc 504 endfunc
459 505
460 func Test_raw_one_time_callback_ipv6() 506 func Test_raw_one_time_callback_ipv6()
461 CheckIPv6 507 CheckIPv6
462 call Test_raw_one_time_callback() 508 call Test_raw_one_time_callback()
509 endfunc
510
511 func Test_raw_one_time_callback_unix()
512 CheckUnix
513 call Test_raw_one_time_callback()
514 call delete('Xtestsocket')
463 endfunc 515 endfunc
464 516
465 """"""""" 517 """""""""
466 518
467 " Test that trying to connect to a non-existing port fails quickly. 519 " Test that trying to connect to a non-existing port fails quickly.
1396 unlet s:channelfd 1448 unlet s:channelfd
1397 endfunc 1449 endfunc
1398 1450
1399 " Test that "unlet handle" in a handler doesn't crash Vim. 1451 " Test that "unlet handle" in a handler doesn't crash Vim.
1400 func Ch_unlet_handle(port) 1452 func Ch_unlet_handle(port)
1401 let s:channelfd = ch_open(s:localhost . a:port, s:chopt) 1453 let s:channelfd = ch_open(s:address(a:port), s:chopt)
1402 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')}) 1454 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
1403 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)}) 1455 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
1404 endfunc 1456 endfunc
1405 1457
1406 func Test_unlet_handle() 1458 func Test_unlet_handle()
1420 eval s:channelfd->ch_close() 1472 eval s:channelfd->ch_close()
1421 endfunc 1473 endfunc
1422 1474
1423 " Test that "unlet handle" in a handler doesn't crash Vim. 1475 " Test that "unlet handle" in a handler doesn't crash Vim.
1424 func Ch_close_handle(port) 1476 func Ch_close_handle(port)
1425 let s:channelfd = ch_open(s:localhost . a:port, s:chopt) 1477 let s:channelfd = ch_open(s:address(a:port), s:chopt)
1426 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')}) 1478 call ch_sendexpr(s:channelfd, "test", {'callback': function('Ch_CloseHandler')})
1427 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)}) 1479 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
1428 endfunc 1480 endfunc
1429 1481
1430 func Test_close_handle() 1482 func Test_close_handle()
1437 endfunc 1489 endfunc
1438 1490
1439 """""""""" 1491 """"""""""
1440 1492
1441 func Ch_open_ipv6(port) 1493 func Ch_open_ipv6(port)
1442 let handle = ch_open('[::1]:' .. a:port, s:chopt) 1494 let handle = ch_open(s:address(a:port), s:chopt)
1443 call assert_notequal('fail', ch_status(handle)) 1495 call assert_notequal('fail', ch_status(handle))
1444 endfunc 1496 endfunc
1445 1497
1446 func Test_open_ipv6() 1498 func Test_open_ipv6()
1447 CheckIPv6 1499 CheckIPv6
1477 """""""""" 1529 """"""""""
1478 1530
1479 func Ch_open_delay(port) 1531 func Ch_open_delay(port)
1480 " Wait up to a second for the port to open. 1532 " Wait up to a second for the port to open.
1481 let s:chopt.waittime = 1000 1533 let s:chopt.waittime = 1000
1482 let channel = ch_open(s:localhost . a:port, s:chopt) 1534 let channel = ch_open(s:address(a:port), s:chopt)
1483 if ch_status(channel) == "fail" 1535 if ch_status(channel) == "fail"
1484 call assert_report("Can't open channel") 1536 call assert_report("Can't open channel")
1485 return 1537 return
1486 endif 1538 endif
1487 call assert_equal('got it', channel->ch_evalexpr('hello!')) 1539 call assert_equal('got it', channel->ch_evalexpr('hello!'))
1503 function MyFunction(a,b,c) 1555 function MyFunction(a,b,c)
1504 let g:Ch_call_ret = [a:a, a:b, a:c] 1556 let g:Ch_call_ret = [a:a, a:b, a:c]
1505 endfunc 1557 endfunc
1506 1558
1507 function Ch_test_call(port) 1559 function Ch_test_call(port)
1508 let handle = ch_open(s:localhost . a:port, s:chopt) 1560 let handle = ch_open(s:address(a:port), s:chopt)
1509 if ch_status(handle) == "fail" 1561 if ch_status(handle) == "fail"
1510 call assert_report("Can't open channel") 1562 call assert_report("Can't open channel")
1511 return 1563 return
1512 endif 1564 endif
1513 1565
1525 endfunc 1577 endfunc
1526 1578
1527 func Test_call_ipv6() 1579 func Test_call_ipv6()
1528 CheckIPv6 1580 CheckIPv6
1529 call Test_call() 1581 call Test_call()
1582 endfunc
1583
1584 func Test_call_unix()
1585 CheckUnix
1586 call Test_call()
1587 call delete('Xtestsocket')
1530 endfunc 1588 endfunc
1531 1589
1532 """"""""" 1590 """""""""
1533 1591
1534 let g:Ch_job_exit_ret = 'not yet' 1592 let g:Ch_job_exit_ret = 'not yet'
1603 function MyCloseCb(ch) 1661 function MyCloseCb(ch)
1604 let g:Ch_close_ret = 'closed' 1662 let g:Ch_close_ret = 'closed'
1605 endfunc 1663 endfunc
1606 1664
1607 function Ch_test_close_callback(port) 1665 function Ch_test_close_callback(port)
1608 let handle = ch_open(s:localhost . a:port, s:chopt) 1666 let handle = ch_open(s:address(a:port), s:chopt)
1609 if ch_status(handle) == "fail" 1667 if ch_status(handle) == "fail"
1610 call assert_report("Can't open channel") 1668 call assert_report("Can't open channel")
1611 return 1669 return
1612 endif 1670 endif
1613 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'}) 1671 call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
1623 func Test_close_callback_ipv6() 1681 func Test_close_callback_ipv6()
1624 CheckIPv6 1682 CheckIPv6
1625 call Test_close_callback() 1683 call Test_close_callback()
1626 endfunc 1684 endfunc
1627 1685
1686 func Test_close_callback_unix()
1687 CheckUnix
1688 call Test_close_callback()
1689 call delete('Xtestsocket')
1690 endfunc
1691
1628 function Ch_test_close_partial(port) 1692 function Ch_test_close_partial(port)
1629 let handle = ch_open(s:localhost . a:port, s:chopt) 1693 let handle = ch_open(s:address(a:port), s:chopt)
1630 if ch_status(handle) == "fail" 1694 if ch_status(handle) == "fail"
1631 call assert_report("Can't open channel") 1695 call assert_report("Can't open channel")
1632 return 1696 return
1633 endif 1697 endif
1634 let g:Ch_d = {} 1698 let g:Ch_d = {}
1647 endfunc 1711 endfunc
1648 1712
1649 func Test_close_partial_ipv6() 1713 func Test_close_partial_ipv6()
1650 CheckIPv6 1714 CheckIPv6
1651 call Test_close_partial() 1715 call Test_close_partial()
1716 endfunc
1717
1718 func Test_close_partial_unix()
1719 CheckUnix
1720 call Test_close_partial()
1721 call delete('Xtestsocket')
1652 endfunc 1722 endfunc
1653 1723
1654 func Test_job_start_fails() 1724 func Test_job_start_fails()
1655 " this was leaking memory 1725 " this was leaking memory
1656 call assert_fails("call job_start([''])", "E474:") 1726 call assert_fails("call job_start([''])", "E474:")
1918 unlet g:envstr 1988 unlet g:envstr
1919 endtry 1989 endtry
1920 endfunc 1990 endfunc
1921 1991
1922 function Ch_test_close_lambda(port) 1992 function Ch_test_close_lambda(port)
1923 let handle = ch_open(s:localhost . a:port, s:chopt) 1993 let handle = ch_open(s:address(a:port), s:chopt)
1924 if ch_status(handle) == "fail" 1994 if ch_status(handle) == "fail"
1925 call assert_report("Can't open channel") 1995 call assert_report("Can't open channel")
1926 return 1996 return
1927 endif 1997 endif
1928 let g:Ch_close_ret = '' 1998 let g:Ch_close_ret = ''
1938 endfunc 2008 endfunc
1939 2009
1940 func Test_close_lambda_ipv6() 2010 func Test_close_lambda_ipv6()
1941 CheckIPv6 2011 CheckIPv6
1942 call Test_close_lambda() 2012 call Test_close_lambda()
2013 endfunc
2014
2015 func Test_close_lambda_unix()
2016 CheckUnix
2017 call Test_close_lambda()
2018 call delete('Xtestsocket')
1943 endfunc 2019 endfunc
1944 2020
1945 func s:test_list_args(cmd, out, remove_lf) 2021 func s:test_list_args(cmd, out, remove_lf)
1946 try 2022 try
1947 let g:out = '' 2023 let g:out = ''
2241 CheckExecutable cat 2317 CheckExecutable cat
2242 2318
2243 let job = job_start("cat ", #{in_io: 'null'}) 2319 let job = job_start("cat ", #{in_io: 'null'})
2244 call WaitForAssert({-> assert_equal("dead", job_status(job))}) 2320 call WaitForAssert({-> assert_equal("dead", job_status(job))})
2245 call assert_equal(0, job_info(job).exitval) 2321 call assert_equal(0, job_info(job).exitval)
2322
2323 call delete('Xtestsocket')
2246 endfunc 2324 endfunc
2247 2325
2248 func Test_ch_getbufnr() 2326 func Test_ch_getbufnr()
2249 let ch = test_null_channel() 2327 let ch = test_null_channel()
2250 call assert_equal(-1, ch_getbufnr(ch, 'in')) 2328 call assert_equal(-1, ch_getbufnr(ch, 'in'))