CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/image_source/disk_spec.rb
Views: 11783
1
# -*- coding:binary -*-
2
require 'spec_helper'
3
4
require 'rex/image_source/disk'
5
6
RSpec.describe Rex::ImageSource::Disk do
7
8
let(:path) do
9
File.join(Msf::Config.data_directory, "templates", "template_x86_windows_old.exe")
10
end
11
12
let(:file) do
13
File.new(path)
14
end
15
16
subject do
17
described_class.new(file)
18
end
19
20
it_should_behave_like 'Rex::ImageSource::ImageSource'
21
22
describe "#initialize" do
23
subject(:disk_class) do
24
described_class.allocate
25
end
26
27
context "when _len not sent as argument" do
28
let(:_file) { file }
29
30
it "initializes size to file length" do
31
disk_class.send(:initialize, file)
32
expect(disk_class.size).to eq(4608)
33
end
34
end
35
36
context "when _offset not sent as argument" do
37
let(:_file) { file }
38
it "initializes file_offset to 0" do
39
disk_class.send(:initialize, file)
40
expect(disk_class.file_offset).to eq(0)
41
end
42
end
43
end
44
45
describe "#read" do
46
context "when offset less than 0" do
47
let(:offset) { -1 }
48
let(:len) { 20 }
49
50
it "raises a RangeError" do
51
expect { subject.read(offset, len) }.to raise_error(RangeError)
52
end
53
end
54
55
context "offset plus len greater than size" do
56
let(:offset) { 0 }
57
let(:len) { 16000 }
58
59
it "raises a RangeError" do
60
expect { subject.read(offset, len) }.to raise_error(RangeError)
61
end
62
end
63
64
context "when offset and len inside range" do
65
let(:offset) { 0 }
66
let(:len) { 2 }
67
68
it "returns file contents" do
69
expect(subject.read(offset, len)). to eq('MZ')
70
end
71
end
72
73
context "instance with tampered size" do
74
let(:tampered_size) { 6000 }
75
76
subject(:tampered) do
77
described_class.new(file, 0, tampered_size)
78
end
79
80
context "when reading offset after the real file length" do
81
let(:offset) { 5000 }
82
let(:len) { 2 }
83
it "returns nil" do
84
expect(tampered.read(offset, len)).to be_nil
85
end
86
end
87
end
88
end
89
90
describe "#index" do
91
let(:search) { 'MZ' }
92
93
it "returns index of first search occurrence" do
94
expect(subject.index(search)).to eq(0)
95
end
96
97
context "when offset out of range" do
98
it "returns nil" do
99
expect(subject.index(search, 6000)).to be_nil
100
end
101
end
102
103
context "when search string not found" do
104
it "returns nil" do
105
expect(subject.index(search, 4600)).to be_nil
106
end
107
end
108
109
context "instance with tampered size" do
110
let(:tampered_size) { 6000 }
111
112
subject(:tampered) do
113
described_class.new(file, 0, tampered_size)
114
end
115
116
context "when searching offset after the real file length" do
117
let(:offset) { 5000 }
118
it "raises NoMethodError" do
119
expect{ tampered.index(search, offset) }.to raise_error(NoMethodError)
120
end
121
end
122
end
123
end
124
125
describe "#subsource" do
126
let(:offset) { 2 }
127
let(:len) { 512 }
128
129
it "returns a new Rex::ImageSource::Disk" do
130
expect(subject.subsource(offset, len)).to be_kind_of(described_class)
131
end
132
133
it "returns a new Rex::ImageSource::Disk with same file" do
134
expect(subject.subsource(offset, len).file).to eq(subject.file)
135
end
136
137
it "returns a new Rex::ImageSource::Disk with provided size" do
138
expect(subject.subsource(offset, len).size).to eq(len)
139
end
140
141
it "returns a new Rex::ImageSource::Disk with file_offset added to the original" do
142
expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)
143
end
144
end
145
146
describe "#close" do
147
it "returns nil" do
148
expect(subject.close).to be_nil
149
end
150
151
it "closes the associated file" do
152
expect(subject.file.closed?).to be_falsey
153
subject.close
154
expect(subject.file.closed?).to be_truthy
155
end
156
end
157
end
158
159