bindata 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Record do
6
6
  it "is not registered" do
@@ -117,7 +117,7 @@ describe BinData::Record, "with anonymous fields" do
117
117
  str = "\001\002\003\004\005"
118
118
  obj.read(str)
119
119
  obj.a.clear
120
- obj.to_binary_s.must_equal "\012\002\003\004\012"
120
+ obj.to_binary_s.must_equal_binary "\012\002\003\004\012"
121
121
  end
122
122
  end
123
123
 
@@ -184,7 +184,7 @@ describe BinData::Record, "with multiple fields" do
184
184
  end
185
185
 
186
186
  it "writes ordered" do
187
- obj.to_binary_s.must_equal "\x01\x02"
187
+ obj.to_binary_s.must_equal_binary "\x01\x02"
188
188
  end
189
189
 
190
190
  it "reads ordered" do
@@ -374,7 +374,60 @@ describe BinData::Record, "with an endian defined" do
374
374
 
375
375
  lambdaed = [1, 2.0, 3, 4, 5, 6, 7, 8].pack('veCCVvNn')
376
376
 
377
- obj.to_binary_s.must_equal lambdaed
377
+ obj.to_binary_s.must_equal_binary lambdaed
378
+ end
379
+ end
380
+
381
+ describe BinData::Record, "with search_prefix" do
382
+ class ASprefix < BinData::Int8; end
383
+ class BSprefix < BinData::Int8; end
384
+
385
+ class RecordWithSearchPrefix < BinData::Record
386
+ search_prefix :a
387
+ sprefix :f
388
+ end
389
+
390
+ class RecordWithParentSearchPrefix < BinData::Record
391
+ search_prefix :a
392
+ struct :s do
393
+ sprefix :f
394
+ end
395
+ end
396
+
397
+ class RecordWithNestedSearchPrefix < BinData::Record
398
+ search_prefix :a
399
+ struct :s do
400
+ search_prefix :x
401
+ sprefix :f
402
+ end
403
+ end
404
+
405
+ class RecordWithPrioritisedNestedSearchPrefix < BinData::Record
406
+ search_prefix :b
407
+ struct :s do
408
+ search_prefix :a
409
+ sprefix :f
410
+ end
411
+ end
412
+
413
+ it "uses search_prefix" do
414
+ obj = RecordWithSearchPrefix.new
415
+ obj.f.class.name.must_equal "ASprefix"
416
+ end
417
+
418
+ it "uses parent search_prefix" do
419
+ obj = RecordWithParentSearchPrefix.new
420
+ obj.s.f.class.name.must_equal "ASprefix"
421
+ end
422
+
423
+ it "uses nested search_prefix" do
424
+ obj = RecordWithNestedSearchPrefix.new
425
+ obj.s.f.class.name.must_equal "ASprefix"
426
+ end
427
+
428
+ it "uses prioritised nested search_prefix" do
429
+ obj = RecordWithPrioritisedNestedSearchPrefix.new
430
+ obj.s.f.class.name.must_equal "ASprefix"
378
431
  end
379
432
  end
380
433
 
@@ -392,12 +445,12 @@ describe BinData::Record, "with endian :big_and_little" do
392
445
 
393
446
  it "creates big endian version" do
394
447
  obj = RecordWithBnLEndianBe.new
395
- obj.to_binary_s.must_equal binary("\x00\x01")
448
+ obj.to_binary_s.must_equal_binary "\x00\x01"
396
449
  end
397
450
 
398
451
  it "creates little endian version" do
399
452
  obj = RecordWithBnLEndianLe.new
400
- obj.to_binary_s.must_equal binary("\x01\x00")
453
+ obj.to_binary_s.must_equal_binary "\x01\x00"
401
454
  end
402
455
 
403
456
  it "requires :endian as argument" do
@@ -408,7 +461,28 @@ describe BinData::Record, "with endian :big_and_little" do
408
461
 
409
462
  it "accepts :endian as argument" do
410
463
  obj = RecordWithBnLEndian.new(:endian => :little)
411
- obj.to_binary_s.must_equal binary("\x01\x00")
464
+ obj.to_binary_s.must_equal_binary "\x01\x00"
465
+ end
466
+ end
467
+
468
+ describe BinData::Record, "with endian :big_and_little and search_prefix" do
469
+ class NsBNLIntBe < BinData::Int16be; end
470
+ class NsBNLIntLe < BinData::Int16le; end
471
+
472
+ class RecordWithBnLEndianAndSearchPrefix < BinData::Record
473
+ endian :big_and_little
474
+ search_prefix :ns
475
+ bnl_int :a, :value => 1
476
+ end
477
+
478
+ it "creates big endian version" do
479
+ obj = RecordWithBnLEndianAndSearchPrefixBe.new
480
+ obj.to_binary_s.must_equal_binary "\x00\x01"
481
+ end
482
+
483
+ it "creates little endian version" do
484
+ obj = RecordWithBnLEndianAndSearchPrefixLe.new
485
+ obj.to_binary_s.must_equal_binary "\x01\x00"
412
486
  end
413
487
  end
414
488
 
@@ -429,12 +503,12 @@ describe BinData::Record, "with endian :big_and_little when subclassed" do
429
503
 
430
504
  it "creates big endian version" do
431
505
  obj = BRecordWithBnLEndianBe.new
432
- obj.to_binary_s.must_equal binary("\x00\x01\x00\x02")
506
+ obj.to_binary_s.must_equal_binary "\x00\x01\x00\x02"
433
507
  end
434
508
 
435
509
  it "creates little endian version" do
436
510
  obj = BRecordWithBnLEndianLe.new
437
- obj.to_binary_s.must_equal binary("\x01\x00\x02\x00")
511
+ obj.to_binary_s.must_equal_binary "\x01\x00\x02\x00"
438
512
  end
439
513
 
440
514
  it "requires :endian as argument" do
@@ -445,7 +519,7 @@ describe BinData::Record, "with endian :big_and_little when subclassed" do
445
519
 
446
520
  it "accepts :endian as argument" do
447
521
  obj = BRecordWithBnLEndian.new(:endian => :little)
448
- obj.to_binary_s.must_equal binary("\x01\x00\x02\x00")
522
+ obj.to_binary_s.must_equal_binary "\x01\x00\x02\x00"
449
523
  end
450
524
  end
451
525
 
@@ -481,7 +555,7 @@ describe BinData::Record, "defined recursively" do
481
555
  obj.val = 5
482
556
  obj.nxt.val = 6
483
557
  obj.nxt.nxt.val = 7
484
- obj.to_binary_s.must_equal "\x00\x05\x01\x00\x06\x01\x00\x07\x00"
558
+ obj.to_binary_s.must_equal_binary "\x00\x05\x01\x00\x06\x01\x00\x07\x00"
485
559
  end
486
560
  end
487
561
 
@@ -544,7 +618,7 @@ describe BinData::Record, "with :onlyif" do
544
618
  it "initial state" do
545
619
  obj.num_bytes.must_equal 2
546
620
  obj.snapshot.must_equal({:a => 3, :b => 5})
547
- obj.to_binary_s.must_equal "\x03\x05"
621
+ obj.to_binary_s.must_equal_binary "\x03\x05"
548
622
  end
549
623
 
550
624
  it "identifies if fields are included" do
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Registry do
6
6
  A = Class.new
@@ -59,10 +59,10 @@ describe BinData::Registry, "with numerics" do
59
59
  let(:r) { BinData::RegisteredClasses }
60
60
 
61
61
  it "lookup integers with endian" do
62
- r.lookup("int24", :big).to_s.must_equal "BinData::Int24be"
63
- r.lookup("int24", :little).to_s.must_equal "BinData::Int24le"
64
- r.lookup("uint24", :big).to_s.must_equal "BinData::Uint24be"
65
- r.lookup("uint24", :little).to_s.must_equal "BinData::Uint24le"
62
+ r.lookup("int24", {:endian => :big}).to_s.must_equal "BinData::Int24be"
63
+ r.lookup("int24", {:endian => :little}).to_s.must_equal "BinData::Int24le"
64
+ r.lookup("uint24", {:endian => :big}).to_s.must_equal "BinData::Uint24be"
65
+ r.lookup("uint24", {:endian => :little}).to_s.must_equal "BinData::Uint24le"
66
66
  end
67
67
 
68
68
  it "does not lookup integers without endian" do
@@ -76,18 +76,18 @@ describe BinData::Registry, "with numerics" do
76
76
  r.lookup("int3")
77
77
  }.must_raise BinData::UnRegisteredTypeError
78
78
  lambda {
79
- r.lookup("int3", :big)
79
+ r.lookup("int3", {:endian => :big})
80
80
  }.must_raise BinData::UnRegisteredTypeError
81
81
  lambda {
82
- r.lookup("int3", :little)
82
+ r.lookup("int3", {:endian => :little})
83
83
  }.must_raise BinData::UnRegisteredTypeError
84
84
  end
85
85
 
86
86
  it "lookup floats with endian" do
87
- r.lookup("float", :big).to_s.must_equal "BinData::FloatBe"
88
- r.lookup("float", :little).to_s.must_equal "BinData::FloatLe"
89
- r.lookup("double", :big).to_s.must_equal "BinData::DoubleBe"
90
- r.lookup("double", :little).to_s.must_equal "BinData::DoubleLe"
87
+ r.lookup("float", {:endian => :big}).to_s.must_equal "BinData::FloatBe"
88
+ r.lookup("float", {:endian => :little}).to_s.must_equal "BinData::FloatLe"
89
+ r.lookup("double", {:endian => :big}).to_s.must_equal "BinData::DoubleBe"
90
+ r.lookup("double", {:endian => :little}).to_s.must_equal "BinData::DoubleLe"
91
91
  end
92
92
 
93
93
  it "lookup bits" do
@@ -97,17 +97,17 @@ describe BinData::Registry, "with numerics" do
97
97
  end
98
98
 
99
99
  it "lookup bits by ignoring endian" do
100
- r.lookup("bit2", :big).to_s.must_equal "BinData::Bit2"
101
- r.lookup("bit3le", :big).to_s.must_equal "BinData::Bit3le"
102
- r.lookup("bit2", :little).to_s.must_equal "BinData::Bit2"
103
- r.lookup("bit3le", :little).to_s.must_equal "BinData::Bit3le"
100
+ r.lookup("bit2", {:endian => :big}).to_s.must_equal "BinData::Bit2"
101
+ r.lookup("bit3le", {:endian => :big}).to_s.must_equal "BinData::Bit3le"
102
+ r.lookup("bit2", {:endian => :little}).to_s.must_equal "BinData::Bit2"
103
+ r.lookup("bit3le", {:endian => :little}).to_s.must_equal "BinData::Bit3le"
104
104
  end
105
105
 
106
106
  it "lookup signed bits by ignoring endian" do
107
- r.lookup("sbit2", :big).to_s.must_equal "BinData::Sbit2"
108
- r.lookup("sbit3le", :big).to_s.must_equal "BinData::Sbit3le"
109
- r.lookup("sbit2", :little).to_s.must_equal "BinData::Sbit2"
110
- r.lookup("sbit3le", :little).to_s.must_equal "BinData::Sbit3le"
107
+ r.lookup("sbit2", {:endian => :big}).to_s.must_equal "BinData::Sbit2"
108
+ r.lookup("sbit3le", {:endian => :big}).to_s.must_equal "BinData::Sbit3le"
109
+ r.lookup("sbit2", {:endian => :little}).to_s.must_equal "BinData::Sbit2"
110
+ r.lookup("sbit3le", {:endian => :little}).to_s.must_equal "BinData::Sbit3le"
111
111
  end
112
112
  end
113
113
 
@@ -120,16 +120,37 @@ describe BinData::Registry, "with endian specific types" do
120
120
  end
121
121
 
122
122
  it "lookup little endian types" do
123
- r.lookup('a', :little).must_equal A
123
+ r.lookup('a', {:endian => :little}).must_equal A
124
124
  end
125
125
 
126
126
  it "lookup big endian types" do
127
- r.lookup('b', :big).must_equal B
127
+ r.lookup('b', {:endian => :big}).must_equal B
128
128
  end
129
129
 
130
130
  it "does not lookup types with non existent endian" do
131
131
  lambda {
132
- r.lookup('a', :big)
132
+ r.lookup('a', {:endian => :big})
133
133
  }.must_raise BinData::UnRegisteredTypeError
134
134
  end
135
135
  end
136
+
137
+ describe BinData::Registry, "with search_prefix" do
138
+ let(:r) { BinData::Registry.new }
139
+
140
+ before do
141
+ r.register('a_f', A)
142
+ r.register('b_f', B)
143
+ end
144
+
145
+ it "lookup single search_prefix" do
146
+ r.lookup('f', {:search_prefix => :a}).must_equal A
147
+ end
148
+
149
+ it "lookup multiple search_prefix" do
150
+ r.lookup('f', {:search_prefix => [:x, :a]}).must_equal A
151
+ end
152
+
153
+ it "lookup first match in search_prefix" do
154
+ r.lookup('f', {:search_prefix => [:a, :b]}).must_equal A
155
+ end
156
+ end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Rest do
6
6
  let(:obj) { BinData::Rest.new }
@@ -17,7 +17,7 @@ describe BinData::Rest do
17
17
  it "allows setting value for completeness" do
18
18
  obj.assign("123")
19
19
  obj.must_equal "123"
20
- obj.to_binary_s.must_equal "123"
20
+ obj.to_binary_s.must_equal_binary "123"
21
21
  end
22
22
 
23
23
  it "accepts BinData::BasePrimitive parameters" do
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Skip do
6
6
  let(:obj) { BinData::Skip.new(:length => 5) }
@@ -8,7 +8,7 @@ describe BinData::Skip do
8
8
 
9
9
  it "initial state" do
10
10
  obj.must_equal ""
11
- obj.to_binary_s.must_equal "\000" * 5
11
+ obj.to_binary_s.must_equal_binary "\000" * 5
12
12
  end
13
13
 
14
14
  it "skips bytes" do
@@ -18,11 +18,11 @@ describe BinData::Skip do
18
18
 
19
19
  it "has expected binary representation after setting value" do
20
20
  obj.assign("123")
21
- obj.to_binary_s.must_equal "\000" * 5
21
+ obj.to_binary_s.must_equal_binary "\000" * 5
22
22
  end
23
23
 
24
24
  it "has expected binary representation after reading" do
25
25
  obj.read(io)
26
- obj.to_binary_s.must_equal "\000" * 5
26
+ obj.to_binary_s.must_equal_binary "\000" * 5
27
27
  end
28
28
  end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::String, "with mutually exclusive parameters" do
6
6
  it ":value and :initial_value" do
@@ -249,7 +249,7 @@ describe BinData::String, "with :pad_front" do
249
249
 
250
250
  it "has to_binary_s" do
251
251
  obj.assign("abc")
252
- obj.to_binary_s.must_equal "RRabc"
252
+ obj.to_binary_s.must_equal_binary "RRabc"
253
253
  end
254
254
 
255
255
  it "reads" do
@@ -267,19 +267,19 @@ describe BinData::String, "with Ruby 1.9 encodings" do
267
267
  end
268
268
 
269
269
  let(:obj) { UTF8String.new }
270
- let(:binary_str) { binary("\xC3\x85\xC3\x84\xC3\x96") }
270
+ let(:binary_str) { "\xC3\x85\xC3\x84\xC3\x96" }
271
271
  let(:utf8_str) { binary_str.dup.force_encoding('UTF-8') }
272
272
 
273
273
  it "stores assigned values as binary" do
274
274
  obj.assign(utf8_str)
275
- obj.to_binary_s.must_equal binary_str
275
+ obj.to_binary_s.must_equal_binary binary_str
276
276
  end
277
277
 
278
278
  it "stores read values as binary" do
279
- obj = UTF8String.new(:read_length => binary_str.length)
279
+ obj = UTF8String.new(:read_length => binary_str.bytesize)
280
280
  obj.read(binary_str)
281
281
 
282
- obj.to_binary_s.must_equal binary_str
282
+ obj.to_binary_s.must_equal_binary binary_str
283
283
  end
284
284
 
285
285
  it "returns values in correct encoding" do
@@ -291,7 +291,35 @@ describe BinData::String, "with Ruby 1.9 encodings" do
291
291
  it "has correct num_bytes" do
292
292
  obj.assign(utf8_str)
293
293
 
294
- obj.num_bytes.must_equal binary_str.length
294
+ obj.num_bytes.must_equal binary_str.bytesize
295
295
  end
296
296
  end
297
297
 
298
+ class Object
299
+ def must_warn(msg, &block)
300
+ result = ""
301
+ callable = proc { |str|
302
+ result = str
303
+ }
304
+ self.stub(:warn, callable) do
305
+ block.call
306
+ end
307
+ result.must_equal msg
308
+ end
309
+ end
310
+
311
+ describe BinData::String, "warnings" do
312
+ it "warns if has :asserted_value but no :length" do
313
+ obj = BinData::String.new(:asserted_value => "ABC")
314
+ obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
315
+ lambda { obj.read("abcde") }.must_raise BinData::ValidityError
316
+ end
317
+ end
318
+
319
+ it "warns if has :value but no :read_length" do
320
+ obj = BinData::String.new(:value => "ABC")
321
+ obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
322
+ obj.read("abcde")
323
+ end
324
+ end
325
+ end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require File.expand_path(File.join(File.dirname(__FILE__), "common"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
4
4
 
5
5
  describe BinData::Stringz, "when empty" do
6
6
  let(:obj) { BinData::Stringz.new }
@@ -8,7 +8,7 @@ describe BinData::Stringz, "when empty" do
8
8
  it "initial state" do
9
9
  obj.value.must_equal ""
10
10
  obj.num_bytes.must_equal 1
11
- obj.to_binary_s.must_equal "\0"
11
+ obj.to_binary_s.must_equal_binary "\0"
12
12
  end
13
13
  end
14
14
 
@@ -18,7 +18,7 @@ describe BinData::Stringz, "with value set" do
18
18
  it "initial state" do
19
19
  obj.value.must_equal "abcd"
20
20
  obj.num_bytes.must_equal 5
21
- obj.to_binary_s.must_equal "abcd\0"
21
+ obj.to_binary_s.must_equal_binary "abcd\0"
22
22
  end
23
23
  end
24
24
 
@@ -112,16 +112,16 @@ describe BinData::Stringz, "with max_length" do
112
112
 
113
113
  it "writes values greater than max_length" do
114
114
  obj.assign("abcde")
115
- obj.to_binary_s.must_equal "abcd\0"
115
+ obj.to_binary_s.must_equal_binary "abcd\0"
116
116
  end
117
117
 
118
118
  it "writes values less than max_length" do
119
119
  obj.assign("abc")
120
- obj.to_binary_s.must_equal "abc\0"
120
+ obj.to_binary_s.must_equal_binary "abc\0"
121
121
  end
122
122
 
123
123
  it "writes values exactly max_length" do
124
124
  obj.assign("abcd")
125
- obj.to_binary_s.must_equal "abcd\0"
125
+ obj.to_binary_s.must_equal_binary "abcd\0"
126
126
  end
127
127
  end