PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rex/text.rb.ut.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 193 lines | 168 code | 22 blank | 3 comment | 0 complexity | e7056afd2f7c0fb965e5e89b81edb7b5 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/text'
  6. require 'rex/exceptions'
  7. class Rex::Text::UnitTest < Test::Unit::TestCase
  8. def test_pattern_create
  9. set1 = %w{AB ab 12}
  10. assert_equal 'Aa1Aa2', Rex::Text.pattern_create(6,set1)
  11. set2 = %w{ABC abc 123}
  12. assert_equal 'Aa1Aa2Aa3Ab1Ab2Ab3', Rex::Text.pattern_create(18,set2)
  13. assert_equal 'Zz8Zz9', Rex::Text.pattern_create(20280)[-6,6] # Bug #2952
  14. end
  15. def test_pattern_create_silly
  16. assert_equal "", Rex::Text.pattern_create(nil)
  17. assert_equal "", Rex::Text.pattern_create(0)
  18. assert_equal 'AAAAAAAAA', Rex::Text.pattern_create(9,['A'])
  19. end
  20. def test_uri_encode
  21. srand(0)
  22. assert_equal('A1%21', Rex::Text.uri_encode('A1!'), 'uri encode')
  23. assert_equal('A1!', Rex::Text.uri_encode('A1!', 'none'), 'uri encode: none')
  24. assert_equal('A1%21', Rex::Text.uri_encode('A1!', 'hex-normal'), 'uri encode: hex-normal')
  25. assert_equal('%41%31%21', Rex::Text.uri_encode('A1!', 'hex-all'), 'uri encode: hex-all')
  26. assert_equal('A1%u01c3', Rex::Text.uri_encode('A1!', 'u-normal'), 'uri encode: u-normal')
  27. assert_equal('%uff21%u2081%uff01', Rex::Text.uri_encode('A1!', 'u-all'), 'uri encode: u-all')
  28. srand(0)
  29. assert_equal("%uff2d%uff49%uff43%uff52%uff4f%uff53%uff4f%uff46%uff54%u2004%uff45%uff4e%uff43%uff4f%uff44%uff49%uff4e%uff47%u3000%uff44%uff52%uff49%uff56%uff45%uff53%u2005%uff4d%uff45%u2000%uff43%uff52%uff41%uff5a%uff59%uff01", Rex::Text.uri_encode('Microsoft encoding drives me crazy!', 'u-half'))
  30. assert_raises(TypeError) {
  31. Rex::Text.uri_encode('a', 'umpa lumpa')
  32. }
  33. end
  34. def test_html_encode
  35. assert_equal('&#x41;', Rex::Text.html_encode('A'), 'html_encode default')
  36. assert_equal('&#x41;', Rex::Text.html_encode('A','hex'), 'html_encode hex')
  37. assert_equal('&#65;', Rex::Text.html_encode('A','int'), 'html_encode int')
  38. assert_equal('&#0000065;', Rex::Text.html_encode('A','int-wide'), 'html_encode int-wide')
  39. assert_raises(TypeError) {
  40. Rex::Text.html_encode('a', 'umpa lumpa')
  41. }
  42. end
  43. def test_rand_text
  44. srand(0)
  45. assert_equal("\254/u\300C\373\303g\t\323", Rex::Text.rand_text(10), 'rand text 1')
  46. assert_equal("\025\362$WF\330X\214:\301", Rex::Text.rand_text(10), 'rand text 2')
  47. assert_equal("\346'W\256XQ\245\031MH", Rex::Text.rand_text(10), 'rand text 3')
  48. assert_equal('bababbabba', Rex::Text.rand_text(10, nil, 'ab'), 'rand text with specified "good"')
  49. assert_equal('MA', Rex::Text.rand_state(), 'rand state')
  50. assert_equal('xzdttongb.5gfk0xjly3.aak.fmo0rp.com', Rex::Text.rand_hostname(), 'rand hostname')
  51. assert_equal('9aaf811799', Rex::Text.rand_text_hex(10), 'rand hex')
  52. end
  53. def test_unicode
  54. assert_equal("a\x00b\x00c\x00", Rex::Text.to_unicode('abc'), 'unicode, default = little endian')
  55. assert_equal("a\x00b\x00c\x00", Rex::Text.to_unicode('abc', 'utf-16le'), 'utf-16le')
  56. assert_equal("\x00a\x00b\x00c", Rex::Text.to_unicode('abc', 'utf-16be'), 'utf-16be')
  57. assert_equal("a\x00\x00\x00b\x00\x00\x00c\x00\x00\x00", Rex::Text.to_unicode('abc', 'utf-32le'), 'utf-32le')
  58. assert_equal("\x00\x00\x00a\x00\x00\x00b\x00\x00\x00c", Rex::Text.to_unicode('abc', 'utf-32be'), 'utf-32be')
  59. assert_equal("abc+-abc-+AAA-", Rex::Text.to_unicode("abc+abc-\x00", 'utf-7'), 'utf-7')
  60. assert_equal("+AGE-+AGI-+AGM-+ACs-+AGE-+AGI-+AGM-+AC0-+AAA-", Rex::Text.to_unicode("abc+abc-\x00", 'utf-7', 'all'), 'utf-7-all')
  61. assert_equal("a\303\272", Rex::Text.to_unicode("a\xFA", 'utf-8'))
  62. assert_equal("\xC1\xA1", Rex::Text.to_unicode('a', 'utf-8', 'overlong', 2), 'utf-8 overlong')
  63. assert_equal("\xE0\x81\xA1", Rex::Text.to_unicode('a', 'utf-8', 'overlong', 3), 'utf-8 overlong')
  64. assert_equal("\xF0\x80\x81\xA1", Rex::Text.to_unicode('a', 'utf-8', 'overlong', 4), 'utf-8 overlong')
  65. assert_equal("\xF8\x80\x80\x81\xA1", Rex::Text.to_unicode('a', 'utf-8', 'overlong', 5), 'utf-8 overlong')
  66. assert_equal("\xFC\x80\x80\x80\x81\xA1", Rex::Text.to_unicode('a', 'utf-8', 'overlong', 6), 'utf-8 overlong')
  67. assert_equal("\xFE\x80\x80\x80\x80\x81\xA1", Rex::Text.to_unicode('a', 'utf-8', 'overlong', 7), 'utf-8 overlong')
  68. 100.times {
  69. assert(["\xC1\x21","\xC1\x61","\xC1\xE1"].include?(Rex::Text.to_unicode('a', 'utf-8', 'invalid')), 'utf-8 invalid')
  70. assert(["\xE0\x01\x21","\xE0\x01\x61","\xE0\x01\xA1","\xE0\x01\xE1","\xE0\x41\x21","\xE0\x41\x61","\xE0\x41\xA1","\xE0\x41\xE1","\xE0\x81\x21","\xE0\x81\x61","\xE0\x81\xA1","\xE0\x81\xE1","\xE0\xC1\x21","\xE0\xC1\x61","\xE0\xC1\xA1","\xE0\xC1\xE1"].include?(Rex::Text.to_unicode('a', 'utf-8', 'invalid', 3)), 'utf-8 invalid 3 byte')
  71. }
  72. a = ["\xC1\x21","\xC1\x61","\xC1\xE1"]
  73. 10.times {
  74. encoded = Rex::Text.to_unicode('a', 'utf-8', 'invalid')
  75. if a.include?(encoded)
  76. a.delete(encoded)
  77. end
  78. }
  79. assert_equal([], a, 'all possible values')
  80. assert_raises(TypeError) {
  81. Rex::Text.to_unicode('a', 'utf-8', '', 8)
  82. }
  83. assert_raises(TypeError) {
  84. Rex::Text.to_unicode('a', 'utf-8', 'foo', 6)
  85. }
  86. assert_raises(TypeError) {
  87. Rex::Text.to_unicode('a', 'uhwtfms', -1)
  88. }
  89. 100.times {
  90. assert(["\x01\x00","\x01\x02","\x01\x04","\x01\xcd","\x01\xde","\xff\x21"].include?(Rex::Text.to_unicode('A', 'uhwtfms')), 'uhwtfms')
  91. assert(["\x00\xc0","\x00\xc1","\x00\xc2","\x00\xc3","\x00\xc4","\x00\xc5"].include?(Rex::Text.to_unicode('A', 'uhwtfms', 949)), 'uhwtfms codepage 949')
  92. }
  93. a = ["\x01\x00","\x01\x02","\x01\x04","\x01\xcd","\x01\xde","\xff\x21"]
  94. 20.times {
  95. encoded = Rex::Text.to_unicode('A', 'uhwtfms')
  96. if a.include?(encoded)
  97. a.delete(encoded)
  98. end
  99. }
  100. assert_equal([], a, 'all possible values uhwtfms')
  101. assert_raises(TypeError) {
  102. Rex::Text.to_unicode('a', 'uhwtfms-half', 1)
  103. }
  104. assert_equal("\xFF\x01", Rex::Text.to_unicode('!', 'uhwtfms-half'))
  105. srand(0)
  106. assert_equal("\xff\x2d\xff\x49\xff\x43\xff\x52\xff\x4f\xff\x53\xff\x4f\xff\x46\xff\x54\x20\x04\xff\x45\xff\x4e\xff\x43\xff\x4f\xff\x44\xff\x49\xff\x4e\xff\x47\x30\x00\xff\x44\xff\x52\xff\x49\xff\x56\xff\x45\xff\x53\x20\x05\xff\x4d\xff\x45\x20\x00\xff\x43\xff\x52\xff\x41\xff\x5a\xff\x59\xff\x01", Rex::Text.to_unicode('Microsoft encoding drives me crazy!', 'uhwtfms-half'))
  107. end
  108. def test_zlib
  109. assert_equal("x\332\313\310T\310\315\317\005\000\a\225\002;", Rex::Text.zlib_deflate('hi mom'), 'compress')
  110. assert_equal('hi mom', Rex::Text.zlib_inflate("x\234\313\310T\310\315\317\005\000\a\225\002;"), 'decompress')
  111. end
  112. def test_gzip
  113. string = Rex::Text.gzip('hi mom')
  114. assert_equal("\x1f\x8b\x08\x00", string.slice!(0,4), 'gzip headers')
  115. # skip the next 6 bytes as it is host & time specific (zlib's example gun does, so why not us too?)
  116. string.slice!(0,6)
  117. assert_equal("\xcb\xc8\x54\xc8\xcd\xcf\x05\x00\x68\xa4\x1c\xf0\x06\x00\x00\x00", string, 'gzip data')
  118. assert_equal('hi mom', Rex::Text.ungzip("\037\213\010\000|\261\275C\002\003\313\310T\310\315\317\005\000h\244\034\360\006\000\000\000"), 'ungzip')
  119. end
  120. def test_badchar_index
  121. assert_equal(nil, Rex::Text.badchar_index('abcdef', 'gzk'))
  122. assert_equal(2, Rex::Text.badchar_index('123avd', 'ly3'))
  123. end
  124. def test_hexify
  125. str = "\x01\x02\xff\x00"
  126. assert_equal("\\x01\\x02\\xff\\x00", Rex::Text.to_hex(str), 'to_hex')
  127. assert_equal("ABC01ABC02ABCffABC00", Rex::Text.to_hex(str, 'ABC'), 'to_hex with prefix')
  128. assert_equal('%u0102%uff00', Rex::Text.to_hex(str, '%u', 2), 'to_hex with chunk size of 2')
  129. # to_hex, without providing enouigh data to chunk on a given size
  130. assert_raises(RuntimeError){ Rex::Text.to_hex('a', '', 2) }
  131. assert_equal("buf = \n\"\\x01\\x02\\xff\\x00\"\n", Rex::Text.to_ruby(str), 'to_ruby')
  132. assert_equal("my $buf = \n\"\\x01\\x02\\xff\\x00\";\n", Rex::Text.to_perl(str), 'to_perl')
  133. assert_equal("export buf=\\\n$'\\x01\\x02\\xff\\x00\'\n", Rex::Text.to_bash(str), 'to_bash')
  134. assert_equal("unsigned char buf[] = \n\"\\x01\\x02\\xff\\x00\";\n", Rex::Text.to_c(str), 'to_c')
  135. # 0 -> 20
  136. str = "\000\001\002\003\004\005\006\a\010\t\n\v\f\r\016\017\020\021\022\023"
  137. assert_equal("buf = \n\"\\x00\\x01\\x02\\x03\" +\n\"\\x04\\x05\\x06\\x07\" +\n\"\\x08\\x09\\x0a\\x0b\" +\n\"\\x0c\\x0d\\x0e\\x0f\" +\n\"\\x10\\x11\\x12\\x13\"\n", Rex::Text.to_ruby(str, 20), 'to_ruby with wrap')
  138. assert_equal("my $buf = \n\"\\x00\\x01\\x02\\x03\" .\n\"\\x04\\x05\\x06\\x07\" .\n\"\\x08\\x09\\x0a\\x0b\" .\n\"\\x0c\\x0d\\x0e\\x0f\" .\n\"\\x10\\x11\\x12\\x13\";\n", Rex::Text.to_perl(str, 20), 'to_perl with wrap')
  139. assert_equal("export buf=\\\n$'\\x00\\x01\\x02\\x03\'\\\n$'\\x04\\x05\\x06\\x07\'\\\n$'\\x08\\x09\\x0a\\x0b'\\\n$'\\x0c\\x0d\\x0e\\x0f'\\\n$'\\x10\\x11\\x12\\x13\'\n", Rex::Text.to_bash(str, 20), 'to_bash with wrap')
  140. assert_equal("unsigned char buf[] = \n\"\\x00\\x01\\x02\\x03\\x04\"\n\"\\x05\\x06\\x07\\x08\\x09\"\n\"\\x0a\\x0b\\x0c\\x0d\\x0e\"\n\"\\x0f\\x10\\x11\\x12\\x13\";\n", Rex::Text.to_c(str, 20, "buf"), 'to_c with wrap')
  141. assert_equal("\\x0a", Rex::Text.to_hex("\n"), 'to_hex newline')
  142. str = "\x05Hello\x06World!\x03\x41\x42\x43"
  143. assert_equal("\\x05Hello\\x06World!\\x03ABC", Rex::Text.to_hex_ascii(str))
  144. end
  145. def test_xml_char_encode
  146. str = "\x05hello\x06world"
  147. assert_equal("&#x05;hello&#x06;world", Rex::Text.xml_char_encode(str))
  148. end
  149. def test_wordwrap
  150. txt = "this is a test of the word wrap features"
  151. assert_equal("this is a \ntest of \nthe word \nwrap \nfeatures\n", Rex::Text.wordwrap(txt, 0, 10))
  152. end
  153. def test_transforms
  154. assert_equal("acbd18db4cc2f85cedef654fccc4a4d8", Rex::Text.md5('foo'))
  155. end
  156. end