Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/rex/image_source/disk_spec.rb
Views: 11783
# -*- coding:binary -*-1require 'spec_helper'23require 'rex/image_source/disk'45RSpec.describe Rex::ImageSource::Disk do67let(:path) do8File.join(Msf::Config.data_directory, "templates", "template_x86_windows_old.exe")9end1011let(:file) do12File.new(path)13end1415subject do16described_class.new(file)17end1819it_should_behave_like 'Rex::ImageSource::ImageSource'2021describe "#initialize" do22subject(:disk_class) do23described_class.allocate24end2526context "when _len not sent as argument" do27let(:_file) { file }2829it "initializes size to file length" do30disk_class.send(:initialize, file)31expect(disk_class.size).to eq(4608)32end33end3435context "when _offset not sent as argument" do36let(:_file) { file }37it "initializes file_offset to 0" do38disk_class.send(:initialize, file)39expect(disk_class.file_offset).to eq(0)40end41end42end4344describe "#read" do45context "when offset less than 0" do46let(:offset) { -1 }47let(:len) { 20 }4849it "raises a RangeError" do50expect { subject.read(offset, len) }.to raise_error(RangeError)51end52end5354context "offset plus len greater than size" do55let(:offset) { 0 }56let(:len) { 16000 }5758it "raises a RangeError" do59expect { subject.read(offset, len) }.to raise_error(RangeError)60end61end6263context "when offset and len inside range" do64let(:offset) { 0 }65let(:len) { 2 }6667it "returns file contents" do68expect(subject.read(offset, len)). to eq('MZ')69end70end7172context "instance with tampered size" do73let(:tampered_size) { 6000 }7475subject(:tampered) do76described_class.new(file, 0, tampered_size)77end7879context "when reading offset after the real file length" do80let(:offset) { 5000 }81let(:len) { 2 }82it "returns nil" do83expect(tampered.read(offset, len)).to be_nil84end85end86end87end8889describe "#index" do90let(:search) { 'MZ' }9192it "returns index of first search occurrence" do93expect(subject.index(search)).to eq(0)94end9596context "when offset out of range" do97it "returns nil" do98expect(subject.index(search, 6000)).to be_nil99end100end101102context "when search string not found" do103it "returns nil" do104expect(subject.index(search, 4600)).to be_nil105end106end107108context "instance with tampered size" do109let(:tampered_size) { 6000 }110111subject(:tampered) do112described_class.new(file, 0, tampered_size)113end114115context "when searching offset after the real file length" do116let(:offset) { 5000 }117it "raises NoMethodError" do118expect{ tampered.index(search, offset) }.to raise_error(NoMethodError)119end120end121end122end123124describe "#subsource" do125let(:offset) { 2 }126let(:len) { 512 }127128it "returns a new Rex::ImageSource::Disk" do129expect(subject.subsource(offset, len)).to be_kind_of(described_class)130end131132it "returns a new Rex::ImageSource::Disk with same file" do133expect(subject.subsource(offset, len).file).to eq(subject.file)134end135136it "returns a new Rex::ImageSource::Disk with provided size" do137expect(subject.subsource(offset, len).size).to eq(len)138end139140it "returns a new Rex::ImageSource::Disk with file_offset added to the original" do141expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)142end143end144145describe "#close" do146it "returns nil" do147expect(subject.close).to be_nil148end149150it "closes the associated file" do151expect(subject.file.closed?).to be_falsey152subject.close153expect(subject.file.closed?).to be_truthy154end155end156end157158159