diff options
Diffstat (limited to 'spec/ruby/core/string/unpack')
31 files changed, 629 insertions, 387 deletions
diff --git a/spec/ruby/core/string/unpack/a_spec.rb b/spec/ruby/core/string/unpack/a_spec.rb index 18882c91a6..a68e842e15 100644 --- a/spec/ruby/core/string/unpack/a_spec.rb +++ b/spec/ruby/core/string/unpack/a_spec.rb @@ -1,14 +1,16 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/string', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/string' +require_relative 'shared/taint' describe "String#unpack with format 'A'" do it_behaves_like :string_unpack_basic, 'A' it_behaves_like :string_unpack_no_platform, 'A' it_behaves_like :string_unpack_string, 'A' it_behaves_like :string_unpack_Aa, 'A' + it_behaves_like :string_unpack_taint, 'A' it "removes trailing space and NULL bytes from the decoded string" do [ ["a\x00 b \x00", ["a\x00 b", ""]], @@ -29,8 +31,8 @@ describe "String#unpack with format 'A'" do end it "decodes into raw (ascii) string values" do - str = "str".force_encoding('UTF-8').unpack("A*")[0] - str.encoding.name.should == 'ASCII-8BIT' + str = "str".dup.force_encoding('UTF-8').unpack("A*")[0] + str.encoding.should == Encoding::BINARY end end @@ -40,6 +42,7 @@ describe "String#unpack with format 'a'" do it_behaves_like :string_unpack_no_platform, 'a' it_behaves_like :string_unpack_string, 'a' it_behaves_like :string_unpack_Aa, 'a' + it_behaves_like :string_unpack_taint, 'a' it "does not remove trailing whitespace or NULL bytes from the decoded string" do [ ["a\x00 b \x00", ["a\x00 b \x00"]], @@ -57,7 +60,7 @@ describe "String#unpack with format 'a'" do it "decodes into raw (ascii) string values" do str = "".unpack("a*")[0] - str.encoding.name.should == 'ASCII-8BIT' + str.encoding.should == Encoding::BINARY end end diff --git a/spec/ruby/core/string/unpack/at_spec.rb b/spec/ruby/core/string/unpack/at_spec.rb index 70cbebd2ba..f4999f5922 100644 --- a/spec/ruby/core/string/unpack/at_spec.rb +++ b/spec/ruby/core/string/unpack/at_spec.rb @@ -1,7 +1,7 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' describe "String#unpack with format '@'" do it_behaves_like :string_unpack_basic, '@' @@ -24,6 +24,6 @@ describe "String#unpack with format '@'" do end it "raises an ArgumentError if the count exceeds the size of the String" do - lambda { "\x01\x02\x03\x04".unpack("C2@5C") }.should raise_error(ArgumentError) + -> { "\x01\x02\x03\x04".unpack("C2@5C") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/b_spec.rb b/spec/ruby/core/string/unpack/b_spec.rb index fa632e6526..fac6ef5151 100644 --- a/spec/ruby/core/string/unpack/b_spec.rb +++ b/spec/ruby/core/string/unpack/b_spec.rb @@ -1,11 +1,13 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/taint' describe "String#unpack with format 'B'" do it_behaves_like :string_unpack_basic, 'B' it_behaves_like :string_unpack_no_platform, 'B' + it_behaves_like :string_unpack_taint, 'B' it "decodes one bit from each byte for each format character starting with the most significant bit" do [ ["\x00", "B", ["0"]], @@ -84,18 +86,26 @@ describe "String#unpack with format 'B'" do ].should be_computed_by(:unpack, "BBB") end - it "ignores NULL bytes between directives" do - "\x80\x00".unpack("B\x00B").should == ["1", "0"] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x80\x00".unpack("B\x00B") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do "\x80\x00".unpack("B B").should == ["1", "0"] end + + it "decodes into US-ASCII string values" do + str = "s".dup.force_encoding('UTF-8').unpack("B*")[0] + str.encoding.name.should == 'US-ASCII' + end end describe "String#unpack with format 'b'" do it_behaves_like :string_unpack_basic, 'b' it_behaves_like :string_unpack_no_platform, 'b' + it_behaves_like :string_unpack_taint, 'b' it "decodes one bit from each byte for each format character starting with the least significant bit" do [ ["\x00", "b", ["0"]], @@ -174,8 +184,10 @@ describe "String#unpack with format 'b'" do ].should be_computed_by(:unpack, "bbb") end - it "ignores NULL bytes between directives" do - "\x01\x00".unpack("b\x00b").should == ["1", "0"] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x00".unpack("b\x00b") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -183,8 +195,7 @@ describe "String#unpack with format 'b'" do end it "decodes into US-ASCII string values" do - str = "s".force_encoding('UTF-8').unpack("b*")[0] + str = "s".dup.force_encoding('UTF-8').unpack("b*")[0] str.encoding.name.should == 'US-ASCII' end - end diff --git a/spec/ruby/core/string/unpack/c_spec.rb b/spec/ruby/core/string/unpack/c_spec.rb index 36de462cac..d881015b5e 100644 --- a/spec/ruby/core/string/unpack/c_spec.rb +++ b/spec/ruby/core/string/unpack/c_spec.rb @@ -1,7 +1,7 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' describe :string_unpack_8bit, shared: true do it "decodes one byte for a single format character" do @@ -20,7 +20,7 @@ describe :string_unpack_8bit, shared: true do "abc".unpack(unpack_format('*')).should == [97, 98, 99] end - it "decodes the remaining bytes when passed the '*' modifer after another directive" do + it "decodes the remaining bytes when passed the '*' modifier after another directive" do "abc".unpack(unpack_format()+unpack_format('*')).should == [97, 98, 99] end @@ -35,8 +35,10 @@ describe :string_unpack_8bit, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - it "ignores NULL bytes between directives" do - "abc".unpack(unpack_format("\000", 2)).should == [97, 98] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "abc".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/carret_spec.rb b/spec/ruby/core/string/unpack/carret_spec.rb new file mode 100644 index 0000000000..815df0c718 --- /dev/null +++ b/spec/ruby/core/string/unpack/carret_spec.rb @@ -0,0 +1,43 @@ +# encoding: binary +ruby_version_is "4.1" do + require_relative '../../../spec_helper' + require_relative '../fixtures/classes' + require_relative 'shared/basic' + + describe "String#unpack with format '^'" do + it_behaves_like :string_unpack_basic, '^' + it_behaves_like :string_unpack_no_platform, '^' + + it "returns the current offset that start from 0" do + "".unpack("^").should == [0] + end + + it "returns the current offset after the last decode ended" do + "a".unpack("CC^").should == [97, nil, 1] + end + + it "returns the current offset that start from the given offset" do + "abc".unpack("^", offset: 1).should == [1] + end + + it "returns the offset moved by 'X'" do + "\x01\x02\x03\x04".unpack("C3X2^").should == [1, 2, 3, 1] + end + + it "returns the offset moved by 'x'" do + "\x01\x02\x03\x04".unpack("Cx2^").should == [1, 3] + end + + it "returns the offset to the position the previous decode ended" do + "foo".unpack("A4^").should == ["foo", 3] + "foo".unpack("a4^").should == ["foo", 3] + "foo".unpack("Z5^").should == ["foo", 3] + end + + it "returns the offset including truncated part" do + "foo ".unpack("A*^").should == ["foo", 6] + "foo\0".unpack("Z*^").should == ["foo", 4] + "foo\0\0\0".unpack("Z5^").should == ["foo", 5] + end + end +end diff --git a/spec/ruby/core/string/unpack/comment_spec.rb b/spec/ruby/core/string/unpack/comment_spec.rb index 884960b337..050d2b7fc0 100644 --- a/spec/ruby/core/string/unpack/comment_spec.rb +++ b/spec/ruby/core/string/unpack/comment_spec.rb @@ -1,6 +1,6 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' describe "String#unpack" do it "ignores directives text from '#' to the first newline" do diff --git a/spec/ruby/core/string/unpack/d_spec.rb b/spec/ruby/core/string/unpack/d_spec.rb index db4638f8ef..0e4f57ec04 100644 --- a/spec/ruby/core/string/unpack/d_spec.rb +++ b/spec/ruby/core/string/unpack/d_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/float', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/float' little_endian do describe "String#unpack with format 'D'" do diff --git a/spec/ruby/core/string/unpack/e_spec.rb b/spec/ruby/core/string/unpack/e_spec.rb index cb74c00206..c958be1c8b 100644 --- a/spec/ruby/core/string/unpack/e_spec.rb +++ b/spec/ruby/core/string/unpack/e_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/float', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/float' describe "String#unpack with format 'E'" do it_behaves_like :string_unpack_basic, 'E' diff --git a/spec/ruby/core/string/unpack/f_spec.rb b/spec/ruby/core/string/unpack/f_spec.rb index 60dad46703..ec8b9d435e 100644 --- a/spec/ruby/core/string/unpack/f_spec.rb +++ b/spec/ruby/core/string/unpack/f_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/float', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/float' little_endian do describe "String#unpack with format 'F'" do diff --git a/spec/ruby/core/string/unpack/g_spec.rb b/spec/ruby/core/string/unpack/g_spec.rb index f5bec1534e..ffc423b152 100644 --- a/spec/ruby/core/string/unpack/g_spec.rb +++ b/spec/ruby/core/string/unpack/g_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/float', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/float' describe "String#unpack with format 'G'" do it_behaves_like :string_unpack_basic, 'G' diff --git a/spec/ruby/core/string/unpack/h_spec.rb b/spec/ruby/core/string/unpack/h_spec.rb index 00d6d68eee..0cf8d943a7 100644 --- a/spec/ruby/core/string/unpack/h_spec.rb +++ b/spec/ruby/core/string/unpack/h_spec.rb @@ -1,11 +1,13 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/taint' describe "String#unpack with format 'H'" do it_behaves_like :string_unpack_basic, 'H' it_behaves_like :string_unpack_no_platform, 'H' + it_behaves_like :string_unpack_taint, 'H' it "decodes one nibble from each byte for each format character starting with the most significant bit" do [ ["\x8f", "H", ["8"]], @@ -54,18 +56,25 @@ describe "String#unpack with format 'H'" do ].should be_computed_by(:unpack, "HHH") end - it "ignores NULL bytes between directives" do - "\x01\x10".unpack("H\x00H").should == ["0", "1"] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x10".unpack("H\x00H") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do "\x01\x10".unpack("H H").should == ["0", "1"] end + + it "should make strings with US_ASCII encoding" do + "\x01".unpack("H")[0].encoding.should == Encoding::US_ASCII + end end describe "String#unpack with format 'h'" do it_behaves_like :string_unpack_basic, 'h' it_behaves_like :string_unpack_no_platform, 'h' + it_behaves_like :string_unpack_taint, 'h' it "decodes one nibble from each byte for each format character starting with the least significant bit" do [ ["\x8f", "h", ["f"]], @@ -114,11 +123,17 @@ describe "String#unpack with format 'h'" do ].should be_computed_by(:unpack, "hhh") end - it "ignores NULL bytes between directives" do - "\x01\x10".unpack("h\x00h").should == ["1", "0"] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x10".unpack("h\x00h") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do "\x01\x10".unpack("h h").should == ["1", "0"] end + + it "should make strings with US_ASCII encoding" do + "\x01".unpack("h")[0].encoding.should == Encoding::US_ASCII + end end diff --git a/spec/ruby/core/string/unpack/i_spec.rb b/spec/ruby/core/string/unpack/i_spec.rb index f3183afe99..b4bbba1923 100644 --- a/spec/ruby/core/string/unpack/i_spec.rb +++ b/spec/ruby/core/string/unpack/i_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' describe "String#unpack with format 'I'" do describe "with modifier '<'" do diff --git a/spec/ruby/core/string/unpack/j_spec.rb b/spec/ruby/core/string/unpack/j_spec.rb index 23d26d896e..3c2baad642 100644 --- a/spec/ruby/core/string/unpack/j_spec.rb +++ b/spec/ruby/core/string/unpack/j_spec.rb @@ -1,274 +1,272 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) - -ruby_version_is '2.3' do - platform_is pointer_size: 64 do - little_endian do - describe "String#unpack with format 'J'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_64bit_le, 'J_' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J_' - end - - describe "with modifier '!'" do - it_behaves_like :string_unpack_64bit_le, 'J!' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J!' - end - end +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' - describe "String#unpack with format 'j'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_64bit_le, 'j_' - it_behaves_like :string_unpack_64bit_le_signed, 'j_' - end +platform_is pointer_size: 64 do + little_endian do + describe "String#unpack with format 'J'" do + describe "with modifier '_'" do + it_behaves_like :string_unpack_64bit_le, 'J_' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J_' + end - describe "with modifier '!'" do - it_behaves_like :string_unpack_64bit_le, 'j!' - it_behaves_like :string_unpack_64bit_le_signed, 'j!' - end + describe "with modifier '!'" do + it_behaves_like :string_unpack_64bit_le, 'J!' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J!' end end - big_endian do - describe "String#unpack with format 'J'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_64bit_be, 'J_' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J_' - end - - describe "with modifier '!'" do - it_behaves_like :string_unpack_64bit_be, 'J!' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J!' - end + describe "String#unpack with format 'j'" do + describe "with modifier '_'" do + it_behaves_like :string_unpack_64bit_le, 'j_' + it_behaves_like :string_unpack_64bit_le_signed, 'j_' end - describe "String#unpack with format 'j'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_64bit_be, 'j_' - it_behaves_like :string_unpack_64bit_be_signed, 'j_' - end - - describe "with modifier '!'" do - it_behaves_like :string_unpack_64bit_be, 'j!' - it_behaves_like :string_unpack_64bit_be_signed, 'j!' - end + describe "with modifier '!'" do + it_behaves_like :string_unpack_64bit_le, 'j!' + it_behaves_like :string_unpack_64bit_le_signed, 'j!' end end + end + big_endian do describe "String#unpack with format 'J'" do - describe "with modifier '<'" do - it_behaves_like :string_unpack_64bit_le, 'J<' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J<' - end - - describe "with modifier '>'" do - it_behaves_like :string_unpack_64bit_be, 'J>' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J>' + describe "with modifier '_'" do + it_behaves_like :string_unpack_64bit_be, 'J_' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J_' end - describe "with modifier '<' and '_'" do - it_behaves_like :string_unpack_64bit_le, 'J<_' - it_behaves_like :string_unpack_64bit_le, 'J_<' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J<_' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J_<' - end - - describe "with modifier '<' and '!'" do - it_behaves_like :string_unpack_64bit_le, 'J<!' - it_behaves_like :string_unpack_64bit_le, 'J!<' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J<!' - it_behaves_like :string_unpack_64bit_le_unsigned, 'J!<' + describe "with modifier '!'" do + it_behaves_like :string_unpack_64bit_be, 'J!' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J!' end + end - describe "with modifier '>' and '_'" do - it_behaves_like :string_unpack_64bit_be, 'J>_' - it_behaves_like :string_unpack_64bit_be, 'J_>' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J>_' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J_>' + describe "String#unpack with format 'j'" do + describe "with modifier '_'" do + it_behaves_like :string_unpack_64bit_be, 'j_' + it_behaves_like :string_unpack_64bit_be_signed, 'j_' end - describe "with modifier '>' and '!'" do - it_behaves_like :string_unpack_64bit_be, 'J>!' - it_behaves_like :string_unpack_64bit_be, 'J!>' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J>!' - it_behaves_like :string_unpack_64bit_be_unsigned, 'J!>' + describe "with modifier '!'" do + it_behaves_like :string_unpack_64bit_be, 'j!' + it_behaves_like :string_unpack_64bit_be_signed, 'j!' end end + end - describe "String#unpack with format 'j'" do - describe "with modifier '<'" do - it_behaves_like :string_unpack_64bit_le, 'j<' - it_behaves_like :string_unpack_64bit_le_signed, 'j<' - end + describe "String#unpack with format 'J'" do + describe "with modifier '<'" do + it_behaves_like :string_unpack_64bit_le, 'J<' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J<' + end - describe "with modifier '>'" do - it_behaves_like :string_unpack_64bit_be, 'j>' - it_behaves_like :string_unpack_64bit_be_signed, 'j>' - end + describe "with modifier '>'" do + it_behaves_like :string_unpack_64bit_be, 'J>' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J>' + end - describe "with modifier '<' and '_'" do - it_behaves_like :string_unpack_64bit_le, 'j<_' - it_behaves_like :string_unpack_64bit_le, 'j_<' - it_behaves_like :string_unpack_64bit_le_signed, 'j<_' - it_behaves_like :string_unpack_64bit_le_signed, 'j_<' - end + describe "with modifier '<' and '_'" do + it_behaves_like :string_unpack_64bit_le, 'J<_' + it_behaves_like :string_unpack_64bit_le, 'J_<' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J<_' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J_<' + end - describe "with modifier '<' and '!'" do - it_behaves_like :string_unpack_64bit_le, 'j<!' - it_behaves_like :string_unpack_64bit_le, 'j!<' - it_behaves_like :string_unpack_64bit_le_signed, 'j<!' - it_behaves_like :string_unpack_64bit_le_signed, 'j!<' - end + describe "with modifier '<' and '!'" do + it_behaves_like :string_unpack_64bit_le, 'J<!' + it_behaves_like :string_unpack_64bit_le, 'J!<' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J<!' + it_behaves_like :string_unpack_64bit_le_unsigned, 'J!<' + end - describe "with modifier '>' and '_'" do - it_behaves_like :string_unpack_64bit_be, 'j>_' - it_behaves_like :string_unpack_64bit_be, 'j_>' - it_behaves_like :string_unpack_64bit_be_signed, 'j>_' - it_behaves_like :string_unpack_64bit_be_signed, 'j_>' - end + describe "with modifier '>' and '_'" do + it_behaves_like :string_unpack_64bit_be, 'J>_' + it_behaves_like :string_unpack_64bit_be, 'J_>' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J>_' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J_>' + end - describe "with modifier '>' and '!'" do - it_behaves_like :string_unpack_64bit_be, 'j>!' - it_behaves_like :string_unpack_64bit_be, 'j!>' - it_behaves_like :string_unpack_64bit_be_signed, 'j>!' - it_behaves_like :string_unpack_64bit_be_signed, 'j!>' - end + describe "with modifier '>' and '!'" do + it_behaves_like :string_unpack_64bit_be, 'J>!' + it_behaves_like :string_unpack_64bit_be, 'J!>' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J>!' + it_behaves_like :string_unpack_64bit_be_unsigned, 'J!>' end end - platform_is pointer_size: 32 do - little_endian do - describe "String#unpack with format 'J'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_32bit_le, 'J_' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J_' - end - - describe "with modifier '!'" do - it_behaves_like :string_unpack_32bit_le, 'J!' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J!' - end - end - - describe "String#unpack with format 'j'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_32bit_le, 'j_' - it_behaves_like :string_unpack_32bit_le_signed, 'j_' - end + describe "String#unpack with format 'j'" do + describe "with modifier '<'" do + it_behaves_like :string_unpack_64bit_le, 'j<' + it_behaves_like :string_unpack_64bit_le_signed, 'j<' + end - describe "with modifier '!'" do - it_behaves_like :string_unpack_32bit_le, 'j!' - it_behaves_like :string_unpack_32bit_le_signed, 'j!' - end - end + describe "with modifier '>'" do + it_behaves_like :string_unpack_64bit_be, 'j>' + it_behaves_like :string_unpack_64bit_be_signed, 'j>' end - big_endian do - describe "String#unpack with format 'J'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_32bit_be, 'J_' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J_' - end + describe "with modifier '<' and '_'" do + it_behaves_like :string_unpack_64bit_le, 'j<_' + it_behaves_like :string_unpack_64bit_le, 'j_<' + it_behaves_like :string_unpack_64bit_le_signed, 'j<_' + it_behaves_like :string_unpack_64bit_le_signed, 'j_<' + end - describe "with modifier '!'" do - it_behaves_like :string_unpack_32bit_be, 'J!' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J!' - end - end + describe "with modifier '<' and '!'" do + it_behaves_like :string_unpack_64bit_le, 'j<!' + it_behaves_like :string_unpack_64bit_le, 'j!<' + it_behaves_like :string_unpack_64bit_le_signed, 'j<!' + it_behaves_like :string_unpack_64bit_le_signed, 'j!<' + end - describe "String#unpack with format 'j'" do - describe "with modifier '_'" do - it_behaves_like :string_unpack_32bit_be, 'j_' - it_behaves_like :string_unpack_32bit_be_signed, 'j_' - end + describe "with modifier '>' and '_'" do + it_behaves_like :string_unpack_64bit_be, 'j>_' + it_behaves_like :string_unpack_64bit_be, 'j_>' + it_behaves_like :string_unpack_64bit_be_signed, 'j>_' + it_behaves_like :string_unpack_64bit_be_signed, 'j_>' + end - describe "with modifier '!'" do - it_behaves_like :string_unpack_32bit_be, 'j!' - it_behaves_like :string_unpack_32bit_be_signed, 'j!' - end - end + describe "with modifier '>' and '!'" do + it_behaves_like :string_unpack_64bit_be, 'j>!' + it_behaves_like :string_unpack_64bit_be, 'j!>' + it_behaves_like :string_unpack_64bit_be_signed, 'j>!' + it_behaves_like :string_unpack_64bit_be_signed, 'j!>' end + end +end +platform_is pointer_size: 32 do + little_endian do describe "String#unpack with format 'J'" do - describe "with modifier '<'" do - it_behaves_like :string_unpack_32bit_le, 'J<' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J<' + describe "with modifier '_'" do + it_behaves_like :string_unpack_32bit_le, 'J_' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J_' end - describe "with modifier '>'" do - it_behaves_like :string_unpack_32bit_be, 'J>' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J>' + describe "with modifier '!'" do + it_behaves_like :string_unpack_32bit_le, 'J!' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J!' end + end - describe "with modifier '<' and '_'" do - it_behaves_like :string_unpack_32bit_le, 'J<_' - it_behaves_like :string_unpack_32bit_le, 'J_<' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J<_' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J_<' + describe "String#unpack with format 'j'" do + describe "with modifier '_'" do + it_behaves_like :string_unpack_32bit_le, 'j_' + it_behaves_like :string_unpack_32bit_le_signed, 'j_' end - describe "with modifier '<' and '!'" do - it_behaves_like :string_unpack_32bit_le, 'J<!' - it_behaves_like :string_unpack_32bit_le, 'J!<' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J<!' - it_behaves_like :string_unpack_32bit_le_unsigned, 'J!<' + describe "with modifier '!'" do + it_behaves_like :string_unpack_32bit_le, 'j!' + it_behaves_like :string_unpack_32bit_le_signed, 'j!' end + end + end - describe "with modifier '>' and '_'" do - it_behaves_like :string_unpack_32bit_be, 'J>_' - it_behaves_like :string_unpack_32bit_be, 'J_>' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J>_' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J_>' + big_endian do + describe "String#unpack with format 'J'" do + describe "with modifier '_'" do + it_behaves_like :string_unpack_32bit_be, 'J_' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J_' end - describe "with modifier '>' and '!'" do - it_behaves_like :string_unpack_32bit_be, 'J>!' - it_behaves_like :string_unpack_32bit_be, 'J!>' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J>!' - it_behaves_like :string_unpack_32bit_be_unsigned, 'J!>' + describe "with modifier '!'" do + it_behaves_like :string_unpack_32bit_be, 'J!' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J!' end end describe "String#unpack with format 'j'" do - describe "with modifier '<'" do - it_behaves_like :string_unpack_32bit_le, 'j<' - it_behaves_like :string_unpack_32bit_le_signed, 'j<' + describe "with modifier '_'" do + it_behaves_like :string_unpack_32bit_be, 'j_' + it_behaves_like :string_unpack_32bit_be_signed, 'j_' end - describe "with modifier '>'" do - it_behaves_like :string_unpack_32bit_be, 'j>' - it_behaves_like :string_unpack_32bit_be_signed, 'j>' + describe "with modifier '!'" do + it_behaves_like :string_unpack_32bit_be, 'j!' + it_behaves_like :string_unpack_32bit_be_signed, 'j!' end + end + end - describe "with modifier '<' and '_'" do - it_behaves_like :string_unpack_32bit_le, 'j<_' - it_behaves_like :string_unpack_32bit_le, 'j_<' - it_behaves_like :string_unpack_32bit_le_signed, 'j<_' - it_behaves_like :string_unpack_32bit_le_signed, 'j_<' - end + describe "String#unpack with format 'J'" do + describe "with modifier '<'" do + it_behaves_like :string_unpack_32bit_le, 'J<' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J<' + end - describe "with modifier '<' and '!'" do - it_behaves_like :string_unpack_32bit_le, 'j<!' - it_behaves_like :string_unpack_32bit_le, 'j!<' - it_behaves_like :string_unpack_32bit_le_signed, 'j<!' - it_behaves_like :string_unpack_32bit_le_signed, 'j!<' - end + describe "with modifier '>'" do + it_behaves_like :string_unpack_32bit_be, 'J>' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J>' + end - describe "with modifier '>' and '_'" do - it_behaves_like :string_unpack_32bit_be, 'j>_' - it_behaves_like :string_unpack_32bit_be, 'j_>' - it_behaves_like :string_unpack_32bit_be_signed, 'j>_' - it_behaves_like :string_unpack_32bit_be_signed, 'j_>' - end + describe "with modifier '<' and '_'" do + it_behaves_like :string_unpack_32bit_le, 'J<_' + it_behaves_like :string_unpack_32bit_le, 'J_<' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J<_' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J_<' + end - describe "with modifier '>' and '!'" do - it_behaves_like :string_unpack_32bit_be, 'j>!' - it_behaves_like :string_unpack_32bit_be, 'j!>' - it_behaves_like :string_unpack_32bit_be_signed, 'j>!' - it_behaves_like :string_unpack_32bit_be_signed, 'j!>' - end + describe "with modifier '<' and '!'" do + it_behaves_like :string_unpack_32bit_le, 'J<!' + it_behaves_like :string_unpack_32bit_le, 'J!<' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J<!' + it_behaves_like :string_unpack_32bit_le_unsigned, 'J!<' + end + + describe "with modifier '>' and '_'" do + it_behaves_like :string_unpack_32bit_be, 'J>_' + it_behaves_like :string_unpack_32bit_be, 'J_>' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J>_' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J_>' + end + + describe "with modifier '>' and '!'" do + it_behaves_like :string_unpack_32bit_be, 'J>!' + it_behaves_like :string_unpack_32bit_be, 'J!>' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J>!' + it_behaves_like :string_unpack_32bit_be_unsigned, 'J!>' + end + end + + describe "String#unpack with format 'j'" do + describe "with modifier '<'" do + it_behaves_like :string_unpack_32bit_le, 'j<' + it_behaves_like :string_unpack_32bit_le_signed, 'j<' + end + + describe "with modifier '>'" do + it_behaves_like :string_unpack_32bit_be, 'j>' + it_behaves_like :string_unpack_32bit_be_signed, 'j>' + end + + describe "with modifier '<' and '_'" do + it_behaves_like :string_unpack_32bit_le, 'j<_' + it_behaves_like :string_unpack_32bit_le, 'j_<' + it_behaves_like :string_unpack_32bit_le_signed, 'j<_' + it_behaves_like :string_unpack_32bit_le_signed, 'j_<' + end + + describe "with modifier '<' and '!'" do + it_behaves_like :string_unpack_32bit_le, 'j<!' + it_behaves_like :string_unpack_32bit_le, 'j!<' + it_behaves_like :string_unpack_32bit_le_signed, 'j<!' + it_behaves_like :string_unpack_32bit_le_signed, 'j!<' + end + + describe "with modifier '>' and '_'" do + it_behaves_like :string_unpack_32bit_be, 'j>_' + it_behaves_like :string_unpack_32bit_be, 'j_>' + it_behaves_like :string_unpack_32bit_be_signed, 'j>_' + it_behaves_like :string_unpack_32bit_be_signed, 'j_>' + end + + describe "with modifier '>' and '!'" do + it_behaves_like :string_unpack_32bit_be, 'j>!' + it_behaves_like :string_unpack_32bit_be, 'j!>' + it_behaves_like :string_unpack_32bit_be_signed, 'j>!' + it_behaves_like :string_unpack_32bit_be_signed, 'j!>' end end end diff --git a/spec/ruby/core/string/unpack/l_spec.rb b/spec/ruby/core/string/unpack/l_spec.rb index 11f0648fc7..0adb567eca 100644 --- a/spec/ruby/core/string/unpack/l_spec.rb +++ b/spec/ruby/core/string/unpack/l_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' describe "String#unpack with format 'L'" do describe "with modifier '<'" do @@ -14,7 +14,7 @@ describe "String#unpack with format 'L'" do it_behaves_like :string_unpack_32bit_be_unsigned, 'L>' end - guard -> { platform_is wordsize: 32 or platform_is :mingw32 } do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_32bit_le, 'L<_' it_behaves_like :string_unpack_32bit_le, 'L_<' @@ -44,7 +44,7 @@ describe "String#unpack with format 'L'" do end end - guard -> { platform_is wordsize: 64 and platform_is_not :mingw32 } do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_64bit_le, 'L<_' it_behaves_like :string_unpack_64bit_le, 'L_<' @@ -86,7 +86,7 @@ describe "String#unpack with format 'l'" do it_behaves_like :string_unpack_32bit_be_signed, 'l>' end - guard -> { platform_is wordsize: 32 or platform_is :mingw32 } do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_32bit_le, 'l<_' it_behaves_like :string_unpack_32bit_le, 'l_<' @@ -116,7 +116,7 @@ describe "String#unpack with format 'l'" do end end - guard -> { platform_is wordsize: 64 and platform_is_not :mingw32 } do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_64bit_le, 'l<_' it_behaves_like :string_unpack_64bit_le, 'l_<' @@ -160,7 +160,7 @@ little_endian do it_behaves_like :string_unpack_32bit_le_signed, 'l' end - guard -> { platform_is wordsize: 32 or platform_is :mingw32 } do + platform_is c_long_size: 32 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_32bit_le, 'L_' it_behaves_like :string_unpack_32bit_le_unsigned, 'L_' @@ -182,7 +182,7 @@ little_endian do end end - guard -> { platform_is wordsize: 64 and platform_is_not :mingw32 } do + platform_is c_long_size: 64 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_64bit_le, 'L_' it_behaves_like :string_unpack_64bit_le_unsigned, 'L_' @@ -218,7 +218,7 @@ big_endian do it_behaves_like :string_unpack_32bit_be_signed, 'l' end - guard -> { platform_is wordsize: 32 or platform_is :mingw32 } do + platform_is c_long_size: 32 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_32bit_be, 'L_' it_behaves_like :string_unpack_32bit_be_unsigned, 'L_' @@ -240,7 +240,7 @@ big_endian do end end - guard -> { platform_is wordsize: 64 and platform_is_not :mingw32 } do + platform_is c_long_size: 64 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_64bit_be, 'L_' it_behaves_like :string_unpack_64bit_be_unsigned, 'L_' diff --git a/spec/ruby/core/string/unpack/m_spec.rb b/spec/ruby/core/string/unpack/m_spec.rb index 104f282fed..c1c1eea629 100644 --- a/spec/ruby/core/string/unpack/m_spec.rb +++ b/spec/ruby/core/string/unpack/m_spec.rb @@ -1,11 +1,13 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/taint' describe "String#unpack with format 'M'" do it_behaves_like :string_unpack_basic, 'M' it_behaves_like :string_unpack_no_platform, 'M' + it_behaves_like :string_unpack_taint, 'M' it "decodes an empty string" do "".unpack("M").should == [""] @@ -95,11 +97,17 @@ describe "String#unpack with format 'M'" do ["=FF=\n", ["\xff"]] ].should be_computed_by(:unpack, "M") end + + it "unpacks incomplete escape sequences as literal characters" do + "foo=".unpack("M").should == ["foo="] + "foo=4".unpack("M").should == ["foo=4"] + end end describe "String#unpack with format 'm'" do it_behaves_like :string_unpack_basic, 'm' it_behaves_like :string_unpack_no_platform, 'm' + it_behaves_like :string_unpack_taint, 'm' it "decodes an empty string" do "".unpack("m").should == [""] @@ -167,4 +175,18 @@ describe "String#unpack with format 'm'" do "".unpack("m").first.encoding.should == Encoding::BINARY "Ojs8PT4/QA==\n".unpack("m").first.encoding.should == Encoding::BINARY end + + it "does not raise an error for an invalid base64 character" do + "dGV%zdA==".unpack("m").should == ["test"] + end + + describe "when given count 0" do + it "decodes base64" do + "dGVzdA==".unpack("m0").should == ["test"] + end + + it "raises an ArgumentError for an invalid base64 character" do + -> { "dGV%zdA==".unpack("m0") }.should.raise(ArgumentError) + end + end end diff --git a/spec/ruby/core/string/unpack/n_spec.rb b/spec/ruby/core/string/unpack/n_spec.rb index 6e85346338..09173f4fcb 100644 --- a/spec/ruby/core/string/unpack/n_spec.rb +++ b/spec/ruby/core/string/unpack/n_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' describe "String#unpack with format 'N'" do it_behaves_like :string_unpack_basic, 'N' diff --git a/spec/ruby/core/string/unpack/p_spec.rb b/spec/ruby/core/string/unpack/p_spec.rb index 7c9a502a15..4103730269 100644 --- a/spec/ruby/core/string/unpack/p_spec.rb +++ b/spec/ruby/core/string/unpack/p_spec.rb @@ -1,21 +1,44 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/taint' describe "String#unpack with format 'P'" do it_behaves_like :string_unpack_basic, 'P' + it_behaves_like :string_unpack_taint, 'P' - it "returns a random object after consuming a size-of a machine word bytes" do - str = "\0" * 1.size - str.unpack("P").should be_kind_of(Object) + it "round-trips a string through pack and unpack" do + ["hello"].pack("P").unpack("P5").should == ["hello"] + end + + it "cannot unpack a string except from the same object that created it, or a duplicate of it" do + packed = ["hello"].pack("P") + packed.unpack("P5").should == ["hello"] + packed.dup.unpack("P5").should == ["hello"] + -> { packed.to_sym.to_s.unpack("P5") }.should.raise(ArgumentError, /no associated pointer/) + end + + it "reads as many characters as specified" do + ["hello"].pack("P").unpack("P1").should == ["h"] + end + + it "reads only as far as a NUL character" do + ["hello"].pack("P").unpack("P10").should == ["hello"] end end describe "String#unpack with format 'p'" do it_behaves_like :string_unpack_basic, 'p' + it_behaves_like :string_unpack_taint, 'p' + + it "round-trips a string through pack and unpack" do + ["hello"].pack("p").unpack("p").should == ["hello"] + end - it "returns a random object after consuming a size-of a machine word bytes" do - str = "\0" * 1.size - str.unpack("p").should be_kind_of(Object) + it "cannot unpack a string except from the same object that created it, or a duplicate of it" do + packed = ["hello"].pack("p") + packed.unpack("p").should == ["hello"] + packed.dup.unpack("p").should == ["hello"] + -> { packed.to_sym.to_s.unpack("p") }.should.raise(ArgumentError, /no associated pointer/) end end diff --git a/spec/ruby/core/string/unpack/percent_spec.rb b/spec/ruby/core/string/unpack/percent_spec.rb index 38cf81b037..7142bbf241 100644 --- a/spec/ruby/core/string/unpack/percent_spec.rb +++ b/spec/ruby/core/string/unpack/percent_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) +require_relative '../../../spec_helper' describe "String#unpack with format '%'" do it "raises an Argument Error" do - lambda { "abc".unpack("%") }.should raise_error(ArgumentError) + -> { "abc".unpack("%") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/q_spec.rb b/spec/ruby/core/string/unpack/q_spec.rb index 91e65a9405..2f667d6c4d 100644 --- a/spec/ruby/core/string/unpack/q_spec.rb +++ b/spec/ruby/core/string/unpack/q_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' describe "String#unpack with format 'Q'" do describe "with modifier '<'" do diff --git a/spec/ruby/core/string/unpack/r_spec.rb b/spec/ruby/core/string/unpack/r_spec.rb new file mode 100644 index 0000000000..a385951aa8 --- /dev/null +++ b/spec/ruby/core/string/unpack/r_spec.rb @@ -0,0 +1,85 @@ +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' + +ruby_version_is "4.1" do + describe "String#unpack with format 'R'" do + it_behaves_like :string_unpack_basic, 'R' + it_behaves_like :string_unpack_no_platform, 'R' + + it "decodes a ULEB128 integer" do + [ ["\x00", [0]], + ["\x01", [1]], + ["\x7f", [127]], + ["\x80\x01", [128]], + ["\xff\x7f", [0x3fff]], + ["\x80\x80\x01", [0x4000]], + ["\xff\xff\xff\xff\x0f", [0xffffffff]], + ["\x80\x80\x80\x80\x10", [0x100000000]], + ["\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", [0xffff_ffff_ffff_ffff]], + ].should be_computed_by(:unpack, "R") + end + + it "decodes multiple values with '*' modifier" do + "\x01\x02".unpack("R*").should == [1, 2] + "\x7f\x80\x01".unpack("R*").should == [127, 128] + end + + it "returns nil for incomplete data" do + "\xFF".unpack("R").should == [nil] + "\xFF".unpack1("R").should == nil + end + + it "returns nil for remaining incomplete values after a valid one" do + bytes = [256].pack("R") + (bytes + "\xFF").unpack("RRRR").should == [256, nil, nil, nil] + end + + it "skips incomplete values with '*' modifier" do + "\xFF".unpack("R*").should == [] + end + end + + describe "String#unpack with format 'r'" do + it_behaves_like :string_unpack_basic, 'r' + it_behaves_like :string_unpack_no_platform, 'r' + + it "decodes a SLEB128 integer" do + [ ["\x00", [0]], + ["\x01", [1]], + ["\x7f", [-1]], + ["\x7e", [-2]], + ["\xff\x00", [127]], + ["\x80\x01", [128]], + ["\x81\x7f", [-127]], + ["\x80\x7f", [-128]], + ].should be_computed_by(:unpack, "r") + end + + it "decodes larger numbers" do + "\xff\xff\x00".unpack("r").should == [0x3fff] + "\x80\x80\x01".unpack("r").should == [0x4000] + "\x81\x80\x7f".unpack("r").should == [-0x3fff] + "\x80\x80\x7f".unpack("r").should == [-0x4000] + end + + it "decodes multiple values with '*' modifier" do + "\x00\x01\x7f".unpack("r*").should == [0, 1, -1] + end + + it "returns nil for incomplete data" do + "\xFF".unpack("r").should == [nil] + "\xFF".unpack1("r").should == nil + end + + it "returns nil for remaining incomplete values after a valid one" do + bytes = [256].pack("r") + (bytes + "\xFF").unpack("rrrr").should == [256, nil, nil, nil] + end + + it "skips incomplete values with '*' modifier" do + "\xFF".unpack("r*").should == [] + end + end +end diff --git a/spec/ruby/core/string/unpack/s_spec.rb b/spec/ruby/core/string/unpack/s_spec.rb index c6b079b0a6..d331fd720e 100644 --- a/spec/ruby/core/string/unpack/s_spec.rb +++ b/spec/ruby/core/string/unpack/s_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' describe "String#unpack with format 'S'" do describe "with modifier '<'" do diff --git a/spec/ruby/core/string/unpack/shared/basic.rb b/spec/ruby/core/string/unpack/shared/basic.rb index 0ecbf615af..2ee2d6899a 100644 --- a/spec/ruby/core/string/unpack/shared/basic.rb +++ b/spec/ruby/core/string/unpack/shared/basic.rb @@ -1,29 +1,27 @@ describe :string_unpack_basic, shared: true do it "ignores whitespace in the format string" do - "abc".unpack("a \t\n\v\f\r"+unpack_format).should be_an_instance_of(Array) + "abc".unpack("a \t\n\v\f\r"+unpack_format).should.instance_of?(Array) end it "calls #to_str to coerce the directives string" do d = mock("unpack directive") d.should_receive(:to_str).and_return("a"+unpack_format) - "abc".unpack(d).should be_an_instance_of(Array) + "abc".unpack(d).should.instance_of?(Array) end - it "raises a TypeError when passed nil" do - lambda { "abc".unpack(nil) }.should raise_error(TypeError) - end - - it "raises a TypeError when passed an Integer" do - lambda { "abc".unpack(1) }.should raise_error(TypeError) + it "raises ArgumentError when a directive is unknown" do + -> { "abcdefgh".unpack("a K" + unpack_format) }.should.raise(ArgumentError, "unknown unpack directive 'K' in 'a K#{unpack_format}'") + -> { "abcdefgh".unpack("a 0" + unpack_format) }.should.raise(ArgumentError, "unknown unpack directive '0' in 'a 0#{unpack_format}'") + -> { "abcdefgh".unpack("a :" + unpack_format) }.should.raise(ArgumentError, "unknown unpack directive ':' in 'a :#{unpack_format}'") end end describe :string_unpack_no_platform, shared: true do it "raises an ArgumentError when the format modifier is '_'" do - lambda { "abcdefgh".unpack(unpack_format("_")) }.should raise_error(ArgumentError) + -> { "abcdefgh".unpack(unpack_format("_")) }.should.raise(ArgumentError) end it "raises an ArgumentError when the format modifier is '!'" do - lambda { "abcdefgh".unpack(unpack_format("!")) }.should raise_error(ArgumentError) + -> { "abcdefgh".unpack(unpack_format("!")) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/shared/float.rb b/spec/ruby/core/string/unpack/shared/float.rb index 208dc357af..5525c3fe73 100644 --- a/spec/ruby/core/string/unpack/shared/float.rb +++ b/spec/ruby/core/string/unpack/shared/float.rb @@ -1,4 +1,4 @@ -# -*- encoding: ascii-8bit -*- +# encoding: binary describe :string_unpack_float_le, shared: true do it "decodes one float for a single format character" do @@ -53,12 +53,13 @@ describe :string_unpack_float_le, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - it "ignores NULL bytes between directives" do - array = "\x9a\x999@33\xb3?".unpack(unpack_format("\000", 2)) - array.should == [2.9000000953674316, 1.399999976158142] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x9a\x999@33\xb3?".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -120,12 +121,13 @@ describe :string_unpack_float_be, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - it "ignores NULL bytes between directives" do - array = "@9\x99\x9a?\xb333".unpack(unpack_format("\000", 2)) - array.should == [2.9000000953674316, 1.399999976158142] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "@9\x99\x9a?\xb333".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -190,11 +192,13 @@ describe :string_unpack_double_le, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - it "ignores NULL bytes between directives" do - "333333\x07@ffffff\xf6?".unpack(unpack_format("\000", 2)).should == [2.9, 1.4] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "333333\x07@ffffff\xf6?".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -258,11 +262,13 @@ describe :string_unpack_double_be, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - it "ignores NULL bytes between directives" do - "@\x07333333?\xf6ffffff".unpack(unpack_format("\000", 2)).should == [2.9, 1.4] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "@\x07333333?\xf6ffffff".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/shared/integer.rb b/spec/ruby/core/string/unpack/shared/integer.rb index 03dfb5c682..c66156536b 100644 --- a/spec/ruby/core/string/unpack/shared/integer.rb +++ b/spec/ruby/core/string/unpack/shared/integer.rb @@ -1,4 +1,4 @@ -# -*- encoding: ascii-8bit -*- +# encoding: binary describe :string_unpack_16bit_le, shared: true do it "decodes one short for a single format character" do @@ -32,8 +32,10 @@ describe :string_unpack_16bit_le, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - it "ignores NULL bytes between directives" do - "abcd".unpack(unpack_format("\000", 2)).should == [25185, 25699] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "abcd".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -85,8 +87,10 @@ describe :string_unpack_16bit_be, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - it "ignores NULL bytes between directives" do - "badc".unpack(unpack_format("\000", 2)).should == [25185, 25699] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "badc".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -139,8 +143,10 @@ describe :string_unpack_32bit_le, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - it "ignores NULL bytes between directives" do - "abcdefgh".unpack(unpack_format("\000", 2)).should == [1684234849, 1751606885] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "abcdefgh".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -193,8 +199,10 @@ describe :string_unpack_32bit_be, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - it "ignores NULL bytes between directives" do - "dcbahgfe".unpack(unpack_format("\000", 2)).should == [1684234849, 1751606885] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "dcbahgfe".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -243,9 +251,10 @@ describe :string_unpack_64bit_le, shared: true do "abc".unpack(unpack_format('*')).should == [] end - it "ignores NULL bytes between directives" do - array = "abcdefghabghefcd".unpack(unpack_format("\000", 2)) - array.should == [7523094288207667809, 7233738012216484449] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "badc".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -305,9 +314,10 @@ describe :string_unpack_64bit_be, shared: true do "abc".unpack(unpack_format('*')).should == [] end - it "ignores NULL bytes between directives" do - array = "hgfedcbadcfehgba".unpack(unpack_format("\000", 2)) - array.should == [7523094288207667809, 7233738012216484449] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "hgfedcbadcfehgba".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/shared/taint.rb b/spec/ruby/core/string/unpack/shared/taint.rb new file mode 100644 index 0000000000..79c7251f01 --- /dev/null +++ b/spec/ruby/core/string/unpack/shared/taint.rb @@ -0,0 +1,2 @@ +describe :string_unpack_taint, shared: true do +end diff --git a/spec/ruby/core/string/unpack/shared/unicode.rb b/spec/ruby/core/string/unpack/shared/unicode.rb index a2b4e142b2..9b4e0c09de 100644 --- a/spec/ruby/core/string/unpack/shared/unicode.rb +++ b/spec/ruby/core/string/unpack/shared/unicode.rb @@ -50,8 +50,10 @@ describe :string_unpack_unicode, shared: true do "\xc2\x80".unpack("UUUU").should == [0x80] end - it "ignores NULL bytes between directives" do - "\x01\x02".unpack("U\x00U").should == [1, 2] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x02".unpack("U\x00U") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/u_spec.rb b/spec/ruby/core/string/unpack/u_spec.rb index 0765da8d96..720c1b8583 100644 --- a/spec/ruby/core/string/unpack/u_spec.rb +++ b/spec/ruby/core/string/unpack/u_spec.rb @@ -1,26 +1,29 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/unicode', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/unicode' +require_relative 'shared/taint' describe "String#unpack with format 'U'" do it_behaves_like :string_unpack_basic, 'U' it_behaves_like :string_unpack_no_platform, 'U' it_behaves_like :string_unpack_unicode, 'U' + it_behaves_like :string_unpack_taint, 'U' it "raises ArgumentError on a malformed byte sequence" do - lambda { "\xE3".unpack('U') }.should raise_error(ArgumentError) + -> { "\xE3".unpack('U') }.should.raise(ArgumentError) end it "raises ArgumentError on a malformed byte sequence and doesn't continue when used with the * modifier" do - lambda { "\xE3".unpack('U*') }.should raise_error(ArgumentError) + -> { "\xE3".unpack('U*') }.should.raise(ArgumentError) end end describe "String#unpack with format 'u'" do it_behaves_like :string_unpack_basic, 'u' it_behaves_like :string_unpack_no_platform, 'u' + it_behaves_like :string_unpack_taint, 'u' it "decodes an empty string as an empty string" do "".unpack("u").should == [""] @@ -28,10 +31,10 @@ describe "String#unpack with format 'u'" do it "decodes into raw (ascii) string values" do str = "".unpack("u")[0] - str.encoding.name.should == 'ASCII-8BIT' + str.encoding.should == Encoding::BINARY - str = "1".force_encoding('UTF-8').unpack("u")[0] - str.encoding.name.should == 'ASCII-8BIT' + str = "1".dup.force_encoding('UTF-8').unpack("u")[0] + str.encoding.should == Encoding::BINARY end it "decodes the complete string ignoring newlines when given a single directive" do diff --git a/spec/ruby/core/string/unpack/v_spec.rb b/spec/ruby/core/string/unpack/v_spec.rb index 33cf23c68b..929e8712cb 100644 --- a/spec/ruby/core/string/unpack/v_spec.rb +++ b/spec/ruby/core/string/unpack/v_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/integer', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/integer' describe "String#unpack with format 'V'" do it_behaves_like :string_unpack_basic, 'V' diff --git a/spec/ruby/core/string/unpack/w_spec.rb b/spec/ruby/core/string/unpack/w_spec.rb index 22f5980a46..cc9aecac9c 100644 --- a/spec/ruby/core/string/unpack/w_spec.rb +++ b/spec/ruby/core/string/unpack/w_spec.rb @@ -1,7 +1,7 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' describe "String#unpack with directive 'w'" do it_behaves_like :string_unpack_basic, 'w' @@ -15,11 +15,23 @@ describe "String#unpack with directive 'w'" do ].should be_computed_by(:unpack, "w") end - it "ignores NULL bytes between directives" do - "\x01\x02\x03".unpack("w\x00w").should == [1, 2] + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x02\x03".unpack("w\x00w") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do "\x01\x02\x03".unpack("w w").should == [1, 2] end end + +describe "String#unpack with directive 'w*'" do + + it "decodes BER-compressed integers" do + "\x01\x02\x03\x04".unpack("w*").should == [1, 2, 3, 4] + "\x00\xCE\x0F\x84\x80\x80\x80\x80\x80\x80\x80\x80\x00\x01\x00".unpack("w*").should == [0, 9999, 2**65, 1, 0] + "\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00\x90\x80\x80\x80\x80\x80\x80\x80\x03\x01\x02".unpack("w*").should == [2**63, (2**60 + 3), 1, 2] + end + +end diff --git a/spec/ruby/core/string/unpack/x_spec.rb b/spec/ruby/core/string/unpack/x_spec.rb index e765472413..fb2e79fc1f 100644 --- a/spec/ruby/core/string/unpack/x_spec.rb +++ b/spec/ruby/core/string/unpack/x_spec.rb @@ -1,7 +1,7 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' describe "String#unpack with format 'X'" do it_behaves_like :string_unpack_basic, 'X' @@ -24,11 +24,11 @@ describe "String#unpack with format 'X'" do end it "raises an ArgumentError when passed the '*' modifier if the remaining bytes exceed the bytes from the index to the start of the String" do - lambda { "abcd".unpack("CX*C") }.should raise_error(ArgumentError) + -> { "abcd".unpack("CX*C") }.should.raise(ArgumentError) end it "raises an ArgumentError if the count exceeds the bytes from current index to the start of the String" do - lambda { "\x01\x02\x03\x04".unpack("C3X4C") }.should raise_error(ArgumentError) + -> { "\x01\x02\x03\x04".unpack("C3X4C") }.should.raise(ArgumentError) end end @@ -57,6 +57,6 @@ describe "String#unpack with format 'x'" do end it "raises an ArgumentError if the count exceeds the size of the String" do - lambda { "\x01\x02\x03\x04".unpack("C2x3C") }.should raise_error(ArgumentError) + -> { "\x01\x02\x03\x04".unpack("C2x3C") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/z_spec.rb b/spec/ruby/core/string/unpack/z_spec.rb index 7c3d167ac2..1030390550 100644 --- a/spec/ruby/core/string/unpack/z_spec.rb +++ b/spec/ruby/core/string/unpack/z_spec.rb @@ -1,13 +1,15 @@ -# -*- encoding: ascii-8bit -*- -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) -require File.expand_path('../shared/basic', __FILE__) -require File.expand_path('../shared/string', __FILE__) +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' +require_relative 'shared/string' +require_relative 'shared/taint' describe "String#unpack with format 'Z'" do it_behaves_like :string_unpack_basic, 'Z' it_behaves_like :string_unpack_no_platform, 'Z' it_behaves_like :string_unpack_string, 'Z' + it_behaves_like :string_unpack_taint, 'Z' it "stops decoding at NULL bytes when passed the '*' modifier" do "a\x00\x00 b \x00c".unpack('Z*Z*Z*Z*').should == ["a", "", " b ", "c"] @@ -18,4 +20,9 @@ describe "String#unpack with format 'Z'" do ["\x00a\x00 bc \x00", ["", "c"]] ].should be_computed_by(:unpack, "Z5Z") end + + it "does not advance past the null byte when given a 'Z' format specifier" do + "a\x00\x0f".unpack('Zxc').should == ['a', 15] + "a\x00\x0f".unpack('Zcc').should == ['a', 0, 15] + end end |
