source: .tests/test1.py @ 5bcaaa7

Last change on this file since 5bcaaa7 was 8ef7977, checked in by / <>, at 2021-03-26T11:16:24Z

dumb morning

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