spec/binary/template_spec.rb in ronin-support-0.5.1 vs spec/binary/template_spec.rb in ronin-support-0.5.2

- old
+ new

@@ -1,97 +1,99 @@ +# encoding: US-ASCII + require 'spec_helper' require 'ronin/binary/template' describe Binary::Template do describe "TYPES" do subject { described_class::TYPES } - it("uint8 => C") { subject[:uint8].should == 'C' } - it("uint16 => S") { subject[:uint16].should == 'S' } - it("uint32 => L") { subject[:uint32].should == 'L' } - it("uint64 => Q") { subject[:uint64].should == 'Q' } - it("int8 => C") { subject[:int8].should == 'c' } - it("int16 => S") { subject[:int16].should == 's' } - it("int32 => L") { subject[:int32].should == 'l' } - it("int64 => Q") { subject[:int64].should == 'q' } + it("uint8 => C") { expect(subject[:uint8]).to eq('C' ) } + it("uint16 => S") { expect(subject[:uint16]).to eq('S' ) } + it("uint32 => L") { expect(subject[:uint32]).to eq('L' ) } + it("uint64 => Q") { expect(subject[:uint64]).to eq('Q' ) } + it("int8 => C") { expect(subject[:int8]).to eq('c' ) } + it("int16 => S") { expect(subject[:int16]).to eq('s' ) } + it("int32 => L") { expect(subject[:int32]).to eq('l' ) } + it("int64 => Q") { expect(subject[:int64]).to eq('q' ) } if RUBY_VERSION < '1.9.' - it("uint16_le => v") { subject[:uint16_le].should == 'v' } - it("uint32_le => V") { subject[:uint32_le].should == 'V' } - it("uint16_be => n") { subject[:uint16_be].should == 'n' } - it("uint32_be => N") { subject[:uint32_be].should == 'N' } + it("uint16_le => v") { expect(subject[:uint16_le]).to eq('v' ) } + it("uint32_le => V") { expect(subject[:uint32_le]).to eq('V' ) } + it("uint16_be => n") { expect(subject[:uint16_be]).to eq('n' ) } + it("uint32_be => N") { expect(subject[:uint32_be]).to eq('N' ) } end - it("uchar => Z") { subject[:uchar].should == 'Z' } - it("ushort => S!") { subject[:ushort].should == 'S!'} - it("uint => I!") { subject[:uint].should == 'I!'} - it("ulong => L!") { subject[:ulong].should == 'L!'} - it("ulong_long => Q") { subject[:ulong_long].should == 'Q' } - it("char => Z") { subject[:char].should == 'Z' } - it("short => s!") { subject[:short].should == 's!'} - it("int => i!") { subject[:int].should == 'i!'} - it("long => l!") { subject[:long].should == 'l!'} - it("long_long => q") { subject[:long_long].should == 'q' } - it("utf8 => U") { subject[:utf8].should == 'U' } - it("float => F") { subject[:float].should == 'F' } - it("double => D") { subject[:double].should == 'D' } - it("float_le => e") { subject[:float_le].should == 'e' } - it("double_le => E") { subject[:double_le].should == 'E' } - it("float_be => g") { subject[:float_be].should == 'g' } - it("double_ge => G") { subject[:double_be].should == 'G' } - it("ubyte => C") { subject[:ubyte].should == 'C' } - it("byte => c") { subject[:byte].should == 'c' } - it("string => Z*") { subject[:string].should == 'Z*'} + it("uchar => Z") { expect(subject[:uchar]).to eq('Z' ) } + it("ushort => S!") { expect(subject[:ushort]).to eq('S!') } + it("uint => I!") { expect(subject[:uint]).to eq('I!') } + it("ulong => L!") { expect(subject[:ulong]).to eq('L!') } + it("ulong_long => Q") { expect(subject[:ulong_long]).to eq('Q' ) } + it("char => Z") { expect(subject[:char]).to eq('Z' ) } + it("short => s!") { expect(subject[:short]).to eq('s!') } + it("int => i!") { expect(subject[:int]).to eq('i!') } + it("long => l!") { expect(subject[:long]).to eq('l!') } + it("long_long => q") { expect(subject[:long_long]).to eq('q' ) } + it("utf8 => U") { expect(subject[:utf8]).to eq('U' ) } + it("float => F") { expect(subject[:float]).to eq('F' ) } + it("double => D") { expect(subject[:double]).to eq('D' ) } + it("float_le => e") { expect(subject[:float_le]).to eq('e' ) } + it("double_le => E") { expect(subject[:double_le]).to eq('E' ) } + it("float_be => g") { expect(subject[:float_be]).to eq('g' ) } + it("double_ge => G") { expect(subject[:double_be]).to eq('G' ) } + it("ubyte => C") { expect(subject[:ubyte]).to eq('C' ) } + it("byte => c") { expect(subject[:byte]).to eq('c' ) } + it("string => Z*") { expect(subject[:string]).to eq('Z*') } if RUBY_VERSION > '1.9.' context "Ruby 1.9" do - it("uint16_le => S<") { subject[:uint16_le].should == 'S<' } - it("uint32_le => L<") { subject[:uint32_le].should == 'L<' } - it("uint64_le => Q<") { subject[:uint64_le].should == 'Q<' } - it("int16_le => S<") { subject[:int16_le].should == 's<' } - it("int32_le => L<") { subject[:int32_le].should == 'l<' } - it("int64_le => Q<") { subject[:int64_le].should == 'q<' } - it("uint16_be => S>") { subject[:uint16_be].should == 'S>' } - it("uint32_be => L>") { subject[:uint32_be].should == 'L>' } - it("uint64_be => Q>") { subject[:uint64_be].should == 'Q>' } - it("int16_be => S>") { subject[:int16_be].should == 's>' } - it("int32_be => L>") { subject[:int32_be].should == 'l>' } - it("int64_be => Q>") { subject[:int64_be].should == 'q>' } - it("ushort_le => S!<") { subject[:ushort_le].should == 'S!<'} - it("uint_le => I!<") { subject[:uint_le].should == 'I!<'} - it("ulong_le => L!<") { subject[:ulong_le].should == 'L!<'} - it("ulong_long_le => L!<") { subject[:ulong_long_le].should == 'Q<' } - it("short_le => S!<") { subject[:short_le].should == 's!<'} - it("int_le => I!<") { subject[:int_le].should == 'i!<'} - it("long_le => L!<") { subject[:long_le].should == 'l!<'} - it("long_long_le => L!<") { subject[:long_long_le].should == 'q<' } - it("ushort_be => S!>") { subject[:ushort_be].should == 'S!>'} - it("uint_be => I!>") { subject[:uint_be].should == 'I!>'} - it("ulong_be => L!>") { subject[:ulong_be].should == 'L!>'} - it("ulong_long_be => L!>") { subject[:ulong_long_be].should == 'Q>' } - it("short_be => S!>") { subject[:short_be].should == 's!>'} - it("int_be => I!>") { subject[:int_be].should == 'i!>'} - it("long_be => L!>") { subject[:long_be].should == 'l!>'} - it("long_long_be => L!>") { subject[:long_long_be].should == 'q>' } + it("uint16_le => S<") { expect(subject[:uint16_le]).to eq('S<' ) } + it("uint32_le => L<") { expect(subject[:uint32_le]).to eq('L<' ) } + it("uint64_le => Q<") { expect(subject[:uint64_le]).to eq('Q<' ) } + it("int16_le => S<") { expect(subject[:int16_le]).to eq('s<' ) } + it("int32_le => L<") { expect(subject[:int32_le]).to eq('l<' ) } + it("int64_le => Q<") { expect(subject[:int64_le]).to eq('q<' ) } + it("uint16_be => S>") { expect(subject[:uint16_be]).to eq('S>' ) } + it("uint32_be => L>") { expect(subject[:uint32_be]).to eq('L>' ) } + it("uint64_be => Q>") { expect(subject[:uint64_be]).to eq('Q>' ) } + it("int16_be => S>") { expect(subject[:int16_be]).to eq('s>' ) } + it("int32_be => L>") { expect(subject[:int32_be]).to eq('l>' ) } + it("int64_be => Q>") { expect(subject[:int64_be]).to eq('q>' ) } + it("ushort_le => S!<") { expect(subject[:ushort_le]).to eq('S!<') } + it("uint_le => I!<") { expect(subject[:uint_le]).to eq('I!<') } + it("ulong_le => L!<") { expect(subject[:ulong_le]).to eq('L!<') } + it("ulong_long_le => L!<") { expect(subject[:ulong_long_le]).to eq('Q<' ) } + it("short_le => S!<") { expect(subject[:short_le]).to eq('s!<') } + it("int_le => I!<") { expect(subject[:int_le]).to eq('i!<') } + it("long_le => L!<") { expect(subject[:long_le]).to eq('l!<') } + it("long_long_le => L!<") { expect(subject[:long_long_le]).to eq('q<' ) } + it("ushort_be => S!>") { expect(subject[:ushort_be]).to eq('S!>') } + it("uint_be => I!>") { expect(subject[:uint_be]).to eq('I!>') } + it("ulong_be => L!>") { expect(subject[:ulong_be]).to eq('L!>') } + it("ulong_long_be => L!>") { expect(subject[:ulong_long_be]).to eq('Q>' ) } + it("short_be => S!>") { expect(subject[:short_be]).to eq('s!>') } + it("int_be => I!>") { expect(subject[:int_be]).to eq('i!>') } + it("long_be => L!>") { expect(subject[:long_be]).to eq('l!>') } + it("long_long_be => L!>") { expect(subject[:long_long_be]).to eq('q>' ) } end end end describe "translate" do subject { described_class } context "when given :endian" do it "should translate endian-types" do - subject.translate(:uint, :endian => :little).should == :uint_le + expect(subject.translate(:uint, :endian => :little)).to eq(:uint_le) end it "should not translate non-endian-types" do - subject.translate(:string, :endian => :little).should == :string + expect(subject.translate(:string, :endian => :little)).to eq(:string) end it "should raise an ArgumentError for unknown endianness" do - lambda { + expect { subject.translate(:uint, :endian => :foo) - }.should raise_error(ArgumentError) + }.to raise_error(ArgumentError) end end end describe "compile" do @@ -99,38 +101,38 @@ let(:code) { subject::TYPES[type] } subject { described_class } it "should translate types to their pack codes" do - subject.compile([type]).should == code + expect(subject.compile([type])).to eq(code) end it "should support specifying the length of a field" do - subject.compile([[type, 10]]).should == "#{code}10" + expect(subject.compile([[type, 10]])).to eq("#{code}10") end it "should raise ArgumentError for unknown types" do - lambda { + expect { subject.compile([:foo]) - }.should raise_error(ArgumentError) + }.to raise_error(ArgumentError) end end describe "#initialize" do subject { described_class.new [:uint32, :string] } it "should store the types" do - subject.fields.should == [ + expect(subject.fields).to eq([ :uint32, :string - ] + ]) end it "should raise ArgumentError for unknown types" do - lambda { + expect { described_class.new [:foo] - }.should raise_error(ArgumentError) + }.to raise_error(ArgumentError) end end ENDIANNESS = if [0x1234].pack('S') == "\x34\x12" :little @@ -159,256 +161,256 @@ describe "#pack" do context ":byte" do subject { described_class.new [:byte] } it "should pack a signed byte" do - subject.pack(byte).should == char + expect(subject.pack(byte)).to eq(char) end end context "[:byte, n]" do let(:n) { string.length } subject { described_class.new [[:byte, n]] } it "should pack multiple signed characters" do - subject.pack(*bytes).should == chars + expect(subject.pack(*bytes)).to eq(chars) end end context ":char" do subject { described_class.new [:char] } it "should pack a signed character" do - subject.pack(char).should == char + expect(subject.pack(char)).to eq(char) end end context "[:char, n]" do let(:n) { string.length } subject { described_class.new [[:char, n]] } it "should pack multiple signed characters" do - subject.pack(*chars).should == string + expect(subject.pack(*chars)).to eq(string) end context "padding" do let(:padding) { 10 } subject { described_class.new [[:char, n + padding]] } it "should pad the string with '\\0' characters" do - subject.pack(*chars).should == (string + ("\0" * padding)) + expect(subject.pack(*chars)).to eq((string + ("\0" * padding))) end end end context ":uint8" do subject { described_class.new [:uint8] } it "should pack an unsigned 8bit integer" do - subject.pack(uint8).should == "\xff" + expect(subject.pack(uint8)).to eq("\xff") end end context ":uint16" do subject { described_class.new [:uint16] } it "should pack an unsigned 16bit integer" do - subject.pack(uint16).should == "\xff\xff" + expect(subject.pack(uint16)).to eq("\xff\xff") end end context ":uint32" do subject { described_class.new [:uint32] } it "should pack an unsigned 32bit integer" do - subject.pack(uint32).should == "\xff\xff\xff\xff" + expect(subject.pack(uint32)).to eq("\xff\xff\xff\xff") end end context ":uint64" do subject { described_class.new [:uint64] } it "should pack an unsigned 64bit integer" do - subject.pack(uint64).should == "\xff\xff\xff\xff\xff\xff\xff\xff" + expect(subject.pack(uint64)).to eq("\xff\xff\xff\xff\xff\xff\xff\xff") end end context ":int8" do subject { described_class.new [:int8] } it "should pack an signed 8bit integer" do - subject.pack(int8).should == "\xff" + expect(subject.pack(int8)).to eq("\xff") end end context ":int16" do subject { described_class.new [:int16] } it "should pack an unsigned 16bit integer" do - subject.pack(int16).should == "\xff\xff" + expect(subject.pack(int16)).to eq("\xff\xff") end end context ":int32" do subject { described_class.new [:int32] } it "should pack an unsigned 32bit integer" do - subject.pack(int32).should == "\xff\xff\xff\xff" + expect(subject.pack(int32)).to eq("\xff\xff\xff\xff") end end context ":int64" do subject { described_class.new [:int64] } it "should pack an unsigned 64bit integer" do - subject.pack(int64).should == "\xff\xff\xff\xff\xff\xff\xff\xff" + expect(subject.pack(int64)).to eq("\xff\xff\xff\xff\xff\xff\xff\xff") end end context ":string" do subject { described_class.new [:string] } it "should pack a string" do - subject.pack(string).should == "#{string}\0" + expect(subject.pack(string)).to eq("#{string}\0") end end end describe "#unpack" do context ":byte" do subject { described_class.new [:byte] } it "should unpack a signed byte" do - subject.unpack(char).should == [byte] + expect(subject.unpack(char)).to eq([byte]) end end context "[:byte, n]" do let(:n) { string.length } subject { described_class.new [[:byte, n]] } it "should pack multiple signed characters" do - subject.unpack(chars).should == bytes + expect(subject.unpack(chars)).to eq(bytes) end end context ":char" do subject { described_class.new [:char] } it "should unpack a signed character" do - subject.unpack(char).should == [char] + expect(subject.unpack(char)).to eq([char]) end end context "[:char, n]" do let(:n) { string.length } subject { described_class.new [[:char, n]] } it "should unpack multiple signed characters" do - subject.unpack(string).should == [chars] + expect(subject.unpack(string)).to eq([chars]) end context "padding" do let(:padding) { 10 } subject { described_class.new [[:char, n + padding]] } it "should strip '\\0' padding characters" do - subject.unpack(string + ("\0" * padding)).should == [chars] + expect(subject.unpack(string + ("\0" * padding))).to eq([chars]) end end end context ":uint8" do subject { described_class.new [:uint8] } it "should unpack an unsigned 8bit integer" do - subject.unpack("\xff").should == [uint8] + expect(subject.unpack("\xff")).to eq([uint8]) end end context ":uint16" do subject { described_class.new [:uint16] } it "should unpack an unsigned 16bit integer" do - subject.unpack("\xff\xff").should == [uint16] + expect(subject.unpack("\xff\xff")).to eq([uint16]) end end context ":uint32" do subject { described_class.new [:uint32] } it "should unpack an unsigned 32bit integer" do - subject.unpack("\xff\xff\xff\xff").should == [uint32] + expect(subject.unpack("\xff\xff\xff\xff")).to eq([uint32]) end end context ":uint64" do subject { described_class.new [:uint64] } it "should unpack an unsigned 64bit integer" do - subject.unpack("\xff\xff\xff\xff\xff\xff\xff\xff").should == [uint64] + expect(subject.unpack("\xff\xff\xff\xff\xff\xff\xff\xff")).to eq([uint64]) end end context ":int8" do subject { described_class.new [:int8] } it "should unpack an signed 8bit integer" do - subject.unpack("\xff").should == [int8] + expect(subject.unpack("\xff")).to eq([int8]) end end context ":int16" do subject { described_class.new [:int16] } it "should unpack an unsigned 16bit integer" do - subject.unpack("\xff\xff").should == [int16] + expect(subject.unpack("\xff\xff")).to eq([int16]) end end context ":int32" do subject { described_class.new [:int32] } it "should unpack an unsigned 32bit integer" do - subject.unpack("\xff\xff\xff\xff").should == [int32] + expect(subject.unpack("\xff\xff\xff\xff")).to eq([int32]) end end context ":int64" do subject { described_class.new [:int64] } it "should unpack an unsigned 64bit integer" do - subject.unpack("\xff\xff\xff\xff\xff\xff\xff\xff").should == [int64] + expect(subject.unpack("\xff\xff\xff\xff\xff\xff\xff\xff")).to eq([int64]) end end context ":string" do subject { described_class.new [:string] } it "should unpack a string" do - subject.unpack("#{string}\0").should == [string] + expect(subject.unpack("#{string}\0")).to eq([string]) end end end describe "#to_s" do subject { described_class.new [:uint32, :string] } it "should return the pack format String" do - subject.to_s.should == "LZ*" + expect(subject.to_s).to eq("LZ*") end end describe "#inspect" do let(:template) { described_class.new [:uint32, :string] } subject { template.inspect } it "should inspect the class" do - subject.should include(described_class.name) + expect(subject).to include(described_class.name) end it "should inspect the template" do - subject.should include(template.fields.inspect) + expect(subject).to include(template.fields.inspect) end end end