PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 65 lines | 47 code | 13 blank | 5 comment | 1 complexity | 58a81b66ba08d81c0e51b9dc395b9567 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'
  6. class Rex::Socket::Tcp::UnitTest < Test::Unit::TestCase
  7. def test_tcp
  8. port = 65434
  9. listener = Rex::Socket.create_tcp_server( 'LocalPort' => port )
  10. client = nil
  11. begin
  12. # Connect to the temp server
  13. assert_nothing_raised {
  14. client = Rex::Socket.create_tcp(
  15. 'PeerHost' => '127.0.0.1',
  16. 'PeerPort' => port)
  17. }
  18. assert_kind_of(Rex::Socket::Tcp, client, 'kindof?')
  19. assert_equal('127.0.0.1', client.peerhost, 'peerhost')
  20. assert_equal(port, client.peerport, 'peerport')
  21. # Accept the client connection
  22. server = listener.accept
  23. assert_kind_of(Socket, server, "valid server socket connection")
  24. # do all of the tests, once for each side
  25. { 'c/s' => [client, server], 's/c' => [server, client] }.each_pair { |mode, sockets|
  26. a = sockets[0]
  27. b = sockets[1]
  28. string = "test\n"
  29. assert_equal(false, a.has_read_data?(1), "#{mode} : has_read_data?, no data")
  30. assert_equal(string.length, b.write(string), "#{mode} : write")
  31. assert_equal(true, a.has_read_data?(1), "#{mode} : has_read_data?, with data")
  32. assert_equal(string, a.recv(string.length), "#{mode} : recv")
  33. string = "string\rtest\nwith\x00null"
  34. assert_equal(string.length, a << string, "#{mode} : append")
  35. tmp = ''; tmp = b.>>
  36. assert_equal(string, tmp, "#{mode} : append (reverse)")
  37. string = "\x00foobar\x00"
  38. assert_equal(string.length, a.send(string, 0), "#{mode} : send")
  39. assert_equal(string, b.get(), "#{mode} : get")
  40. }
  41. assert_equal(true, client.shutdown(::Socket::SHUT_RD), 'client: shutdown read handle')
  42. assert_equal(true, client.shutdown(::Socket::SHUT_WR), 'client: shutdown write handle')
  43. assert_nothing_raised {
  44. client.close
  45. client = nil
  46. }
  47. ensure
  48. client.close if (client)
  49. listener.close
  50. end
  51. end
  52. end