#!/usr/bin/python
# Copyright 2012 Google, Inc. All rights reserved.
"""TestCreator creates test templates from pcap files."""
import argparse
import base64
import glob
import re
import string
import subprocess
import sys
class Packet(object):
"""Helper class encapsulating packet from a pcap file."""
def __init__(self, packet_lines):
self.packet_lines = packet_lines
self.data = self._DecodeText(packet_lines)
@classmethod
def _DecodeText(cls, packet_lines):
packet_bytes = []
# First line is timestamp and stuff, skip it.
# Format: 0x0010: 0000 0020 3aff 3ffe 0000 0000 0000 0000 ....:.?.........
for line in packet_lines[1:]:
m = re.match(r'\s+0x[a-f\d]+:\s+((?:[\da-f]{2,4}\s)*)', line, re.IGNORECASE)
if m is None: continue
for hexpart in m.group(1).split():
packet_bytes.append(base64.b16decode(hexpart.upper()))
return ''.join(packet_bytes)
def Test(self, name, link_type):
"""Yields a test using this packet, as a set of lines."""
yield '// testPacket%s is the packet:' % name
for line in self.packet_lines:
yield '// ' + line
yield 'var testPacket%s = []byte{' % name
data = list(self.data)
while data:
linebytes, data = data[:16], data[16:]
yield ''.join(['\t'] + ['0x%02x, ' % ord(c) for c in linebytes])
yield '}'
yield 'func TestPacket%s(t *testing.T) {' % name
yield '\tp := gopacket.NewPacket(testPacket%s, LinkType%s, gopacket.Default)' % (name, link_type)
yield '\tif p.ErrorLayer() != nil {'
yield '\t\tt.Error("Failed to decode packet:", p.ErrorLayer().Error())'
yield '\t}'
yield '\tcheckLayers(p, []gopacket.LayerType{LayerType%s, FILL_ME_IN_WITH_ACTUAL_LAYERS}, t)' % link_type
yield '}'
yield 'func BenchmarkDecodePacket%s(b *testing.B) {' % name
yield '\tfor i := 0; i < b.N; i++ {'
yield '\t\tgopacket.NewPacket(testPacket%s, LinkType%s, gopacket.NoCopy)' % (name, link_type)
yield '\t}'
yield '}'
def GetTcpdumpOutput(filename):
"""Runs tcpdump on the given file, returning output as string."""
return subprocess.check_output(
['tcpdump', '-XX', '-s', '0', '-n', '-r', filename])
def TcpdumpOutputToPackets(output):
"""Reads a pcap file with TCPDump, yielding Packet objects."""
pdata = []
for line in output.splitlines():
if line[0] not in string.whitespace and pdata:
yield Packet(pdata)
pdata = []
pdata.append(line)
if pdata:
yield Packet(pdata)
def main():
class CustomHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
def _format_usage(self, usage, actions, groups, prefix=None):
header =('TestCreator creates gopacket tests using a pcap file.\n\n'
'Tests are written to standard out... they can then be \n'
'copied into the file of your choice and modified as \n'
'you see.\n\n')
return header + argparse.ArgumentDefaultsHelpFormatter._format_usage(
self, usage, actions, groups, prefix)
parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
parser.add_argument('--link_type', default='Ethernet', help='the link type (default: %(default)s)')
parser.add_argument('--name', default='Packet%d', help='the layer type, must have "%d" inside it')
parser.add_argument('files', metavar='file.pcap', type=str, nargs='+', help='the files to process')
args = parser.parse_args()
for arg in args.files:
for path in glob.glob(arg):
for i, packet in enumerate(TcpdumpOutputToPackets(GetTcpdumpOutput(path))):
print '\n'.join(packet.Test(
args.name % i, args.link_type))
if __name__ == '__main__':
main()
没有合适的资源?快使用搜索试试~ 我知道了~
为Go提供包处理能力_Go_下载.zip
共293个文件
go:212个
pcapng:52个
sh:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 46 浏览量
2023-04-26
10:48:54
上传
评论
收藏 1.05MB ZIP 举报
温馨提示
为Go提供包处理能力_Go_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
为Go提供包处理能力_Go_下载.zip (293个子文件)
27d23183d8ce7b719228870c23869cf21bf8829d7160c82da88f80aeff2d861c 74B
3b53f220d321f20980b59f64e1617d6b334b152a90b141a7407c3c8fa4837a31 43B
AUTHORS 2KB
f539b7a397cf68c2129abd63d4c7cfb1979c489ad9bad6898510a4d4759bb85f 78B
fe20300d6b2057b406a06ec4f04065f6d0dda6b2b362c0a89192c1933e927adf 55B
gc 6KB
.gitignore 591B
valid_mac_prefixes.go 1.37MB
iana_ports.go 230KB
sflow.go 98KB
defrag_test.go 81KB
dot11.go 74KB
sflow_test.go 74KB
decode_test.go 62KB
dns_test.go 61KB
tcpassembly_test.go 57KB
ngread_test.go 50KB
lldp.go 47KB
ospf_test.go 45KB
tcpassembly.go 38KB
dns.go 33KB
enums.go 30KB
radiotap.go 30KB
dot11_test.go 27KB
packet.go 26KB
assembly.go 26KB
ndp.go 26KB
pcap.go 26KB
layertypes.go 25KB
pcap_windows.go 25KB
sctp.go 22KB
radius.go 22KB
ospf.go 21KB
ngread.go 20KB
ip6.go 19KB
mldv2.go 19KB
cdp.go 19KB
pcap_unix.go 19KB
main.go 19KB
bfd.go 19KB
dhcpv6_options.go 18KB
doc.go 18KB
ntp.go 18KB
afpacket.go 17KB
icmp6msg.go 17KB
dhcpv4.go 17KB
udp_test.go 16KB
tls_test.go 16KB
radius_test.go 16KB
tcpcheck_test.go 16KB
ip6_test.go 15KB
routing_test.go 15KB
pfring.go 13KB
gre_test.go 13KB
sip.go 13KB
enums_generated.go 13KB
igmp.go 12KB
parser.go 12KB
sip_test.go 12KB
ngwrite.go 12KB
bfd_test.go 11KB
assembly_test.go 11KB
defrag.go 10KB
tcp.go 9KB
dhcpv6.go 9KB
writer.go 9KB
ntp_test.go 9KB
capture.go 9KB
ip4.go 8KB
eapol.go 8KB
pcapng.go 8KB
read.go 8KB
icmp6.go 8KB
usb.go 8KB
icmp4.go 8KB
ipsec_test.go 8KB
benchmark.go 8KB
prism_test.go 8KB
main.go 8KB
flows.go 8KB
pcap_test.go 8KB
main.go 7KB
igmp_test.go 7KB
asf_presencepong.go 7KB
read_test.go 7KB
geneve_test.go 7KB
tls.go 7KB
bsd_bpf_sniffer.go 7KB
main.go 6KB
reader.go 6KB
routing.go 6KB
mldv1_test.go 6KB
options.go 6KB
dhcp_test.go 6KB
gtp_test.go 6KB
ngwrite_test.go 6KB
decode.go 6KB
header.go 6KB
lcm.go 6KB
ip4_test.go 6KB
共 293 条
- 1
- 2
- 3
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功