PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/lib/rex/socket_spec.rb

https://github.com/Jonono2/metasploit-framework
Ruby | 166 lines | 140 code | 25 blank | 1 comment | 13 complexity | d8cf602310420a1511f325629ddf777c MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-3.0, LGPL-2.1, GPL-2.0
  1. # -*- coding:binary -*-
  2. require 'rex/socket/range_walker'
  3. describe Rex::Socket do
  4. describe '.addr_itoa' do
  5. context 'with explicit v6' do
  6. it "should convert a number to a human-readable IPv6 address" do
  7. described_class.addr_itoa(1, true).should == "::1"
  8. end
  9. end
  10. context 'with explicit v4' do
  11. it "should convert a number to a human-readable IPv4 address" do
  12. described_class.addr_itoa(1, false).should == "0.0.0.1"
  13. end
  14. end
  15. context 'without explicit version' do
  16. it "should convert a number within the range of possible v4 addresses to a human-readable IPv4 address" do
  17. described_class.addr_itoa(0).should == "0.0.0.0"
  18. described_class.addr_itoa(1).should == "0.0.0.1"
  19. described_class.addr_itoa(0xffff_ffff).should == "255.255.255.255"
  20. end
  21. it "should convert a number larger than possible v4 addresses to a human-readable IPv6 address" do
  22. described_class.addr_itoa(0xfe80_0000_0000_0000_0000_0000_0000_0001).should == "fe80::1"
  23. described_class.addr_itoa(0x1_0000_0001).should == "::1:0:1"
  24. end
  25. end
  26. end
  27. describe '.addr_aton' do
  28. subject(:nbo) do
  29. described_class.addr_aton(try)
  30. end
  31. context 'with ipv6' do
  32. let(:try) { "fe80::1" }
  33. it { should be_a(String) }
  34. it { should have(16).bytes }
  35. it "should be in the right order" do
  36. nbo.should == "\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
  37. end
  38. end
  39. context 'with ipv4' do
  40. let(:try) { "127.0.0.1" }
  41. it { should be_a(String) }
  42. it { should have(4).bytes }
  43. it "should be in the right order" do
  44. nbo.should == "\x7f\x00\x00\x01"
  45. end
  46. end
  47. context 'with a hostname' do
  48. let(:try) { "localhost" }
  49. it "should resolve" do
  50. nbo.should be_a(String)
  51. nbo.encoding.should == Encoding.find('binary')
  52. [ 4, 16 ].should include(nbo.length)
  53. end
  54. end
  55. end
  56. describe '.compress_address' do
  57. subject(:compressed) do
  58. described_class.compress_address(try)
  59. end
  60. context 'with lots of single 0s' do
  61. let(:try) { "fe80:0:0:0:0:0:0:1" }
  62. it { should == "fe80::1" }
  63. end
  64. end
  65. describe '.getaddress' do
  66. subject { described_class.getaddress('whatever') }
  67. before(:each) do
  68. Socket.stub(:gethostbyname).and_return(['name', ['aliases'], response_afamily, *response_addresses])
  69. end
  70. context 'when ::Socket.gethostbyname returns IPv4 responses' do
  71. let(:response_afamily) { Socket::AF_INET }
  72. let(:response_addresses) { ["\x01\x01\x01\x01", "\x02\x02\x02\x02"] }
  73. it { should be_a(String) }
  74. it "should return the first ASCII address" do
  75. subject.should == "1.1.1.1"
  76. end
  77. end
  78. context 'when ::Socket.gethostbyname returns IPv6 responses' do
  79. let(:response_afamily) { Socket::AF_INET6 }
  80. let(:response_addresses) { ["\xfe\x80"+("\x00"*13)+"\x01", "\xfe\x80"+("\x00"*13)+"\x02"] }
  81. it { should be_a(String) }
  82. it "should return the first ASCII address" do
  83. subject.should == "fe80::1"
  84. end
  85. end
  86. context "with rubinius' bug returning ASCII addresses" do
  87. let(:response_afamily) { Socket::AF_INET }
  88. let(:response_addresses) { ["1.1.1.1", "2.2.2.2"] }
  89. it { should be_a(String) }
  90. it "should return the first ASCII address" do
  91. subject.should == "1.1.1.1"
  92. end
  93. end
  94. end
  95. describe '.getaddresses' do
  96. subject { described_class.getaddresses('whatever') }
  97. before(:each) do
  98. Socket.stub(:gethostbyname).and_return(['name', ['aliases'], response_afamily, *response_addresses])
  99. end
  100. context 'when ::Socket.gethostbyname returns IPv4 responses' do
  101. let(:response_afamily) { Socket::AF_INET }
  102. let(:response_addresses) { ["\x01\x01\x01\x01", "\x02\x02\x02\x02"] }
  103. it { should be_a(Array) }
  104. it { should have(2).addresses }
  105. it "should return the ASCII addresses" do
  106. subject.should include("1.1.1.1")
  107. subject.should include("2.2.2.2")
  108. end
  109. end
  110. context 'when ::Socket.gethostbyname returns IPv6 responses' do
  111. let(:response_afamily) { Socket::AF_INET6 }
  112. let(:response_addresses) { ["\xfe\x80"+("\x00"*13)+"\x01", "\xfe\x80"+("\x00"*13)+"\x02"] }
  113. it { should be_a(Array) }
  114. it { should have(2).addresses }
  115. it "should return the ASCII addresses" do
  116. subject.should include("fe80::1")
  117. subject.should include("fe80::2")
  118. end
  119. end
  120. context "with rubinius' bug returning ASCII addresses" do
  121. let(:response_afamily) { Socket::AF_INET }
  122. let(:response_addresses) { ["1.1.1.1", "2.2.2.2"] }
  123. it { should be_a(Array) }
  124. it { should have(2).addresses }
  125. it "should return the ASCII addresses" do
  126. subject.should include("1.1.1.1")
  127. subject.should include("2.2.2.2")
  128. end
  129. end
  130. end
  131. end