The trunk can use the main server again (for the time being).
[switzerland.git] / tests / PcapPlayback.py
blobccf6c3a5ddb0b482897aa8ab8640a4541fb26342
1 #!/usr/bin/env python2.5
3 import sys
4 import threading
5 import random
6 import time
7 import unittest
8 import os
10 sys.path.append(".")
11 sys.path.append("..")
13 # for PacketCollector
14 os.environ["PATH"] += ":../switzerland/client/"
16 from switzerland.common import util
17 from switzerland.common import local_ip
18 from switzerland.client import Alice
19 from switzerland.client.AliceConfig import AliceConfig
20 from switzerland.client.AliceLink import LocalAliceLink
21 from switzerland.server import Switzerland
22 from switzerland.server.SwitzerlandConfig import SwitzerlandConfig
23 import socket as s
25 class PcapPlayback(unittest.TestCase):
26 """
27 Inherit from this class to create tests by playing back PCAP logs.
28 """
30 def setUp(self):
31 self.port = random.randint(17000,18000)
32 self.server = Switzerland.SwitzerlandMasterServer(SwitzerlandConfig(port=self.port, keep_threads=True, keep_reconciliators=True, seriousness=seriousness_threshold, allow_fake_ips=True))
33 util.ThreadLauncher(self.server.accept_connections).start()
35 def tearDown(self):
36 del self.server
38 def pcap_playback(self, filename1, ip_in_pcap1, filename2, ip_in_pcap2, skew_pcap2, playback_offset=0, keep_archives=False, force_public_ip1=False, force_public_ip2=False):
39 ip = local_ip.get_local_ip()
40 localhost = "127.0.0.1"
41 if ip == localhost:
42 print "Can't run system tests without a non-localhost interface!!"
43 return 0
45 # We turn the packet filter off, because it would require extra waiting
46 # mechanisms for the new-members messages to arrive in time for the
47 # filtering mechanism. NTP is off because we don't need it. Cleaning has
48 # to be off because the timestamps in the pcaps are antique.
49 if not force_public_ip1:
50 public_ip1 = ip_in_pcap1
51 else:
52 public_ip1 = force_public_ip1
54 client1= Alice.Alice(linkobj=LocalAliceLink,\
55 config=AliceConfig(host="localhost", port=self.port, use_ntp=False,\
56 do_cleaning=False, seriousness=seriousness_threshold,\
57 filter_packets=False, keep_archives=keep_archives,\
58 force_public_ip=public_ip1, pcap_playback=filename1,\
59 force_private_ip=ip_in_pcap1, logfile=None, pcap_logdir=None))
61 if not force_public_ip2:
62 public_ip2 = ip_in_pcap2
63 else:
64 public_ip2 = force_public_ip2
66 client2= Alice.Alice(\
67 config=AliceConfig(host=ip, port=self.port, use_ntp=False,\
68 do_cleaning=False, seriousness=seriousness_threshold, skew=skew_pcap2,\
69 filter_packets=False, keep_archives=keep_archives,\
70 force_public_ip=public_ip2, pcap_playback=filename2,\
71 force_private_ip=ip_in_pcap2, logfile=None, \
72 pcap_logdir=None))
73 time.sleep(2)
74 # if we have a negative playback_offset, switch the order of events
75 if playback_offset < 0.0:
76 playback_offset *= -1
77 client1, client2 = client2, client1
78 client2.listener.start()
79 client2.start()
80 time.sleep(playback_offset)
81 client1.listener.start()
82 client1.start()
83 client1.listener.done.wait()
84 client2.listener.done.wait()
86 seriousness_threshold = 0
88 def main():
89 if "-s" in sys.argv:
90 pos = sys.argv.index("-s")
91 try:
92 # unittest does things with argv, so lets get there first :)
93 global seriousness_threshold
94 seriousness_threshold = int(sys.argv[pos +1])
95 print "Setting Debug Seriousness to", seriousness_threshold
96 del sys.argv[pos:pos+2] # deletes 2 args
97 except IndexError:
98 pass
99 unittest.main()