Skip to content

Commit

Permalink
Updated spec style.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Jul 24, 2012
1 parent 78d6919 commit ed3222d
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 155 deletions.
74 changes: 39 additions & 35 deletions spec/data_link_spec.rb
@@ -1,65 +1,69 @@
require 'spec_helper'

describe DataLink do
before(:all) do
@datalink = DataLink.new(0)
end
subject { described_class.new(0) }

it "should map datalink names to datalink layer type values" do
DataLink.name_to_val(:en10mb).should == 1
described_class.name_to_val(:en10mb).should == 1
end

it "should map datalink layer type values to datalink names" do
DataLink.val_to_name(1).should == "EN10MB"
described_class.val_to_name(1).should == "EN10MB"
end

it "should be initialized from a pcap datalink value" do
@datalink.name.should == 'NULL'
subject.name.should == 'NULL'
end

it "should support initialization from a pcap datalink name symbol" do
@datalink = DataLink.new(:null)
DataLink.should === @datalink
it "should have a description" do
subject.description.should_not be_empty
end

it "should support initialization from a pcap datalink name string" do
dl = DataLink.new('en10mb')
DataLink.should === dl
end
describe "#initialize" do
it "should support initialization from a pcap datalink name Symbol" do
link = described_class.new(:null)

it "should allow equality comparison against numeric values" do
(@datalink == 0).should == true
(@datalink == 1).should == false
end
link.name.should == 'NULL'
end

it "should allow equality comparison against String names" do
(@datalink == "null").should == true
(@datalink == "en10mb").should == false
it "should support initialization from a pcap datalink name String" do
link = described_class.new('en10mb')

link.name.should == 'EN10MB'
end
end

it "should allow equality comparison against Symbol names" do
(@datalink == :null).should == true
(@datalink == :en10mb).should == false
end
describe "#==" do
it "should allow equality comparison against numeric values" do
(subject == 0).should == true
(subject == 1).should == false
end

it "should allow comparison against another DataLink" do
(@datalink == DataLink.new(0)).should == true
(@datalink == DataLink.new(1)).should == false
end
it "should allow equality comparison against String names" do
(subject == "null").should == true
(subject == "en10mb").should == false
end

it "should still compare correctly against any other object" do
(@datalink == Object.new).should == false
end
it "should allow equality comparison against Symbol names" do
(subject == :null).should == true
(subject == :en10mb).should == false
end

it "should have a description" do
@datalink.description.should_not be_empty
it "should allow comparison against another described_class" do
(subject == described_class.new(0)).should == true
(subject == described_class.new(1)).should == false
end

it "should still compare correctly against any other object" do
(subject == Object.new).should == false
end
end

it "should convert to an Integer for the DLT value" do
@datalink.to_i.should == 0
subject.to_i.should == 0
end

it "should convert to a String for the DLT name" do
@datalink.to_s.should == 'NULL'
subject.to_s.should == 'NULL'
end
end
4 changes: 0 additions & 4 deletions spec/dead_spec.rb
Expand Up @@ -26,9 +26,5 @@

@pcap.close
end


end

end

16 changes: 8 additions & 8 deletions spec/error_buffer_spec.rb
@@ -1,17 +1,17 @@
require 'spec_helper'

describe ErrorBuffer do
before(:all) do
@errbuf = ErrorBuffer.create
end
subject { ErrorBuffer.create }

it "should have a size of 256" do
@errbuf.size.should == 256
it "should have a default size of 256" do
subject.size.should == 256
end

it "should return an error message with to_s" do
@errbuf.to_s.should be_empty
FFI::PCap.pcap_open_offline("/this/file/wont/exist/#{rand(0xFFFF)}", @errbuf )
@errbuf.to_s.should_not be_empty
subject.to_s.should be_empty

FFI::PCap.pcap_open_offline("/this/file/wont/exist",subject)

subject.to_s.should_not be_empty
end
end
27 changes: 13 additions & 14 deletions spec/file_header_spec.rb
@@ -1,28 +1,27 @@
require 'spec_helper'

describe FileHeader do
before(:all) do
@file_header = FileHeader.new( :raw => File.read(PCAP_TESTFILE) )
subject do
described_class.new(:raw => File.read(PCAP_TESTFILE))
end

it "should parse a pcap file correctly" do
@file_header.magic.should == 0xa1b2c3d4
@file_header.version_major.should == 2
@file_header.version_minor.should == 4
@file_header.thiszone.should == 0
@file_header.sigfigs.should == 0
@file_header.snaplen.should == 96
@file_header.linktype.should == 1
subject.magic.should == 0xa1b2c3d4
subject.version_major.should == 2
subject.version_minor.should == 4
subject.thiszone.should == 0
subject.sigfigs.should == 0
subject.snaplen.should == 96
subject.linktype.should == 1
end

it "should return a file format version string" do
String.should === @file_header.version
@file_header.version.should == "2.4"
subject.version.should == "2.4"
end

it "should return a DataLink for the linktype using datalink()" do
DataLink.should === @file_header.datalink
(@file_header.datalink == :en10mb).should == true
end
subject.datalink.should be_kind_of(DataLink)

(subject.datalink == :en10mb).should == true
end
end
17 changes: 12 additions & 5 deletions spec/live_spec.rb
Expand Up @@ -26,28 +26,34 @@

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
Stat.should === 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
Packet.should === pkt
pkt.should be_kind_of(Packet)

(Time.now - pkt.time).should_not > 1000
i+=1

i += 1
end

i.should == 2
end


describe "live packets" do
before(:all) do
@pcap = Live.new(
Expand All @@ -64,7 +70,6 @@
end

it_should_behave_like "FFI::PCap::Packet populated"

end

describe "yielding to a block" do
Expand All @@ -78,7 +83,9 @@
end

start_traffic_generator()

it_should_behave_like "FFI::PCap::CaptureWrapper"

stop_traffic_generator()
@pcap.close
end
Expand Down
21 changes: 10 additions & 11 deletions spec/offline_spec.rb
Expand Up @@ -2,31 +2,32 @@
require 'wrapper_behaviors'

describe Offline do
before(:each) do
@pcap = Offline.new(PCAP_TESTFILE)
end
before(:each) { @pcap = Offline.new(PCAP_TESTFILE) }

after(:each) do
@pcap.close
end
after(:each) { @pcap.close }

it_should_behave_like "FFI::PCap::CaptureWrapper"

it "should return a nil from next() at the end of the dump file" do
i = 0
@pcap.loop {|this,pkt| i+=1 }

@pcap.loop {|this,pkt| i += 1 }

i.should > 0
@pcap.next.should be_nil
end

it "should yield packets with a timestamp using loop()" do
i = 0
@pkt = nil
@pcap.loop(:count => 2) do |this, pkt|

@pcap.loop(:count => 2) do |this,pkt|
this.should == @pcap

pkt.should_not be_nil
Packet.should === pkt
pkt.should be_kind_of(Packet)
pkt.time.to_i.should > 0

i+=1
end
i.should == 2
Expand All @@ -41,7 +42,6 @@
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
Expand All @@ -55,7 +55,6 @@

@pcap.close
end

end
end

15 changes: 10 additions & 5 deletions spec/packet_behaviors.rb
@@ -1,28 +1,34 @@
require 'spec_helper'

shared_examples_for "FFI::PCap::Packet" do
it "should supply a way to get a pointer for the body" do
@pkt.body_ptr.should_not be_nil
::FFI::Pointer.should === @pkt.body_ptr

@pkt.body_ptr.should be_kind_of(FFI::Pointer)
end

it "should supply a way to get a String for the body" do
@pkt.body.should_not be_nil
String.should === @pkt.body

@pkt.body.should be_kind_of(String)
end

it "should supply a timestamp as a Time object" do
@pkt.time.should_not be_nil
Time.should === @pkt.time

@pkt.time.should be_kind_of(Time)
end

it "should allow time timestamp to be changed" do
t = Time.now
lambda {@pkt.time = t}.should_not raise_error(Exception)

@pkt.time = t
@pkt.time.to_i.should == t.to_i
end

it "should return a deep copy of itself with copy()" do
cp = @pkt.copy()

cp.object_id.should_not == @pkt.object_id
cp.body_ptr.object_id.should_not == @pkt.body_ptr.object_id
cp.body.should == @pkt.body
Expand Down Expand Up @@ -56,7 +62,6 @@
it "should have a non-null body pointer" do
@pkt.body_ptr.should_not be_null
end

end

shared_examples_for "FFI::PCap::Packet composed" do
Expand Down
17 changes: 9 additions & 8 deletions spec/packet_injection_spec.rb
Expand Up @@ -3,15 +3,15 @@
describe FFI::PCap::Live do
describe "packet injection" do
before(:all) do
@pcap = FFI::PCap.open_live :device => PCAP_DEV,
:promisc => false,
:timeout => 100,
:snaplen => 8192
@pcap = FFI::PCap.open_live(
:device => PCAP_DEV,
:promisc => false,
:timeout => 100,
:snaplen => 8192
)
end

after(:all) do
@pcap.close
end
after(:all) { @pcap.close }

it "should detect when an invalid argument is supplied" do
lambda { @pcap.inject(Object.new)}.should raise_error(ArgumentError)
Expand All @@ -26,13 +26,14 @@

it "should allow injection of a String using inject()" do
test_data = "A" * 1024

@pcap.inject(test_data).should == test_data.size
end

it "should allow injection of a Packet using inject()" do
test_data = "B" * 512

@pcap.inject(Packet.from_string(test_data)).should == test_data.size
end

end
end

0 comments on commit ed3222d

Please sign in to comment.