test_creator.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/python
  2. # Copyright 2012 Google, Inc. All rights reserved.
  3. """TestCreator creates test templates from pcap files."""
  4. import argparse
  5. import base64
  6. import glob
  7. import re
  8. import string
  9. import subprocess
  10. import sys
  11. class Packet(object):
  12. """Helper class encapsulating packet from a pcap file."""
  13. def __init__(self, packet_lines):
  14. self.packet_lines = packet_lines
  15. self.data = self._DecodeText(packet_lines)
  16. @classmethod
  17. def _DecodeText(cls, packet_lines):
  18. packet_bytes = []
  19. # First line is timestamp and stuff, skip it.
  20. # Format: 0x0010: 0000 0020 3aff 3ffe 0000 0000 0000 0000 ....:.?.........
  21. for line in packet_lines[1:]:
  22. m = re.match(r'\s+0x[a-f\d]+:\s+((?:[\da-f]{2,4}\s)*)', line, re.IGNORECASE)
  23. if m is None: continue
  24. for hexpart in m.group(1).split():
  25. packet_bytes.append(base64.b16decode(hexpart.upper()))
  26. return ''.join(packet_bytes)
  27. def Test(self, name, link_type):
  28. """Yields a test using this packet, as a set of lines."""
  29. yield '// testPacket%s is the packet:' % name
  30. for line in self.packet_lines:
  31. yield '// ' + line
  32. yield 'var testPacket%s = []byte{' % name
  33. data = list(self.data)
  34. while data:
  35. linebytes, data = data[:16], data[16:]
  36. yield ''.join(['\t'] + ['0x%02x, ' % ord(c) for c in linebytes])
  37. yield '}'
  38. yield 'func TestPacket%s(t *testing.T) {' % name
  39. yield '\tp := gopacket.NewPacket(testPacket%s, LinkType%s, gopacket.Default)' % (name, link_type)
  40. yield '\tif p.ErrorLayer() != nil {'
  41. yield '\t\tt.Error("Failed to decode packet:", p.ErrorLayer().Error())'
  42. yield '\t}'
  43. yield '\tcheckLayers(p, []gopacket.LayerType{LayerType%s, FILL_ME_IN_WITH_ACTUAL_LAYERS}, t)' % link_type
  44. yield '}'
  45. yield 'func BenchmarkDecodePacket%s(b *testing.B) {' % name
  46. yield '\tfor i := 0; i < b.N; i++ {'
  47. yield '\t\tgopacket.NewPacket(testPacket%s, LinkType%s, gopacket.NoCopy)' % (name, link_type)
  48. yield '\t}'
  49. yield '}'
  50. def GetTcpdumpOutput(filename):
  51. """Runs tcpdump on the given file, returning output as string."""
  52. return subprocess.check_output(
  53. ['tcpdump', '-XX', '-s', '0', '-n', '-r', filename])
  54. def TcpdumpOutputToPackets(output):
  55. """Reads a pcap file with TCPDump, yielding Packet objects."""
  56. pdata = []
  57. for line in output.splitlines():
  58. if line[0] not in string.whitespace and pdata:
  59. yield Packet(pdata)
  60. pdata = []
  61. pdata.append(line)
  62. if pdata:
  63. yield Packet(pdata)
  64. def main():
  65. class CustomHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
  66. def _format_usage(self, usage, actions, groups, prefix=None):
  67. header =('TestCreator creates gopacket tests using a pcap file.\n\n'
  68. 'Tests are written to standard out... they can then be \n'
  69. 'copied into the file of your choice and modified as \n'
  70. 'you see.\n\n')
  71. return header + argparse.ArgumentDefaultsHelpFormatter._format_usage(
  72. self, usage, actions, groups, prefix)
  73. parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
  74. parser.add_argument('--link_type', default='Ethernet', help='the link type (default: %(default)s)')
  75. parser.add_argument('--name', default='Packet%d', help='the layer type, must have "%d" inside it')
  76. parser.add_argument('files', metavar='file.pcap', type=str, nargs='+', help='the files to process')
  77. args = parser.parse_args()
  78. for arg in args.files:
  79. for path in glob.glob(arg):
  80. for i, packet in enumerate(TcpdumpOutputToPackets(GetTcpdumpOutput(path))):
  81. print '\n'.join(packet.Test(
  82. args.name % i, args.link_type))
  83. if __name__ == '__main__':
  84. main()