# encoding: US-ASCII require 'spec_helper' require 'ronin/formatting/extensions/binary/string' require 'ostruct' describe String do subject { "hello" } it "should provide String#unpack_original" do expect(subject).to respond_to(:unpack_original) end it "should provide String#unpack" do expect(subject).to respond_to(:unpack) end it "should provide String#depack" do expect(subject).to respond_to(:depack) end it "should provide String#zlib_inflate" do expect(subject).to respond_to(:zlib_inflate) end it "should provide String#zlib_deflate" do expect(subject).to respond_to(:zlib_deflate) end it "should provide String#hex_unescape" do expect(subject).to respond_to(:hex_unescape) end it "should provide String#xor" do expect(subject).to respond_to(:xor) end it "should provide String#unhexdump" do expect(subject).to respond_to(:unhexdump) end describe "#unpack" do subject { "\x34\x12\x00\x00hello\0" } let(:data) { [0x1234, "hello"] } context "when given only a String" do it "should unpack Strings using String#unpack template Strings" do expect(subject.unpack('VZ*')).to eq(data) end end context "otherwise" do it "should unpack Strings using Binary::Template" do expect(subject.unpack(:uint32_le, :string)).to eq(data) end end end context "deprecated" do describe "#depack" do subject { 0x1337 } let(:i386) do OpenStruct.new(:endian => :little, :address_length => 4) end let(:ppc) do OpenStruct.new(:endian => :big, :address_length => 4) end let(:i386_packed_int) { "7\023\000\000" } let(:i386_packed_short) { "7\023" } let(:i386_packed_long) { "7\023\000\000" } let(:i386_packed_quad) { "7\023\000\000\000\000\000\000" } let(:ppc_packed_int) { "\000\000\0237" } let(:ppc_packed_short) { "\0237" } let(:ppc_packed_long) { "\000\000\0237" } let(:ppc_packed_quad) { "\000\000\000\000\000\000\0237" } it "should depack itself for a little-endian architecture" do expect(i386_packed_int.depack(i386)).to eq(subject) end it "should depack itself as a short for a little-endian architecture" do expect(i386_packed_short.depack(i386,2)).to eq(subject) end it "should depack itself as a long for a little-endian architecture" do expect(i386_packed_long.depack(i386,4)).to eq(subject) end it "should depack itself as a quad for a little-endian architecture" do expect(i386_packed_quad.depack(i386,8)).to eq(subject) end it "should depack itself for a big-endian architecture" do expect(ppc_packed_int.depack(ppc)).to eq(subject) end it "should depack itself as a short for a big-endian architecture" do expect(ppc_packed_short.depack(ppc,2)).to eq(subject) end it "should depack itself as a long for a big-endian architecture" do expect(ppc_packed_long.depack(ppc,4)).to eq(subject) end it "should depack itself as a quad for a big-endian architecture" do expect(ppc_packed_quad.depack(ppc,8)).to eq(subject) end it "should accept String#unpack template strings" do expect(i386_packed_long.depack('V')).to eq([subject]) end end end describe "#base64_encode" do subject { "hello\0" } it "should base64 encode a String" do expect(subject.base64_encode).to eq("aGVsbG8A\n") end end describe "#base64_decode" do subject { "aGVsbG8A\n" } it "should base64 decode a String" do expect(subject.base64_decode).to eq("hello\0") end end describe "#zlib_inflate" do subject do "x\xda3H\xb3H3MM6\xd354II\xd651K5\xd7M43N\xd4M\xb3\xb0L2O14423Mb\0\0\xc02\t\xae" end it "should inflate a zlib deflated String" do expect(subject.zlib_inflate).to eq("0f8f5ec6-14dc-46e7-a63a-f89b7d11265b\0") end end describe "#zlib_deflate" do subject { "hello" } it "should zlib deflate a String" do expect(subject.zlib_deflate).to eq("x\x9c\xcbH\xcd\xc9\xc9\a\0\x06,\x02\x15") end end describe "#hex_escape" do subject { "hello\x4e" } it "should hex escape a String" do expect(subject.hex_escape).to eq("\\x68\\x65\\x6c\\x6c\\x6f\\x4e") end end describe "#xor" do subject { 'hello' } let(:key) { 0x50 } let(:keys) { [0x50, 0x55] } it "should not contain the key used in the xor" do expect(subject).to_not include(key.chr) end it "should not equal the original string" do expect(subject.xor(key)).to_not be == subject end it "should be able to be decoded with another xor" do expect(subject.xor(key).xor(key)).to eq(subject) end it "should allow xoring against a single key" do expect(subject.xor(key)).to eq("85<