summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/addrinfo/family_addrinfo_spec.rb
blob: d3419daaaf6205e287071a8f05ecbff76a9532b2 (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
require_relative '../spec_helper'

describe 'Addrinfo#family_addrinfo' do
  it 'raises ArgumentError if no arguments are given' do
    addr = Addrinfo.tcp('127.0.0.1', 0)

    lambda { addr.family_addrinfo }.should raise_error(ArgumentError)
  end

  describe 'using multiple arguments' do
    describe 'with an IP Addrinfo' do
      before do
        @source = Addrinfo.tcp('127.0.0.1', 0)
      end

      it 'raises ArgumentError if only 1 argument is given' do
        lambda { @source.family_addrinfo('127.0.0.1') }.should raise_error(ArgumentError)
      end

      it 'raises ArgumentError if more than 2 arguments are given' do
        lambda { @source.family_addrinfo('127.0.0.1', 0, 666) }.should raise_error(ArgumentError)
      end

      it 'returns an Addrinfo when a host and port are given' do
        addr = @source.family_addrinfo('127.0.0.1', 0)

        addr.should be_an_instance_of(Addrinfo)
      end

      describe 'the returned Addrinfo' do
        before do
          @addr = @source.family_addrinfo('127.0.0.1', 0)
        end

        it 'uses the same address family as the source Addrinfo' do
          @addr.afamily.should == @source.afamily
        end

        it 'uses the same protocol family as the source Addrinfo' do
          @addr.pfamily.should == @source.pfamily
        end

        it 'uses the same socket type as the source Addrinfo' do
          @addr.socktype.should == @source.socktype
        end

        it 'uses the same protocol as the source Addrinfo' do
          @addr.protocol.should == @source.protocol
        end
      end
    end

    with_feature :unix_socket do
      describe 'with a UNIX Addrinfo' do
        before do
          @source = Addrinfo.unix('cats')
        end

        it 'raises ArgumentError if more than 1 argument is given' do
          lambda { @source.family_addrinfo('foo', 'bar') }.should raise_error(ArgumentError)
        end

        it 'returns an Addrinfo when a UNIX socket path is given' do
          addr = @source.family_addrinfo('dogs')

          addr.should be_an_instance_of(Addrinfo)
        end

        describe 'the returned Addrinfo' do
          before do
            @addr = @source.family_addrinfo('dogs')
          end

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

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

          it 'uses the given socket path' do
            @addr.unix_path.should == 'dogs'
          end
        end
      end
    end
  end

  describe 'using an Addrinfo as the 1st argument' do
    before do
      @source = Addrinfo.tcp('127.0.0.1', 0)
    end

    it 'returns the input Addrinfo' do
      input = Addrinfo.tcp('127.0.0.2', 0)
      @source.family_addrinfo(input).should == input
    end

    it 'raises ArgumentError if more than 1 argument is given' do
      input = Addrinfo.tcp('127.0.0.2', 0)
      lambda { @source.family_addrinfo(input, 666) }.should raise_error(ArgumentError)
    end

    it "raises ArgumentError if the protocol families don't match" do
      input = Addrinfo.tcp('::1', 0)
      lambda { @source.family_addrinfo(input) }.should raise_error(ArgumentError)
    end

    it "raises ArgumentError if the socket types don't match" do
      input = Addrinfo.udp('127.0.0.1', 0)
      lambda { @source.family_addrinfo(input) }.should raise_error(ArgumentError)
    end
  end
end