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

/lib/rex/socket.rb.ut.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 108 lines | 78 code | 17 blank | 13 comment | 0 complexity | 7bc845e13e3c7288ff72dcf65f50ef7a MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, MIT
  1. #!/usr/bin/env ruby
  2. # -*- coding: binary -*-
  3. $:.unshift(File.join(File.dirname(__FILE__), '..'))
  4. require 'test/unit'
  5. require 'rex/socket'
  6. require 'rex/socket/tcp'
  7. class Rex::Socket::UnitTest < Test::Unit::TestCase
  8. def test_ip
  9. assert_equal(true,Rex::Socket.dotted_ip?('0.0.0.0'), 'valid IP min')
  10. assert_equal(true,Rex::Socket.dotted_ip?('255.255.255.255'), 'valid IP max')
  11. assert_equal(false,Rex::Socket.dotted_ip?('0.0.0.0.0'), 'too many sections')
  12. assert_equal(false,Rex::Socket.dotted_ip?('0..0.0.0'), 'too many dots')
  13. assert_equal(false,Rex::Socket.dotted_ip?('00.0.0'), 'not enough dots')
  14. assert_equal(false,Rex::Socket.dotted_ip?('256.256.256.256'), 'numbers too big')
  15. end
  16. def test_create
  17. port = 64442
  18. serv = TCPServer.new('127.0.0.1', port)
  19. sock = nil
  20. assert_nothing_raised {
  21. sock = Rex::Socket.create(
  22. 'PeerHost' => '127.0.0.1',
  23. 'PeerPort' => port,
  24. 'Proto' => 'tcp')
  25. }
  26. assert_kind_of(Rex::Socket::Tcp, sock, "socket factory creation")
  27. sock = nil
  28. assert_nothing_raised {
  29. sock = Rex::Socket.create_tcp(
  30. 'PeerHost' => '127.0.0.1',
  31. 'PeerPort' => port)
  32. }
  33. assert_kind_of(Rex::Socket::Tcp, sock, "tcp socket factory creation")
  34. serv.close
  35. end
  36. def test_to_sockaddr
  37. assert_equal(([2] + [0]*14).pack("sC*"), Rex::Socket.to_sockaddr(0, 0), "null sockaddr")
  38. =begin
  39. # This is platform dependent, pain to test
  40. if (Rex::Socket.support_ipv6?)
  41. # Use the constant for AF_INET6 since it is different per platform
  42. # (10 on linux and 28 on BSD)
  43. inaddr_any_sockaddr = ([::Socket::AF_INET6, 22] + [0]*24).pack('sSC*')
  44. else
  45. inaddr_any_sockaddr = ([2, 22] + [0]*12).pack('snC*')
  46. end
  47. =end
  48. assert_equal(([2, 0x16, 1, 2, 3, 4] + [0]*8).pack('snC*'), Rex::Socket.to_sockaddr("1.2.3.4", 22), "1.2.3.4 addr, port 22 sockaddr")
  49. end
  50. def test_from_sockaddr
  51. # 1.9.1 raises ArgumentError if we don't have an af == AF_INET or AF_INET6
  52. af, host, port = Rex::Socket.from_sockaddr(([2, 0] + [0]*12).pack('snC*'))
  53. assert_equal(2, af, "af = 2")
  54. assert_equal('0.0.0.0', host, "zero host")
  55. assert_equal(0, port, "zero port")
  56. af, host, port = Rex::Socket.from_sockaddr(([2, 22]+[0]*12).pack('snC*'))
  57. assert_equal(2, af, "af = 2")
  58. assert_equal(22, port, "port = 22")
  59. assert_equal('0.0.0.0', host, "zero host")
  60. af, host, port = Rex::Socket.from_sockaddr(([2, 22, 1, 2, 3, 4] + [0]*8).pack('snC*') )
  61. assert_equal(2, af, "af = 2")
  62. assert_equal('1.2.3.4', host, "host = '1.2.3.4'")
  63. assert_equal(22, port, "port = 22")
  64. end
  65. def test_resolv_nbo
  66. assert_equal("\x04\x03\x02\x01", Rex::Socket.resolv_nbo("4.3.2.1"))
  67. end
  68. def test_net2bitmask
  69. assert_equal(32, Rex::Socket.net2bitmask('255.255.255.255'))
  70. assert_equal(28, Rex::Socket.net2bitmask('255.255.255.240'))
  71. assert_equal(24, Rex::Socket.net2bitmask('255.255.255.0'))
  72. assert_equal(16, Rex::Socket.net2bitmask('255.255.0.0'))
  73. end
  74. def test_bit2netmask
  75. assert_equal("255.255.255.255", Rex::Socket.bit2netmask(32))
  76. assert_equal("255.255.255.254", Rex::Socket.bit2netmask(31))
  77. assert_equal("255.255.255.240", Rex::Socket.bit2netmask(28))
  78. assert_equal("255.255.255.0", Rex::Socket.bit2netmask(24))
  79. assert_equal("255.255.0.0", Rex::Socket.bit2netmask(16))
  80. end
  81. def test_is_internal
  82. assert( ! Rex::Socket.is_internal?("1.2.3.4"))
  83. assert( ! Rex::Socket.is_internal?("172.15.3.4"))
  84. assert( ! Rex::Socket.is_internal?("172.32.3.4"))
  85. assert(Rex::Socket.is_internal?("10.2.3.4"))
  86. assert(Rex::Socket.is_internal?("192.168.3.4"))
  87. 16.upto(31) do |octet|
  88. assert(Rex::Socket.is_internal?("172.#{octet}.3.4"))
  89. end
  90. end
  91. end