ronin-support 0.4.1 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/ChangeLog.md +75 -32
  2. data/Gemfile +17 -18
  3. data/README.md +9 -10
  4. data/Rakefile +10 -2
  5. data/gemspec.yml +1 -1
  6. data/lib/ronin/binary.rb +21 -0
  7. data/lib/ronin/binary/hexdump.rb +20 -0
  8. data/lib/ronin/binary/hexdump/parser.rb +411 -0
  9. data/lib/ronin/binary/struct.rb +579 -0
  10. data/lib/ronin/binary/template.rb +437 -0
  11. data/lib/ronin/extensions/ip_addr.rb +17 -13
  12. data/lib/ronin/extensions/regexp.rb +45 -0
  13. data/lib/ronin/extensions/string.rb +3 -3
  14. data/lib/ronin/formatting/extensions/binary.rb +1 -0
  15. data/lib/ronin/formatting/extensions/binary/array.rb +63 -0
  16. data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
  17. data/lib/ronin/formatting/extensions/binary/file.rb +39 -6
  18. data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
  19. data/lib/ronin/formatting/extensions/binary/integer.rb +56 -43
  20. data/lib/ronin/formatting/extensions/binary/string.rb +75 -187
  21. data/lib/ronin/formatting/extensions/text/string.rb +61 -0
  22. data/lib/ronin/fuzzing/extensions/string.rb +21 -8
  23. data/lib/ronin/fuzzing/fuzzing.rb +19 -17
  24. data/lib/ronin/network.rb +2 -1
  25. data/lib/ronin/network/dns.rb +57 -15
  26. data/lib/ronin/network/extensions.rb +0 -1
  27. data/lib/ronin/network/ftp.rb +145 -0
  28. data/lib/ronin/network/http/http.rb +13 -14
  29. data/lib/ronin/network/imap.rb +11 -10
  30. data/lib/ronin/network/mixins.rb +1 -0
  31. data/lib/ronin/network/mixins/ftp.rb +155 -0
  32. data/lib/ronin/network/mixins/ssl.rb +1 -1
  33. data/lib/ronin/network/mixins/tcp.rb +39 -6
  34. data/lib/ronin/network/mixins/udp.rb +121 -1
  35. data/lib/ronin/network/mixins/unix.rb +279 -0
  36. data/lib/ronin/network/pop3.rb +5 -5
  37. data/lib/ronin/network/proxy.rb +578 -0
  38. data/lib/ronin/network/smtp/email.rb +1 -1
  39. data/lib/ronin/network/smtp/smtp.rb +7 -8
  40. data/lib/ronin/network/ssl.rb +1 -6
  41. data/lib/ronin/network/tcp.rb +2 -305
  42. data/lib/ronin/network/tcp/proxy.rb +377 -0
  43. data/lib/ronin/network/tcp/tcp.rb +435 -0
  44. data/lib/ronin/network/telnet.rb +27 -23
  45. data/lib/ronin/network/udp.rb +2 -266
  46. data/lib/ronin/network/udp/proxy.rb +169 -0
  47. data/lib/ronin/network/udp/udp.rb +442 -0
  48. data/lib/ronin/network/unix.rb +287 -0
  49. data/lib/ronin/path.rb +2 -2
  50. data/lib/ronin/spec/ui/output.rb +1 -7
  51. data/lib/ronin/support.rb +1 -0
  52. data/lib/ronin/support/inflector.rb +3 -7
  53. data/lib/ronin/support/support.rb +2 -1
  54. data/lib/ronin/support/version.rb +1 -1
  55. data/lib/ronin/ui/output/helpers.rb +13 -15
  56. data/lib/ronin/ui/output/output.rb +2 -2
  57. data/lib/ronin/ui/output/terminal/color.rb +10 -4
  58. data/lib/ronin/wordlist.rb +92 -17
  59. data/ronin-support.gemspec +38 -109
  60. data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
  61. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
  62. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
  63. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
  64. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
  65. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
  66. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
  67. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
  68. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
  69. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
  70. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
  71. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
  72. data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
  73. data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
  74. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
  75. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
  76. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
  77. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
  78. data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
  79. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
  80. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
  81. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
  82. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
  83. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
  84. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
  85. data/spec/binary/hexdump/parser_spec.rb +302 -0
  86. data/spec/binary/struct_spec.rb +496 -0
  87. data/spec/binary/template_spec.rb +400 -0
  88. data/spec/extensions/ip_addr_spec.rb +58 -32
  89. data/spec/extensions/regexp_spec.rb +60 -0
  90. data/spec/extensions/string_spec.rb +1 -1
  91. data/spec/formatting/binary/array_spec.rb +22 -0
  92. data/spec/formatting/binary/base64_spec.rb +50 -0
  93. data/spec/formatting/binary/float_spec.rb +30 -0
  94. data/spec/formatting/binary/integer_spec.rb +54 -40
  95. data/spec/formatting/binary/string_spec.rb +69 -182
  96. data/spec/formatting/text/string_spec.rb +30 -0
  97. data/spec/network/dns_spec.rb +64 -0
  98. data/spec/network/ftp_spec.rb +65 -0
  99. data/spec/network/proxy_spec.rb +121 -0
  100. data/spec/network/shared/unix_server.rb +31 -0
  101. data/spec/network/tcp/proxy_spec.rb +116 -0
  102. data/spec/network/{tcp_spec.rb → tcp/tcp_spec.rb} +24 -1
  103. data/spec/network/telnet_spec.rb +67 -0
  104. data/spec/network/{udp_spec.rb → udp/udp_spec.rb} +24 -1
  105. data/spec/network/unix_spec.rb +183 -0
  106. data/spec/wordlist_spec.rb +74 -13
  107. metadata +129 -85
  108. data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
@@ -0,0 +1,17 @@
1
+ 0000000 7.949928895127363e-275 3.6919162048650923e-236
2
+ 0000020 1.846323925681849e-197 8.567745616612358e-159
3
+ 0000040 4.287943403239047e-120 1.9882885252746607e-81
4
+ 0000060 9.958334378896745e-43 0.00046141357891195693
5
+ 0000100 2.3127085096212408e+35 1.0707780659396515e+74
6
+ 0000120 5.37095661886286e+112 2.4848873176582267e+151
7
+ 0000140 1.2473230926066734e+190 5.766497103752564e+228
8
+ 0000160 2.8966956840444004e+267 1.3381833135892986e+306
9
+ 0000200 -2.081576000531694e-272 -9.609176777827115e-234
10
+ 0000220 -4.834030884500847e-195 -2.2299033716331566e-156
11
+ 0000240 -1.1225952939479768e-117 -5.1746869438707237e-79
12
+ 0000260 -2.606955873096698e-40 -0.12008266051518646
13
+ 0000300 -6.0539778500895675e+37 -2.786600511251514e+76
14
+ 0000320 -1.405868428700574e+115 -6.466470811086963e+153
15
+ 0000340 -3.264714813035785e+192 -1.5005764835379223e+231
16
+ 0000360 -7.581280411902108e+269 -nan
17
+ 0000400
@@ -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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.parse(hexdump).should == 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
+ subject.class.layout.should == [
18
+ :x,
19
+ :y
20
+ ]
21
+ end
22
+
23
+ context "when given fields" do
24
+ it "should populate fields" do
25
+ subject.class.fields.should == {
26
+ :x => [:uint, nil],
27
+ :y => [:uint, nil]
28
+ }
29
+ end
30
+
31
+ it "should define reader methods" do
32
+ subject.should respond_to(:x)
33
+ subject.should respond_to(:y)
34
+ end
35
+
36
+ it "should define writer methods" do
37
+ subject.should respond_to(:x=)
38
+ subject.should 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
+ subject.field?(:x).should == true
52
+ subject.field?(:foo).should == 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
+ subject.endian.should == :little
65
+ end
66
+
67
+ context "when given an argument" do
68
+ it "should set the endianness" do
69
+ subject.endian :big
70
+
71
+ subject.endian.should == :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
+ subject.typedefs.should have_key(:test_t)
91
+ end
92
+
93
+ it "should resolve the existing type" do
94
+ subject.typedefs[:test_t].should == :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
+ subject[:int].should == 0
123
+ end
124
+
125
+ it "should set arrays of integers to [0, ...]" do
126
+ subject[:int_array].should == [0, 0]
127
+ end
128
+
129
+ it "should set floats to 0.0" do
130
+ subject[:float].should == 0.0
131
+ end
132
+
133
+ it "should set arrays of floats to [0.0, ...]" do
134
+ subject[:float_array].should == [0.0, 0.0]
135
+ end
136
+
137
+ it "should set chars to '\\0'" do
138
+ subject[:char].should == "\0"
139
+ end
140
+
141
+ it "should set arrays of chars to ''" do
142
+ subject[:char_array].should == ''
143
+ end
144
+
145
+ it "should set strings to ''" do
146
+ subject[:string].should == ''
147
+ end
148
+
149
+ it "should initialize nested structs" do
150
+ subject[:struct][:int].should == 0
151
+ end
152
+
153
+ it "should initialize arrays of nested structs" do
154
+ subject[:struct_array][0][:int].should == 0
155
+ subject[:struct_array][1][:int].should == 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(:all) do
171
+ subject.instance_variable_set('@x',10)
172
+ end
173
+
174
+ it "should access the instance variable" do
175
+ subject[:x].should == 10
176
+ end
177
+
178
+ it "should still call the underlying reader method" do
179
+ subject.should_receive(:x).and_return(10)
180
+
181
+ subject[:x]
182
+ end
183
+
184
+ it "should raise ArgumentError for unknown fields" do
185
+ lambda {
186
+ subject[:foo]
187
+ }.should 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(:each) 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
+ subject.instance_variable_get('@x').should == 20
210
+ end
211
+
212
+ it "should still call the underlying writer method" do
213
+ subject.should_receive(:x=).with(20)
214
+
215
+ subject[:x] = 20
216
+ end
217
+
218
+ it "should raise ArgumentError for unknown fields" do
219
+ lambda {
220
+ subject[:foo] = 20
221
+ }.should 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(:each) do
240
+ subject.x = x
241
+ subject.y = y
242
+ end
243
+
244
+ it "should return the values of the fields" do
245
+ subject.values.should == [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(:each) do
268
+ subject.z.int = z
269
+ end
270
+
271
+ it "should nest the values of nested structs" do
272
+ subject.values.should == [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(:each) 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
+ subject.values.should == [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(:all) 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
+ subject.x.should == 0
333
+ subject.y.should == 0.0
334
+ end
335
+
336
+ it "should reinitialize nested structs" do
337
+ subject.z.int.should == 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(:each) do
356
+ subject.chars = string
357
+ end
358
+
359
+ it "should pack arrays of chars into a String" do
360
+ subject.pack.should == 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(:each) do
383
+ subject.int = 10
384
+ subject.struct.int = 20
385
+ end
386
+
387
+ it "should pack the nested struct fields" do
388
+ subject.pack.should == 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(:each) 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
+ subject.pack.should == 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
+ subject.chars.should == 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
+ subject.int.should == 10
465
+ subject.struct.int.should == 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
+ subject.int.should == 10
491
+ subject.struct[0].int.should == 20
492
+ subject.struct[1].int.should == 30
493
+ end
494
+ end
495
+ end
496
+ end