dakrone-pcap-ffi 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ require 'pcap/handler'
2
+
3
+ require 'spec_helper'
4
+
5
+ shared_examples_for "Handler" do
6
+ it "must have a datalink" do
7
+ datalink = @pcap.datalink
8
+
9
+ datalink.value.should_not be_nil
10
+ datalink.name.should_not be_empty
11
+ end
12
+
13
+ it "should pass packets to a callback" do
14
+ @pcap.callback do |user,header,bytes|
15
+ header.captured.should_not == 0
16
+ header.length.should_not == 0
17
+
18
+ bytes.should_not be_null
19
+ end
20
+
21
+ @pcap.loop
22
+ end
23
+
24
+ it "should be able to get the next packet" do
25
+ header, data = @pcap.next
26
+
27
+ header.should_not be_nil
28
+ header.captured.should_not == 0
29
+ header.length.should_not == 0
30
+
31
+ data.should_not be_nil
32
+ data.should_not be_null
33
+ end
34
+
35
+ it "should be able to open a dump file" do
36
+ lambda {
37
+ dumper = @pcap.open_dump(Tempfile.new.path)
38
+ dumper.close
39
+ }.should_not raise_error(RuntimeError)
40
+ end
41
+
42
+ it "should raise an exception when opening a bad dump file" do
43
+ lambda {
44
+ @pcap.open_dump(File.join('','obviously','not','there'))
45
+ }.should raise_error(RuntimeError)
46
+ end
47
+
48
+ it "should return an empty String when an error has not occurred" do
49
+ @pcap.error.should be_empty
50
+ end
51
+
52
+ it "should be able to break out of the Handler#loop" do
53
+ stopped = false
54
+
55
+ @pcap.loop do |user,pkthdr,bytes|
56
+ stopped = true
57
+ @pcap.stop
58
+ end
59
+
60
+ stopped.should == true
61
+ end
62
+
63
+ it "should prevent double closes" do
64
+ @pcap.close
65
+ @pcap.should be_closed
66
+
67
+ lambda {
68
+ @pcap.close
69
+ }.should_not raise_error(StandardError)
70
+ end
71
+ end
@@ -0,0 +1,80 @@
1
+ require 'pcap/handler'
2
+
3
+ require 'spec_helper'
4
+ require 'helpers/dumps'
5
+ require 'handler_examples'
6
+
7
+ describe PCap::Handler do
8
+ describe "offline" do
9
+ before(:each) do
10
+ @pcap = PCap.open_offline(dump_path('simple_tcp'))
11
+ end
12
+
13
+ after(:each) do
14
+ @pcap.close
15
+ end
16
+
17
+ it_should_behave_like "Handler"
18
+
19
+ it "should not support non-blocking mode" do
20
+ @pcap.non_blocking = true
21
+ @pcap.should_not be_non_blocking
22
+ end
23
+
24
+ it "should return a nil if there are no packets left in the dump file" do
25
+ @pcap.loop
26
+
27
+ header, data = @pcap.next
28
+
29
+ header.should be_nil
30
+ data.should be_nil
31
+ end
32
+
33
+ it "should raise a ReadError when reading past the end of a dump file" do
34
+ @pcap.loop
35
+
36
+ lambda {
37
+ @pcap.next_extra
38
+ }.should raise_error(ReadError)
39
+ end
40
+ end
41
+
42
+ describe "live" do
43
+ before(:each) do
44
+ @pcap = PCap.open_live(:count => 2)
45
+ end
46
+
47
+ after(:each) do
48
+ @pcap.close
49
+ end
50
+
51
+ it_should_behave_like "Handler"
52
+
53
+ it "should support non-blocking mode" do
54
+ @pcap.non_blocking = true
55
+ @pcap.should be_non_blocking
56
+ end
57
+
58
+ it "should provide statistics about packets received/dropped" do
59
+ @pcap.loop
60
+
61
+ stats = @pcap.stats
62
+ stats.received.should > 0
63
+ end
64
+ end
65
+
66
+ describe "dead" do
67
+ before(:each) do
68
+ @pcap = PCap.open_dead
69
+ end
70
+
71
+ after(:each) do
72
+ @pcap.close
73
+ end
74
+
75
+ it "should support non-blocking mode" do
76
+ @pcap.non_blocking = true
77
+ @pcap.should be_non_blocking
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ def dump_path(name)
2
+ File.expand_path(File.join(File.dirname(__FILE__),'..','dumps',"#{name}.pcap"))
3
+ end
data/spec/pcap_spec.rb ADDED
@@ -0,0 +1,51 @@
1
+ require 'pcap/pcap'
2
+ require 'pcap/version'
3
+
4
+ require 'spec_helper'
5
+ require 'helpers/dumps'
6
+
7
+ describe PCap do
8
+ it "should define a VERSION constant" do
9
+ PCap.const_defined?('VERSION').should == true
10
+ end
11
+
12
+ it "should have a library version" do
13
+ PCap.lib_version.should_not be_empty
14
+ end
15
+
16
+ it "should return the name of a device suitable for open_live" do
17
+ dev = PCap.device
18
+
19
+ dev.should_not be_nil
20
+ dev.should_not be_empty
21
+ end
22
+
23
+ it "should enumerate over all usable devices" do
24
+ PCap.each_device do |dev|
25
+ dev.should_not be_nil
26
+ dev.should_not be_null
27
+ dev.class.should == PCap::IF
28
+ end
29
+ end
30
+
31
+ it "should be able to open a live pcap handler" do
32
+ lambda {
33
+ pcap = PCap.open_live
34
+ pcap.close
35
+ }.should_not raise_error(StandardError)
36
+ end
37
+
38
+ it "should be able to open a dead pcap handler" do
39
+ lambda {
40
+ pcap = PCap.open_dead('null')
41
+ pcap.close
42
+ }.should_not raise_error(StandardError)
43
+ end
44
+
45
+ it "should be able to open a pcap dump file" do
46
+ lambda {
47
+ pcap = PCap.open_offline(dump_path('simple_tcp'))
48
+ pcap.close
49
+ }.should_not raise_error(StandardError)
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.12'
3
+ require 'spec'
4
+
5
+ require 'pcap/version'
6
+
7
+ include FFI
8
+ include FFI::PCap
data/tasks/spec.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ desc "Run all specifications"
4
+ Spec::Rake::SpecTask.new(:spec) do |t|
5
+ t.libs += ['lib', 'spec']
6
+ t.spec_opts = ['--colour', '--format', 'specdoc']
7
+ end
8
+
9
+ task :default => :spec
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dakrone-pcap-ffi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Postmodern, Dakrone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Bindings to sniff packets using the FFI interface in Ruby.
17
+ email: postmodern.mod3@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - .gitignore
26
+ - History.txt
27
+ - Manifest.txt
28
+ - README.txt
29
+ - Rakefile
30
+ - VERSION
31
+ - examples/print_bytes.rb
32
+ - lib/pcap.rb
33
+ - lib/pcap/addr.rb
34
+ - lib/pcap/data_link.rb
35
+ - lib/pcap/dumper.rb
36
+ - lib/pcap/error_buffer.rb
37
+ - lib/pcap/exceptions.rb
38
+ - lib/pcap/exceptions/read_error.rb
39
+ - lib/pcap/ffi.rb
40
+ - lib/pcap/file_header.rb
41
+ - lib/pcap/handler.rb
42
+ - lib/pcap/if.rb
43
+ - lib/pcap/in_addr.rb
44
+ - lib/pcap/packet_header.rb
45
+ - lib/pcap/packets.rb
46
+ - lib/pcap/packets/ethernet.rb
47
+ - lib/pcap/packets/ip.rb
48
+ - lib/pcap/packets/tcp.rb
49
+ - lib/pcap/packets/typedefs.rb
50
+ - lib/pcap/pcap.rb
51
+ - lib/pcap/sock_addr.rb
52
+ - lib/pcap/sock_addr_in.rb
53
+ - lib/pcap/stat.rb
54
+ - lib/pcap/time_val.rb
55
+ - lib/pcap/typedefs.rb
56
+ - lib/pcap/version.rb
57
+ - pcap-ffi.gemspec
58
+ - spec/data_link_spec.rb
59
+ - spec/dumps/simple_tcp.pcap
60
+ - spec/error_buffer.rb
61
+ - spec/handler_examples.rb
62
+ - spec/handler_spec.rb
63
+ - spec/helpers/dumps.rb
64
+ - spec/pcap_spec.rb
65
+ - spec/spec_helper.rb
66
+ - tasks/spec.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/postmodern/pcap-ffi
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ requirements: []
87
+
88
+ rubyforge_project: pcap-ffi
89
+ rubygems_version: 1.2.0
90
+ signing_key:
91
+ specification_version: 2
92
+ summary: FFI bindings for libpcap
93
+ test_files:
94
+ - spec/pcap_spec.rb
95
+ - spec/data_link_spec.rb
96
+ - spec/error_buffer.rb
97
+ - spec/spec_helper.rb
98
+ - spec/handler_spec.rb
99
+ - spec/handler_examples.rb
100
+ - spec/helpers/dumps.rb
101
+ - examples/print_bytes.rb