source: .tests/btlib.py @ bda5012

Last change on this file since bda5012 was bda5012, checked in by / <>, at 2021-03-31T12:57:23Z

wat

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