File: idb_spec.rb

package info (click to toggle)
ruby-packetfu 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,520 kB
  • sloc: ruby: 8,344; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,868 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
# -*- coding: binary -*-
require 'spec_helper'
require 'packetfu'

module PacketFu
  module PcapNG
    describe IDB do
      before(:each) { @idb = IDB.new }

      it 'should have correct initialization values' do
        expect(@idb).to be_a(IDB)
        expect(@idb.endian).to eq(:little)
        expect(@idb.type.to_i).to eq(PcapNG::IDB_TYPE.to_i)
        expect(@idb.link_type.to_i).to eq(PcapNG::LINKTYPE_ETHERNET)
        expect(@idb.snaplen.to_i).to eq(0)
        expect(@idb.block_len.to_i).to eq(IDB::MIN_SIZE)
        expect(@idb.block_len2).to eq(@idb.block_len)
      end

      it 'should decode tsresol on demand from its options' do
        @idb.options.read [9, 1, 4].pack('vvC')
        expect(@idb.ts_resol).to eq(1E-4)

        @idb.options.read [9, 1, 0x83].pack('vvC')
        expect(@idb.ts_resol(true)).to eq(2**-3)
      end

      context 'when reading' do
        it 'should accept a String' do
          str = ::File.read(::File.join(__dir__, '../..', 'test', 'sample.pcapng'))[52, 32]
          expect { @idb.read(str) }.to_not raise_error
          expect(@idb.type.to_i).to eq(PcapNG::IDB_TYPE.to_i)
          expect(@idb.block_len.to_i).to eq(32)
          expect(@idb.link_type.to_i).to eq(PcapNG::LINKTYPE_ETHERNET)
          expect(@idb.snaplen.to_i).to eq(0xffff)
          expect(@idb.has_options?).to be(true)
        end

        it 'should accept an IO' do
          ::File.open(::File.join(__dir__, '../..', 'test', 'sample.pcapng')) do |f|
            f.seek(52, :CUR)
            @idb.read f
          end
          expect(@idb.type.to_i).to eq(PcapNG::IDB_TYPE.to_i)
          expect(@idb.block_len.to_i).to eq(32)
          expect(@idb.link_type.to_i).to eq(PcapNG::LINKTYPE_ETHERNET)
          expect(@idb.snaplen.to_i).to eq(0xffff)
          expect(@idb.has_options?).to be(true)
        end
      end
    end
  end
end