File: test_pcaprub_unit.rb

package info (click to toggle)
ruby-pcaprub 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 244 kB
  • sloc: ansic: 846; ruby: 308; makefile: 2
file content (176 lines) | stat: -rw-r--r-- 4,047 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby

base = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
$:.unshift(File.join(File.dirname(base)))


require File.expand_path '../test_helper.rb', __FILE__

#
# Simple unit test, requires r00t.
#

class Pcap::UnitTest < Test::Unit::TestCase
  def test_version
    assert_equal(String, Pcap.version.class)
    # puts "Pcaprub version: #{Pcap.version}"
  end

  def test_lookupdev
    assert_equal(String, Pcap.lookupdev.class)
    # puts "Pcaprub default device: #{Pcap.lookupdev}"
  end

  def test_lookupnet
    dev = Pcap.lookupdev
    assert_equal(Array, Pcap.lookupnet(dev).class)
    net = Pcap.lookupnet(dev)
    assert net
    # puts "Pcaprub net (#{dev}): #{net[0]} #{[net[1]].pack("N").unpack("H*")[0]}"
  end

  def test_pcap_new
    o = Pcap.new
    assert_equal(Pcap, o.class)
  end

  def test_pcap_setfilter_bad
    e = nil
    o = Pcap.new
    begin
      o.setfilter("not ip")
    rescue ::Exception => e
    end

    assert_equal(e.class, PCAPRUB::PCAPRUBError)
  end

  def test_pcap_setfilter
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 65535, true, 1)
    r = o.setfilter("not ip")
    assert_equal(Pcap, r.class)
  end

  def test_pcap_inject
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 65535, true, 1)
    r = o.inject("X" * 512)

    assert_equal(512, r)
    # UPDATE: TRAVIS CI is now on a new hardware platform.
    # Travis CI's virtual network interface does not support injection
    #if ENV['CI']
    #  assert_equal(-1,r)
    #else
    #  assert_equal(512, r)
    #end
  end

  def test_pcap_datalink
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 65535, true, 1)
    r = o.datalink
    assert_equal(Integer, r.class)
  end

  def test_pcap_snapshot
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 1344, true, 1)
    r = o.snapshot
    assert_equal(1344, r)
  end

  def test_pcap_stats
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 1344, true, 1)
    r = o.stats
    assert_equal(Hash, r.class)
  end

  def test_pcap_next
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 1344, true, 1)

    @c = 0
    t = Thread.new { while(true); @c += 1; select(nil, nil, nil, 0.10); end; }

    pkt_count = 0
    require 'timeout'
    begin
      Timeout.timeout(10) do
        o.each do |pkt|
          pkt_count += 1
        end
      end
    rescue ::Timeout::Error
    end

    t.kill
    # puts "Background thread ticked #{@c} times while capture was running"
    # puts "Captured #{pkt_count} packets"
    assert(0 < @c, "Background thread failed to tick while capture was running");
    true
  end

  def test_create_from_primitives
    d = Pcap.lookupdev
    o = Pcap.create(d).setsnaplen(65535).settimeout(100).setpromisc(true)
    assert_equal(o, o.activate)
    o.close
  end

  def test_set_datalink
    d = Pcap.lookupdev
    o = Pcap.open_live(d, 65535, true, 1)
    dls = o.listdatalinks
    begin
      assert_equal(o,o.setdatalink(dls.values.first))
    rescue PCAPRUB::LinkTypeError
      print "#{dls} - Data LinkType Binding issue in the environment (Skipping)"
      assert_equal(o,o)
    end
  end

  def test_monitor
    return if RUBY_PLATFORM =~ /mingw/
    d = Pcap.lookupdev
    o = Pcap.create(d)
    assert_equal(o, o.setmonitor(true))
  end

  def test_open_dead
    # No applied filters on OPEN_DEAD just compile checking
    o = Pcap.open_dead(Pcap::DLT_NULL, 65535)
    assert_nothing_raised do
      o.compile("ip host 1.2.3.4")
    end
    assert_raise PCAPRUB::BPFError do
      o.setfilter("ip host 1.2.3.5")
    end
  end

  def test_filter
    d = Pcap.lookupdev
    o = Pcap.create(d)
    o.activate
    assert_nothing_raised do
      o.compile("ip host 1.2.3.4")
    end
    assert_raise PCAPRUB::BPFError do
      o.compile("A non working filter")
    end
  end

  def test_lib_version
    v = Pcap.lib_version.split
    if RUBY_PLATFORM =~ /mingw/
      winpcap = 'WinPcap' == v[0]
      npcap = 'Npcap' == v[0]
      assert winpcap || npcap
    else
      assert_equal "libpcap", v[0]
    end
    assert_equal "version", v[1]
  end
end