Project

General

Profile

Bug #10908 ยป initialize_spec.rb

yorickpeterse (Yorick Peterse), 02/26/2015 11:36 AM

 
1
require 'socket'
2

    
3
describe 'Addrinfo#initialize' do
4
  describe 'using separate arguments for a TCP socket' do
5
    before do
6
      @sockaddr = Socket.sockaddr_in(80, '127.0.0.1')
7
    end
8

    
9
    it 'returns an Addrinfo with the correct IP' do
10
      addr = Addrinfo.new(@sockaddr)
11

    
12
      addr.ip_address.should == '127.0.0.1'
13
    end
14

    
15
    it 'returns an Addrinfo with the correct port' do
16
      addr = Addrinfo.new(@sockaddr)
17

    
18
      addr.ip_port.should == 80
19
    end
20

    
21
    it 'returns an Addrinfo with AF_INET as the default family' do
22
      addr = Addrinfo.new(@sockaddr)
23

    
24
      addr.afamily.should == Socket::AF_INET
25
    end
26

    
27
    it 'returns an Addrinfo always using AF_INET as the family' do
28
      addr = Addrinfo.new(@sockaddr, Socket::AF_INET6)
29

    
30
      addr.afamily.should == Socket::AF_INET
31
    end
32

    
33
    it 'returns an Addrinfo with the correct socket type' do
34
      addr = Addrinfo.new(@sockaddr, nil, Socket::SOCK_STREAM)
35

    
36
      addr.socktype.should == Socket::SOCK_STREAM
37
    end
38

    
39
    it 'returns an Addrinfo with the correct protocol' do
40
      addr = Addrinfo.new(@sockaddr, nil, 0, Socket::IPPROTO_TCP)
41

    
42
      addr.protocol.should == Socket::IPPROTO_TCP
43
    end
44

    
45
    describe 'with Symbols' do
46
      it 'returns an Addrinfo with :AF_INET as the family' do
47
        addr = Addrinfo.new(@sockaddr, :AF_INET)
48

    
49
        addr.afamily.should == Socket::AF_INET
50
      end
51

    
52
      it 'returns an Addrinfo with :INET as the family' do
53
        addr = Addrinfo.new(@sockaddr, :INET)
54

    
55
        addr.afamily.should == Socket::AF_INET
56
      end
57

    
58
      it 'returns an Addrinfo with :SOCK_STREAM as the socket type' do
59
        addr = Addrinfo.new(@sockaddr, nil, :SOCK_STREAM)
60

    
61
        addr.socktype.should == Socket::SOCK_STREAM
62
      end
63

    
64
      it 'returns an Addrinfo with :STREAM as the socket type' do
65
        addr = Addrinfo.new(@sockaddr, nil, :STREAM)
66

    
67
        addr.socktype.should == Socket::SOCK_STREAM
68
      end
69
    end
70

    
71
    describe 'with Strings' do
72
      it 'returns an Addrinfo with "AF_INET" as the family' do
73
        addr = Addrinfo.new(@sockaddr, 'AF_INET')
74

    
75
        addr.afamily.should == Socket::AF_INET
76
      end
77

    
78
      it 'returns an Addrinfo with "INET" as the family' do
79
        addr = Addrinfo.new(@sockaddr, 'INET')
80

    
81
        addr.afamily.should == Socket::AF_INET
82
      end
83

    
84
      it 'returns an Addrinfo with "SOCK_STREAM" as the socket type' do
85
        addr = Addrinfo.new(@sockaddr, nil, 'SOCK_STREAM')
86

    
87
        addr.socktype.should == Socket::SOCK_STREAM
88
      end
89

    
90
      it 'returns an Addrinfo with "STREAM" as the socket type' do
91
        addr = Addrinfo.new(@sockaddr, nil, 'STREAM')
92

    
93
        addr.socktype.should == Socket::SOCK_STREAM
94
      end
95
    end
96
  end
97

    
98
  describe 'using separate arguments for a Unix socket' do
99
    before do
100
      @sockaddr = Socket.pack_sockaddr_un('socket')
101
    end
102

    
103
    it 'returns an Addrinfo with the correct unix path' do
104
      Addrinfo.new(@sockaddr).unix_path.should == 'socket'
105
    end
106

    
107
    it 'returns an Addrinfo with the correct socket family' do
108
      Addrinfo.new(@sockaddr).afamily.should == Socket::AF_UNIX
109
    end
110
  end
111

    
112
  describe 'using an Array as a single argument' do
113
    # Uses AF_INET6 since AF_INET is the default, making it harder to test if
114
    # our Addrinfo actually sets the family correctly.
115
    before do
116
      @sockaddr = ['AF_INET6', 80, 'hostname', '::1']
117
    end
118

    
119
    it 'returns an Addrinfo with the correct IP' do
120
      addr = Addrinfo.new(@sockaddr)
121

    
122
      addr.ip_address.should == '::1'
123
    end
124

    
125
    it 'returns an Addrinfo with the correct family' do
126
      addr = Addrinfo.new(@sockaddr)
127

    
128
      addr.afamily.should == Socket::AF_INET6
129
    end
130

    
131
    it 'returns an Addrinfo with the correct port' do
132
      addr = Addrinfo.new(@sockaddr)
133

    
134
      addr.ip_port.should == 80
135
    end
136
  end
137
end