instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Test / test_as_server.py
1 # Written by Arno Bakker, Jie Yang
2 # see LICENSE.txt for license information
3
4 import unittest
5
6 import os
7 import sys
8 import tempfile
9 import random
10 import shutil
11 import time
12 from traceback import print_exc
13
14 from M2Crypto import EC
15
16 from BaseLib.Core.Session import *
17 from BaseLib.Core.SessionConfig import *
18
19
20 DEBUG=False
21
22 class TestAsServer(unittest.TestCase):
23     """ 
24     Parent class for testing the server-side of Tribler
25     """
26     
27     def setUp(self):
28         """ unittest test setup code """
29         self.setUpPreSession()
30         self.session = Session(self.config)
31         self.hisport = self.session.get_listen_port()
32         self.setUpPostSession()
33
34     def setUpPreSession(self):
35         """ Should set self.config_path and self.config """
36         self.config_path = tempfile.mkdtemp()
37
38         self.config = SessionStartupConfig()
39         self.config.set_state_dir(self.config_path)
40         self.config.set_listen_port(random.randint(10000, 60000))
41         self.config.set_buddycast(False)
42         self.config.set_start_recommender(False)
43         self.config.set_torrent_checking(False)
44         self.config.set_superpeer(False)
45         self.config.set_dialback(False)
46         self.config.set_social_networking(False)
47         self.config.set_remote_query(False)
48         self.config.set_internal_tracker(False)
49         self.config.set_bartercast(False)
50         self.config.set_multicast_local_peer_discovery(False)
51         # Assume all test scripts are run from Tribler/Test
52         self.config.set_install_dir(os.path.abspath(os.path.join('..','..')))
53
54         self.my_keypair = EC.gen_params(EC.NID_sect233k1)
55         self.my_keypair.gen_key()
56
57     def setUpPostSession(self):
58         """ Should set self.his_keypair """
59         keypair_filename = os.path.join(self.config_path,'ec.pem')
60         self.his_keypair = EC.load_key(keypair_filename)
61
62     def tearDown(self):
63         """ unittest test tear down code """
64         if self.session is not None:
65             self.session.shutdown()
66             print >>sys.stderr,"test_as_server: sleeping after session shutdown"
67             time.sleep(2)
68         try:
69             shutil.rmtree(self.config_path)
70         except:
71             # Not fatal if something goes wrong here, and Win32 often gives
72             # spurious Permission Denied errors.
73             print_exc()
74