test_telnet.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. require 'atto'
  2. include Atto::Test
  3. require_relative '../lib/telnet'
  4. require_relative '../lib/rfc1143'
  5. include Telnet::Codes
  6. assert { Telnet }
  7. class TestClient
  8. attr_reader :buffer
  9. attr_reader :iac
  10. attr_reader :out
  11. def initialize
  12. @buffer = ''
  13. @iac = nil
  14. @out = ''
  15. end
  16. # Telnet event handlers
  17. def telnet_event(type, *args)
  18. puts("Telnet event received by client: #{type}, #{args}")
  19. meth = "telnet_#{type}".to_sym
  20. self.send(meth, *args)
  21. end
  22. def telnet_send_data(zbuf)
  23. @out << zbuf
  24. end
  25. def telnet_environment(fb, vars)
  26. p fb,vars
  27. end
  28. def telnet_environment(fb, vars)
  29. p fb,vars
  30. end
  31. def telnet_mssp(vars)
  32. @mssp_vars = vars
  33. end
  34. def telnet_ttype_is(term)
  35. @term = term
  36. p "term #{@term}"
  37. end
  38. def telnet_ttype_send(term)
  39. p "term #{term} sent"
  40. end
  41. def telnet_compress(compress)
  42. p "compress #{compress} set"
  43. end
  44. def telnet_subnegotiate(sb_telopt, buffer)
  45. p "received subnegotiate #{sb_telopt} #{buffer}"
  46. end
  47. def telnet_data(data)
  48. @buffer << data
  49. p "received data #{data}"
  50. end
  51. def telnet_iac(byte)
  52. p "received iac #{byte}"
  53. end
  54. def telnet_will(opt)
  55. p "received will #{opt}"
  56. end
  57. def telnet_do(opt)
  58. p "received do #{opt}"
  59. end
  60. def telnet_wont(opt)
  61. p "received wont #{opt}"
  62. end
  63. def telnet_dont(opt)
  64. p "received dont #{opt}"
  65. end
  66. end
  67. assert do
  68. cl = TestClient.new()
  69. tn = Telnet.new(cl)
  70. tn
  71. end
  72. assert do
  73. cl = TestClient.new()
  74. tn = Telnet.new(cl)
  75. tn.telnet_receive("Hello")
  76. tn.telnet_receive(" World")
  77. cl.buffer == "Hello World"
  78. end
  79. assert do
  80. cl = TestClient.new()
  81. tn = Telnet.new(cl)
  82. tn.telnet_receive([TELNET_IAC, TELNET_TELOPT_ECHO].pack('c*'))
  83. end
  84. assert do
  85. cl = TestClient.new()
  86. tn = Telnet.new(cl)
  87. tn.telnet_send_negotiate(TELNET_DO, TELNET_TELOPT_TTYPE)
  88. p cl.out
  89. end
  90. assert do
  91. cl = TestClient.new()
  92. tn = Telnet.new(cl)
  93. tn.telnet_receive([TELNET_IAC, TELNET_WILL, TELNET_TELOPT_NAWS].pack('c*'))
  94. p cl.out
  95. end