source: .tests/test1.py @ 9634fba

Last change on this file since 9634fba was 9634fba, checked in by / <>, at 2021-03-18T11:40:49Z

how about actually installing the thing idk

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