summaryrefslogtreecommitdiff
path: root/test/fiber/test_address_resolve.rb
blob: f2ad680b0a3197e8fa50f3c3d7211e6e5429956f (plain)
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
# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'

class TestAddressResolve < Test::Unit::TestCase
  class NullScheduler < Scheduler
    def address_resolve(*)
    end
  end

  class StubScheduler < Scheduler
    def address_resolve(hostname)
      ["1.2.3.4", "1234::1"]
    end
  end

  def test_addrinfo_getaddrinfo_ipv4_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        result = Addrinfo.getaddrinfo("example.com", 80, :AF_INET, :STREAM)
        assert_equal(1, result.count)

        ai = result.first
        assert_equal("1.2.3.4", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET, ai.afamily)
        assert_equal(Socket::SOCK_STREAM, ai.socktype)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_ipv6_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        result = Addrinfo.getaddrinfo("example.com", 80, :AF_INET6, :STREAM)
        assert_equal(1, result.count)

        ai = result.first
        assert_equal("1234::1", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET6, ai.afamily)
        assert_equal(Socket::SOCK_STREAM, ai.socktype)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_pf_unspec_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        results = Addrinfo.getaddrinfo("example.com", 80, :PF_UNSPEC, :STREAM)
        assert_equal(2, results.count)

        ai_ipv4 = results.first
        assert_equal("1.2.3.4", ai_ipv4.ip_address)
        assert_equal(80, ai_ipv4.ip_port)
        assert_equal(Socket::AF_INET, ai_ipv4.afamily)
        assert_equal(Socket::SOCK_STREAM, ai_ipv4.socktype)

        ai_ipv6 = results.last
        assert_equal("1234::1", ai_ipv6.ip_address)
        assert_equal(80, ai_ipv6.ip_port)
        assert_equal(Socket::AF_INET6, ai_ipv6.afamily)
        assert_equal(Socket::SOCK_STREAM, ai_ipv6.socktype)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_full_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        results = Addrinfo.getaddrinfo("example.com", 80)
        assert_equal(6, results.count)

        ai_ipv4_tcp = results[0]
        assert_equal("1.2.3.4", ai_ipv4_tcp.ip_address)
        assert_equal(80, ai_ipv4_tcp.ip_port)
        assert_equal(Socket::AF_INET, ai_ipv4_tcp.afamily)
        assert_equal(Socket::SOCK_STREAM, ai_ipv4_tcp.socktype)

        ai_ipv4_udp = results[1]
        assert_equal("1.2.3.4", ai_ipv4_udp.ip_address)
        assert_equal(80, ai_ipv4_udp.ip_port)
        assert_equal(Socket::AF_INET, ai_ipv4_udp.afamily)
        assert_equal(Socket::SOCK_DGRAM, ai_ipv4_udp.socktype)

        ai_ipv4_sock_raw = results[2]
        assert_equal("1.2.3.4", ai_ipv4_sock_raw.ip_address)
        assert_equal(80, ai_ipv4_sock_raw.ip_port)
        assert_equal(Socket::AF_INET, ai_ipv4_sock_raw.afamily)
        assert_equal(Socket::SOCK_RAW, ai_ipv4_sock_raw.socktype)

        ai_ipv6_tcp = results[3]
        assert_equal("1234::1", ai_ipv6_tcp.ip_address)
        assert_equal(80, ai_ipv6_tcp.ip_port)
        assert_equal(Socket::AF_INET6, ai_ipv6_tcp.afamily)
        assert_equal(Socket::SOCK_STREAM, ai_ipv6_tcp.socktype)

        ai_ipv6_udp = results[4]
        assert_equal("1234::1", ai_ipv6_udp.ip_address)
        assert_equal(80, ai_ipv6_udp.ip_port)
        assert_equal(Socket::AF_INET6, ai_ipv6_udp.afamily)
        assert_equal(Socket::SOCK_DGRAM, ai_ipv6_udp.socktype)

        ai_ipv6_sock_raw = results[5]
        assert_equal("1234::1", ai_ipv6_sock_raw.ip_address)
        assert_equal(80, ai_ipv6_sock_raw.ip_port)
        assert_equal(Socket::AF_INET6, ai_ipv6_sock_raw.afamily)
        assert_equal(Socket::SOCK_RAW, ai_ipv6_sock_raw.socktype)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_numeric_non_blocking
    Thread.new do
      scheduler = NullScheduler.new # scheduler hook not invoked
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        result = Addrinfo.getaddrinfo("1.2.3.4", 80, :AF_INET, :STREAM)
        assert_equal(1, result.count)

        ai = result.first
        assert_equal("1.2.3.4", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET, ai.afamily)
        assert_equal(Socket::SOCK_STREAM, ai.socktype)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_any_non_blocking
    Thread.new do
      scheduler = NullScheduler.new # scheduler hook not invoked
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        result = Addrinfo.getaddrinfo("<any>", 80, :AF_INET, :STREAM)
        assert_equal(1, result.count)

        ai = result.first
        assert_equal("0.0.0.0", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET, ai.afamily)
        assert_equal(Socket::SOCK_STREAM, ai.socktype)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_localhost
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        results = Addrinfo.getaddrinfo("localhost", 80, :AF_INET, :STREAM)
        assert_equal(1, results.count)

        ai = results.first
        assert_equal("1.2.3.4", ai.ip_address)
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_non_existing_domain_blocking
    Thread.new do
      scheduler = NullScheduler.new # invoked, returns nil
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        assert_raise(SocketError) {
          Addrinfo.getaddrinfo("non-existing-domain.abc", nil)
        }
      end
    end.join
  end

  def test_addrinfo_getaddrinfo_no_host_non_blocking
    Thread.new do
      scheduler = NullScheduler.new # scheduler hook not invoked
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        result = Addrinfo.getaddrinfo(nil, 80, :AF_INET, :STREAM)
        assert_equal(1, result.count)

        ai = result.first
        assert_equal("127.0.0.1", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET, ai.afamily)
        assert_equal(Socket::SOCK_STREAM, ai.socktype)
      end
    end.join
  end

  def test_addrinfo_ip_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        ai = Addrinfo.ip("example.com")

        assert_equal("1.2.3.4", ai.ip_address)
      end
    end.join
  end

  def test_addrinfo_tcp_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        ai = Addrinfo.tcp("example.com", 80)

        assert_equal("1.2.3.4", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET, ai.afamily)
        assert_equal(Socket::SOCK_STREAM, ai.socktype)
      end
    end.join
  end

  def test_addrinfo_udp_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        ai = Addrinfo.udp("example.com", 80)

        assert_equal("1.2.3.4", ai.ip_address)
        assert_equal(80, ai.ip_port)
        assert_equal(Socket::AF_INET, ai.afamily)
        assert_equal(Socket::SOCK_DGRAM, ai.socktype)
      end
    end.join
  end

  def test_ip_socket_getaddress_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        ip = IPSocket.getaddress("example.com")

        assert_equal("1.2.3.4", ip)
      end
    end.join
  end

  # This test "hits deep" into C function call chain.
  def test_socket_getnameinfo_domain_blocking
    Thread.new do
      scheduler = StubScheduler.new
      Fiber.set_scheduler scheduler

      Fiber.schedule do
        result = Socket.getnameinfo(["AF_INET", 80, "example.com"])

        assert_equal([
          "1.2.3.4",
          "http"
        ], result)
      end
    end.join
  end
end