summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/socket/gethostbyname_spec.rb
blob: 9367030e259fa1daebac3fa046bc5b2c3df42ecc (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
# -*- encoding: binary -*-
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe "Socket#gethostbyname" do
  it "returns broadcast address info for '<broadcast>'" do
    addr = Socket.gethostbyname('<broadcast>');
    addr.should == ["255.255.255.255", [], 2, "\377\377\377\377"]
  end

  it "returns broadcast address info for '<any>'" do
    addr = Socket.gethostbyname('<any>');
    addr.should == ["0.0.0.0", [], 2, "\000\000\000\000"]
  end
end

describe 'Socket.gethostbyname' do
  it 'returns an Array' do
    Socket.gethostbyname('127.0.0.1').should be_an_instance_of(Array)
  end

  describe 'the returned Array' do
    before do
      @array = Socket.gethostbyname('127.0.0.1')
    end

    it 'includes the hostname as the first value' do
      @array[0].should == '127.0.0.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
      possible = [Socket::AF_INET, Socket::AF_INET6]

      possible.include?(@array[2]).should == true
    end

    it 'includes the 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

  describe 'using <broadcast> as the input address' do
    describe 'the returned Array' do
      before do
        @addr = Socket.gethostbyname('<broadcast>')
      end

      it 'includes the broadcast address as the first value' do
        @addr[0].should == '255.255.255.255'
      end

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

      it 'includes the address string as the 4th value' do
        @addr[3].should == [255, 255, 255, 255].pack('C4')
      end
    end
  end

  describe 'using <any> as the input address' do
    describe 'the returned Array' do
      before do
        @addr = Socket.gethostbyname('<any>')
      end

      it 'includes the wildcard address as the first value' do
        @addr[0].should == '0.0.0.0'
      end

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

      it 'includes the address string as the 4th value' do
        @addr[3].should == [0, 0, 0, 0].pack('C4')
      end
    end
  end

  describe 'using an IPv4 address' do
    describe 'the returned Array' do
      before do
        @addr = Socket.gethostbyname('127.0.0.1')
      end

      it 'includes the IP address as the first value' do
        @addr[0].should == '127.0.0.1'
      end

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

      it 'includes the address string as the 4th value' do
        @addr[3].should == [127, 0, 0, 1].pack('C4')
      end
    end
  end

  guard -> { SocketSpecs.ipv6_available? } do
    describe 'using an IPv6 address' do
      describe 'the returned Array' do
        before do
          @addr = Socket.gethostbyname('::1')
        end

        it 'includes the IP address as the first value' do
          @addr[0].should == '::1'
        end

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

        it 'includes the address string as the 4th value' do
          @addr[3].should == [0, 0, 0, 0, 0, 0, 0, 1].pack('n8')
        end
      end
    end
  end
end