[439] | 1 | unless SocketTest.win? || SocketTest.cygwin?
|
---|
| 2 |
|
---|
| 3 | def unixserver_test_block
|
---|
| 4 | path = SocketTest.tmppath
|
---|
| 5 | File.unlink path rescue nil
|
---|
| 6 | begin
|
---|
| 7 | result = yield path
|
---|
| 8 | ensure
|
---|
| 9 | File.unlink path rescue nil
|
---|
| 10 | end
|
---|
| 11 | result
|
---|
| 12 | end
|
---|
| 13 |
|
---|
| 14 | def with_unix_server
|
---|
| 15 | unixserver_test_block do |path|
|
---|
| 16 | UNIXServer.open(path) { |server|
|
---|
| 17 | yield path, server
|
---|
| 18 | }
|
---|
| 19 | end
|
---|
| 20 | end
|
---|
| 21 |
|
---|
| 22 | def with_unix_client
|
---|
| 23 | with_unix_server do |path, server|
|
---|
| 24 | UNIXSocket.open(path) do |csock|
|
---|
| 25 | ssock = server.accept
|
---|
| 26 | begin
|
---|
| 27 | yield path, server, ssock, csock
|
---|
| 28 | ensure
|
---|
| 29 | ssock.close unless ssock.closed? rescue nil
|
---|
| 30 | end
|
---|
| 31 | end
|
---|
| 32 | end
|
---|
| 33 | end
|
---|
| 34 |
|
---|
| 35 | assert('UNIXServer.new') do
|
---|
| 36 | unixserver_test_block do |path|
|
---|
| 37 | server = UNIXServer.new(path)
|
---|
| 38 | assert_true server.is_a? UNIXServer
|
---|
| 39 | server.close
|
---|
| 40 | File.unlink path
|
---|
| 41 |
|
---|
| 42 | s2 = nil
|
---|
| 43 | result = UNIXServer.open(path) { |s1|
|
---|
| 44 | assert_true s1.is_a? UNIXServer
|
---|
| 45 | s2 = s1
|
---|
| 46 | 1234
|
---|
| 47 | }
|
---|
| 48 | assert_equal 1234, result
|
---|
| 49 | assert_true s2.is_a? UNIXServer
|
---|
| 50 | assert_true s2.closed?
|
---|
| 51 | end
|
---|
| 52 | end
|
---|
| 53 |
|
---|
| 54 | # assert('UNIXServer#accept_nonblock') - would block if fails
|
---|
| 55 |
|
---|
| 56 | assert('UNIXServer#addr') do
|
---|
| 57 | with_unix_server do |path, server|
|
---|
| 58 | assert_equal [ "AF_UNIX", path], server.addr
|
---|
| 59 | end
|
---|
| 60 | end
|
---|
| 61 |
|
---|
| 62 | assert('UNIXServer#path') do
|
---|
| 63 | with_unix_server do |path, server|
|
---|
| 64 | assert_equal path, server.path
|
---|
| 65 | end
|
---|
| 66 | end
|
---|
| 67 |
|
---|
| 68 | # assert('UNIXServer#peeraddr') - will raise a runtime exception
|
---|
| 69 |
|
---|
| 70 | assert('UNIXServer#listen') do
|
---|
| 71 | with_unix_server do |path, server|
|
---|
| 72 | assert_equal 0, server.listen(1)
|
---|
| 73 | end
|
---|
| 74 | end
|
---|
| 75 |
|
---|
| 76 | assert('UNIXServer#sysaccept') do
|
---|
| 77 | with_unix_server do |path, server|
|
---|
| 78 | UNIXSocket.open(path) do |csock|
|
---|
| 79 | begin
|
---|
| 80 | fd = server.sysaccept
|
---|
| 81 | assert_true fd.kind_of? Integer
|
---|
| 82 | ensure
|
---|
| 83 | IO._sysclose(fd) rescue nil
|
---|
| 84 | end
|
---|
| 85 | end
|
---|
| 86 | end
|
---|
| 87 | end
|
---|
| 88 |
|
---|
| 89 | assert('UNIXSocket.new') do
|
---|
| 90 | with_unix_server do |path, server|
|
---|
| 91 | c = UNIXSocket.new(path)
|
---|
| 92 | assert_true c.is_a? UNIXSocket
|
---|
| 93 | c.close
|
---|
| 94 | true
|
---|
| 95 | end
|
---|
| 96 | end
|
---|
| 97 |
|
---|
| 98 | assert('UNIXSocket#addr') do
|
---|
| 99 | with_unix_client do |path, server, ssock, csock|
|
---|
| 100 | assert_equal [ "AF_UNIX", path ], ssock.addr
|
---|
| 101 | assert_equal [ "AF_UNIX", "" ], csock.addr
|
---|
| 102 | end
|
---|
| 103 | end
|
---|
| 104 |
|
---|
| 105 | assert('UNIXSocket#path') do
|
---|
| 106 | with_unix_client do |path, server, ssock, csock|
|
---|
| 107 | assert_equal path, ssock.path
|
---|
| 108 | assert_equal "", csock.path
|
---|
| 109 | end
|
---|
| 110 | end
|
---|
| 111 |
|
---|
| 112 | assert('UNIXSocket#peeraddr') do
|
---|
| 113 | with_unix_client do |path, server, ssock, csock|
|
---|
| 114 | assert_equal [ "AF_UNIX", "" ], ssock.peeraddr
|
---|
| 115 | assert_equal [ "AF_UNIX", path ], csock.peeraddr
|
---|
| 116 | end
|
---|
| 117 | end
|
---|
| 118 |
|
---|
| 119 | assert('UNIXSocket#recvfrom') do
|
---|
| 120 | with_unix_client do |path, server, ssock, csock|
|
---|
| 121 | str = "0123456789"
|
---|
| 122 | ssock.send str, 0
|
---|
| 123 | a = csock.recvfrom(8)
|
---|
| 124 | assert_equal str[0, 8], a[0]
|
---|
| 125 | assert_equal "AF_UNIX", a[1][0]
|
---|
| 126 | # a[1][1] would be "" or something
|
---|
| 127 | end
|
---|
| 128 | end
|
---|
| 129 |
|
---|
| 130 | end # SocketTest.win?
|
---|