summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/basicsocket/connect_address_spec.rb
blob: 18baac7f17f313fab91248e4333228bc76ad208d (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
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe 'Socket#connect_address' do
  describe 'using an unbound socket' do
    after do
      @sock.close
    end

    it 'raises SocketError' do
      @sock = Socket.new(:INET, :STREAM)

      lambda { @sock.connect_address }.should raise_error(SocketError)
    end
  end

  describe 'using a socket bound to 0.0.0.0' do
    before do
      @sock = Socket.new(:INET, :STREAM)
      @sock.bind(Socket.sockaddr_in(0, '0.0.0.0'))
    end

    after do
      @sock.close
    end

    it 'returns an Addrinfo' do
      @sock.connect_address.should be_an_instance_of(Addrinfo)
    end

    it 'uses 127.0.0.1 as the IP address' do
      @sock.connect_address.ip_address.should == '127.0.0.1'
    end

    it 'uses the correct port number' do
      @sock.connect_address.ip_port.should > 0
    end

    it 'uses AF_INET as the address family' do
      @sock.connect_address.afamily.should == Socket::AF_INET
    end

    it 'uses PF_INET as the address family' do
      @sock.connect_address.pfamily.should == Socket::PF_INET
    end

    it 'uses SOCK_STREAM as the socket type' do
      @sock.connect_address.socktype.should == Socket::SOCK_STREAM
    end

    it 'uses 0 as the protocol' do
      @sock.connect_address.protocol.should == 0
    end
  end

  guard -> { SocketSpecs.ipv6_available? } do
    describe 'using a socket bound to ::' do
      before do
        @sock = Socket.new(:INET6, :STREAM)
        @sock.bind(Socket.sockaddr_in(0, '::'))
      end

      after do
        @sock.close
      end

      it 'returns an Addrinfo' do
        @sock.connect_address.should be_an_instance_of(Addrinfo)
      end

      it 'uses ::1 as the IP address' do
        @sock.connect_address.ip_address.should == '::1'
      end

      it 'uses the correct port number' do
        @sock.connect_address.ip_port.should > 0
      end

      it 'uses AF_INET6 as the address family' do
        @sock.connect_address.afamily.should == Socket::AF_INET6
      end

      it 'uses PF_INET6 as the address family' do
        @sock.connect_address.pfamily.should == Socket::PF_INET6
      end

      it 'uses SOCK_STREAM as the socket type' do
        @sock.connect_address.socktype.should == Socket::SOCK_STREAM
      end

      it 'uses 0 as the protocol' do
        @sock.connect_address.protocol.should == 0
      end
    end
  end

  with_feature :unix_socket do
    platform_is_not :aix do
      describe 'using an unbound UNIX socket' do
        before do
          @path = SocketSpecs.socket_path
          @server = UNIXServer.new(@path)
          @client = UNIXSocket.new(@path)
        end

        after do
          @client.close
          @server.close
          rm_r(@path)
        end

        it 'raises SocketError' do
          lambda { @client.connect_address }.should raise_error(SocketError)
        end
      end
    end

    describe 'using a bound UNIX socket' do
      before do
        @path = SocketSpecs.socket_path
        @sock = UNIXServer.new(@path)
      end

      after do
        @sock.close
        rm_r(@path)
      end

      it 'returns an Addrinfo' do
        @sock.connect_address.should be_an_instance_of(Addrinfo)
      end

      it 'uses the correct socket path' do
        @sock.connect_address.unix_path.should == @path
      end

      it 'uses AF_UNIX as the address family' do
        @sock.connect_address.afamily.should == Socket::AF_UNIX
      end

      it 'uses PF_UNIX as the protocol family' do
        @sock.connect_address.pfamily.should == Socket::PF_UNIX
      end

      it 'uses SOCK_STREAM as the socket type' do
        @sock.connect_address.socktype.should == Socket::SOCK_STREAM
      end

      it 'uses 0 as the protocol' do
        @sock.connect_address.protocol.should == 0
      end
    end
  end
end