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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
# -*- coding: binary -*-
require 'tempfile'
require 'spec_helper'
require 'packetfu'
require_relative 'file_spec_helper'
module PacketFu
module PcapNG
describe File do
before(:all) do
@file = ::File.join(__dir__, '../..', 'test', 'sample.pcapng')
@file_spb = ::File.join(__dir__, '../..', 'test', 'sample-spb.pcapng')
end
before(:each) { @pcapng = File.new }
context '#readfile' do
it 'reads a Pcap-NG file' do
@pcapng.readfile @file
expect(@pcapng.sections.size).to eq(1)
expect(@pcapng.sections[0].unknown_blocks.size).to eq(0)
expect(@pcapng.sections.first.interfaces.size).to eq(1)
intf = @pcapng.sections.first.interfaces.first
expect(intf.section).to eq(@pcapng.sections.first)
expect(intf.packets.size).to eq(11)
packet = intf.packets.first
expect(packet.interface).to eq(intf)
end
it 'reads a Pcap-NG file with Simple Packet blocks' do
@pcapng.readfile @file_spb
expect(@pcapng.sections.size).to eq(1)
expect(@pcapng.sections[0].unknown_blocks.size).to eq(0)
expect(@pcapng.sections.first.interfaces.size).to eq(1)
intf = @pcapng.sections.first.interfaces.first
expect(intf.section).to eq(@pcapng.sections.first)
expect(intf.packets.size).to eq(4)
expect(intf.snaplen.to_i).to eq(0)
packet = intf.packets.first
expect(packet.interface).to eq(intf)
expect(packet.data.size).to eq(packet.orig_len.to_i)
end
it 'yields xPB object per read packet' do
idx = 0
@pcapng.readfile(@file) do |pkt|
expect(pkt).to be_a(PcapNG::EPB)
idx += 1
end
expect(idx).to eq(11)
end
it 'reads many kinds of pcapng file' do
[:little, :big].each do |endian|
base_dir = ::File.join(__dir__, '..', '..', 'test', 'pcapng-test',
endian == :little ? 'output_le' : 'output_be')
PCAPNG_TEST_FILES.each do |file, sections|
next if file == 'difficult/test202.pcapng'
@pcapng.clear
@pcapng.readfile ::File.join(base_dir, file)
expect(@pcapng.sections[0].endian).to eq(endian)
expect(@pcapng.sections.size).to eq(sections.size)
sections.each_with_index do |section, i|
expect(@pcapng.sections[i].unknown_blocks.size).to eq(section[:unknown])
expect(@pcapng.sections[i].interfaces.size).to eq(section[:idb])
packets = @pcapng.sections[i].interfaces.map(&:packets).flatten
expect(packets.grep(EPB).size).to eq(section[:epb])
expect(packets.grep(SPB).size).to eq(section[:spb])
end
end
end
end
it 'reads a file with different sections, with different endians' do
sections = PCAPNG_TEST_FILES['difficult/test202.pcapng']
[:little, :big].each do |endian|
other_endian = endian == :little ? :big : :little
base_dir = ::File.join(__dir__, '..', '..', 'test', 'pcapng-test',
endian == :little ? 'output_le' : 'output_be')
@pcapng.clear
@pcapng.readfile ::File.join(base_dir, 'difficult', 'test202.pcapng')
expect(@pcapng.sections.size).to eq(sections.size)
expect(@pcapng.sections[0].endian).to eq(endian)
expect(@pcapng.sections[1].endian).to eq(other_endian)
expect(@pcapng.sections[2].endian).to eq(endian)
sections.each_with_index do |section, i|
expect(@pcapng.sections[i].unknown_blocks.size).to eq(section[:unknown])
expect(@pcapng.sections[i].interfaces.size).to eq(section[:idb])
@pcapng.sections[i].unknown_blocks.each do |block|
expect(block.endian).to eq(@pcapng.sections[i].endian)
end
@pcapng.sections[i].interfaces.each do |interface|
expect(interface.endian).to eq(@pcapng.sections[i].endian)
interface.packets.each do |packet|
expect(packet.endian).to eq(@pcapng.sections[i].endian)
end
end
packets = @pcapng.sections[i].interfaces.map(&:packets).flatten
expect(packets.grep(EPB).size).to eq(section[:epb])
expect(packets.grep(SPB).size).to eq(section[:spb])
end
end
end
end
context '#read_packets' do
before(:all) do
@expected = [UDPPacket] * 2 + [ICMPPacket] * 3 + [ARPPacket] * 2 +
[TCPPacket] * 3 + [ICMPPacket]
end
it 'returns an array of Packets' do
packets = @pcapng.read_packets(@file)
expect(packets.map(&:class)).to eq(@expected)
icmp = packets[2]
expect(icmp.ip_saddr).to eq('192.168.1.105')
expect(icmp.ip_daddr).to eq('216.75.1.230')
expect(icmp.icmp_type).to eq(8)
expect(icmp.icmp_code).to eq(0)
end
it 'yields Packet object per read packet' do
idx = 0
@pcapng.read_packets(@file) do |pkt|
expect(pkt).to be_a(@expected[idx])
idx += 1
end
expect(idx).to eq(11)
end
end
context '#file_to_array' do
it 'generates an array from object state' do
@pcapng.readfile @file
ary = @pcapng.file_to_array
expect(ary).to be_a(Array)
ary.each do |p|
expect(p).to be_a(String)
end
expect(ary[0]).to eq(@pcapng.sections[0].interfaces[0].packets[0].data)
end
it 'generates an array from given file, clearing object state' do
@pcapng.readfile @file
ary = @pcapng.file_to_array(filename: @file_spb)
expect(@pcapng.sections.size).to eq(1)
expect(@pcapng.sections[0].interfaces[0].packets[0]).to be_a(SPB)
expect(ary).to be_a(Array)
ary.each do |p|
expect(p).to be_a(String)
end
expect(ary[0]).to eq(@pcapng.sections[0].interfaces[0].packets[0].data)
end
it 'generates an array with timestamps' do
@pcapng.readfile @file
ary = @pcapng.file_to_array(keep_timestamps: true)
expect(ary).to be_a(Array)
ary.each do |p|
expect(p).to be_a(Hash)
expect(p.keys.first).to be_a(Time)
expect(p.values.first).to be_a(String)
end
packet1 = @pcapng.sections[0].interfaces[0].packets[0]
expect(ary[0].keys.first).to eq(packet1.timestamp)
expect(ary[0].values.first).to eq(packet1.data)
end
end
context '#to_file' do
before(:each) { @write_file = Tempfile.new('pcapng') }
after(:each) { @write_file.close; @write_file.unlink }
it 'creates a file and write self to it' do
@pcapng.readfile @file
@pcapng.to_file :filename => @write_file.path
@write_file.rewind
expect(@write_file.read).to eq(::File.read(@file))
end
it 'appends a section to an existing file' do
@pcapng.readfile @file
@pcapng.to_file :filename => @write_file.path
@pcapng.to_file :filename => @write_file.path, :append => true
@pcapng.clear
@pcapng.readfile @write_file.path
expect(@pcapng.sections.size).to eq(2)
expect(@pcapng.sections[0].to_s).to eq(@pcapng.sections[1].to_s)
end
end
context '#array_to_file' do
before(:each) do
tmpfile = Tempfile.new('packetfu')
@tmpfilename = tmpfile.path
tmpfile.close
tmpfile.unlink
end
after(:each) { ::File.unlink @tmpfilename if ::File.exist? @tmpfilename }
it 'gets an array of Packet objects' do
packets = @pcapng.read_packets(@file)
@pcapng.clear
@pcapng.array_to_file(packets)
@pcapng.write @tmpfilename
@pcapng.clear
packets2 = @pcapng.read_packets(@tmpfilename)
expect(packets2.map(&:to_s).join).to eq(packets.map(&:to_s).join)
end
it 'gets a hash containing an array of Packet objects' do
packets = @pcapng.read_packets(@file)[0..1]
@pcapng.clear
@pcapng.array_to_file(:array => packets)
@pcapng.write @tmpfilename
@pcapng.clear
packets2 = @pcapng.read_packets(@tmpfilename)
expect(packets2.map(&:to_s).join).to eq(packets.map(&:to_s).join)
end
it 'gets a hash containing an array of Packet objects and a :timestamp key' do
packets = @pcapng.read_packets(@file)[0..1]
@pcapng.clear
@pcapng.array_to_file(:array => packets,
:timestamp => Time.utc(2000, 1, 1),
:ts_inc => 3600*24)
@pcapng.write @tmpfilename
@pcapng.clear
@pcapng.readfile(@tmpfilename)
@pcapng.sections[0].interfaces[0].packets.each_with_index do |pkt, i|
expect(pkt.data).to eq(packets[i].to_s)
expect(pkt.timestamp).to eq(Time.utc(2000, 1, 1+i))
end
end
it 'gets a hash containing couples of Time and Packet objects' do
packets = @pcapng.read_packets(@file)[0..3]
timestamp = Time.utc(2000, 1, 1)
ts_inc = 3600*24 * 2
array = []
packets.each_with_index do |pkt, i|
array << { (timestamp + ts_inc * i) => pkt }
end
@pcapng.clear
@pcapng.array_to_file(:array => array)
@pcapng.write @tmpfilename
@pcapng.clear
@pcapng.readfile(@tmpfilename)
@pcapng.sections[0].interfaces[0].packets.each_with_index do |pkt, i|
expect(pkt.data).to eq(packets[i].to_s)
expect(pkt.timestamp).to eq(Time.utc(2000, 1, 1+2*i))
end
end
it 'gets a hash containing a :filename key' do
packets = @pcapng.read_packets(@file)[0..2]
@pcapng.clear
@pcapng.array_to_file(:array => packets, :filename => @tmpfilename)
@pcapng.clear
packets2 = @pcapng.read_packets(@tmpfilename)
expect(packets2.map(&:to_s).join).to eq(packets.map(&:to_s).join)
end
end
it '#to_s returns object as a String' do
orig_str = PacketFu.force_binary(::File.read(@file))
@pcapng.read orig_str
expect(@pcapng.to_s).to eq(orig_str)
@pcapng.clear
orig_str = PacketFu.force_binary(::File.read(@file_spb))
@pcapng.read orig_str
expect(@pcapng.to_s).to eq(orig_str)
end
end
end
end
|