CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/image_source/memory_spec.rb
Views: 1904
1
# -*- coding:binary -*-
2
require 'spec_helper'
3
4
require 'rex/image_source/memory'
5
6
RSpec.describe Rex::ImageSource::Memory do
7
8
let(:raw_data) { 'ABCDEFGHIJKLMNOP' }
9
10
subject do
11
described_class.new(raw_data)
12
end
13
14
it_should_behave_like 'Rex::ImageSource::ImageSource'
15
16
describe "#initialize" do
17
subject(:memory_class) do
18
described_class.allocate
19
end
20
21
it "initializes size to data length" do
22
memory_class.send(:initialize, raw_data)
23
expect(memory_class.size).to eq(raw_data.length)
24
end
25
26
it "initializes file_offset to 0 by default" do
27
memory_class.send(:initialize, raw_data)
28
expect(memory_class.file_offset).to eq(0)
29
end
30
31
context "when using nil as data" do
32
it "raises an error" do
33
expect { memory_class.send(:initialize, nil) }.to raise_error(NoMethodError)
34
end
35
end
36
end
37
38
describe "#read" do
39
context "when offset is positive" do
40
let(:offset) { 1 }
41
let(:len) { 10 }
42
43
it "returns an String" do
44
expect(subject.read(offset, len)).to be_a_kind_of(String)
45
end
46
47
it "returns an String of provided length" do
48
expect(subject.read(offset, len).length).to eq(10)
49
end
50
51
it "returns an String with _raw_data contents starting at provided offset" do
52
expect(subject.read(offset, len)).to start_with('BCD')
53
end
54
end
55
56
context "when offset is negative" do
57
let(:offset) { -5 }
58
let(:len) { 2 }
59
60
it "returns an String" do
61
expect(subject.read(offset, len)).to be_a_kind_of(String)
62
end
63
64
it "returns an String of provided length" do
65
expect(subject.read(offset, len).length).to eq(2)
66
end
67
68
it "offset is counted from the end of the _raw_data" do
69
expect(subject.read(offset, len)).to eq('LM')
70
end
71
end
72
73
context "when offset is out of range" do
74
let(:offset) { 20 }
75
let(:len) { 2 }
76
77
it "returns nil" do
78
expect(subject.read(offset, len)).to be_nil
79
end
80
end
81
82
context "when len is bigger than _raw_data" do
83
let(:offset) { 0 }
84
let(:len) { 20 }
85
86
it "returns an String" do
87
expect(subject.read(offset, len)).to be_a_kind_of(String)
88
end
89
90
it "returns an String truncated to available contents" do
91
expect(subject.read(offset, len).length).to eq(raw_data.length)
92
end
93
end
94
end
95
96
describe "#subsource" do
97
let(:offset) { 2 }
98
let(:len) { 10 }
99
100
it "returns a new Rex::ImageSource::Memory" do
101
expect(subject.subsource(offset, len)).to be_kind_of(described_class)
102
end
103
104
it "returns a new Rex::ImageSource::Memory with provided size" do
105
expect(subject.subsource(offset, len).size).to eq(len)
106
end
107
108
it "returns a new Rex::ImageSource::Memory with file_offset added to the original" do
109
expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)
110
end
111
112
it "returns a new Rex::ImageSource::Memory with rawdata from the original" do
113
expect(subject.subsource(offset, len).rawdata).to eq(subject.rawdata[offset, len])
114
end
115
116
context "when offset is out of range" do
117
let(:offset) { 20 }
118
let(:len) { 2 }
119
120
it "raises an error" do
121
expect { subject.subsource(offset, len) }.to raise_error(NoMethodError)
122
end
123
end
124
125
context "when len is bigger than source rawdata" do
126
let(:offset) { 2 }
127
let(:len) { 20 }
128
129
it "returns a new Rex::ImageSource::Memory" do
130
expect(subject.subsource(offset, len)).to be_kind_of(described_class)
131
end
132
133
it "returns a new Rex::ImageSource::Memory with provided size truncated" do
134
expect(subject.subsource(offset, len).size).to eq(14)
135
end
136
137
it "returns a new Rex::ImageSource::Memory with file_offset added to the original" do
138
expect(subject.subsource(offset, len).file_offset).to eq(offset + subject.file_offset)
139
end
140
141
it "returns a new Rex::ImageSource::Memory with rawdata truncated" do
142
expect(subject.subsource(offset, len).rawdata).to eq('CDEFGHIJKLMNOP')
143
end
144
end
145
end
146
147
describe "#close" do
148
it "returns nil" do
149
expect(subject.close).to be_nil
150
end
151
end
152
153
describe "#index" do
154
let(:found) { 'FG' }
155
let(:not_found) { 'XYZ' }
156
157
context "when search available substring" do
158
it "returns the index of the first occurrence" do
159
expect(subject.index(found)).to eq(5)
160
end
161
162
context "when using negative offset" do
163
let(:offset) { -14 }
164
it "returns the index of the first occurrence" do
165
expect(subject.index(found, offset)).to eq(5)
166
end
167
end
168
169
context "when using positive offset" do
170
let(:offset) { 1 }
171
it "returns the index of the first occurrence" do
172
expect(subject.index(found, offset)).to eq(5)
173
end
174
end
175
end
176
177
context "when search not available substring" do
178
it "returns nil" do
179
expect(subject.index(not_found)).to be_nil
180
end
181
end
182
183
context "when using negative offset" do
184
let(:offset) { -1 }
185
it "start to search from offset from the end of the string" do
186
expect(subject.index(found, offset)).to be_nil
187
end
188
end
189
end
190
191
end
192
193