aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb
blob: 1bc92e33739a5f8a1cc7e5d93c234d8fe65d9d9e (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
require_relative '../spec_helper'

with_feature :ancillary_data, :pktinfo do
  describe 'Socket::AncillaryData.ip_pktinfo' do
    describe 'with a source address and index' do
      before do
        @data = Socket::AncillaryData.ip_pktinfo(Addrinfo.ip('127.0.0.1'), 4)
      end

      it 'returns a Socket::AncillaryData' do
        @data.should be_an_instance_of(Socket::AncillaryData)
      end

      it 'sets the family to AF_INET' do
        @data.family.should == Socket::AF_INET
      end

      it 'sets the level to IPPROTO_IP' do
        @data.level.should == Socket::IPPROTO_IP
      end

      it 'sets the type to IP_PKTINFO' do
        @data.type.should == Socket::IP_PKTINFO
      end
    end

    describe 'with a source address, index, and destination address' do
      before do
        source = Addrinfo.ip('127.0.0.1')
        dest   = Addrinfo.ip('127.0.0.5')
        @data  = Socket::AncillaryData.ip_pktinfo(source, 4, dest)
      end

      it 'returns a Socket::AncillaryData' do
        @data.should be_an_instance_of(Socket::AncillaryData)
      end

      it 'sets the family to AF_INET' do
        @data.family.should == Socket::AF_INET
      end

      it 'sets the level to IPPROTO_IP' do
        @data.level.should == Socket::IPPROTO_IP
      end

      it 'sets the type to IP_PKTINFO' do
        @data.type.should == Socket::IP_PKTINFO
      end
    end
  end

  describe 'Socket::AncillaryData#ip_pktinfo' do
    describe 'using an Addrinfo without a port number' do
      before do
        @source = Addrinfo.ip('127.0.0.1')
        @dest   = Addrinfo.ip('127.0.0.5')
        @data   = Socket::AncillaryData.ip_pktinfo(@source, 4, @dest)
      end

      it 'returns an Array' do
        @data.ip_pktinfo.should be_an_instance_of(Array)
      end

      describe 'the returned Array' do
        before do
          @info = @data.ip_pktinfo
        end

        it 'stores an Addrinfo at index 0' do
          @info[0].should be_an_instance_of(Addrinfo)
        end

        it 'stores the ifindex at index 1' do
          @info[1].should be_an_instance_of(Fixnum)
        end

        it 'stores an Addrinfo at index 2' do
          @info[2].should be_an_instance_of(Addrinfo)
        end
      end

      describe 'the source Addrinfo' do
        before do
          @addr = @data.ip_pktinfo[0]
        end

        it 'uses the correct IP address' do
          @addr.ip_address.should == '127.0.0.1'
        end

        it 'is not the same object as the input Addrinfo' do
          @addr.should_not == @source
        end
      end

      describe 'the ifindex' do
        it 'is a Fixnum' do
          @data.ip_pktinfo[1].should == 4
        end
      end

      describe 'the destination Addrinfo' do
        before do
          @addr = @data.ip_pktinfo[2]
        end

        it 'uses the correct IP address' do
          @addr.ip_address.should == '127.0.0.5'
        end

        it 'is not the same object as the input Addrinfo' do
          @addr.should_not == @dest
        end
      end
    end

    describe 'using an Addrinfo with a port number' do
      before do
        @source = Addrinfo.tcp('127.0.0.1', 80)
        @dest   = Addrinfo.tcp('127.0.0.5', 85)
        @data   = Socket::AncillaryData.ip_pktinfo(@source, 4, @dest)
      end

      describe 'the source Addrinfo' do
        before do
          @addr = @data.ip_pktinfo[0]
        end

        it 'does not contain a port number' do
          @addr.ip_port.should == 0
        end
      end

      describe 'the destination Addrinfo' do
        before do
          @addr = @data.ip_pktinfo[2]
        end

        it 'does not contain a port number' do
          @addr.ip_port.should == 0
        end
      end
    end
  end
end