Exemplo n.º 1
0
def compare_caps(output, golden, max_diff_sec=0.000005):
    pkts1 = []
    pkts2 = []
    pkts_ts_buckets = defaultdict(list)

    for pkt in RawPcapReader(output):
        ts = pkt[1][0] * 1e6 + pkt[1][1]
        pkts_ts_buckets[ts].append(pkt)
    # don't take last ts bucket, it can be cut in middle and packets inside bucket might be different
    #for ts in sorted(pkts_ts_buckets.keys())[:-1]:
    for ts in sorted(pkts_ts_buckets.keys()):
        pkts1.extend(sorted(pkts_ts_buckets[ts]))
    pkts_ts_buckets.clear()

    for pkt in RawPcapReader(golden):
        ts = pkt[1][0] * 1e6 + pkt[1][1]
        pkts_ts_buckets[ts].append(pkt)
    # don't take last ts bucket, it can be cut in middle and packets inside bucket might be different
    #for ts in sorted(pkts_ts_buckets.keys())[:-1]:
    for ts in sorted(pkts_ts_buckets.keys()):
        pkts2.extend(sorted(pkts_ts_buckets[ts]))

    assert_equal(
        len(pkts1), len(pkts2),
        'Lengths of generated pcap (%s) and golden (%s) are different' %
        (output, golden))

    for pkt1, pkt2, i in zip(pkts1, pkts2, range(1, len(pkts1))):
        ts1 = float(pkt1[1][0]) + (float(pkt1[1][1]) / 1e6)
        ts2 = float(pkt2[1][0]) + (float(pkt2[1][1]) / 1e6)

        if abs(ts1 - ts2) > max_diff_sec:  # 5 nsec
            raise AssertionError(
                "TS error: cap files '{0}', '{1}' differ in cap #{2} - '{3}' vs. '{4}'"
                .format(output, golden, i, ts1, ts2))

        if pkt1[0] != pkt2[0]:
            errmsg = "RAW error: output file '{0}', differs from golden '{1}' in cap #{2}".format(
                output, golden, i)
            print(errmsg)

            print(
                format_text("\ndifferent fields for packet #{0}:".format(i),
                            'underline'))

            scapy_pkt1_info = scapy_pkt_show_to_str(Ether(pkt1[0])).split('\n')
            scapy_pkt2_info = scapy_pkt_show_to_str(Ether(pkt2[0])).split('\n')

            print(format_text("\nGot:\n", 'bold', 'underline'))
            for line, ref in zip(scapy_pkt1_info, scapy_pkt2_info):
                if line != ref:
                    print(format_text(line, 'bold'))

            print(format_text("\nExpected:\n", 'bold', 'underline'))
            for line, ref in zip(scapy_pkt2_info, scapy_pkt1_info):
                if line != ref:
                    print(format_text(line, 'bold'))

            print("\n")
            raise AssertionError(errmsg)
Exemplo n.º 2
0
def compare_caps(cap1, cap2, max_diff_sec=(5 * 1e-6)):
    pkts1 = list(RawPcapReader(cap1))
    pkts2 = list(RawPcapReader(cap2))

    if len(pkts1) != len(pkts2):
        print(('{0} contains {1} packets vs. {2} contains {3} packets'.format(
            cap1, len(pkts1), cap2, len(pkts2))))
        return False

    # to be less strict we define equality if all packets from cap1 exists and in cap2
    # and vice versa
    # 'exists' means the same packet with abs(TS1-TS2) < 5nsec
    # its O(n^2) but who cares, right ?
    for i, pkt1 in enumerate(pkts1):
        ts1 = float(pkt1[1][0]) + (float(pkt1[1][1]) / 1e6)
        found = None
        for j, pkt2 in enumerate(pkts2):
            ts2 = float(pkt2[1][0]) + (float(pkt2[1][1]) / 1e6)

            if abs(ts1 - ts2) > max_diff_sec:
                break

            if pkt1[0] == pkt2[0]:
                found = j
                break

        if found is None:
            print((format_text(
                "cannot find packet #{0} from {1} in {2}\n".format(
                    i, cap1, cap2), 'bold')))
            return False
        else:
            del pkts2[found]

    return True
Exemplo n.º 3
0
def compare_caps_strict (cap1, cap2, max_diff_sec = (5 * 1e-6)):
    pkts1 = list(RawPcapReader(cap1))
    pkts2 = list(RawPcapReader(cap2))

    if len(pkts1) != len(pkts2):
        print('{0} contains {1} packets vs. {1} contains {2} packets'.format(cap1, len(pkts1), cap2, len(pkts2)))
        return False

    # a strict check
    for pkt1, pkt2, i in zip(pkts1, pkts2, range(1, len(pkts1))):
        ts1 = float(pkt1[1][0]) + (float(pkt1[1][1]) / 1e6)
        ts2 = float(pkt2[1][0]) + (float(pkt2[1][1]) / 1e6)

        if abs(ts1-ts2) > 0.000005: # 5 nsec
            print(format_text("TS error: cap files '{0}', '{1}' differ in cap #{2} - '{3}' vs. '{4}'\n".format(cap1, cap2, i, ts1, ts2), 'bold'))
            return False

        if pkt1[0] != pkt2[0]:
            print(format_text("RAW error: cap files '{0}', '{1}' differ in cap #{2}\n".format(cap1, cap2, i), 'bold'))
            
            diff_list = hexdiff(pkt1[0], pkt2[0])

            print("{0} - packet #{1}:\n".format(cap1, i))
            prettyhex(pkt1[0], diff_list)
            
            print("\n{0} - packet #{1}:\n".format(cap2, i))
            prettyhex(pkt2[0], diff_list)

            print("")
            return False

    return True