1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/resolv/test_dns.rb: Use assert_join_threads.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48381 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2014-11-11 13:51:39 +00:00
parent 1fca7620dc
commit 08bb9482a0
2 changed files with 68 additions and 64 deletions

View file

@ -1,3 +1,7 @@
Tue Nov 11 22:51:14 2014 Tanaka Akira <akr@fsij.org>
* test/resolv/test_dns.rb: Use assert_join_threads.
Tue Nov 11 22:33:08 2014 Tanaka Akira <akr@fsij.org> Tue Nov 11 22:33:08 2014 Tanaka Akira <akr@fsij.org>
* test/net/pop/test_pop.rb: Use assert_join_threads. * test/net/pop/test_pop.rb: Use assert_join_threads.

View file

@ -33,72 +33,74 @@ class TestResolvDNS < Test::Unit::TestCase
with_udp('127.0.0.1', 0) {|u| with_udp('127.0.0.1', 0) {|u|
_, server_port, _, server_address = u.addr _, server_port, _, server_address = u.addr
begin begin
th = Thread.new { client_thread = Thread.new {
Resolv::DNS.open(:nameserver_port => [[server_address, server_port]]) {|dns| Resolv::DNS.open(:nameserver_port => [[server_address, server_port]]) {|dns|
dns.getresources("foo.example.org", Resolv::DNS::Resource::IN::A) dns.getresources("foo.example.org", Resolv::DNS::Resource::IN::A)
} }
} }
msg, (_, client_port, _, client_address) = u.recvfrom(4096) server_thread = Thread.new {
id, word2, qdcount, ancount, nscount, arcount = msg.unpack("nnnnnn") msg, (_, client_port, _, client_address) = u.recvfrom(4096)
qr = (word2 & 0x8000) >> 15 id, word2, qdcount, ancount, nscount, arcount = msg.unpack("nnnnnn")
opcode = (word2 & 0x7800) >> 11 qr = (word2 & 0x8000) >> 15
aa = (word2 & 0x0400) >> 10 opcode = (word2 & 0x7800) >> 11
tc = (word2 & 0x0200) >> 9 aa = (word2 & 0x0400) >> 10
rd = (word2 & 0x0100) >> 8 tc = (word2 & 0x0200) >> 9
ra = (word2 & 0x0080) >> 7 rd = (word2 & 0x0100) >> 8
z = (word2 & 0x0070) >> 4 ra = (word2 & 0x0080) >> 7
rcode = word2 & 0x000f z = (word2 & 0x0070) >> 4
rest = msg[12..-1] rcode = word2 & 0x000f
assert_equal(0, qr) # 0:query 1:response rest = msg[12..-1]
assert_equal(0, opcode) # 0:QUERY 1:IQUERY 2:STATUS assert_equal(0, qr) # 0:query 1:response
assert_equal(0, aa) # Authoritative Answer assert_equal(0, opcode) # 0:QUERY 1:IQUERY 2:STATUS
assert_equal(0, tc) # TrunCation assert_equal(0, aa) # Authoritative Answer
assert_equal(1, rd) # Recursion Desired assert_equal(0, tc) # TrunCation
assert_equal(0, ra) # Recursion Available assert_equal(1, rd) # Recursion Desired
assert_equal(0, z) # Reserved for future use assert_equal(0, ra) # Recursion Available
assert_equal(0, rcode) # 0:No-error 1:Format-error 2:Server-failure 3:Name-Error 4:Not-Implemented 5:Refused assert_equal(0, z) # Reserved for future use
assert_equal(1, qdcount) # number of entries in the question section. assert_equal(0, rcode) # 0:No-error 1:Format-error 2:Server-failure 3:Name-Error 4:Not-Implemented 5:Refused
assert_equal(0, ancount) # number of entries in the answer section. assert_equal(1, qdcount) # number of entries in the question section.
assert_equal(0, nscount) # number of entries in the authority records section. assert_equal(0, ancount) # number of entries in the answer section.
assert_equal(0, arcount) # number of entries in the additional records section. assert_equal(0, nscount) # number of entries in the authority records section.
name = [3, "foo", 7, "example", 3, "org", 0].pack("Ca*Ca*Ca*C") assert_equal(0, arcount) # number of entries in the additional records section.
assert_operator(rest, :start_with?, name) name = [3, "foo", 7, "example", 3, "org", 0].pack("Ca*Ca*Ca*C")
rest = rest[name.length..-1] assert_operator(rest, :start_with?, name)
assert_equal(4, rest.length) rest = rest[name.length..-1]
qtype, _ = rest.unpack("nn") assert_equal(4, rest.length)
assert_equal(1, qtype) # A qtype, _ = rest.unpack("nn")
assert_equal(1, qtype) # IN assert_equal(1, qtype) # A
id = id assert_equal(1, qtype) # IN
qr = 1 id = id
opcode = opcode qr = 1
aa = 0 opcode = opcode
tc = 0 aa = 0
rd = rd tc = 0
ra = 1 rd = rd
z = 0 ra = 1
rcode = 0 z = 0
qdcount = 0 rcode = 0
ancount = 1 qdcount = 0
nscount = 0 ancount = 1
arcount = 0 nscount = 0
word2 = (qr << 15) | arcount = 0
(opcode << 11) | word2 = (qr << 15) |
(aa << 10) | (opcode << 11) |
(tc << 9) | (aa << 10) |
(rd << 8) | (tc << 9) |
(ra << 7) | (rd << 8) |
(z << 4) | (ra << 7) |
rcode (z << 4) |
msg = [id, word2, qdcount, ancount, nscount, arcount].pack("nnnnnn") rcode
type = 1 msg = [id, word2, qdcount, ancount, nscount, arcount].pack("nnnnnn")
klass = 1 type = 1
ttl = 3600 klass = 1
rdlength = 4 ttl = 3600
rdata = [192,0,2,1].pack("CCCC") # 192.0.2.1 (TEST-NET address) RFC 3330 rdlength = 4
rr = [name, type, klass, ttl, rdlength, rdata].pack("a*nnNna*") rdata = [192,0,2,1].pack("CCCC") # 192.0.2.1 (TEST-NET address) RFC 3330
msg << rr rr = [name, type, klass, ttl, rdlength, rdata].pack("a*nnNna*")
u.send(msg, 0, client_address, client_port) msg << rr
result = th.value u.send(msg, 0, client_address, client_port)
}
result, _ = assert_join_threads([client_thread, server_thread])
assert_instance_of(Array, result) assert_instance_of(Array, result)
assert_equal(1, result.length) assert_equal(1, result.length)
rr = result[0] rr = result[0]
@ -106,8 +108,6 @@ class TestResolvDNS < Test::Unit::TestCase
assert_instance_of(Resolv::IPv4, rr.address) assert_instance_of(Resolv::IPv4, rr.address)
assert_equal("192.0.2.1", rr.address.to_s) assert_equal("192.0.2.1", rr.address.to_s)
assert_equal(3600, rr.ttl) assert_equal(3600, rr.ttl)
ensure
th.join
end end
} }
end end