Files
metasploit-gs/spec/lib/rex/java/serialization/model/utf_spec.rb
T

107 lines
2.8 KiB
Ruby
Raw Normal View History

2014-12-09 19:39:11 -06:00
# -*- coding:binary -*-
require 'spec_helper'
2014-12-01 19:07:45 -06:00
require 'rex/java'
require 'stringio'
describe Rex::Java::Serialization::Model::Utf do
subject(:utf) do
described_class.new
end
let(:sample_utf) { "\x00\x10java.lang.Number" }
let(:sample_utf_io) { StringIO.new(sample_utf) }
let(:empty_utf) { "\x00\x00" }
let(:empty_utf_io) { StringIO.new(empty_utf) }
let(:incomplete_utf) { "\x00\x10java.lang.Numb" }
let(:incomplete_utf_io) { StringIO.new(incomplete_utf) }
let(:empty_io) { StringIO.new('') }
describe ".new" do
it "Rex::Java::Serialization::Model::Utf" do
expect(utf).to be_a(Rex::Java::Serialization::Model::Utf)
end
it "initializes length to 0" do
expect(utf.length).to eq(0)
end
it "initializes contents with empty string" do
expect(utf.contents).to be_empty
end
end
describe "#encode" do
context "when empty utf" do
it { expect(utf.encode).to eq(empty_utf) }
end
context "when filled utf" do
it do
utf.length = 16
utf.contents = 'java.lang.Number'
expect(utf.encode).to eq(sample_utf)
end
end
end
describe "#decode" do
context "when stream contains empty string" do
2015-04-05 18:43:03 -05:00
it "raises Rex::Java::Serialization::DecodeError" do
expect { utf.decode(empty_io) }.to raise_error(Rex::Java::Serialization::DecodeError)
2014-12-01 19:07:45 -06:00
end
end
context "when stream contains empty utf" do
it "returns a Rex::Java::Serialization::Model::Utf" do
expect(utf.decode(empty_utf_io)).to be_a(Rex::Java::Serialization::Model::Utf)
end
it "sets length to 0" do
utf.decode(empty_utf_io)
expect(utf.length).to eq(0)
end
it "sets contents to empty string" do
utf.decode(empty_utf_io)
expect(utf.contents).to be_empty
end
end
context "when stream contains incomplete utf" do
2015-04-05 18:43:03 -05:00
it "raises Rex::Java::Serialization::DecodeError" do
expect { utf.decode(incomplete_utf_io) }.to raise_error(Rex::Java::Serialization::DecodeError)
2014-12-01 19:07:45 -06:00
end
end
context "when stream contains correct utf" do
it "returns a Rex::Java::Serialization::Model::Utf" do
expect(utf.decode(sample_utf_io)).to be_a(Rex::Java::Serialization::Model::Utf)
end
it "sets length to 0" do
utf.decode(sample_utf_io)
expect(utf.length).to eq(16)
end
it "sets contents to sample string" do
utf.decode(sample_utf_io)
expect(utf.contents).to eq('java.lang.Number')
end
end
end
2014-12-07 17:52:09 -06:00
describe "#to_s" do
it "prints an stream containing a sample utf" do
utf.decode(sample_utf_io)
expect(utf.to_s).to eq('java.lang.Number')
end
it "prints an stream containing an empty utf" do
utf.decode(empty_utf_io)
expect(utf.to_s).to eq('')
end
end
2014-12-01 19:07:45 -06:00
end