ripped out xmlrpc, experimented with xmlrpc but with bencode, finally
[quix0rs-apt-p2p.git] / test_krpc.py
1 from unittest import *
2 from krpc import *
3 from airhook import *
4
5 import sys
6
7 if __name__ =="__main__":
8     tests = unittest.defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])
9     result = unittest.TextTestRunner().run(tests)
10
11
12 def connectionForAddr(host, port):
13     return host
14     
15 class Receiver(protocol.Factory):
16     protocol = KRPC
17     def __init__(self):
18         self.buf = []
19     def krpc_store(self, msg, _krpc_sender):
20         self.buf += [msg]
21     def krpc_echo(self, msg, _krpc_sender):
22         return msg
23
24 class SimpleTest(TestCase):
25     def setUp(self):
26         self.noisy = 0
27         
28         self.af = Receiver()
29         self.bf = Receiver()        
30         self.a = listenAirhookStream(4040, self.af)
31         self.b = listenAirhookStream(4041, self.bf)
32         
33     def testSimpleMessage(self):
34         self.noisy = 1
35         self.a.connectionForAddr(('127.0.0.1', 4041)).protocol.sendRequest('store', {'msg' : "This is a test."})
36         reactor.iterate()
37         reactor.iterate()
38         reactor.iterate()
39         self.assertEqual(self.bf.buf, ["This is a test."])
40
41 class SimpleTest(TestCase):
42     def setUp(self):
43         self.noisy = 0
44         
45         self.af = Receiver()
46         self.bf = Receiver()        
47         self.a = listenAirhookStream(4050, self.af)
48         self.b = listenAirhookStream(4051, self.bf)
49         
50     def testSimpleMessage(self):
51         self.noisy = 1
52         self.a.connectionForAddr(('127.0.0.1', 4051)).protocol.sendRequest('store', {'msg' : "This is a test."})
53         reactor.iterate()
54         reactor.iterate()
55         reactor.iterate()
56         self.assertEqual(self.bf.buf, ["This is a test."])
57
58 class EchoTest(TestCase):
59     def setUp(self):
60         self.noisy = 0
61         self.msg = None
62         
63         self.af = Receiver()
64         self.bf = Receiver()        
65         self.a = listenAirhookStream(4042, self.af)
66         self.b = listenAirhookStream(4043, self.bf)
67         
68     def testEcho(self):
69         self.noisy = 1
70         df = self.a.connectionForAddr(('127.0.0.1', 4043)).protocol.sendRequest('echo', {'msg' : "This is a test."})
71         df.addCallback(self.gotMsg)
72         reactor.iterate()
73         reactor.iterate()
74         reactor.iterate()
75         reactor.iterate()
76         self.assertEqual(self.msg, "This is a test.")
77
78     def gotMsg(self, msg):
79         self.msg = msg
80
81 class MultiEchoTest(TestCase):
82     def setUp(self):
83         self.noisy = 0
84         self.msg = None
85         
86         self.af = Receiver()
87         self.bf = Receiver()        
88         self.a = listenAirhookStream(4048, self.af)
89         self.b = listenAirhookStream(4049, self.bf)
90         
91     def testMultiEcho(self):
92         self.noisy = 1
93         df = self.a.connectionForAddr(('127.0.0.1', 4049)).protocol.sendRequest('echo', {'msg' : "This is a test."})
94         df.addCallback(self.gotMsg)
95         reactor.iterate()
96         reactor.iterate()
97         reactor.iterate()
98         reactor.iterate()
99         self.assertEqual(self.msg, "This is a test.")
100
101         df = self.a.connectionForAddr(('127.0.0.1', 4049)).protocol.sendRequest('echo', {'msg' : "This is another test."})
102         df.addCallback(self.gotMsg)
103         reactor.iterate()
104         reactor.iterate()
105         reactor.iterate()
106         reactor.iterate()
107         self.assertEqual(self.msg, "This is another test.")
108
109         df = self.a.connectionForAddr(('127.0.0.1', 4049)).protocol.sendRequest('echo', {'msg' : "This is yet another test."})
110         df.addCallback(self.gotMsg)
111         reactor.iterate()
112         reactor.iterate()
113         reactor.iterate()
114         reactor.iterate()
115         self.assertEqual(self.msg, "This is yet another test.")
116
117     def gotMsg(self, msg):
118         self.msg = msg
119
120 class UnknownMethErrTest(TestCase):
121     def setUp(self):
122         self.noisy = 0
123         self.err = None
124         self.af = Receiver()
125         self.bf = Receiver()        
126         self.a = listenAirhookStream(4044, self.af)
127         self.b = listenAirhookStream(4045, self.bf)
128         
129     def testUnknownMeth(self):
130         self.noisy = 1
131         df = self.a.connectionForAddr(('127.0.0.1', 4045)).protocol.sendRequest('blahblah', {'msg' : "This is a test."})
132         df.addErrback(self.gotErr)
133         reactor.iterate()
134         reactor.iterate()
135         reactor.iterate()
136         reactor.iterate()
137         self.assertEqual(self.err, KRPC_ERROR_METHOD_UNKNOWN)
138
139     def gotErr(self, err):
140         self.err = err.value