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