-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
live_spec.rb
94 lines (72 loc) · 1.87 KB
/
live_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'spec_helper'
require 'wrapper_behaviors'
require 'packet_behaviors'
describe Live do
before(:each) do
@pcap = Live.new(
:device => PCAP_DEV,
:promisc => true,
:timeout => 1000
)
start_traffic_generator()
end
after(:each) do
stop_traffic_generator()
@pcap.close
end
it_should_behave_like "FFI::PCap::CaptureWrapper"
it "should support non-blocking mode" do
@pcap.non_blocking = true
@pcap.should be_non_blocking
end
it "should provide statistics about packets received/dropped" do
i = 0
@pcap.loop {|this,pkt| @pcap.stop if (i += 1) == 10 }
i.should_not == 0
stats = @pcap.stats
stats.should be_kind_of(Stat)
stats.received.should > 0
stats.received.should >= 10
end
it "should yield packets with a timestamp using loop()" do
i = 0
@pkt = nil
@pcap.loop(:count => 2) do |this, pkt|
this.should == @pcap
pkt.should_not be_nil
pkt.should be_kind_of(Packet)
(Time.now - pkt.time).should_not > 1000
i += 1
end
i.should == 2
end
describe "live packets" do
before(:all) do
@pcap = Live.new(
:device => PCAP_DEV,
:promisc => true
)
@pkt = @pcap.next()
start_traffic_generator()
end
after(:all) do
stop_traffic_generator()
@pcap.close
end
it_should_behave_like "FFI::PCap::Packet populated"
end
describe "yielding to a block" do
# Note we also test all the behaviors here together instead of seperately.
Offline.new(PCAP_TESTFILE) do |this|
@pcap = this
it "should be in a ready state in the block" do
@pcap.should be_ready
@pcap.should_not be_closed
end
start_traffic_generator()
it_should_behave_like "FFI::PCap::CaptureWrapper"
stop_traffic_generator()
@pcap.close
end
end
end