File: unix.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (130 lines) | stat: -rw-r--r-- 2,787 bytes parent folder | download | duplicates (15)
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
unless SocketTest.win? || SocketTest.cygwin?

def unixserver_test_block
  path = SocketTest.tmppath
  File.unlink path rescue nil
  begin
    result = yield path
  ensure
    File.unlink path rescue nil
  end
  result
end

def with_unix_server
  unixserver_test_block do |path|
    UNIXServer.open(path) { |server|
      yield path, server
    }
  end
end

def with_unix_client
  with_unix_server do |path, server|
    UNIXSocket.open(path) do |csock|
      ssock = server.accept
      begin
        yield path, server, ssock, csock
      ensure
        ssock.close unless ssock.closed? rescue nil
      end
    end
  end
end

assert('UNIXServer.new') do
  unixserver_test_block do |path|
    server = UNIXServer.new(path)
    assert_true server.is_a? UNIXServer
    server.close
    File.unlink path

    s2 = nil
    result = UNIXServer.open(path) { |s1|
      assert_true s1.is_a? UNIXServer
      s2 = s1
      1234
    }
    assert_equal 1234, result
    assert_true s2.is_a? UNIXServer
    assert_true s2.closed?
  end
end

# assert('UNIXServer#accept_nonblock') - would block if fails

assert('UNIXServer#addr') do
  with_unix_server do |path, server|
    assert_equal [ "AF_UNIX", path], server.addr
  end
end

assert('UNIXServer#path') do
  with_unix_server do |path, server|
    assert_equal path, server.path
  end
end

# assert('UNIXServer#peeraddr') - will raise a runtime exception

assert('UNIXServer#listen') do
  with_unix_server do |path, server|
    assert_equal 0, server.listen(1)
  end
end

assert('UNIXServer#sysaccept') do
  with_unix_server do |path, server|
    UNIXSocket.open(path) do |csock|
      begin
        fd = server.sysaccept
        assert_true fd.kind_of? Integer
      ensure
        IO._sysclose(fd) rescue nil
      end
    end
  end
end

assert('UNIXSocket.new') do
  with_unix_server do |path, server|
    c = UNIXSocket.new(path)
    assert_true c.is_a? UNIXSocket
    c.close
    true
  end
end

assert('UNIXSocket#addr') do
  with_unix_client do |path, server, ssock, csock|
    assert_equal [ "AF_UNIX", path ], ssock.addr
    assert_equal [ "AF_UNIX", "" ],   csock.addr
  end
end

assert('UNIXSocket#path') do
  with_unix_client do |path, server, ssock, csock|
    assert_equal path, ssock.path
    assert_equal "",   csock.path
  end
end

assert('UNIXSocket#peeraddr') do
  with_unix_client do |path, server, ssock, csock|
    assert_equal [ "AF_UNIX", ""   ], ssock.peeraddr
    assert_equal [ "AF_UNIX", path ], csock.peeraddr
  end
end

assert('UNIXSocket#recvfrom') do
  with_unix_client do |path, server, ssock, csock|
    str = "0123456789"
    ssock.send str, 0
    a = csock.recvfrom(8)
    assert_equal str[0, 8], a[0]
    assert_equal "AF_UNIX", a[1][0]
    # a[1][1] would be "" or something
  end
end

end # SocketTest.win?