1176043Ssilby# Copyright (C) 2008 Michael J. Silbersack.  All rights reserved.
2176043Ssilby#
3176043Ssilby# Redistribution and use in source and binary forms, with or without
4176043Ssilby# modification, are permitted provided that the following conditions
5176043Ssilby# are met:
6176043Ssilby# 1. Redistributions of source code must retain the above copyright
7176043Ssilby#    notice unmodified, this list of conditions, and the following
8176043Ssilby#    disclaimer.
9176043Ssilby# 2. Redistributions in binary form must reproduce the above copyright
10176043Ssilby#    notice, this list of conditions and the following disclaimer in the
11176043Ssilby#    documentation and/or other materials provided with the distribution.
12176043Ssilby#
13176043Ssilby# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14176043Ssilby# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15176043Ssilby# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16176043Ssilby# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17176043Ssilby# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18176043Ssilby# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19176043Ssilby# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20176043Ssilby# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21176043Ssilby# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22176043Ssilby# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23176043Ssilby#
24176043Ssilby# $FreeBSD$
25176043Ssilby#
26176043Ssilby# This is a regression test to verify the proper behavior of IP ID generation
27176043Ssilby# code.  It will push 200000 packets, then report back what the min and max
28176043Ssilby# periods it saw for different IDs were.
29176043Ssilby
30176043Ssilbyimport os
31176043Ssilbyimport signal
32176043Ssilbyimport subprocess
33176043Ssilbyimport time
34176043Ssilby
35176043Ssilbyif os.path.exists('results.pcap'):
36176043Ssilby    os.remove('results.pcap')
37176043Ssilbytcpdump = subprocess.Popen('tcpdump -n -i lo0 -w results.pcap icmp', shell=True)
38176043Ssilbytime.sleep(1) # Give tcpdump time to start
39176043Ssilby
40176043Ssilbyos.system('sysctl net.inet.icmp.icmplim=0')
41176043Ssilbyos.system('ping -q -i .001 -c 100000 127.0.0.1')
42176043Ssilby
43176043Ssilbytime.sleep(3) # Give tcpdump time to catch up
44176043Ssilbyos.kill(tcpdump.pid, signal.SIGTERM)
45176043Ssilby
46176043Ssilbyos.system('tcpdump -n -v -r results.pcap > results.txt')
47176043Ssilby
48176043Ssilbyid_lastseen = {}
49176043Ssilbyid_minperiod = {}
50176043Ssilby
51176043Ssilbycount = 0
52176043Ssilbyfor line in open('results.txt').readlines():
53176043Ssilby    id = int(line.split(' id ')[1].split(',')[0])
54241832Seadler    if id in id_lastseen:
55176043Ssilby        period = count - id_lastseen[id]
56241832Seadler        if id not in id_minperiod or period < id_minperiod[id]:
57176043Ssilby            id_minperiod[id] = period
58176043Ssilby    id_lastseen[id] = count
59176043Ssilby    count += 1
60176043Ssilby
61241832Seadlersorted_minperiod = list(zip(*reversed(list(zip(*list(id_minperiod.items()))))))
62176043Ssilbysorted_minperiod.sort()
63176043Ssilby
64241832Seadlerprint("Lowest 10 ID periods detected:")
65176043Ssilbyx = 0
66176043Ssilbywhile x < 10:
67176043Ssilby    id_tuple = sorted_minperiod.pop(0)
68241832Seadler    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
69176043Ssilby    x += 1
70176043Ssilby
71241832Seadlerprint("Highest 10 ID periods detected:")
72176043Ssilbyx = 0
73176043Ssilbywhile x < 10:
74176043Ssilby    id_tuple = sorted_minperiod.pop()
75241832Seadler    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
76176043Ssilby    x += 1
77