require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

def get_interfaces_from_ifconfig
    str = `/sbin/ifconfig`
    ary = str.split("\n\n")
    ary.map!{|e| e.split("\n")}
    ifs = Hash.new{|v| v = Array.new}
    ary.each do |elem|
        elem.map!{|e| e.squeeze(' ').strip}
    end
    ary.each do |elem|
        key = elem.first.split(' ').first
        ifs[key.to_sym] = elem.select{|e| e =~ /inet addr:/}
    end
    interfaces = {}
    ifs.each_pair do |key,value|
        unless value.empty?
            value.first.gsub!('inet addr:','inet_addr:')
            ary = value.first.split(' ')
            temp_hash = {}
            ary.each do |elem|
                k, v = elem.split(':')
                case k
                when 'Mask'
                    k = 'netmask'
                when 'Bcast'
                    k = 'broadcast'
                end
                temp_hash[k.to_sym] = v
            end
            interfaces[key] = temp_hash
        end
    end
    interfaces
end


describe System do

    context "getifaddrs" do
        before :all do
            @ifconfig_interfaces = get_interfaces_from_ifconfig
            @get_ifaddrs_interfaces = System.get_ifaddrs
        end

        it 'should return a hash' do
            expect(@get_ifaddrs_interfaces).to be_kind_of(Hash)
        end

        it 'should have same number of interfaces than system' do
            expect(@get_ifaddrs_interfaces.keys.size).to be >= @ifconfig_interfaces.keys.size
        end

        it 'should have same interfaces than system' do
            @ifconfig_interfaces.keys.each do |k|
              expect(@get_ifaddrs_interfaces).to include(k)
            end
        end
    end

    context "get_all_ifaddrs" do
      before :all do
        @ifconfig_interfaces = get_interfaces_from_ifconfig
        @get_ifaddrs_interfaces = System.get_all_ifaddrs
      end

      it 'should return an array' do
        expect(@get_ifaddrs_interfaces).to be_kind_of(Array)
      end

      it 'should have same number of interfaces than system' do
          expect(@get_ifaddrs_interfaces.size).to be >= @ifconfig_interfaces.keys.size
      end
    end
end
