summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb
blob: b5f5942668bd5b95816565f47593979f7c919a9f (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
require_relative '../spec_helper'
require_relative '../fixtures/classes'
require 'ipaddr'

describe 'Socket.gethostbyaddr' do
  describe 'using an IPv4 address' do
    before do
      @addr = IPAddr.new('127.0.0.1').hton
    end

    describe 'without an explicit address family' do
      it 'returns an Array' do
        Socket.gethostbyaddr(@addr).should be_an_instance_of(Array)
      end

      describe 'the returned Array' do
        before do
          @array = Socket.gethostbyaddr(@addr)
        end

        # RubyCI Solaris 11x defines 127.0.0.1 as unstable11x
        platform_is_not :"solaris2.11" do
          it 'includes the hostname as the first value' do
            @array[0].should == SocketSpecs.hostname_reverse_lookup
          end
        end

        it 'includes the aliases as the 2nd value' do
          @array[1].should be_an_instance_of(Array)

          @array[1].each do |val|
            val.should be_an_instance_of(String)
          end
        end

        it 'includes the address type as the 3rd value' do
          @array[2].should == Socket::AF_INET
        end

        it 'includes all address strings as the remaining values' do
          @array[3].should == @addr

          @array[4..-1].each do |val|
            val.should be_an_instance_of(String)
          end
        end
      end
    end

    describe 'with an explicit address family' do
      it 'returns an Array when using a Fixnum as the address family' do
        Socket.gethostbyaddr(@addr, Socket::AF_INET).should be_an_instance_of(Array)
      end

      it 'returns an Array when using a Symbol as the address family' do
        Socket.gethostbyaddr(@addr, :INET).should be_an_instance_of(Array)
      end

      it 'raises SocketError when the address is not supported by the family' do
        lambda { Socket.gethostbyaddr(@addr, :INET6) }.should raise_error(SocketError)
      end
    end
  end

  guard -> { SocketSpecs.ipv6_available? } do
    describe 'using an IPv6 address' do
      before do
        @addr = IPAddr.new('::1').hton
      end

      describe 'without an explicit address family' do
        it 'returns an Array' do
          Socket.gethostbyaddr(@addr).should be_an_instance_of(Array)
        end

        describe 'the returned Array' do
          before do
            @array = Socket.gethostbyaddr(@addr)
          end

          it 'includes the hostname as the first value' do
            @array[0].should == SocketSpecs.hostname_reverse_lookup("::1")
          end

          it 'includes the aliases as the 2nd value' do
            @array[1].should be_an_instance_of(Array)

            @array[1].each do |val|
              val.should be_an_instance_of(String)
            end
          end

          it 'includes the address type as the 3rd value' do
            @array[2].should == Socket::AF_INET6
          end

          it 'includes all address strings as the remaining values' do
            @array[3].should be_an_instance_of(String)

            @array[4..-1].each do |val|
              val.should be_an_instance_of(String)
            end
          end
        end
      end

      describe 'with an explicit address family' do
        it 'returns an Array when using a Fixnum as the address family' do
          Socket.gethostbyaddr(@addr, Socket::AF_INET6).should be_an_instance_of(Array)
        end

        it 'returns an Array when using a Symbol as the address family' do
          Socket.gethostbyaddr(@addr, :INET6).should be_an_instance_of(Array)
        end

        platform_is_not :windows do
          it 'raises SocketError when the address is not supported by the family' do
            lambda { Socket.gethostbyaddr(@addr, :INET) }.should raise_error(SocketError)
          end
        end
      end
    end
  end
end