def test_pcap2ammo_output_file(self, prepare_data_file, capsys): """Check pcap2ammo write result in output file correctly""" http_request = "GET https://rambler.ru/ HTTP/1.1\r\n" + \ "Host: rambler.ru\r\n" + \ "Content-Length: 0\r\n\r\n" ethernet = pcap_gen.generate_custom_http_request_packet(http_request) data = [{'timestamp': 1489136209.000001, 'data': ethernet.__bytes__()}] filename = prepare_data_file(data) output_filename = tempfile.NamedTemporaryFile(delete=False).name pcap2ammo.pcap2ammo({ 'input': filename, 'output': output_filename, 'stats_only': False, 'add_header': [], 'delete_header': [], 'filter': None, 'http_filter': None }) captured = capsys.readouterr() assert captured.out == "", "output is not empty" file_content = open(output_filename, 'rb').read().decode("utf-8") os.remove(output_filename) assert file_content == \ str(len(http_request)) + " \n" + \ http_request, "unexpected output file content"
def test_pcap2ammo_delete_header(self, prepare_data_file, capsys): """Check delete-header argument works correctly""" http_request = "GET https://rambler.ru/ HTTP/1.1\r\n" + \ "Host: rambler.ru\r\n" + \ "Content-Length: 0\r\n\r\n" http_request_without_header = \ "GET https://rambler.ru/ HTTP/1.1\r\n" + \ "Host: rambler.ru\r\n\r\n" ethernet = pcap_gen.generate_custom_http_request_packet(http_request) data = [{'timestamp': 1489136209.000001, 'data': ethernet.__bytes__()}] filename = prepare_data_file(data) pcap2ammo.pcap2ammo({ 'input': filename, 'output': None, 'stats_only': False, 'add_header': [], 'delete_header': ['content-length'], 'filter': None, 'http_filter': None, }) captured = capsys.readouterr() assert captured.out == \ str(len(http_request_without_header)) + " \n" + \ http_request_without_header, "unexpected output"
def test_pcap2ammo_http_filter(self, prepare_data_file, capsys): """Check main function parse input file with filter correctly""" http_request = "GET / HTTP/1.1\r\n" + \ "Host: rambler.ru\r\n" + \ "Content-Length: 0\r\n\r\n" params = {'ip': {'src': socket.inet_aton('10.4.0.136')}} ethernet = pcap_gen.generate_custom_http_request_packet( http_request, params) data = [{'timestamp': 1489136209.000001, 'data': ethernet.__bytes__()}] filename = prepare_data_file(data) # match filter pcap2ammo.pcap2ammo({ 'input': filename, 'output': False, 'stats_only': False, 'add_header': [], 'delete_header': [], 'filter': None, 'http_filter': '"rambler.ru" == http.headers["host"]' }) captured = capsys.readouterr() assert captured.out == \ str(len(http_request)) + " \n" + \ http_request, "unexpected output" # unmatch filter pcap2ammo.pcap2ammo({ 'input': filename, 'output': False, 'stats_only': False, 'add_header': [], 'delete_header': [], 'filter': None, 'http_filter': '"rambler.ru" != http.headers["host"]' }) captured = capsys.readouterr() assert captured.out == "", "unexpected output"
def test_pcap2ammo_empty_input_file(self, capsys): """Check empty input filename""" pcap2ammo.pcap2ammo({ 'input': None, 'output': False, 'stats_only': False, 'add_header': [], 'delete_header': [], 'filter': None, 'http_filter': None }) captured = capsys.readouterr() assert captured.err == \ "Error: input filename is not specified or empty\n", \ "unexpected output"
def test_pcap2ammo_stats_only(self, prepare_data_file, capsys): """Check stats-only flag handled correctly""" http_request = "GET https://rambler.ru/ HTTP/1.1\r\n" + \ "Host: rambler.ru\r\n" + \ "Content-Length: 0\r\n\r\n" ethernet = pcap_gen.generate_custom_http_request_packet(http_request) data = [{'timestamp': 1489136209.000001, 'data': ethernet.__bytes__()}] filename = prepare_data_file(data) pcap2ammo.pcap2ammo({ 'input': filename, 'output': False, 'stats_only': True, 'add_header': [], 'delete_header': [], 'filter': None, 'http_filter': None }) captured = capsys.readouterr() assert captured.out == \ "Stats:\n\ttotal: 1\n\tcomplete: 1\n\t" + \ "incorrect: 0\n\tincomplete: 0\n", "unexpected output"
def test_pcap2ammo_add_header_wrong_format(self, prepare_data_file, capsys): """Check wrong formated add-header argument is handled correctly""" http_request = "GET https://rambler.ru/ HTTP/1.1\r\n" + \ "Host: rambler.ru\r\n" + \ "Content-Length: 0\r\n\r\n" ethernet = pcap_gen.generate_custom_http_request_packet(http_request) data = [{'timestamp': 1489136209.000001, 'data': ethernet.__bytes__()}] filename = prepare_data_file(data) pcap2ammo.pcap2ammo({ 'input': filename, 'output': False, 'stats_only': False, 'add_header': ['Referer'], 'delete_header': [], 'filter': None, }) captured = capsys.readouterr() assert captured.err == \ 'Error: Wrong header format, expected ' + \ "\"<header_name>: <header_value>\"\n", \ "unexpected output"