Skip to content

Commit b121764

Browse files
committed
Add specs for Memory#subsource
1 parent 3500e1c commit b121764

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

spec/lib/rex/image_source/memory_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,54 @@
9292
end
9393

9494
describe "#subsource" do
95+
let(:offset) { 2 }
96+
let(:len) { 10 }
9597

98+
it "returns a new Rex::ImageSource::Memory" do
99+
expect(subject.subsource(offset, len)).to be_kind_of(described_class)
100+
end
101+
102+
it "returns a new Rex::ImageSource::Memory with provided size" do
103+
expect(subject.subsource(offset, len).size).to eq(len)
104+
end
105+
106+
it "returns a new Rex::ImageSource::Memory with file_offset added to the original" do
107+
expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)
108+
end
109+
110+
it "returns a new Rex::ImageSource::Memory with rawdata from the original" do
111+
expect(subject.subsource(offset, len).rawdata).to eq(subject.rawdata[offset, len])
112+
end
113+
114+
context "when offset is out of range" do
115+
let(:offset) { 20 }
116+
let(:len) { 2 }
117+
118+
it "raises an error" do
119+
expect { subject.subsource(offset, len) }.to raise_error(NoMethodError)
120+
end
121+
end
122+
123+
context "when len is bigger than source rawdata" do
124+
let(:offset) { 2 }
125+
let(:len) { 20 }
126+
127+
it "returns a new Rex::ImageSource::Memory" do
128+
expect(subject.subsource(offset, len)).to be_kind_of(described_class)
129+
end
130+
131+
it "returns a new Rex::ImageSource::Memory with provided size truncated" do
132+
expect(subject.subsource(offset, len).size).to eq(14)
133+
end
134+
135+
it "returns a new Rex::ImageSource::Memory with file_offset added to the original" do
136+
expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)
137+
end
138+
139+
it "returns a new Rex::ImageSource::Memory with rawdata truncated" do
140+
expect(subject.subsource(offset, len).rawdata).to eq('CDEFGHIJKLMNOP')
141+
end
142+
end
96143
end
97144

98145
describe "#close" do

0 commit comments

Comments
 (0)