|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" Test for dji-firmware-tools, comm_dissector scripts. |
| 4 | +
|
| 5 | + This test verifies functions of the script by using |
| 6 | + `tshark`, command line Wireshark utility. |
| 7 | +""" |
| 8 | + |
| 9 | +# Copyright (C) 2023 Mefistotelis <mefistotelis@gmail.com> |
| 10 | +# Copyright (C) 2023 Original Gangsters <https://dji-rev.slack.com/> |
| 11 | +# |
| 12 | +# This program is free software: you can redistribute it and/or modify |
| 13 | +# it under the terms of the GNU General Public License as published by |
| 14 | +# the Free Software Foundation, either version 3 of the License, or |
| 15 | +# (at your option) any later version. |
| 16 | +# |
| 17 | +# This program is distributed in the hope that it will be useful, |
| 18 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +# GNU General Public License for more details. |
| 21 | +# |
| 22 | +# You should have received a copy of the GNU General Public License |
| 23 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + |
| 25 | +pckt_mavic_air_fcc = [0x55, 0x17, 0x04, 0x38, 0x02, 0x0e, 0x1d, 0x00, 0x40, 0x07, 0x30, 0x55, 0x53, 0x00, 0x00, 0x55, 0x53, 0x00, 0x00, 0x01, 0x00, 0xfe, 0x5d] |
| 26 | +pckt_mini_2_fcc = [0x55, 0x18, 0x04, 0x20, 0x02, 0x09, 0x00, 0x00, 0x40, 0x09, 0x27, 0x00, 0x02, 0x48, 0x00, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x81, 0x1f] |
| 27 | + |
| 28 | +import logging |
| 29 | +import subprocess |
| 30 | +import os |
| 31 | +import re |
| 32 | +import sys |
| 33 | +import struct |
| 34 | +import time |
| 35 | +import datetime |
| 36 | +import pytest |
| 37 | + |
| 38 | +import array |
| 39 | + |
| 40 | +LOGGER = logging.getLogger(__name__) |
| 41 | + |
| 42 | +def write_one_packet_pcap(fname, userdlt, data): |
| 43 | + with open(fname, "wb") as out: |
| 44 | + out.write(struct.pack("=IHHiIII", |
| 45 | + 0xa1b2c3d4, # magic number |
| 46 | + 2, # major version number |
| 47 | + 4, # minor version number |
| 48 | + 0, # GMT to local correction |
| 49 | + 0, # accuracy of timestamps |
| 50 | + 65535, # max length of captured packets, in octets |
| 51 | + 147+userdlt, # data link type (DLT) - USER_0 |
| 52 | + )) |
| 53 | + |
| 54 | + dtime = datetime.datetime.now() |
| 55 | + timestamp = int(time.mktime(dtime.timetuple())) |
| 56 | + out.write(struct.pack("=IIII", |
| 57 | + timestamp, # timestamp seconds |
| 58 | + dtime.microsecond, # timestamp microseconds |
| 59 | + len(data), # number of octets of packet saved in file |
| 60 | + len(data), # actual length of packet |
| 61 | + )) |
| 62 | + out.write(data) |
| 63 | + |
| 64 | +def run_comm_dissector_tshark_show(cmd, inp_file, proto_name, pkt_cmd, env=None): |
| 65 | + command = [cmd, '-r', inp_file, '-V'] |
| 66 | + LOGGER.info(' '.join(command)) |
| 67 | + prc_result = subprocess.run(command, env=env, capture_output=True) |
| 68 | + assert prc_result.returncode == 0 |
| 69 | + # Check if protocol name is found in tshark output (protocol was recognized) |
| 70 | + assert str(prc_result.stdout).find(proto_name) > 0 |
| 71 | + # Check if command from the packet is found in tshark output (packet header was dissected) |
| 72 | + match = re.search(r'\s+Cmd: [A-Za-z0-9 \//,.-]+ [\(]0x([0-9a-f]+)[\)]', str(prc_result.stdout), re.IGNORECASE) |
| 73 | + assert match |
| 74 | + assert int(match.group(1), 16) == pkt_cmd |
| 75 | + |
| 76 | +def case_comm_dissector_tshark_show(pcap_file, userdlt, proto_name, pckt, env=None): |
| 77 | + pkt_cmd = int(pckt[10]) |
| 78 | + write_one_packet_pcap(pcap_file, userdlt, bytearray(pckt)) |
| 79 | + run_comm_dissector_tshark_show("tshark", str(pcap_file), proto_name, pkt_cmd, env=env) |
| 80 | + |
| 81 | +# Make this a separate mark rather than comm, until our CI can execute this correctly |
| 82 | +@pytest.mark.commDISS |
| 83 | +def test_comm_dissector_tshark_show(tmp_path): |
| 84 | + """ Test dissecting a packet with Wiresharks tshark. |
| 85 | + """ |
| 86 | + test_env = os.environ.copy() |
| 87 | + case_comm_dissector_tshark_show(tmp_path / "dji-packet.pcap", 3, "DJI_DUMLv1", pckt_mavic_air_fcc, env=test_env) |
| 88 | + case_comm_dissector_tshark_show(tmp_path / "dji-packet.pcap", 3, "DJI_DUMLv1", pckt_mini_2_fcc, env=test_env) |
0 commit comments