1 | import socket |
---|
2 | import sys |
---|
3 | import time |
---|
4 | import select |
---|
5 | |
---|
6 | YESTEST = True |
---|
7 | MESSAGETEST = True |
---|
8 | BLOCKTEST = False |
---|
9 | OFFLINETEST = True |
---|
10 | RENAMETEST = True |
---|
11 | STATUSTEST= True |
---|
12 | SHOWLOG = False |
---|
13 | SHOWTESTLOG = True |
---|
14 | |
---|
15 | FUN = [ |
---|
16 | "Did I ask you something?", |
---|
17 | "Oh yeah, that's right.", |
---|
18 | "Alright, alright. Now go back to work.", |
---|
19 | "Buuuuuuuuuuuuuuuurp... Excuse me!", |
---|
20 | "Yes?", |
---|
21 | "No?", |
---|
22 | ] |
---|
23 | |
---|
24 | SEPARATOR = "="*60 |
---|
25 | SMOLPARATOR = "-"*60 |
---|
26 | |
---|
27 | class IrcClient: |
---|
28 | def __init__(self, nick, pwd): |
---|
29 | self.nick = nick |
---|
30 | self.pwd = pwd |
---|
31 | self.log = '' |
---|
32 | self.tmplog = '' |
---|
33 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
34 | |
---|
35 | def send_raw(self, msg, loud = False, log = True): |
---|
36 | self.receive() |
---|
37 | if loud: |
---|
38 | print('FROM '+ self.nick + '|| ' + msg) |
---|
39 | if log: |
---|
40 | self.log += msg+'\r\n' |
---|
41 | self.tmplog += msg+'\r\n' |
---|
42 | self.sck.send((msg+'\r\n').encode()) |
---|
43 | |
---|
44 | def send_priv_msg(self, recip, msg, loud = False): |
---|
45 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
46 | |
---|
47 | def connect(self): |
---|
48 | try: |
---|
49 | self.sck.connect(('127.0.0.1', 6667)) |
---|
50 | except: |
---|
51 | print("IRC connection failed for " + self.nick) |
---|
52 | sys.exit(1) |
---|
53 | |
---|
54 | print("IRC connection established for " + self.nick) |
---|
55 | |
---|
56 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
57 | self.send_raw('NICK ' + self.nick) |
---|
58 | self.send_raw('JOIN &bitlbee') |
---|
59 | |
---|
60 | def jabber_login(self): |
---|
61 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
62 | time.sleep(0.3) |
---|
63 | self.send_priv_msg("&bitlbee", "account on") |
---|
64 | time.sleep(1) |
---|
65 | self.receive() |
---|
66 | if self.log.find('Logged in') == -1: |
---|
67 | print("Jabber login failed for " + self.nick) |
---|
68 | sys.exit(1) |
---|
69 | else: |
---|
70 | print("Jabber login successful for " + self.nick) |
---|
71 | |
---|
72 | def receive(self): |
---|
73 | text = '' |
---|
74 | while True: |
---|
75 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
76 | if self.sck in readable: |
---|
77 | text += self.sck.recv(2040).decode() |
---|
78 | for line in text.split('\n'): |
---|
79 | if line.find('PING') != -1: |
---|
80 | self.send_raw('PONG ' + line.split()[1]) |
---|
81 | else: |
---|
82 | break |
---|
83 | self.log += text |
---|
84 | self.tmplog += text |
---|
85 | return text |
---|
86 | |
---|
87 | def add_jabber_buddy(self, nick): |
---|
88 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
89 | |
---|
90 | def block_jabber_buddy(self, nick): |
---|
91 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
92 | |
---|
93 | def unblock_jabber_buddy(self, nick): |
---|
94 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
95 | |
---|
96 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
97 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
98 | |
---|
99 | def msg_comes_thru(sender, receiver, message): |
---|
100 | sender.send_priv_msg(receiver.nick, message) |
---|
101 | received = receiver.receive().find(message) != -1 |
---|
102 | return received |
---|
103 | |
---|
104 | def perform_test(failed, clis, test_function, test_name): |
---|
105 | fail = False |
---|
106 | for cli in clis: |
---|
107 | cli.tmplog="" |
---|
108 | |
---|
109 | print("\n"+SEPARATOR) |
---|
110 | print("Test: "+test_name) |
---|
111 | |
---|
112 | if test_function(clis): |
---|
113 | print("Test passed") |
---|
114 | else: |
---|
115 | print("Test failed") |
---|
116 | failed += [test_name] |
---|
117 | fail = True |
---|
118 | for cli in clis: |
---|
119 | cli.receive() |
---|
120 | |
---|
121 | if fail or SHOWTESTLOG: |
---|
122 | for cli in clis: |
---|
123 | if cli.tmplog != "": |
---|
124 | print(SMOLPARATOR) |
---|
125 | print("Test Log "+ cli.nick+":") |
---|
126 | print(cli.tmplog) |
---|
127 | print(SEPARATOR) |
---|
128 | |
---|
129 | def yes_test(clis): |
---|
130 | ret = False |
---|
131 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
132 | clis[0].receive() |
---|
133 | for x, fun in enumerate(FUN): |
---|
134 | if (clis[0].log.find(fun) != -1): |
---|
135 | ret = True |
---|
136 | if x: |
---|
137 | print("The RNG gods smile upon us") |
---|
138 | break |
---|
139 | return ret |
---|
140 | |
---|
141 | def message_test(clis): |
---|
142 | ret = msg_comes_thru(clis[0], clis[1], 'ohai <3') |
---|
143 | ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*') |
---|
144 | return ret |
---|
145 | |
---|
146 | def block_test(clis): |
---|
147 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
148 | ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?') |
---|
149 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
150 | ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*') |
---|
151 | return ret |
---|
152 | |
---|
153 | def rename_test(clis): |
---|
154 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
155 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
156 | |
---|
157 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
158 | clis[0].send_priv_msg(newname, message) |
---|
159 | ret = clis[1].receive().find(message) != -1 |
---|
160 | |
---|
161 | clis[0].rename_jabber_buddy("-del", newname) |
---|
162 | ret = ret & msg_comes_thru(clis[0], clis[1], "rawr") |
---|
163 | return ret |
---|
164 | |
---|
165 | def status_test(clis): |
---|
166 | status = "get out of my room mom" |
---|
167 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
168 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
169 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
170 | |
---|
171 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
172 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
173 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
174 | return ret |
---|
175 | |
---|
176 | def offline_test(clis): |
---|
177 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
178 | |
---|
179 | junk = clis[0].receive() |
---|
180 | ret = (junk.find(clis[1].nick) != -1) |
---|
181 | ret = ret & (junk.find("QUIT") != -1) |
---|
182 | |
---|
183 | junk = clis[1].receive() |
---|
184 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
185 | ret = ret & (junk.find("QUIT") != -1) |
---|
186 | |
---|
187 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
188 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
189 | |
---|
190 | clis[0].send_priv_msg("&bitlbee", "account on") |
---|
191 | |
---|
192 | junk = clis[0].receive() |
---|
193 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
194 | ret = ret & (junk.find("JOIN") != -1) |
---|
195 | |
---|
196 | junk = clis[1].receive() |
---|
197 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
198 | ret = ret & (junk.find("JOIN") != -1) |
---|
199 | |
---|
200 | return ret |
---|
201 | |
---|
202 | def run_tests(failed): |
---|
203 | clis = [] |
---|
204 | clis += [IrcClient('test1', 'asd')] |
---|
205 | clis += [IrcClient('test2', 'asd')] |
---|
206 | for cli in clis: |
---|
207 | cli.connect() |
---|
208 | |
---|
209 | if YESTEST: |
---|
210 | perform_test(failed, clis, yes_test, "Yes") |
---|
211 | |
---|
212 | print("") |
---|
213 | for cli in clis: |
---|
214 | cli.jabber_login() |
---|
215 | |
---|
216 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
217 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
218 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
219 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
220 | |
---|
221 | if MESSAGETEST: |
---|
222 | perform_test(failed, clis, message_test, "Send message") |
---|
223 | |
---|
224 | if BLOCKTEST: |
---|
225 | perform_test(failed, clis, block_test, "Block user") |
---|
226 | |
---|
227 | if RENAMETEST: |
---|
228 | perform_test(failed, clis, rename_test, "Rename user") |
---|
229 | |
---|
230 | if STATUSTEST: |
---|
231 | perform_test(failed, clis, status_test, "Change status") |
---|
232 | |
---|
233 | if OFFLINETEST: |
---|
234 | perform_test(failed, clis, offline_test, "Go offline") |
---|
235 | |
---|
236 | if failed or SHOWLOG: |
---|
237 | print("") |
---|
238 | for cli in clis: |
---|
239 | print(SMOLPARATOR) |
---|
240 | print("Log "+ cli.nick+":") |
---|
241 | print(cli.log) |
---|
242 | print(SMOLPARATOR) |
---|
243 | |
---|
244 | if failed: |
---|
245 | print("\n" + SEPARATOR + "\nSome test have failed:") |
---|
246 | for fail in failed: |
---|
247 | print(fail) |
---|
248 | else: |
---|
249 | print("\n" + SEPARATOR + "\nAll tests have passed") |
---|
250 | |
---|
251 | if __name__ == "__main__": |
---|
252 | failed = [] |
---|
253 | run_tests(failed) |
---|
254 | if failed: |
---|
255 | sys.exit(1) |
---|