Skip to content

Commit 9d84dda

Browse files
committed
Merge Rex::OLE::DIFAT specs
2 parents 526251f + cf18225 commit 9d84dda

File tree

1 file changed

+291
-0
lines changed

1 file changed

+291
-0
lines changed

spec/lib/rex/ole/difat_spec.rb

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
# -*- coding:binary -*-
2+
require 'spec_helper'
3+
4+
require 'rex/ole'
5+
6+
describe Rex::OLE::DIFAT do
7+
8+
let(:storage) do
9+
Rex::OLE::Storage.new
10+
end
11+
12+
subject(:difat) do
13+
described_class.new(storage)
14+
end
15+
16+
describe ".new" do
17+
it "returns a Rex::OLE::DIFAT instance" do
18+
expect(described_class.new(storage)).to be_a(Rex::OLE::DIFAT)
19+
end
20+
21+
it "initializes @stg" do
22+
expect(difat.instance_variable_get(:@stg)).to eq(storage)
23+
end
24+
25+
it "initializes @entries" do
26+
expect(difat.instance_variable_get(:@entries)).to be_an(Array)
27+
end
28+
29+
it "initializes @entries as empty array" do
30+
expect(difat.instance_variable_get(:@entries)).to be_empty
31+
end
32+
end
33+
34+
describe "#[]=" do
35+
context "when the entry doesn't exist" do
36+
it "sets an element in the @entries array" do
37+
difat[0] = 1
38+
expect(difat.instance_variable_get(:@entries)[0]).to eq(1)
39+
end
40+
end
41+
42+
context "when the entry exists" do
43+
it "replaces the element in the @entries array" do
44+
difat[0] = 1
45+
difat[0] = 2
46+
expect(difat.instance_variable_get(:@entries)[0]).to eq(2)
47+
end
48+
end
49+
end
50+
51+
describe "#[]" do
52+
context "when the entry doesn't exist" do
53+
it "returns nil" do
54+
expect(difat[3]).to eq(nil)
55+
end
56+
end
57+
58+
context "when the entry exists" do
59+
it "returns the entry value" do
60+
difat[3] = 31
61+
expect(difat[3]).to eq(31)
62+
end
63+
end
64+
end
65+
66+
67+
describe "#+" do
68+
context "when @entries is empty" do
69+
it "sets the @entries values" do
70+
difat + [1, 2]
71+
expect(difat.instance_variable_get(:@entries)).to eq([1, 2])
72+
end
73+
end
74+
75+
context "when @entries isn't empty" do
76+
it "concatenates the array to @entries" do
77+
difat[2] = 0
78+
difat + [1, 2]
79+
expect(difat.instance_variable_get(:@entries)).to eq([nil, nil, 0, 1, 2])
80+
end
81+
end
82+
end
83+
84+
describe "#<<" do
85+
it "concatenates the element to the @entries array" do
86+
difat[0] = 1
87+
difat << 3
88+
expect(difat.instance_variable_get(:@entries)).to eq([1, 3])
89+
end
90+
end
91+
92+
describe "#length" do
93+
subject(:difat_length) do
94+
difat.length
95+
end
96+
97+
context "when @entries is empty" do
98+
it "returns 0" do
99+
is_expected.to eq(0)
100+
end
101+
end
102+
103+
context "when @entries isn't empty" do
104+
it "returns the @entries length" do
105+
difat[0] = 1
106+
difat[1] = 2
107+
is_expected.to eq(2)
108+
end
109+
end
110+
end
111+
112+
describe "#slice!" do
113+
context "when @entries is empty" do
114+
it "returns empty array" do
115+
expect(difat.slice!(0, 1)).to eq([])
116+
end
117+
end
118+
119+
context "when start is out of range" do
120+
it "returns nil" do
121+
difat[0] = 1
122+
expect(difat.slice!(10, 1)).to eq(nil)
123+
end
124+
end
125+
126+
context "when stop is 0" do
127+
it "returns empty array" do
128+
difat[0] = 1
129+
expect(difat.slice!(0, 0)).to eq([])
130+
end
131+
132+
it "doesn't delete nothing" do
133+
difat[0] = 1
134+
difat.slice!(0, 0)
135+
expect(difat[0]).to eq(1)
136+
end
137+
end
138+
139+
context "when @entries is long enough" do
140+
it "returns the deleted elements" do
141+
difat + [1, 2]
142+
expect(difat.slice!(0, 1)).to eq([1])
143+
end
144+
145+
it "deletes the elements in the range" do
146+
difat + [1, 2]
147+
difat.slice!(0, 1)
148+
expect(difat.instance_variable_get(:@entries)).to eq([2])
149+
end
150+
end
151+
end
152+
153+
describe "#reset" do
154+
it "resets the @entries array" do
155+
difat[0] = 1
156+
difat.reset
157+
expect(difat.length).to eq(0)
158+
end
159+
end
160+
161+
describe "#each" do
162+
it "calls the block for every @entries element" do
163+
difat + [1, 2, 3]
164+
res = 0
165+
difat.each { |elem| res += elem}
166+
expect(res).to eq(1 + 2 + 3)
167+
end
168+
end
169+
170+
describe "#to_s" do
171+
subject(:difat_string) do
172+
difat.to_s
173+
end
174+
175+
it "returns an String" do
176+
is_expected.to be_an(String)
177+
end
178+
179+
it "starts with {" do
180+
is_expected.to start_with('{')
181+
end
182+
183+
it "ends with }" do
184+
is_expected.to end_with('}')
185+
end
186+
187+
it "contains @entries values" do
188+
difat + [Rex::OLE::SECT_FAT, 1, 2, 3, Rex::OLE::SECT_DIF, Rex::OLE::SECT_FREE, Rex::OLE::SECT_END]
189+
is_expected.to match(/FAT, 0x1, 0x2, 0x3, DIF, FREE, END/)
190+
end
191+
end
192+
193+
describe "#read" do
194+
context "when difat is empty" do
195+
it "returns nil" do
196+
expect(difat.read).to be_nil
197+
end
198+
end
199+
end
200+
201+
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
289+
290+
end
291+
end

0 commit comments

Comments
 (0)