source: .tests/test1.py @ 22cef81

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

dumb morning

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[946c6da]1import socket
2import sys
3import time
4import select
[90417ce]5
[d28ca33]6SHOWLOG = False
7SHOWTESTLOG = True
[a4623f4]8
[da75c3d]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
[c248e37]18SEPARATOR = "="*60
19SMOLPARATOR = "-"*60
[946c6da]20
21class IrcClient:
22    def __init__(self, nick, pwd):
[90417ce]23        self.nick = nick
[946c6da]24        self.pwd = pwd
[90417ce]25        self.log = ''
[23fd63d]26        self.tmplog = ''
[90417ce]27        self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
28
[0167270]29    def send_raw(self, msg, loud = False, log = True):
[d24e6f7]30        self.receive()
[ef2cbca]31        if loud:
32            print('FROM '+ self.nick + '|| ' + msg)
[0167270]33        if log:
34            self.log += msg+'\r\n'
35            self.tmplog += msg+'\r\n'
[90417ce]36        self.sck.send((msg+'\r\n').encode())
37
[d28ca33]38    def send_priv_msg(self, recip, msg, loud = False):
[946c6da]39        self.send_raw('PRIVMSG '+recip+' :'+msg, loud)
[90417ce]40
41    def connect(self):
[314c3f8]42        try:
43            self.sck.connect(('127.0.0.1', 6667))
44        except:
[ef2cbca]45            print("IRC connection failed for " + self.nick)
[314c3f8]46            sys.exit(1)
[ef2cbca]47       
48        print("IRC connection established for " + self.nick)
[314c3f8]49
[946c6da]50        self.send_raw('USER ' + (self.nick + " ")*3)
51        self.send_raw('NICK ' + self.nick)
52        self.send_raw('JOIN &bitlbee')
[90417ce]53
[946c6da]54    def jabber_login(self):
55        self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd)
[90417ce]56        time.sleep(0.3)
[946c6da]57        self.send_priv_msg("&bitlbee", "account on")
[314c3f8]58        time.sleep(1)
59        self.receive()
60        if self.log.find('Logged in') == -1:
[ef2cbca]61            print("Jabber login failed for " + self.nick)
[314c3f8]62            sys.exit(1)
[ef2cbca]63        else:
64            print("Jabber login successful for " + self.nick)
[90417ce]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:
[946c6da]74                        self.send_raw('PONG ' + line.split()[1])
[90417ce]75            else:
76                break
77        self.log += text
[23fd63d]78        self.tmplog += text
[90417ce]79        return text
80
[946c6da]81    def add_jabber_buddy(self, nick):
82        self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost")
[ef2cbca]83   
[946c6da]84    def block_jabber_buddy(self, nick):
85        self.send_priv_msg("&bitlbee", "block " + nick)
[43a257d]86
[946c6da]87    def unblock_jabber_buddy(self, nick):
88        self.send_priv_msg("&bitlbee", "allow " + nick)
[ef2cbca]89
[946c6da]90    def rename_jabber_buddy(self, oldnick, newnick):
91        self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick)
[ef2cbca]92       
[a4623f4]93def msg_comes_thru(sender, receiver, message):
[946c6da]94    sender.send_priv_msg(receiver.nick, message)
[12886d4]95    received = receiver.receive().find(message) != -1
[a4623f4]96    return received
97
[f0e9ee1]98def debug_check(debug_file):
99    error_found = False
100    debug_log = ''
101    while True:
[8ef7977]102        line = debug_file.readline()
[f0e9ee1]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):
[a4623f4]110    for cli in clis:
111        cli.tmplog=""
112
113    print("\n"+SEPARATOR)
114    print("Test: "+test_name)
115
[f0e9ee1]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:
[a4623f4]123        print("Test failed")
[d28ca33]124        failed += [test_name]
[f0e9ee1]125    else:
126        print("Test passed")
127
[f1ba258]128    for cli in clis:
129        cli.receive()
[a4623f4]130
[d28ca33]131    if fail or SHOWTESTLOG:
[a4623f4]132        for cli in clis:
133            if cli.tmplog != "":
134                print(SMOLPARATOR)
135                print("Test Log "+ cli.nick+":")
136                print(cli.tmplog)
[f0e9ee1]137
138    if debug_error:
139        print(SMOLPARATOR)
140        print("Debug Log:")
141        print(debug_log)
142
[c248e37]143    print(SEPARATOR)
[a4623f4]144
145def yes_test(clis):
146    ret = False
[a3d01fb]147    clis[0].send_priv_msg("&bitlbee", "yes")
[da75c3d]148    clis[0].receive()
149    for x, fun in enumerate(FUN):
150        if (clis[0].log.find(fun) != -1):
[a4623f4]151            ret = True
[da75c3d]152            if x:
153                print("The RNG gods smile upon us")
[a4623f4]154            break
155    return ret
156
[51f144a]157def add_buddy_test(clis):
158    clis[0].add_jabber_buddy(clis[1].nick)
[2c7f522]159    clis[1].send_priv_msg("&bitlbee", "yes")
[51f144a]160
[2c7f522]161    clis[1].add_jabber_buddy(clis[0].nick)
[51f144a]162    clis[0].send_priv_msg("&bitlbee", "yes")
163
164    clis[0].send_priv_msg("&bitlbee", "blist")
[8d7cc55]165    junk = clis[0].receive()
166    ret = junk.find(clis[1].nick) != -1
167    ret = ret & (junk.find("1 available") != -1)
[4f22234c]168    '''
[4c87970]169    clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick)
[51f144a]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")
[663f018]175    time.sleep(1)
[51f144a]176    clis[0].send_priv_msg("&bitlbee", "blist")
[8d7cc55]177    junk = clis[0].receive()
[663f018]178    ret = ret & (junk.find("1 available") != -1)
[8d7cc55]179    ret = ret & (junk.find(clis[1].nick) != -1)
[4f22234c]180    '''
[da215ef]181    return ret
[51f144a]182
[a4623f4]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
[43a257d]206
[0e2c8b4]207def status_test(clis):
[76f0fd1]208    status = "get out of my room mom"
209    clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'")
[0e2c8b4]210    clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick)
[35c72b7]211    ret = (clis[0].receive().find("jabber - Status message: "+status) != -1)
[0e2c8b4]212
[3d4cb51]213    clis[1].send_priv_msg("&bitlbee", "set")
214    ret = ret & (clis[1].receive().find(status) != -1)
215
[0e2c8b4]216    clis[1].send_priv_msg("&bitlbee", "set -del status")
217    clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick)
[2e9b39a]218    ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1)
[0e2c8b4]219    return ret
220
221def offline_test(clis):
[35c72b7]222    clis[0].send_priv_msg("&bitlbee", "account off")
223
224    junk = clis[0].receive()
225    ret = (junk.find(clis[1].nick) != -1)
[76f0fd1]226    ret = ret & (junk.find("QUIT") != -1)
[35c72b7]227
228    junk = clis[1].receive()
229    ret = ret & (junk.find(clis[0].nick) != -1)
[76f0fd1]230    ret = ret & (junk.find("QUIT") != -1)
[35c72b7]231
[2e9b39a]232    clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom")
[76f0fd1]233    ret = ret & (clis[0].receive().find("No such nick/channel") != -1)
[35c72b7]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)
[76f0fd1]239    ret = ret & (junk.find("JOIN") != -1)
[35c72b7]240
241    junk = clis[1].receive()
242    ret = ret & (junk.find(clis[0].nick) != -1)
[76f0fd1]243    ret = ret & (junk.find("JOIN") != -1)
[35c72b7]244
245    return ret
[0e2c8b4]246
[7361a6a]247def default_target_test(clis):
248    clis[0].send_priv_msg("&bitlbee", "set default_target last")
[5552588]249    clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now")
250   
251    ret = (clis[1].receive().find("ur mah default now") != -1)
[7361a6a]252
[5552588]253    clis[0].send_priv_msg("&bitlbee", "mrow")
254    ret = ret & (clis[1].receive().find("mrow") != -1)
[7361a6a]255
256    clis[0].send_priv_msg("root", "set default_target root")
[68ba9f4]257    junk = clis[0].receive()
258    ret = ret & (junk.find("default_target") != -1)
259    ret = ret & (junk.find("root") != -1)
[7361a6a]260
261    clis[0].send_priv_msg("&bitlbee", "yes")
262    ret = ret & (clis[1].receive().find("yes") == -1)
263    return ret
264
[3d4cb51]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
[d28ca33]273def run_tests(failed):
[66930ca]274    debug_file = open('./debuglog','r')
[90417ce]275    clis = []
[946c6da]276    clis += [IrcClient('test1', 'asd')]
277    clis += [IrcClient('test2', 'asd')]
[90417ce]278    for cli in clis:
279        cli.connect()
[946c6da]280
[f0e9ee1]281    perform_test(failed, clis, yes_test, "Yes", debug_file)
[23fd63d]282
[0167270]283    print("")
[23fd63d]284    for cli in clis:
285        cli.jabber_login()
[d28ca33]286
[f0e9ee1]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)
[7361a6a]295
[89925a7]296    if failed or SHOWLOG:
[a4623f4]297        print("")
298        for cli in clis:
299            print(SMOLPARATOR)
300            print("Log "+ cli.nick+":")
301            print(cli.log)
302        print(SMOLPARATOR)
303
[d28ca33]304    if failed:
[a4623f4]305        print("\n" + SEPARATOR + "\nSome test have failed:")
[d28ca33]306        for fail in failed:
[a4623f4]307            print(fail)
308    else:
309        print("\n" + SEPARATOR + "\nAll tests have passed")
[ef2cbca]310   
[f0e9ee1]311    debug_file.close()
312   
[90417ce]313if __name__ == "__main__":
[d28ca33]314    failed = []
315    run_tests(failed)
316    if failed:
[946c6da]317        sys.exit(1)
Note: See TracBrowser for help on using the repository browser.