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

platform_is_not :aix, :"solaris2.10" do
describe 'Socket.getifaddrs' do
  before do
    @ifaddrs = Socket.getifaddrs
  end

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

  describe 'the returned Array' do
    it 'should not be empty' do
      @ifaddrs.should_not be_empty
    end

    it 'contains instances of Socket::Ifaddr' do
      @ifaddrs.each do |ifaddr|
        ifaddr.should be_an_instance_of(Socket::Ifaddr)
      end
    end
  end

  describe 'each returned Socket::Ifaddr' do
    it 'has an interface index' do
      @ifaddrs.each do |ifaddr|
        ifaddr.ifindex.should be_an_instance_of(Fixnum)
      end
    end

    it 'has an interface name' do
      @ifaddrs.each do |ifaddr|
        ifaddr.name.should be_an_instance_of(String)
      end
    end

    it 'has a set of flags' do
      @ifaddrs.each do |ifaddr|
        ifaddr.flags.should be_an_instance_of(Fixnum)
      end
    end
  end

  describe 'the Socket::Ifaddr address' do
    before do
      @addrs = @ifaddrs.map(&:addr).compact
    end

    it 'is an Addrinfo' do
      @addrs.each do |addr|
        addr.should be_an_instance_of(Addrinfo)
      end
    end

    it 'has an address family' do
      @addrs.each do |addr|
        addr.afamily.should be_an_instance_of(Fixnum)
        addr.afamily.should_not == Socket::AF_UNSPEC
      end
    end
  end

  platform_is_not :windows do
    describe 'the Socket::Ifaddr broadcast address' do
      before do
        @addrs = @ifaddrs.map(&:broadaddr).compact
      end

      it 'is an Addrinfo' do
        @addrs.each do |addr|
          addr.should be_an_instance_of(Addrinfo)
        end
      end

      it 'has an address family' do
        @addrs.each do |addr|
          addr.afamily.should be_an_instance_of(Fixnum)
          addr.afamily.should_not == Socket::AF_UNSPEC
        end
      end
    end

    describe 'the Socket::Ifaddr netmask address' do
      before do
        @addrs = @ifaddrs.map(&:netmask).compact.select(&:ip?)
      end

      it 'is an Addrinfo' do
        @addrs.each do |addr|
          addr.should be_an_instance_of(Addrinfo)
        end
      end

      it 'has an address family' do
        @addrs.each do |addr|
          addr.afamily.should be_an_instance_of(Fixnum)
          addr.afamily.should_not == Socket::AF_UNSPEC
        end
      end

      it 'has an IP address' do
        @addrs.each do |addr|
          addr.ip_address.should be_an_instance_of(String)
        end
      end
    end
  end
end
end