Skip to content

Commit 89c98eb

Browse files
David MaloneyDavid Maloney
authored andcommitted
Finished tests for GroupTlv ftmp
1 parent 468f637 commit 89c98eb

File tree

1 file changed

+141
-2
lines changed

1 file changed

+141
-2
lines changed

spec/lib/rex/post/meterpreter/packet_spec.rb

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
it "should replace any existing TLV of the same type when the replace flag is set to true" do
189189
subject.add_tlv(Rex::Post::Meterpreter::TLV_TYPE_STRING,"test")
190190
subject.add_tlv(Rex::Post::Meterpreter::TLV_TYPE_STRING,"test2", true)
191+
subject.tlvs.count.should == 1
191192
subject.tlvs.first.value.should == "test2"
192193
end
193194

@@ -200,13 +201,151 @@
200201
end
201202

202203
context "the add_tlvs method" do
204+
it "should be able to add an array of type-value hashes" do
205+
tlv_array = [
206+
{'type' => Rex::Post::Meterpreter::TLV_TYPE_STRING, 'value' => "test"},
207+
{'type' => Rex::Post::Meterpreter::TLV_TYPE_STRING, 'value' => "test2"}
208+
]
209+
subject.add_tlvs(tlv_array)
210+
subject.tlvs.count.should == 2
211+
subject.tlvs.first.value.should == "test"
212+
subject.tlvs.last.value.should == "test2"
213+
end
214+
215+
it "should raise an error when given something other than nil or an array" do
216+
pending "RM #7598"
217+
subject.add_tlvs("bad value").should raise_error
218+
end
219+
220+
it "should raise an error when given an array of objects other than hashes" do
221+
pending "RM #7598"
222+
subject.add_tlvs([1,2,3]).should raise_error
223+
end
203224

225+
it "should raise an error when any of the hashes are missing a key" do
226+
pending "RM #7598"
227+
tlv_array = [
228+
{:type => Rex::Post::Meterpreter::TLV_TYPE_STRING, :value => "test"},
229+
{:type => Rex::Post::Meterpreter::TLV_TYPE_STRING}
230+
]
231+
subject.add_tlvs(tlv_array).should raise_error
232+
end
204233
end
205234

206235
context "with TLVs added" do
207-
before(:all) do
208-
#subject.add_tlv
236+
before(:each) do
237+
subject.reset
238+
tlv_array = [
239+
{'type' => Rex::Post::Meterpreter::TLV_TYPE_STRING, 'value' => "test"},
240+
{'type' => Rex::Post::Meterpreter::TLV_TYPE_STRING, 'value' => "test2"},
241+
{'type' => Rex::Post::Meterpreter::TLV_TYPE_UINT, 'value' => 5}
242+
]
243+
subject.add_tlvs(tlv_array)
244+
@raw_group = "\x00\x00\x00/@\x00\x005\x00\x00\x00\r\x00\x01\x00\ntest\x00\x00\x00\x00\x0E\x00\x01\x00\ntest2\x00\x00\x00\x00\f\x00\x02\x00\v\x00\x00\x00\x05"
209245
end
246+
247+
it "should empty the array of TLV when reset is called" do
248+
subject.reset
249+
subject.tlvs.should == []
250+
end
251+
252+
it "should convert to raw bytes when to_r is called" do
253+
subject.to_r.should == @raw_group
254+
end
255+
256+
257+
context "the from_r method" do
258+
it "should build the TLV group when given the propper raw bytes" do
259+
subject.reset
260+
subject.from_r( @raw_group)
261+
subject.tlvs[0].inspect.should == "#<Rex::Post::Meterpreter::Tlv type=STRING meta=STRING value=\"test\">"
262+
subject.tlvs[1].inspect.should == "#<Rex::Post::Meterpreter::Tlv type=STRING meta=STRING value=\"test2\">"
263+
subject.tlvs[2].inspect.should == "#<Rex::Post::Meterpreter::Tlv type=UINT meta=INT value=5>"
264+
end
265+
end
266+
267+
268+
context "the get_tlvs method" do
269+
it "should return all TLVs of the supplied type" do
270+
tlvs = subject.get_tlvs(Rex::Post::Meterpreter::TLV_TYPE_STRING)
271+
tlvs.count.should == 2
272+
tlvs.first.value.should == "test"
273+
tlvs.last.value.should == "test2"
274+
end
275+
276+
it "should return all TLVs when supplied the ANY TLV type" do
277+
tlvs = subject.get_tlvs(Rex::Post::Meterpreter::TLV_TYPE_ANY)
278+
tlvs.count.should == subject.tlvs.count
279+
end
280+
281+
it "should return an empty array for a TLV type that isn't present" do
282+
subject.get_tlvs(Rex::Post::Meterpreter::TLV_TYPE_BOOL).should == []
283+
end
284+
285+
it "should return an empty array for a nonexistant TLV type" do
286+
subject.get_tlvs(55555555).should == []
287+
end
288+
end
289+
290+
context "the get tlv_method" do
291+
it "should return the first TLV of the specified type by default" do
292+
subject.get_tlv(Rex::Post::Meterpreter::TLV_TYPE_STRING).should == subject.tlvs.first
293+
subject.get_tlv(Rex::Post::Meterpreter::TLV_TYPE_UINT).should == subject.tlvs.last
294+
end
295+
296+
it "should return the correct TLV of the specified type for the given index" do
297+
subject.get_tlv(Rex::Post::Meterpreter::TLV_TYPE_STRING,1).should == subject.tlvs[1]
298+
end
299+
300+
it "should return nil if given an out of bounds index" do
301+
subject.get_tlv(Rex::Post::Meterpreter::TLV_TYPE_STRING,5).should == nil
302+
end
303+
304+
it "should return nil if given a non-present TLV type" do
305+
subject.get_tlv(Rex::Post::Meterpreter::TLV_TYPE_BOOL).should == nil
306+
end
307+
end
308+
309+
context "the get_tlv_value method" do
310+
it "should return the value of the first TLV with the given type" do
311+
subject.get_tlv_value(Rex::Post::Meterpreter::TLV_TYPE_STRING).should == subject.tlvs.first.value
312+
end
313+
314+
it "should return the correct TLV value of the specified type for the given index" do
315+
subject.get_tlv_value(Rex::Post::Meterpreter::TLV_TYPE_STRING,1).should == subject.tlvs[1].value
316+
end
317+
318+
it "should return nil if given an out of bounds index" do
319+
subject.get_tlv_value(Rex::Post::Meterpreter::TLV_TYPE_STRING,5).should == nil
320+
end
321+
322+
it "should return nil if given a non-present TLV type" do
323+
subject.get_tlv_value(Rex::Post::Meterpreter::TLV_TYPE_BOOL).should == nil
324+
end
325+
end
326+
327+
context "the get_tlv_values method" do
328+
it "should return an array of values for the designated TLV types" do
329+
subject.get_tlv_values(Rex::Post::Meterpreter::TLV_TYPE_STRING).should == ["test", "test2"]
330+
end
331+
332+
it "should return an empty array for a non-present TLV type" do
333+
subject.get_tlv_values(Rex::Post::Meterpreter::TLV_TYPE_BOOL).should == []
334+
end
335+
end
336+
337+
context "the has_tlv? method" do
338+
it "should return true if the TLV Type is present" do
339+
subject.has_tlv?(Rex::Post::Meterpreter::TLV_TYPE_STRING).should == true
340+
end
341+
342+
it "should return false if the TLV type is not present" do
343+
subject.has_tlv?(Rex::Post::Meterpreter::TLV_TYPE_BOOL).should == false
344+
end
345+
end
346+
347+
348+
210349
end
211350

212351

0 commit comments

Comments
 (0)