instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Test / test_friend.py
1 # Written by Jie Yang, Arno Bakker
2 # see LICENSE.txt for license information
3 import os
4 import tempfile
5 import unittest
6 from sets import Set
7 import base64
8
9 from BaseLib.Core.CacheDB.friends import ExternalFriendList
10 from BaseLib.Core.CacheDB.sqlitecachedb import PeerDB
11
12 lines = [
13 'Jie Yang 2, MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAc6ebdH+dmvvgKiE7oOZuQba5I4msyuTJmVpJQVPAT+R9Pg8zsLsuJPV6RjU30RKHnCiaJvjtFW6pLXo, 130.161.158.51, 3966, 23623\n',
14 'Pawel, MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAJ114tMJ6C8TkLkSv8QlVFlj/RpF2ibbar1P8GbzASpMDb1kSUBnmldfMFsNTNSK5cJGsTgAGFjYEJ78, 130.37.198.247, 6882a\n',
15 '#Johan, MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAUo6nahUzz+NtYWfabmtkvBryqX3ToxgdBKIllVtADv1Et+W0OyT9J0F8VPqSeBZVA1TPuLUpt3I9QHP, 130.37.193.64, 6883\n',
16 'Arno Bakker 2, MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAWAiRwei5Kw9b2he6qmwh5Hr5fNR3FlgHQ1WhXY0AC4w8RQD59rp4Jbo2NdjyXUGb5y1BCeMCGoRCaFy, 130.37.193.64a, 6881\n'
17 'Arno Bakker, MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAWAiRwei5Kw9b2he6qmwh5Hr5fNR3FlgHQ1WhXY0AC4w8RQD59rp4Jbo2NdjyXUGb5y1BCeMCGoRCaFy, 130.37.193.64, 6881\n'
18 'Jie Yang, MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAc6ebdH+dmvvgKiE7oOZuQba5I4msyuTJmVpJQVPAT+R9Pg8zsLsuJPV6RjU30RKHnCiaJvjtFW6pLXo, 130.161.158.51, 3966, 23623\n',
19 ]
20
21 class TestFriendList(unittest.TestCase):
22     
23     def setUp(self):
24         self.tmpfilepath = tempfile.mktemp()
25         self.tmpdirpath = os.path.join(tempfile.gettempdir(), 'testdb')
26         self.flist = ExternalFriendList(friend_file=self.tmpfilepath, db_dir=self.tmpdirpath)
27         
28     def tearDown(self):
29         self.flist.clear()
30         try:
31             os.remove(self.tmpfilepath)
32         except Exception, msg:
33             pass
34
35     def writeFriends(self):
36         tf = open(self.tmpfilepath, "w")
37         tf.writelines(lines)
38         tf.close()
39             
40     def test_readFriendList(self):
41         self.writeFriends()
42         res = self.flist.readFriendList(self.tmpfilepath)
43         assert len(res) == 3, res
44         assert res[1]['name'] == 'Arno Bakker', res[0]
45         
46     def test_updateDB(self):
47         self.writeFriends()
48         res = self.flist.readFriendList()
49         self.flist.updateDB(res)
50         self.db_is_ok()
51         
52     def test_updateFriendList(self):
53         self.writeFriends()
54         self.flist.updateFriendList()
55         self.db_is_ok()
56         
57     def db_is_ok(self):
58         self.my_db = MyDB.getInstance()
59         self.peer_db = PeerDB.getInstance()
60         assert Set(self.my_db._get('friends')) == Set([
61         base64.decodestring('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAc6ebdH+dmvvgKiE7oOZuQba5I4msyuTJmVpJQVPAT+R9Pg8zsLsuJPV6RjU30RKHnCiaJvjtFW6pLXo\n'),
62         base64.decodestring('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAWAiRwei5Kw9b2he6qmwh5Hr5fNR3FlgHQ1WhXY0AC4w8RQD59rp4Jbo2NdjyXUGb5y1BCeMCGoRCaFy\n')
63         ]), self.my_db._get('friends')
64         assert self.peer_db._size() == 2
65         
66     def test_getFriends(self):
67         self.writeFriends()
68         self.flist.updateFriendList()
69         friends = self.flist.getFriends()
70         answer = [
71                    {'permid': base64.decodestring('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAWAiRwei5Kw9b2he6qmwh5Hr5fNR3FlgHQ1WhXY0AC4w8RQD59rp4Jbo2NdjyXUGb5y1BCeMCGoRCaFy\n'),
72                    'name':'Arno Bakker',
73                    'ip':'130.37.193.64', 
74                    'port':6881,
75                    'similarity':0,
76                    'last_seen':0,
77                    'buddycast_times':0,
78                    'last_buddycast_time':0,
79                    'oversion':0,
80                    'connected_times':0,
81                    'npeers':0,
82                    'ntorrents':0,
83                    'nprefs':0,
84                    'nqueries':0
85                    },
86                    {'permid':base64.decodestring('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAc6ebdH+dmvvgKiE7oOZuQba5I4msyuTJmVpJQVPAT+R9Pg8zsLsuJPV6RjU30RKHnCiaJvjtFW6pLXo\n'),
87                    'name':'Jie Yang',
88                    'ip':'130.161.158.51',
89                    'port':3966,
90                    'similarity':0,
91                    'last_seen':0,
92                    'buddycast_times':0,
93                    'last_buddycast_time':0,
94                    'oversion':0,                   
95                    'connected_times':0,
96                    'npeers':0,
97                    'ntorrents':0,
98                    'nprefs':0,
99                    'nqueries':0
100                    },
101                    ]
102         assert len(friends) == 2, len(friends)
103         # Arno: last_seen is set automatically these days :-(
104         for friend in friends:
105             friend['last_seen'] = 0
106
107         """
108         for f in friends:
109             k = f.keys()[:]
110             k.sort()
111             print "FRIEND",k
112         for a in answer:
113             k = a.keys()[:]
114             k.sort()
115             print "ANSWER",k
116         """
117         
118         assert friends == answer or (friends[0] == answer[1] and friends[1] == answer[0]), friends
119         #self.flist.writeFriendList('tmp.txt')
120         self.flist.deleteFriend(answer[0]['permid'])
121         friends = self.flist.getFriends()
122         assert len(friends) == 1, len(friends)
123         assert friends[0]['permid'] == answer[1]['permid']
124
125     def xxtest_normal(self):
126         flist = ExternalFriendList()
127         flist.updateFriendList()
128         friends = flist.getFriends()
129         print friends
130
131 def test_suite():
132     suite = unittest.TestSuite()
133     suite.addTest(unittest.makeSuite(TestFriendList))
134     
135     return suite
136