ronin-support 0.4.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +11 -0
  3. data/.github/workflows/ruby.yml +28 -0
  4. data/.ruby-version +1 -0
  5. data/.yardopts +1 -1
  6. data/ChangeLog.md +121 -33
  7. data/Gemfile +17 -18
  8. data/README.md +57 -33
  9. data/Rakefile +10 -3
  10. data/gemspec.yml +16 -7
  11. data/lib/ronin/binary.rb +21 -0
  12. data/lib/ronin/binary/hexdump.rb +20 -0
  13. data/lib/ronin/binary/hexdump/parser.rb +403 -0
  14. data/lib/ronin/binary/struct.rb +567 -0
  15. data/lib/ronin/binary/template.rb +454 -0
  16. data/lib/ronin/extensions.rb +5 -5
  17. data/lib/ronin/extensions/enumerable.rb +5 -5
  18. data/lib/ronin/extensions/file.rb +39 -33
  19. data/lib/ronin/extensions/ip_addr.rb +29 -31
  20. data/lib/ronin/extensions/kernel.rb +5 -5
  21. data/lib/ronin/extensions/meta.rb +5 -5
  22. data/lib/ronin/extensions/regexp.rb +50 -5
  23. data/lib/ronin/extensions/resolv.rb +7 -9
  24. data/lib/ronin/extensions/string.rb +10 -10
  25. data/lib/ronin/formatting.rb +5 -5
  26. data/lib/ronin/formatting/binary.rb +5 -5
  27. data/lib/ronin/formatting/digest.rb +5 -5
  28. data/lib/ronin/formatting/extensions.rb +5 -5
  29. data/lib/ronin/formatting/extensions/binary.rb +7 -5
  30. data/lib/ronin/formatting/extensions/binary/array.rb +61 -0
  31. data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
  32. data/lib/ronin/formatting/extensions/binary/file.rb +44 -11
  33. data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
  34. data/lib/ronin/formatting/extensions/binary/integer.rb +66 -50
  35. data/lib/ronin/formatting/extensions/binary/string.rb +81 -205
  36. data/lib/ronin/formatting/extensions/digest.rb +5 -5
  37. data/lib/ronin/formatting/extensions/digest/file.rb +5 -5
  38. data/lib/ronin/formatting/extensions/digest/string.rb +5 -5
  39. data/lib/ronin/formatting/extensions/html.rb +5 -5
  40. data/lib/ronin/formatting/extensions/html/integer.rb +9 -13
  41. data/lib/ronin/formatting/extensions/html/string.rb +31 -39
  42. data/lib/ronin/formatting/extensions/http.rb +5 -5
  43. data/lib/ronin/formatting/extensions/http/integer.rb +6 -6
  44. data/lib/ronin/formatting/extensions/http/string.rb +7 -7
  45. data/lib/ronin/formatting/extensions/sql.rb +5 -5
  46. data/lib/ronin/formatting/extensions/sql/string.rb +22 -24
  47. data/lib/ronin/formatting/extensions/text.rb +5 -5
  48. data/lib/ronin/formatting/extensions/text/array.rb +13 -11
  49. data/lib/ronin/formatting/extensions/text/string.rb +70 -13
  50. data/lib/ronin/formatting/html.rb +5 -5
  51. data/lib/ronin/formatting/http.rb +5 -5
  52. data/lib/ronin/formatting/sql.rb +5 -5
  53. data/lib/ronin/formatting/text.rb +5 -5
  54. data/lib/ronin/fuzzing.rb +5 -5
  55. data/lib/ronin/fuzzing/extensions.rb +5 -5
  56. data/lib/ronin/fuzzing/extensions/string.rb +42 -213
  57. data/lib/ronin/fuzzing/fuzzer.rb +110 -0
  58. data/lib/ronin/fuzzing/fuzzing.rb +33 -26
  59. data/lib/ronin/fuzzing/mutator.rb +161 -0
  60. data/lib/ronin/fuzzing/repeater.rb +81 -0
  61. data/lib/ronin/fuzzing/template.rb +133 -0
  62. data/lib/ronin/mixin.rb +2 -2
  63. data/lib/ronin/network.rb +7 -5
  64. data/lib/ronin/network/dns.rb +64 -24
  65. data/lib/ronin/network/esmtp.rb +5 -5
  66. data/lib/ronin/network/extensions.rb +5 -5
  67. data/lib/ronin/network/extensions/dns.rb +5 -5
  68. data/lib/ronin/network/extensions/dns/net.rb +5 -5
  69. data/lib/ronin/network/extensions/esmtp.rb +5 -5
  70. data/lib/ronin/network/extensions/esmtp/net.rb +5 -5
  71. data/lib/ronin/network/extensions/http.rb +5 -5
  72. data/lib/ronin/network/extensions/http/net.rb +5 -5
  73. data/lib/ronin/network/extensions/http/uri/http.rb +5 -5
  74. data/lib/ronin/network/extensions/imap.rb +5 -5
  75. data/lib/ronin/network/extensions/imap/net.rb +5 -5
  76. data/lib/ronin/network/extensions/pop3.rb +5 -5
  77. data/lib/ronin/network/extensions/pop3/net.rb +5 -5
  78. data/lib/ronin/network/extensions/smtp.rb +5 -5
  79. data/lib/ronin/network/extensions/smtp/net.rb +5 -5
  80. data/lib/ronin/network/extensions/ssl.rb +5 -5
  81. data/lib/ronin/network/extensions/ssl/net.rb +5 -5
  82. data/lib/ronin/network/extensions/tcp.rb +5 -5
  83. data/lib/ronin/network/extensions/tcp/net.rb +5 -5
  84. data/lib/ronin/network/extensions/telnet.rb +5 -5
  85. data/lib/ronin/network/extensions/telnet/net.rb +5 -5
  86. data/lib/ronin/network/extensions/udp.rb +5 -5
  87. data/lib/ronin/network/extensions/udp/net.rb +5 -5
  88. data/lib/ronin/network/ftp.rb +149 -0
  89. data/lib/ronin/network/http.rb +5 -5
  90. data/lib/ronin/network/http/exceptions.rb +5 -5
  91. data/lib/ronin/network/http/exceptions/unknown_request.rb +5 -5
  92. data/lib/ronin/network/http/http.rb +65 -70
  93. data/lib/ronin/network/http/proxy.rb +5 -5
  94. data/lib/ronin/network/imap.rb +16 -15
  95. data/lib/ronin/network/mixins.rb +6 -5
  96. data/lib/ronin/network/mixins/dns.rb +5 -5
  97. data/lib/ronin/network/mixins/esmtp.rb +5 -5
  98. data/lib/ronin/network/mixins/ftp.rb +155 -0
  99. data/lib/ronin/network/mixins/http.rb +58 -587
  100. data/lib/ronin/network/mixins/imap.rb +5 -5
  101. data/lib/ronin/network/mixins/mixin.rb +5 -5
  102. data/lib/ronin/network/mixins/pop3.rb +5 -5
  103. data/lib/ronin/network/mixins/smtp.rb +5 -5
  104. data/lib/ronin/network/mixins/ssl.rb +5 -5
  105. data/lib/ronin/network/mixins/tcp.rb +43 -10
  106. data/lib/ronin/network/mixins/telnet.rb +5 -5
  107. data/lib/ronin/network/mixins/udp.rb +126 -6
  108. data/lib/ronin/network/mixins/unix.rb +279 -0
  109. data/lib/ronin/network/network.rb +5 -5
  110. data/lib/ronin/network/pop3.rb +10 -10
  111. data/lib/ronin/network/proxy.rb +578 -0
  112. data/lib/ronin/network/smtp.rb +5 -5
  113. data/lib/ronin/network/smtp/email.rb +6 -6
  114. data/lib/ronin/network/smtp/smtp.rb +12 -13
  115. data/lib/ronin/network/ssl.rb +16 -17
  116. data/lib/ronin/network/tcp.rb +7 -310
  117. data/lib/ronin/network/tcp/proxy.rb +417 -0
  118. data/lib/ronin/network/tcp/tcp.rb +452 -0
  119. data/lib/ronin/network/telnet.rb +34 -28
  120. data/lib/ronin/network/udp.rb +7 -271
  121. data/lib/ronin/network/udp/proxy.rb +191 -0
  122. data/lib/ronin/network/udp/udp.rb +452 -0
  123. data/lib/ronin/network/unix.rb +286 -0
  124. data/lib/ronin/path.rb +35 -39
  125. data/lib/ronin/spec/ui/output.rb +6 -12
  126. data/lib/ronin/support.rb +6 -5
  127. data/lib/ronin/support/inflector.rb +8 -12
  128. data/lib/ronin/support/support.rb +7 -5
  129. data/lib/ronin/support/version.rb +6 -6
  130. data/lib/ronin/templates.rb +5 -5
  131. data/lib/ronin/templates/erb.rb +5 -5
  132. data/lib/ronin/templates/template.rb +5 -5
  133. data/lib/ronin/ui/output.rb +5 -5
  134. data/lib/ronin/ui/output/helpers.rb +42 -28
  135. data/lib/ronin/ui/output/output.rb +17 -21
  136. data/lib/ronin/ui/output/terminal.rb +5 -5
  137. data/lib/ronin/ui/output/terminal/color.rb +15 -9
  138. data/lib/ronin/ui/output/terminal/raw.rb +5 -5
  139. data/lib/ronin/ui/shell.rb +8 -11
  140. data/lib/ronin/wordlist.rb +110 -30
  141. data/ronin-support.gemspec +39 -109
  142. data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
  143. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
  144. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
  145. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
  146. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
  147. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
  148. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
  149. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
  150. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
  151. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
  152. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
  153. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
  154. data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
  155. data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
  156. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
  157. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
  158. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
  159. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
  160. data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
  161. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
  162. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
  163. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
  164. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
  165. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
  166. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
  167. data/spec/binary/hexdump/parser_spec.rb +302 -0
  168. data/spec/binary/struct_spec.rb +496 -0
  169. data/spec/binary/template_spec.rb +416 -0
  170. data/spec/extensions/enumerable_spec.rb +4 -4
  171. data/spec/extensions/file_spec.rb +12 -14
  172. data/spec/extensions/ip_addr_spec.rb +76 -50
  173. data/spec/extensions/kernel_spec.rb +7 -7
  174. data/spec/extensions/regexp_spec.rb +119 -59
  175. data/spec/extensions/resolv_spec.rb +2 -2
  176. data/spec/extensions/string_spec.rb +31 -30
  177. data/spec/formatting/binary/array_spec.rb +26 -0
  178. data/spec/formatting/binary/base64_spec.rb +50 -0
  179. data/spec/formatting/binary/float_spec.rb +36 -0
  180. data/spec/formatting/binary/integer_spec.rb +76 -50
  181. data/spec/formatting/binary/string_spec.rb +91 -198
  182. data/spec/formatting/digest/string_spec.rb +5 -5
  183. data/spec/formatting/html/integer_spec.rb +6 -6
  184. data/spec/formatting/html/string_spec.rb +10 -10
  185. data/spec/formatting/http/integer_spec.rb +3 -3
  186. data/spec/formatting/http/string_spec.rb +5 -5
  187. data/spec/formatting/sql/string_spec.rb +21 -19
  188. data/spec/formatting/text/array_spec.rb +15 -15
  189. data/spec/formatting/text/string_spec.rb +58 -28
  190. data/spec/fuzzing/extensions/string_spec.rb +87 -0
  191. data/spec/fuzzing/fuzzer_spec.rb +109 -0
  192. data/spec/fuzzing/fuzzing_spec.rb +24 -0
  193. data/spec/fuzzing/mutator_spec.rb +112 -0
  194. data/spec/fuzzing/repeater_spec.rb +57 -0
  195. data/spec/fuzzing/template_spec.rb +54 -0
  196. data/spec/mixin_spec.rb +10 -12
  197. data/spec/network/dns_spec.rb +89 -23
  198. data/spec/network/ftp_spec.rb +81 -0
  199. data/spec/network/http/http_spec.rb +237 -144
  200. data/spec/network/http/proxy_spec.rb +37 -37
  201. data/spec/network/network_spec.rb +2 -2
  202. data/spec/network/proxy_spec.rb +121 -0
  203. data/spec/network/shared/unix_server.rb +31 -0
  204. data/spec/network/smtp/email_spec.rb +14 -14
  205. data/spec/network/ssl_spec.rb +53 -3
  206. data/spec/network/tcp/proxy_spec.rb +118 -0
  207. data/spec/network/tcp/tcp_spec.rb +316 -0
  208. data/spec/network/telnet_spec.rb +67 -0
  209. data/spec/network/udp/udp_spec.rb +298 -0
  210. data/spec/network/unix_spec.rb +182 -0
  211. data/spec/path_spec.rb +43 -18
  212. data/spec/spec_helper.rb +2 -3
  213. data/spec/support/inflector_spec.rb +4 -4
  214. data/spec/support_spec.rb +1 -1
  215. data/spec/templates/erb_spec.rb +3 -3
  216. data/spec/templates/template_spec.rb +10 -10
  217. data/spec/ui/shell_spec.rb +15 -15
  218. data/spec/wordlist_spec.rb +80 -19
  219. metadata +176 -121
  220. data/.gemtest +0 -0
  221. data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
  222. data/spec/fuzzing/string_spec.rb +0 -158
  223. data/spec/network/tcp_spec.rb +0 -247
  224. data/spec/network/udp_spec.rb +0 -248
@@ -0,0 +1,17 @@
1
+ 0000000 3.8204714e-37 1.0082514e-34 2.6584628e-32 7.003653e-30
2
+ 0000020 1.8436203e-27 4.849422e-25 1.274669e-22 3.348188e-20
3
+ 0000040 8.789052e-18 2.3057262e-15 6.045325e-13 1.5841256e-10
4
+ 0000060 4.148859e-08 1.0860433e-05 0.0028415453 0.74312186
5
+ 0000100 194.25488 50757.266 13257032 3.4611722e+09
6
+ 0000120 9.033073e+11 2.3566197e+14 6.145978e+16 1.6023064e+19
7
+ 0000140 4.1759808e+21 1.0880146e+24 2.833864e+26 7.3789715e+28
8
+ 0000160 1.9208323e+31 4.9987784e+33 1.3005379e+36 3.3827546e+38
9
+ 0000200 -7.670445e-37 -2.0240553e-34 -5.33626e-32 -1.4056803e-29
10
+ 0000220 -3.6999117e-27 -9.731282e-25 -2.557642e-22 -6.7176346e-20
11
+ 0000240 -1.7632526e-17 -4.6253843e-15 -1.2126316e-12 -3.1773817e-10
12
+ 0000260 -8.321092e-08 -2.1780703e-05 -0.005698409 -1.4901652
13
+ 0000300 -389.51367 -101771.53 -26579856 -6.939187e+09
14
+ 0000320 -1.8109264e+12 -4.7242775e+14 -1.2320213e+17 -3.2118467e+19
15
+ 0000340 -8.3704803e+21 -2.18077e+24 -5.6798647e+26 -1.4789012e+29
16
+ 0000360 -3.8496183e+31 -1.00179184e+34 -2.6062884e+36 -nan
17
+ 0000400
@@ -0,0 +1,17 @@
1
+ 0000000 nul soh stx etx eot enq ack bel bs ht nl vt ff cr so si
2
+ 0000020 dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us
3
+ 0000040 sp ! " # $ % & ' ( ) * + , - . /
4
+ 0000060 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
5
+ 0000100 @ A B C D E F G H I J K L M N O
6
+ 0000120 P Q R S T U V W X Y Z [ \ ] ^ _
7
+ 0000140 ` a b c d e f g h i j k l m n o
8
+ 0000160 p q r s t u v w x y z { | } ~ del
9
+ 0000200 nul soh stx etx eot enq ack bel bs ht nl vt ff cr so si
10
+ 0000220 dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us
11
+ 0000240 sp ! " # $ % & ' ( ) * + , - . /
12
+ 0000260 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
13
+ 0000300 @ A B C D E F G H I J K L M N O
14
+ 0000320 P Q R S T U V W X Y Z [ \ ] ^ _
15
+ 0000340 ` a b c d e f g h i j k l m n o
16
+ 0000360 p q r s t u v w x y z { | } ~ del
17
+ 0000400
@@ -0,0 +1,302 @@
1
+ require 'spec_helper'
2
+ require 'binary/hexdump/helpers/hexdumps'
3
+
4
+ require 'ronin/binary/hexdump/parser'
5
+
6
+ describe Binary::Hexdump::Parser do
7
+ include Helpers
8
+
9
+ context "GNU hexdump" do
10
+ let(:ascii) { load_binary_data('ascii') }
11
+ let(:repeated) { load_binary_data('repeated') }
12
+
13
+ context "octal bytes" do
14
+ subject do
15
+ described_class.new(:format => :hexdump, :encoding => :octal_bytes)
16
+ end
17
+
18
+ let(:hexdump) { load_hexdump('hexdump_octal_bytes') }
19
+
20
+ it "should unhexdump octal-byte hexdump output" do
21
+ expect(subject.parse(hexdump)).to eq(ascii)
22
+ end
23
+ end
24
+
25
+ context " hex bytes" do
26
+ subject do
27
+ described_class.new(:format => :hexdump, :encoding => :hex_bytes)
28
+ end
29
+
30
+ let(:hexdump) { load_hexdump('hexdump_hex_bytes') }
31
+
32
+ it "should unhexdump hex-byte hexdump output" do
33
+ expect(subject.parse(hexdump)).to eq(ascii)
34
+ end
35
+ end
36
+
37
+ context "decimal shorts" do
38
+ subject do
39
+ described_class.new(:format => :hexdump, :encoding => :decimal_shorts)
40
+ end
41
+
42
+ let(:hexdump) { load_hexdump('hexdump_decimal_shorts') }
43
+
44
+ it "should unhexdump decimal-short hexdump output" do
45
+ expect(subject.parse(hexdump)).to eq(ascii)
46
+ end
47
+ end
48
+
49
+ context "octal shorts" do
50
+ subject do
51
+ described_class.new(:format => :hexdump, :encoding => :octal_shorts)
52
+ end
53
+
54
+ let(:hexdump) { load_hexdump('hexdump_octal_shorts') }
55
+
56
+ it "should unhexdump octal-short hexdump output" do
57
+ expect(subject.parse(hexdump)).to eq(ascii)
58
+ end
59
+ end
60
+
61
+ context "hex shorts" do
62
+ subject do
63
+ described_class.new(:format => :hexdump, :encoding => :hex_shorts)
64
+ end
65
+
66
+ let(:hexdump) { load_hexdump('hexdump_hex_shorts') }
67
+
68
+ it "should unhexdump hex-short hexdump output" do
69
+ expect(subject.parse(hexdump)).to eq(ascii)
70
+ end
71
+ end
72
+
73
+ context "repeated" do
74
+ subject do
75
+ described_class.new(:format => :hexdump, :encoding => :hex_bytes)
76
+ end
77
+
78
+ let(:hexdump) { load_hexdump('hexdump_repeated') }
79
+
80
+ it "should unhexdump repeated hexdump output" do
81
+ expect(subject.parse(hexdump)).to eq(repeated)
82
+ end
83
+ end
84
+ end
85
+
86
+ context "od" do
87
+ let(:ascii) { load_binary_data('ascii') }
88
+ let(:repeated) { load_binary_data('repeated') }
89
+
90
+ context "octal bytes" do
91
+ subject do
92
+ described_class.new(:format => :od, :encoding => :octal_bytes)
93
+ end
94
+
95
+ let(:hexdump) { load_hexdump('od_octal_bytes') }
96
+
97
+ it "should unhexdump octal-byte hexdump output" do
98
+ expect(subject.parse(hexdump)).to eq(ascii)
99
+ end
100
+ end
101
+
102
+ context "octal shorts" do
103
+ subject do
104
+ described_class.new(:format => :od, :encoding => :octal_shorts)
105
+ end
106
+
107
+ let(:hexdump) { load_hexdump('od_octal_shorts') }
108
+
109
+ it "should unhexdump octal-shorts hexdump output" do
110
+ expect(subject.parse(hexdump)).to eq(ascii)
111
+ end
112
+ end
113
+
114
+ context "octal ints" do
115
+ subject do
116
+ described_class.new(:format => :od, :encoding => :octal_ints)
117
+ end
118
+
119
+ let(:hexdump) { load_hexdump('od_octal_ints') }
120
+
121
+ it "should unhexdump octal-ints hexdump output" do
122
+ expect(subject.parse(hexdump)).to eq(ascii)
123
+ end
124
+ end
125
+
126
+ if RUBY_VERSION > '1.9.'
127
+ context "octal quads" do
128
+ subject do
129
+ described_class.new(:format => :od, :encoding => :octal_quads)
130
+ end
131
+
132
+ let(:hexdump) { load_hexdump('od_octal_quads') }
133
+
134
+ it "should unhexdump octal-quads hexdump output" do
135
+ expect(subject.parse(hexdump)).to eq(ascii)
136
+ end
137
+ end
138
+ end
139
+
140
+ context "decimal bytes" do
141
+ subject do
142
+ described_class.new(:format => :od, :encoding => :decimal_bytes)
143
+ end
144
+
145
+ let(:hexdump) { load_hexdump('od_decimal_bytes') }
146
+
147
+ it "should unhexdump decimal-byte hexdump output" do
148
+ expect(subject.parse(hexdump)).to eq(ascii)
149
+ end
150
+ end
151
+
152
+ context "decimal shorts" do
153
+ subject do
154
+ described_class.new(:format => :od, :encoding => :decimal_shorts)
155
+ end
156
+
157
+ let(:hexdump) { load_hexdump('od_decimal_shorts') }
158
+
159
+ it "should unhexdump decimal-shorts hexdump output" do
160
+ expect(subject.parse(hexdump)).to eq(ascii)
161
+ end
162
+ end
163
+
164
+ context "decimal ints" do
165
+ subject do
166
+ described_class.new(:format => :od, :encoding => :decimal_ints)
167
+ end
168
+
169
+ let(:hexdump) { load_hexdump('od_decimal_ints') }
170
+
171
+ it "should unhexdump decimal-ints hexdump output" do
172
+ expect(subject.parse(hexdump)).to eq(ascii)
173
+ end
174
+ end
175
+
176
+ if RUBY_VERSION > '1.9.'
177
+ context "decimal quads" do
178
+ subject do
179
+ described_class.new(:format => :od, :encoding => :decimal_quads)
180
+ end
181
+
182
+ let(:hexdump) { load_hexdump('od_decimal_quads') }
183
+
184
+ it "should unhexdump decimal-quads hexdump output" do
185
+ expect(subject.parse(hexdump)).to eq(ascii)
186
+ end
187
+ end
188
+ end
189
+
190
+ context "named chars" do
191
+ subject do
192
+ described_class.new(:format => :od, :encoding => :named_chars)
193
+ end
194
+
195
+ let(:mask) { ~(1 << 7) }
196
+ let(:data) { ascii.bytes.map { |b| (b & mask).chr }.join }
197
+ let(:hexdump) { load_hexdump('od_named_chars') }
198
+
199
+ it "should unhexdump named characters" do
200
+ expect(subject.parse(hexdump)).to eq(data)
201
+ end
202
+ end
203
+
204
+ context "hex bytes" do
205
+ subject do
206
+ described_class.new(:format => :od, :encoding => :hex_bytes)
207
+ end
208
+
209
+ let(:hexdump) { load_hexdump('od_hex_bytes') }
210
+
211
+ it "should unhexdump hex-byte hexdump output" do
212
+ expect(subject.parse(hexdump)).to eq(ascii)
213
+ end
214
+ end
215
+
216
+ context "hex shorts" do
217
+ subject do
218
+ described_class.new(:format => :od, :encoding => :hex_shorts)
219
+ end
220
+
221
+ let(:hexdump) { load_hexdump('od_hex_shorts') }
222
+
223
+ it "should unhexdump hex-shorts hexdump output" do
224
+ expect(subject.parse(hexdump)).to eq(ascii)
225
+ end
226
+ end
227
+
228
+ context "hex ints" do
229
+ subject do
230
+ described_class.new(:format => :od, :encoding => :hex_ints)
231
+ end
232
+
233
+ let(:hexdump) { load_hexdump('od_hex_ints') }
234
+
235
+ it "should unhexdump hex-ints hexdump output" do
236
+ expect(subject.parse(hexdump)).to eq(ascii)
237
+ end
238
+ end
239
+
240
+ if RUBY_VERSION > '1.9.'
241
+ context "hex quads" do
242
+ subject do
243
+ described_class.new(:format => :od, :encoding => :hex_quads)
244
+ end
245
+
246
+ let(:hexdump) { load_hexdump('od_hex_quads') }
247
+
248
+ it "should unhexdump hex-quads hexdump output" do
249
+ expect(subject.parse(hexdump)).to eq(ascii)
250
+ end
251
+ end
252
+ end
253
+
254
+ context "floats" do
255
+ subject do
256
+ described_class.new(:format => :od, :encoding => :floats)
257
+ end
258
+
259
+ let(:data) do
260
+ data = ascii.dup
261
+ data[-4..-1] = ("\0" * 4)
262
+ data
263
+ end
264
+
265
+ let(:hexdump) { load_hexdump('od_floats') }
266
+
267
+ it "should unhexdump floats" do
268
+ expect(subject.parse(hexdump)).to eq(data)
269
+ end
270
+ end
271
+
272
+ context "doubles" do
273
+ subject do
274
+ described_class.new(:format => :od, :encoding => :doubles)
275
+ end
276
+
277
+ let(:data) do
278
+ data = ascii.dup
279
+ data[-8..-1] = ("\0" * 8)
280
+ data
281
+ end
282
+
283
+ let(:hexdump) { load_hexdump('od_doubles') }
284
+
285
+ it "should unhexdump doubles" do
286
+ expect(subject.parse(hexdump)).to eq(data)
287
+ end
288
+ end
289
+
290
+ context "repeated" do
291
+ subject do
292
+ described_class.new(:format => :od, :encoding => :octal_shorts)
293
+ end
294
+
295
+ let(:hexdump) { load_hexdump('od_repeated') }
296
+
297
+ it "should unhexdump repeated hexdump output" do
298
+ expect(subject.parse(hexdump)).to eq(repeated)
299
+ end
300
+ end
301
+ end
302
+ end
@@ -0,0 +1,496 @@
1
+ require 'spec_helper'
2
+ require 'ronin/binary/struct'
3
+
4
+ describe Binary::Struct do
5
+ describe "layout" do
6
+ subject do
7
+ struct = Class.new(described_class)
8
+ struct.class_eval do
9
+ layout :x, :uint,
10
+ :y, :uint
11
+ end
12
+
13
+ struct.new
14
+ end
15
+
16
+ it "should return the layout" do
17
+ expect(subject.class.layout).to eq([
18
+ :x,
19
+ :y
20
+ ])
21
+ end
22
+
23
+ context "when given fields" do
24
+ it "should populate fields" do
25
+ expect(subject.class.fields).to eq({
26
+ :x => [:uint, nil],
27
+ :y => [:uint, nil]
28
+ })
29
+ end
30
+
31
+ it "should define reader methods" do
32
+ expect(subject).to respond_to(:x)
33
+ expect(subject).to respond_to(:y)
34
+ end
35
+
36
+ it "should define writer methods" do
37
+ expect(subject).to respond_to(:x=)
38
+ expect(subject).to respond_to(:y=)
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "field?" do
44
+ subject do
45
+ struct = Class.new(described_class)
46
+ struct.class_eval { layout :x, :uint }
47
+ struct
48
+ end
49
+
50
+ it "should determine if fields exist" do
51
+ expect(subject.field?(:x)).to be(true)
52
+ expect(subject.field?(:foo)).to be(false)
53
+ end
54
+ end
55
+
56
+ describe "endian" do
57
+ subject do
58
+ struct = Class.new(described_class)
59
+ struct.class_eval { endian :little }
60
+ struct
61
+ end
62
+
63
+ it "should return the endianness of the Struct" do
64
+ expect(subject.endian).to eq(:little)
65
+ end
66
+
67
+ context "when given an argument" do
68
+ it "should set the endianness" do
69
+ subject.endian :big
70
+
71
+ expect(subject.endian).to eq(:big)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "typedefs" do
77
+ end
78
+
79
+ describe "typedef" do
80
+ subject do
81
+ struct = Class.new(described_class)
82
+ struct.class_eval do
83
+ typedef :uint32_t, :test_t
84
+ end
85
+
86
+ struct
87
+ end
88
+
89
+ it "should register a new type in typedefs" do
90
+ expect(subject.typedefs).to have_key(:test_t)
91
+ end
92
+
93
+ it "should resolve the existing type" do
94
+ expect(subject.typedefs[:test_t]).to eq(:uint32)
95
+ end
96
+ end
97
+
98
+ describe "#initialize" do
99
+ subject do
100
+ struct = Class.new(described_class)
101
+ struct.class_eval do
102
+ nested_struct = Class.new(Ronin::Binary::Struct)
103
+ nested_struct.class_eval do
104
+ layout :int, :uint
105
+ end
106
+
107
+ layout :int, :uint,
108
+ :int_array, [:uint, 2],
109
+ :float, :float,
110
+ :float_array, [:float, 2],
111
+ :char, :char,
112
+ :char_array, [:char, 2],
113
+ :string, :string,
114
+ :struct, nested_struct,
115
+ :struct_array, [nested_struct, 2]
116
+ end
117
+
118
+ struct.new
119
+ end
120
+
121
+ it "should set integers to 0" do
122
+ expect(subject[:int]).to eq(0)
123
+ end
124
+
125
+ it "should set arrays of integers to [0, ...]" do
126
+ expect(subject[:int_array]).to eq([0, 0])
127
+ end
128
+
129
+ it "should set floats to 0.0" do
130
+ expect(subject[:float]).to eq(0.0)
131
+ end
132
+
133
+ it "should set arrays of floats to [0.0, ...]" do
134
+ expect(subject[:float_array]).to eq([0.0, 0.0])
135
+ end
136
+
137
+ it "should set chars to '\\0'" do
138
+ expect(subject[:char]).to eq("\0")
139
+ end
140
+
141
+ it "should set arrays of chars to ''" do
142
+ expect(subject[:char_array]).to eq('')
143
+ end
144
+
145
+ it "should set strings to ''" do
146
+ expect(subject[:string]).to eq('')
147
+ end
148
+
149
+ it "should initialize nested structs" do
150
+ expect(subject[:struct][:int]).to eq(0)
151
+ end
152
+
153
+ it "should initialize arrays of nested structs" do
154
+ expect(subject[:struct_array][0][:int]).to eq(0)
155
+ expect(subject[:struct_array][1][:int]).to eq(0)
156
+ end
157
+ end
158
+
159
+ describe "#[]" do
160
+ subject do
161
+ struct = Class.new(described_class)
162
+ struct.class_eval do
163
+ layout :x, :uint,
164
+ :y, :uint
165
+ end
166
+
167
+ struct.new
168
+ end
169
+
170
+ before do
171
+ subject.instance_variable_set('@x',10)
172
+ end
173
+
174
+ it "should access the instance variable" do
175
+ expect(subject[:x]).to eq(10)
176
+ end
177
+
178
+ it "should still call the underlying reader method" do
179
+ expect(subject).to receive(:x).and_return(10)
180
+
181
+ subject[:x]
182
+ end
183
+
184
+ it "should raise ArgumentError for unknown fields" do
185
+ expect {
186
+ subject[:foo]
187
+ }.to raise_error(ArgumentError)
188
+ end
189
+ end
190
+
191
+ describe "#[]=" do
192
+ subject do
193
+ struct = Class.new(described_class)
194
+ struct.class_eval do
195
+ layout :x, :uint,
196
+ :y, :uint
197
+ end
198
+
199
+ struct.new
200
+ end
201
+
202
+ before do
203
+ subject.instance_variable_set('@x',0)
204
+ end
205
+
206
+ it "should set the underlying instance variable" do
207
+ subject[:x] = 20
208
+
209
+ expect(subject.instance_variable_get('@x')).to eq(20)
210
+ end
211
+
212
+ it "should still call the underlying writer method" do
213
+ expect(subject).to receive(:x=).with(20)
214
+
215
+ subject[:x] = 20
216
+ end
217
+
218
+ it "should raise ArgumentError for unknown fields" do
219
+ expect {
220
+ subject[:foo] = 20
221
+ }.to raise_error(ArgumentError)
222
+ end
223
+ end
224
+
225
+ describe "#values" do
226
+ let(:x) { 10 }
227
+ let(:y) { 20 }
228
+
229
+ subject do
230
+ struct = Class.new(described_class)
231
+ struct.class_eval do
232
+ layout :x, :uint,
233
+ :y, :uint
234
+ end
235
+
236
+ struct.new
237
+ end
238
+
239
+ before do
240
+ subject.x = x
241
+ subject.y = y
242
+ end
243
+
244
+ it "should return the values of the fields" do
245
+ expect(subject.values).to eq([x, y])
246
+ end
247
+
248
+ context "nested structs" do
249
+ let(:z) { 30 }
250
+
251
+ subject do
252
+ struct = Class.new(described_class)
253
+ struct.class_eval do
254
+ nested_struct = Class.new(Ronin::Binary::Struct)
255
+ nested_struct.class_eval do
256
+ layout :int, :uint
257
+ end
258
+
259
+ layout :x, :uint,
260
+ :y, :uint,
261
+ :z, nested_struct
262
+ end
263
+
264
+ struct.new
265
+ end
266
+
267
+ before do
268
+ subject.z.int = z
269
+ end
270
+
271
+ it "should nest the values of nested structs" do
272
+ expect(subject.values).to eq([x, y, [z]])
273
+ end
274
+ end
275
+
276
+ context "arrays of nested structs" do
277
+ let(:z) { 30 }
278
+
279
+ subject do
280
+ struct = Class.new(described_class)
281
+ struct.class_eval do
282
+ nested_struct = Class.new(Ronin::Binary::Struct)
283
+ nested_struct.class_eval do
284
+ layout :int, :uint
285
+ end
286
+
287
+ layout :x, :uint,
288
+ :y, :uint,
289
+ :z, [nested_struct, 2]
290
+ end
291
+
292
+ struct.new
293
+ end
294
+
295
+ before do
296
+ subject.z[0].int = z
297
+ subject.z[1].int = z
298
+ end
299
+
300
+ it "should nest the values of nested structs" do
301
+ expect(subject.values).to eq([x, y, [[z], [z]]])
302
+ end
303
+ end
304
+ end
305
+
306
+ describe "#clear" do
307
+ subject do
308
+ struct = Class.new(described_class)
309
+ struct.class_eval do
310
+ nested_struct = Class.new(Ronin::Binary::Struct)
311
+ nested_struct.class_eval do
312
+ layout :int, :int
313
+ end
314
+
315
+ layout :x, :uint,
316
+ :y, :float,
317
+ :z, nested_struct
318
+ end
319
+
320
+ struct.new
321
+ end
322
+
323
+ before do
324
+ subject.x = 100
325
+ subject.y = 15.0
326
+ subject.z.int = -1
327
+
328
+ subject.clear
329
+ end
330
+
331
+ it "should reset fields to their default values" do
332
+ expect(subject.x).to eq(0)
333
+ expect(subject.y).to eq(0.0)
334
+ end
335
+
336
+ it "should reinitialize nested structs" do
337
+ expect(subject.z.int).to eq(0)
338
+ end
339
+ end
340
+
341
+ describe "#pack" do
342
+ context "arrays of chars" do
343
+ let(:string) { "hello" }
344
+ let(:packed) { string.ljust(10,"\0") }
345
+
346
+ subject do
347
+ struct = Class.new(described_class)
348
+ struct.class_eval do
349
+ layout :chars, [:char, 10]
350
+ end
351
+
352
+ struct.new
353
+ end
354
+
355
+ before do
356
+ subject.chars = string
357
+ end
358
+
359
+ it "should pack arrays of chars into a String" do
360
+ expect(subject.pack).to eq(packed)
361
+ end
362
+ end
363
+
364
+ context "structs" do
365
+ let(:packed) { "\x0a\x00\x14\x00\x00\x00" }
366
+
367
+ subject do
368
+ struct = Class.new(described_class)
369
+ struct.class_eval do
370
+ nested_struct = Class.new(Ronin::Binary::Struct)
371
+ nested_struct.class_eval do
372
+ layout :int, :uint32_le
373
+ end
374
+
375
+ layout :int, :uint16_le,
376
+ :struct, nested_struct
377
+ end
378
+
379
+ struct.new
380
+ end
381
+
382
+ before do
383
+ subject.int = 10
384
+ subject.struct.int = 20
385
+ end
386
+
387
+ it "should pack the nested struct fields" do
388
+ expect(subject.pack).to eq(packed)
389
+ end
390
+ end
391
+
392
+ context "arrays of structs" do
393
+ let(:packed) { "\x0a\x00\x14\x00\x00\x00\x1e\x00\x00\x00" }
394
+
395
+ subject do
396
+ struct = Class.new(described_class)
397
+ struct.class_eval do
398
+ nested_struct = Class.new(Ronin::Binary::Struct)
399
+ nested_struct.class_eval do
400
+ layout :int, :uint32_le
401
+ end
402
+
403
+ layout :int, :uint16_le,
404
+ :struct, [nested_struct, 2]
405
+ end
406
+
407
+ struct.new
408
+ end
409
+
410
+ before do
411
+ subject.int = 10
412
+ subject.struct[0].int = 20
413
+ subject.struct[1].int = 30
414
+ end
415
+
416
+ it "should pack the nested fields" do
417
+ expect(subject.pack).to eq(packed)
418
+ end
419
+ end
420
+ end
421
+
422
+ describe "#unpack" do
423
+ context "arrays of chars" do
424
+ let(:string) { "hello" }
425
+ let(:packed) { string.ljust(10,"\0") }
426
+
427
+ subject do
428
+ struct = Class.new(described_class)
429
+ struct.class_eval do
430
+ layout :chars, [:char, 10]
431
+ end
432
+
433
+ struct.new
434
+ end
435
+
436
+ it "should unpack arrays of chars into a String" do
437
+ subject.unpack(packed)
438
+
439
+ expect(subject.chars).to eq(string)
440
+ end
441
+ end
442
+
443
+ context "structs" do
444
+ let(:packed) { "\x0a\x00\x14\x00\x00\x00" }
445
+
446
+ subject do
447
+ struct = Class.new(described_class)
448
+ struct.class_eval do
449
+ nested_struct = Class.new(Ronin::Binary::Struct)
450
+ nested_struct.class_eval do
451
+ layout :int, :uint32_le
452
+ end
453
+
454
+ layout :int, :uint16_le,
455
+ :struct, nested_struct
456
+ end
457
+
458
+ struct.new
459
+ end
460
+
461
+ it "should unpack the nested struct fields" do
462
+ subject.unpack(packed)
463
+
464
+ expect(subject.int).to eq(10)
465
+ expect(subject.struct.int).to eq(20)
466
+ end
467
+ end
468
+
469
+ context "arrays of structs" do
470
+ let(:packed) { "\x0a\x00\x14\x00\x00\x00\x1e\x00\x00\x00" }
471
+
472
+ subject do
473
+ struct = Class.new(described_class)
474
+ struct.class_eval do
475
+ nested_struct = Class.new(Ronin::Binary::Struct)
476
+ nested_struct.class_eval do
477
+ layout :int, :uint32_le
478
+ end
479
+
480
+ layout :int, :uint16_le,
481
+ :struct, [nested_struct, 2]
482
+ end
483
+
484
+ struct.new
485
+ end
486
+
487
+ it "should unpack the nested fields" do
488
+ subject.unpack(packed)
489
+
490
+ expect(subject.int).to eq(10)
491
+ expect(subject.struct[0].int).to eq(20)
492
+ expect(subject.struct[1].int).to eq(30)
493
+ end
494
+ end
495
+ end
496
+ end