Add specs for Disk#read and Disk#index
parent
46e4235e79
commit
3f1eea55a6
|
@ -13,23 +13,111 @@ describe Rex::ImageSource::Disk do
|
|||
File.new(path)
|
||||
end
|
||||
|
||||
subject do
|
||||
described_class.new(file)
|
||||
end
|
||||
|
||||
describe "#initialize" do
|
||||
subject(:disk_class) do
|
||||
described_class.allocate
|
||||
end
|
||||
|
||||
context "when _len not send as argument" do
|
||||
let(:_file) { file }
|
||||
|
||||
it "initializes size to file length" do
|
||||
disk_class.send(:initialize, file)
|
||||
expect(disk_class.size).to eq(4608)
|
||||
end
|
||||
end
|
||||
|
||||
context "when _offset not send argument" do
|
||||
let(:_file) { file }
|
||||
it "initializes file_offset to 0" do
|
||||
disk_class.send(:initialize, file)
|
||||
expect(disk_class.file_offset).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#read" do
|
||||
context "offset minor than 0" do
|
||||
context "when offset minor than 0" do
|
||||
let(:offset) { -1 }
|
||||
let(:len) { 20 }
|
||||
|
||||
it "raises a RangeError" do
|
||||
expect { subject.read(offset, len) }.to raise_error(RangeError)
|
||||
end
|
||||
end
|
||||
|
||||
context "when offset plus len major than size" do
|
||||
let(:offset) { 0 }
|
||||
let(:len) { 16000 }
|
||||
|
||||
it "raises a RangeError" do
|
||||
expect { subject.read(offset, len) }.to raise_error(RangeError)
|
||||
end
|
||||
end
|
||||
|
||||
context "when offset and len inside range" do
|
||||
let(:offset) { 0 }
|
||||
let(:len) { 2 }
|
||||
|
||||
it "returns file contents" do
|
||||
expect(subject.read(offset, len)). to eq('MZ')
|
||||
end
|
||||
end
|
||||
|
||||
context "instance with tampered size" do
|
||||
let(:tampered_size) { 6000 }
|
||||
|
||||
subject(:tampered) do
|
||||
described_class.new(file, 0, tampered_size)
|
||||
end
|
||||
|
||||
context "when reading offset after the real file length" do
|
||||
let(:offset) { 5000 }
|
||||
let(:len) { 2 }
|
||||
it "returns nil" do
|
||||
expect(tampered.read(offset, len)).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#index" do
|
||||
let(:search) { 'MZ' }
|
||||
|
||||
it "returns index of first search occurrence" do
|
||||
expect(subject.index(search)).to eq(0)
|
||||
end
|
||||
|
||||
context "when offset out of range" do
|
||||
it "returns nil" do
|
||||
expect(subject.index(search, 6000)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when search string not found" do
|
||||
it "returns nil" do
|
||||
expect(subject.index(search, 4600)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "instance with tampered size" do
|
||||
let(:tampered_size) { 6000 }
|
||||
|
||||
subject(:tampered) do
|
||||
described_class.new(file, 0, tampered_size)
|
||||
end
|
||||
|
||||
context "when searching offset after the real file length" do
|
||||
let(:offset) { 5000 }
|
||||
it "raises NoMethodError" do
|
||||
expect{ tampered.index(search, offset) }.to raise_error(NoMethodError)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#subsource" do
|
||||
|
@ -39,5 +127,4 @@ describe Rex::ImageSource::Disk do
|
|||
describe "#close" do
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue