File: test_socket.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (412 lines) | stat: -rw-r--r-- 10,784 bytes parent folder | download | duplicates (4)
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
require 'test/unit'
require 'socket'
require 'thread'
require 'test/test_helper'

class SocketTest < Test::Unit::TestCase
  include TestHelper

  def test_tcp_socket_allows_nil_for_hostname
    assert_nothing_raised do
      server = TCPServer.new(nil, 7789)
      t = Thread.new do
        s = server.accept
        s.close
      end
      client = TCPSocket.new(nil, 7789)
      client.write ""
      t.join
    end
  end

  #JRUBY-3827
  def test_nil_hostname_and_passive_returns_inaddr_any
    assert_nothing_raised do
      addrs = Socket::getaddrinfo(nil, 7789, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
      assert_equal(1, addrs.size)
      assert_equal("0.0.0.0", addrs[0][2])
      assert_equal("0.0.0.0", addrs[0][3])
    end
  end

  def test_nil_hostname_and_no_flags_returns_localhost
    assert_nothing_raised do
      addrs = Socket::getaddrinfo(nil, 7789, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0)
      assert_equal(1, addrs.size)
      
      # FIXME, behaves differently on Windows, both JRuby and MRI.
      # JRuby returns "127.0.0.1", "127.0.0.1"
      # MRI returns  "<actual_hostname>", "127.0.0.1"
      unless WINDOWS
        #assert_equal("localhost", addrs[0][2])
        assert_equal("127.0.0.1", addrs[0][3])
      end
    end
  end

  def test_basic_socket_reverse_lookup
    assert_nothing_raised do
      reverse = BasicSocket.do_not_reverse_lookup
      BasicSocket.do_not_reverse_lookup = !reverse
      assert_equal(reverse, !BasicSocket.do_not_reverse_lookup)
      BasicSocket.do_not_reverse_lookup = reverse
    end
  end
  
  #JRUBY-2147
  def test_tcp_close_read
    socket = TCPServer.new(nil, 9999)
    socket.close_read
    assert(!socket.closed?)
    socket.close
  end
  
  #JRUBY-2146
  def test_tcp_close_write
    socket = TCPServer.new(nil, 8888)
    socket.close_write
    assert(!socket.closed?)
    socket.close
  end
  
  def test_tcp_close_read_then_write_should_close_socket
    socket = TCPServer.new(nil, 7777)
    socket.close_write
    assert(!socket.closed?)
    socket.close_read
    assert(socket.closed?)
  end

  # JRUBY-2874
  def test_raises_socket_error_on_out_of_range_port
    [-2**16, -2**8, -2, -1, 2**16, 2**16 + 1, 2**17, 2**30 -1].each do |port|
      assert_raises(SocketError) do
        TCPSocket.new('localhost', port)
      end
    end
  end

  # JRUBY-4299
  def test_tcp_socket_reuse_addr
    socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)

    socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
    assert_equal 1, socket.getsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR).unpack('i')[0]
  ensure
    socket.close
  end

  # JRUBY-4299
  def test_udp_socket_reuse_addr
    socket = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)

    socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
    assert_equal 1, socket.getsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR).unpack('i')[0]
  ensure
    socket.close
  end
end

class UNIXSocketTests < Test::Unit::TestCase
  # this is intentional, otherwise test run fails on windows
  def test_dummy; end

  if defined?(UNIXSocket)
    def test_unix_socket_path
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      server = UNIXServer.open(path)
      assert_equal path, server.path
      
      cli = UNIXSocket.open(path)

      assert_equal "", cli.path
      
      cli.close
      server.close
      File.unlink(path) if File.exist?(path)
    end

    def test_unix_socket_addr
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      server = UNIXServer.open(path)
      assert_equal ["AF_UNIX", path], server.addr
      
      cli = UNIXSocket.open(path)

      assert_equal ["AF_UNIX", ""], cli.addr
      
      cli.close
      server.close
      File.unlink(path) if File.exist?(path)
    end

    def test_unix_socket_peeraddr_raises_enotconn
      path = "/tmp/sample"
      File.unlink(path) if File.exist?(path)

      server = UNIXServer.open(path)
      assert_raises(Errno::ENOTCONN) do
        server.peeraddr
      end
      File.unlink(path) if File.exist?(path)
    end

    def test_unix_socket_peeraddr
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      server = UNIXServer.open(path)

      cli = UNIXSocket.open(path)

      ssrv = server.accept
      
      assert_equal ["AF_UNIX", ""], ssrv.peeraddr
      assert_equal ["AF_UNIX", path], cli.peeraddr

      ssrv.close
      cli.close
      server.close
      File.unlink(path) if File.exist?(path)
    end

    def test_unix_socket_raises_exception_on_too_long_path
      assert_raises(ArgumentError) do 
        # on some platforms, 103 is invalid length (MacOS)
        # on others, 108 (Linux), we'll take the biggest one
        UNIXSocket.new("a" * 108)
      end
    end
    
    def test_unix_socket_raises_exception_on_path_that_cant_exist 
      assert_raises(Errno::ENOENT) do 
        UNIXSocket.new("a")
      end
    end
    
    def test_can_create_socket_server
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      sock = UNIXServer.open(path)
      assert File.exist?(path)
      sock.close

      File.unlink(path) if File.exist?(path)
    end

    def test_can_create_socket_server_and_accept_nonblocking
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      sock = UNIXServer.open(path)
      assert File.exist?(path)

      assert_raises(Errno::EAGAIN) do 
        sock.accept_nonblock
      end

      cli = UNIXSocket.open(path)

      sock.accept_nonblock.close
      
      cli.close
      
      sock.close

      File.unlink(path) if File.exist?(path)
    end
    
    def test_can_create_socket_server_and_relisten
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      sock = UNIXServer.open(path)
      
      assert File.exist?(path)

      sock.listen(1)

      assert File.exist?(path)

      sock.close

      File.unlink(path) if File.exist?(path)
    end

    def test_can_create_socket_server_and_client_connected_to_it
      path = "/tmp/sample"

      File.unlink(path) if File.exist?(path)
      
      sock = UNIXServer.open(path)
      assert File.exist?(path)
      
      cli = UNIXSocket.open(path)
      cli.close
      
      sock.close

      File.unlink(path) if File.exist?(path)
    end

    def test_can_create_socket_server_and_client_connected_to_it_and_send_from_client_to_server
      path = "/tmp/sample"
      File.unlink(path) if File.exist?(path)
      sock = UNIXServer.open(path)
      assert File.exist?(path)
      cli = UNIXSocket.open(path)
      servsock = sock.accept
      cli.send("hello",0)
      assert_equal "hello", servsock.recv(5)
      servsock.close
      cli.close
      sock.close
      File.unlink(path) if File.exist?(path)
    end

    def test_can_create_socket_server_and_client_connected_to_it_and_send_from_server_to_client
      path = "/tmp/sample"
      File.unlink(path) if File.exist?(path)
      sock = UNIXServer.open(path)
      assert File.exist?(path)
      cli = UNIXSocket.open(path)
      servsock = sock.accept
      servsock.send("hello",0)
      assert_equal "hello", cli.recv(5)
      servsock.close
      cli.close
      sock.close
      File.unlink(path) if File.exist?(path)
    end


    def test_can_create_socket_server_and_client_connected_to_it_and_send_from_client_to_server_using_recvfrom
      path = "/tmp/sample"
      File.unlink(path) if File.exist?(path)
      sock = UNIXServer.open(path)
      assert File.exist?(path)
      cli = UNIXSocket.open(path)
      servsock = sock.accept
      cli.send("hello",0)
      assert_equal ["hello", ["AF_UNIX", ""]], servsock.recvfrom(5)
      servsock.close
      cli.close
      sock.close
      File.unlink(path) if File.exist?(path)
    end

    def test_can_create_socket_server_and_client_connected_to_it_and_send_from_server_to_client_using_recvfrom
      path = "/tmp/sample"
      File.unlink(path) if File.exist?(path)
      sock = UNIXServer.open(path)
      assert File.exist?(path)
      cli = UNIXSocket.open(path)
      servsock = sock.accept
      servsock.send("hello",0)
      data = cli.recvfrom(5)
      assert_equal "hello", data[0]
      assert_equal "AF_UNIX", data[1][0]
      servsock.close
      cli.close
      sock.close
      File.unlink(path) if File.exist?(path)
    end

    def test_can_create_socketpair_and_send_from_one_to_the_other
      sock1, sock2 = UNIXSocket.socketpair
      
      sock1.send("hello", 0)
      assert_equal "hello", sock2.recv(5)
      
      sock1.close
      sock2.close
    end

    def test_can_create_socketpair_and_can_send_from_the_other
      sock1, sock2 = UNIXSocket.socketpair
      
      sock2.send("hello", 0)
      assert_equal "hello", sock1.recv(5)
      
      sock2.close
      sock1.close
    end

    def test_can_create_socketpair_and_can_send_from_the_other_with_recvfrom
      sock1, sock2 = UNIXSocket.socketpair
      
      sock2.send("hello", 0)
      assert_equal ["hello", ["AF_UNIX", ""]], sock1.recvfrom(5)
      
      sock2.close
      sock1.close
    end
    
    def test_can_read_and_get_minus_one
      sock1, sock2 = UNIXSocket.socketpair
      
      sock2.send("hello", 0)
      assert_equal "hell", sock1.recv(4)
      assert_equal "", sock1.recv(0)
      assert_equal "o", sock1.recv(1)

      sock2.close
      sock1.close
      
      assert_raises(IOError) do 
        sock1.recv(1)
      end
    end

  end
end

class ServerTest < Test::Unit::TestCase
  def test_server_close_interrupts_pending_accepts
    # unfortunately this test is going to not be 100% reliable
    # since it involves thread interaction and it's impossible to
    # do things like wait until the other thread blocks
    port = 41258
    server = TCPServer.new('localhost', port)
    queue = Queue.new
    thread = Thread.new do
      server.accept
    end
    # wait until the thread is sleeping (ready to accept)
    Thread.pass while thread.alive? && thread.status != "sleep"
    # close the server
    server.close
    # propagate the thread's termination error, checking it for IOError
    assert_raise(IOError) {thread.value}
  end

  # JRUBY-2874
  def test_raises_socket_error_on_out_of_range_port
    [-2**16, -2**8, -2, -1, 2**16, 2**16 + 1, 2**17, 2**30 -1].each do |port|
      assert_raises(SocketError) do
        TCPServer.new('localhost', port)
      end
    end
  end

  # JRUBY-4299
  def test_server_reuse_addr
    socket = TCPServer.new("127.0.0.1", 7777)

    socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
    assert_equal 1, socket.getsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR).unpack('i')[0]
  ensure
    socket.close
  end
end