Skip to content

Commit cf18225

Browse files
committed
Finish Rex::OLE::DIFAT specs
1 parent 35e385f commit cf18225

File tree

1 file changed

+106
-7
lines changed

1 file changed

+106
-7
lines changed

spec/lib/rex/ole/difat_spec.rb

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,21 @@
9090
end
9191

9292
describe "#length" do
93+
subject(:difat_length) do
94+
difat.length
95+
end
96+
9397
context "when @entries is empty" do
9498
it "returns 0" do
95-
expect(difat.length).to eq(0)
99+
is_expected.to eq(0)
96100
end
97101
end
98102

99103
context "when @entries isn't empty" do
100104
it "returns the @entries length" do
101105
difat[0] = 1
102106
difat[1] = 2
103-
expect(difat.length).to eq(2)
107+
is_expected.to eq(2)
104108
end
105109
end
106110
end
@@ -164,29 +168,124 @@
164168
end
165169

166170
describe "#to_s" do
171+
subject(:difat_string) do
172+
difat.to_s
173+
end
174+
167175
it "returns an String" do
168-
expect(difat.to_s).to be_an(String)
176+
is_expected.to be_an(String)
169177
end
170178

171179
it "starts with {" do
172-
expect(difat.to_s).to start_with('{')
180+
is_expected.to start_with('{')
173181
end
174182

175183
it "ends with }" do
176-
expect(difat.to_s).to end_with('}')
184+
is_expected.to end_with('}')
177185
end
178186

179187
it "contains @entries values" do
180188
difat + [Rex::OLE::SECT_FAT, 1, 2, 3, Rex::OLE::SECT_DIF, Rex::OLE::SECT_FREE, Rex::OLE::SECT_END]
181-
expect(difat.to_s).to match(/FAT, 0x1, 0x2, 0x3, DIF, FREE, END/)
189+
is_expected.to match(/FAT, 0x1, 0x2, 0x3, DIF, FREE, END/)
182190
end
183191
end
184192

185193
describe "#read" do
186-
194+
context "when difat is empty" do
195+
it "returns nil" do
196+
expect(difat.read).to be_nil
197+
end
198+
end
187199
end
188200

189201
describe "#write" do
202+
context "when entries is empty" do
203+
it "returns 0" do
204+
expect(difat.write).to eq(0)
205+
end
206+
207+
it "fills the first 109 FAT sectors in the storage header" do
208+
difat.write
209+
storage = difat.instance_variable_get(:@stg)
210+
expect(storage.header._sectFat.length).to eq(109)
211+
end
212+
213+
it "fills the first 109 FAT sectors in the storage header with SECT_FREE" do
214+
difat.write
215+
storage = difat.instance_variable_get(:@stg)
216+
storage.header._sectFat.each { |s|
217+
expect(s).to eq(Rex::OLE::SECT_FREE)
218+
}
219+
end
220+
end
221+
222+
context "when entries length is less than 109" do
223+
let(:entries) { [1] * 20 }
224+
225+
it "returns the number of entries" do
226+
difat + entries
227+
expect(difat.write).to eq(20)
228+
end
229+
230+
it "fills the first 109 FAT sectors in the storage header" do
231+
difat + entries
232+
difat.write
233+
storage = difat.instance_variable_get(:@stg)
234+
expect(storage.header._sectFat.length).to eq(109)
235+
end
236+
237+
it "fills the first FAT sectors with the entries" do
238+
difat + entries
239+
difat.write
240+
storage = difat.instance_variable_get(:@stg)
241+
(0..entries.length - 1).each { |i|
242+
expect(storage.header._sectFat[i]).to eq(1)
243+
}
244+
end
245+
246+
it "fills the remaining FAT sectors with FREE sectors" do
247+
difat + entries
248+
difat.write
249+
storage = difat.instance_variable_get(:@stg)
250+
(entries.length..109 - 1).each { |i|
251+
expect(storage.header._sectFat[i]).to eq(Rex::OLE::SECT_FREE)
252+
}
253+
end
254+
end
255+
256+
context "when entries length is 109" do
257+
let(:entries) { [1] * 109 }
258+
259+
it "returns the number of entries" do
260+
difat + entries
261+
expect(difat.write).to eq(109)
262+
end
263+
264+
it "fills the first 109 FAT sectors in the storage header" do
265+
difat + entries
266+
difat.write
267+
storage = difat.instance_variable_get(:@stg)
268+
expect(storage.header._sectFat.length).to eq(109)
269+
end
270+
271+
it "fills the first 109 FAT sectors with the entries" do
272+
difat + entries
273+
difat.write
274+
storage = difat.instance_variable_get(:@stg)
275+
(0..storage.header._sectFat.length - 1).each { |i|
276+
expect(storage.header._sectFat[i]).to eq(1)
277+
}
278+
end
279+
end
280+
281+
context "when entries length is greater than 109" do
282+
let(:entries) { [1] * 110 }
283+
284+
it "raises a RuntimeError" do
285+
difat + entries
286+
expect { difat.write }.to raise_error(RuntimeError)
287+
end
288+
end
190289

191290
end
192291
end

0 commit comments

Comments
 (0)