# encoding: utf-8 # # This file is part of the pincerna gem. Copyright (C) 2013 and above Shogun . # Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit. # require "spec_helper" describe Pincerna::Ip do subject { Pincerna::Ip.new("", "yml") } before(:each) do @ifconfig = < mtu 16384 options=3 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 gif0: flags=8010 mtu 1280 stf0: flags=0<> mtu 1280 en0: flags=8863 mtu 1500 options=2b ether 11:22:33:44:55:66 media: autoselect (none) status: inactive en1: flags=8863 mtu 1500 ether 11:22:33:44:55:66 inet6 fe80::6aa8:6dff:fe2e:5afc%en1 prefixlen 64 scopeid 0x5 inet 192.168.1.21 netmask 0xffffff00 broadcast 192.168.1.255 media: autoselect status: active fw0: flags=8822 mtu 4078 lladdr 11:22:33:44:55:66:77:88 media: autoselect status: inactive p2p0: flags=8843 mtu 2304 ether 11:22:33:44:55:66 media: autoselect status: inactive utun0: flags=8051 mtu 1380 inet6 fe80::b5bc:eb1b:ee64:cd82%utun0 prefixlen 64 scopeid 0x8 inet6 fd3e:ebdd:9d87:a608:b5bc:eb1b:ee64:cd82 prefixlen 64 EOIP @networksetup = < "Ethernet", "fw0" => "FireWire", "en1" => "Wi-Fi", "en2" => "Bluetooth PAN", "lo0" => "Loopback"}) expect(subject).to receive(:execute_command).with("/sbin/ifconfig").and_return(@ifconfig) end it "should return a list of addresses" do expect(subject.get_local_addresses).to eq([ {interface: "Loopback (lo0)", address: "fe80::1%lo0"}, {interface: "Loopback (lo0)", address: "127.0.0.1"}, {interface: "Loopback (lo0)", address: "::1"}, {interface: "Wi-Fi (en1)", address: "fe80::6aa8:6dff:fe2e:5afc%en1"}, {interface: "Wi-Fi (en1)", address: "192.168.1.21"}, {interface: "utun0", address: "fe80::b5bc:eb1b:ee64:cd82%utun0"}, {interface: "utun0", address: "fd3e:ebdd:9d87:a608:b5bc:eb1b:ee64:cd82"} ]) end end describe "#get_public_address", :vcr, :synchronous do it "should return public IP address" do address = subject.get_public_address expect(address.keys).to eq([:interface, :address]) expect(address[:interface]).to be_nil expect(address[:address]).to eq("8.8.8.8") end end describe "#compare_ip_classes" do it "should compare IP classes" do expect(subject.compare_ip_classes("127.0.0.1", "::1")).to eq(-1) expect(subject.compare_ip_classes("::1", "::fe80:aabb")).to eq(0) expect(subject.compare_ip_classes("::1", "127.0.0.1")).to eq(1) end end describe "#compare_ip_addresses" do it "should correctly compare IP addresses" do expect(subject.compare_ip_addresses("127.0.0.1", "::1")).to eq(-1) expect(subject.compare_ip_addresses("::1", "127.0.0.1")).to eq(1) expect(subject.compare_ip_addresses("127.0.0.1", "192.168.0.1")).to eq(-1) expect(subject.compare_ip_addresses("192.168.0.0", "192.168.0.1")).to eq(-1) expect(subject.compare_ip_addresses("192.168.0.1", "192.168.0.1")).to eq(0) expect(subject.compare_ip_addresses("127.0.0.1", "10.0.0.1")).to eq(1) expect(subject.compare_ip_addresses("::1", "::fe80:aabb")).to eq(-1) expect(subject.compare_ip_addresses("::fe80:aabe", "::fe80:aabd")).to eq(1) expect(subject.compare_ip_addresses("::fe80:aaaa", "::fe80:aaaa")).to eq(0) end end describe "#get_interfaces_names" do before(:each) do expect(subject).to receive(:execute_command).with("/usr/sbin/networksetup", "-listallhardwareports").and_return(@networksetup) end it "should return the current names of system interfaces" do expect(subject.get_interfaces_names).to eq({"en0" => "Ethernet", "fw0" => "FireWire", "en1" => "Wi-Fi", "en2" => "Bluetooth PAN", "lo0" => "Loopback"}) end end end