source: .tests/btlib.py @ 478a5f6

Last change on this file since 478a5f6 was 478a5f6, checked in by / <>, at 2021-03-31T12:27:28Z

typos

  • Property mode set to 100644
File size: 9.3 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
65    def receive(self):
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:
75                break
76        self.log += text
[23fd63d]77        self.tmplog += text
[90417ce]78        return text
79
[946c6da]80    def add_jabber_buddy(self, nick):
81        self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost")
[ef2cbca]82   
[946c6da]83    def block_jabber_buddy(self, nick):
84        self.send_priv_msg("&bitlbee", "block " + nick)
[43a257d]85
[946c6da]86    def unblock_jabber_buddy(self, nick):
87        self.send_priv_msg("&bitlbee", "allow " + nick)
[ef2cbca]88
[946c6da]89    def rename_jabber_buddy(self, oldnick, newnick):
90        self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick)
[ef2cbca]91       
[a4623f4]92def msg_comes_thru(sender, receiver, message):
[946c6da]93    sender.send_priv_msg(receiver.nick, message)
[12886d4]94    received = receiver.receive().find(message) != -1
[a4623f4]95    return received
96
[e7434db]97def perform_test(test_function):
[2806b5e]98    clis = []
[25b84f06]99    clis += [IrcClient('test1', 'asd')]
100    clis += [IrcClient('test2', 'asd')]
[a4623f4]101
[f0e9ee1]102    fail = not test_function(clis)
103
[50efbf4]104    if ALWAYSSHOWLOG or fail:
105        for cli in clis:
106            cli.receive()
107            print(SEPARATOR)
108            print("Test Log "+ cli.nick+":")
109            print(cli.tmplog)
[2806b5e]110        print(SEPARATOR)
111   
112    if fail:
113        sys.exit(1)
[f0e9ee1]114
[c386390]115def connect_test(clis):
116    ret = True
117    for cli in clis:
118        ret = ret & cli.connect()
119    return ret
[a4623f4]120
121def yes_test(clis):
122    ret = False
[a3d01fb]123    clis[0].send_priv_msg("&bitlbee", "yes")
[da75c3d]124    clis[0].receive()
125    for x, fun in enumerate(FUN):
126        if (clis[0].log.find(fun) != -1):
[a4623f4]127            ret = True
[da75c3d]128            if x:
129                print("The RNG gods smile upon us")
[a4623f4]130            break
131    return ret
132
[c386390]133def jabber_login_test(clis):
134    ret = True
135    for cli in clis:
136        ret = ret & cli.jabber_login()
137    return ret
138
[542b7d5]139def jabber_delete_account_test(clis):
140    ret = True
[0a98f92]141    clis[1].receive()
[542b7d5]142    clis[1].send_priv_msg("&bitlbee", "account list")
143    time.sleep(0.5)
[0a98f92]144    ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1)
[542b7d5]145
146    clis[1].send_priv_msg("&bitlbee", "account on")
147    clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del")
[0a98f92]148    clis[1].receive()
[542b7d5]149    clis[1].send_priv_msg("&bitlbee", "account list")
150    time.sleep(0.5)
[0a98f92]151    ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1)
[542b7d5]152
153    clis[1].send_priv_msg("&bitlbee", "account off")
154    clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del")
[0a98f92]155    clis[1].receive()
[542b7d5]156    clis[1].send_priv_msg("&bitlbee", "account list")
157    time.sleep(0.5)
[0a98f92]158    ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') == -1)
[542b7d5]159    return ret
160
[5d927631]161def register_test(clis):
162    clis[1].send_priv_msg("&bitlbee", "register "+clis[1].pwd*2)
163    time.sleep(0.5)
164    return (clis[1].receive().find('Account successfully created') != -1)
165   
166def unregister_test(clis):
167    clis[1].send_priv_msg("&bitlbee", "drop "+clis[1].pwd*2)
168    time.sleep(0.5)
169    ret = (clis[1].receive().find('removed') != -1)
170    clis[1].send_priv_msg("&bitlbee", "drop "+clis[1].pwd*2)
171    time.sleep(0.5)
172    ret = ret & (clis[1].receive().find('That account does not exist') != -1)
173    return ret
174
175def identify_test(clis):
176    ret = True
[478a5f6]177    clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd)
[5d927631]178    time.sleep(0.5)
179    ret = ret & (clis[1].receive().find('Incorrect password') != -1)
180
[478a5f6]181    clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd*2)
[5d927631]182    time.sleep(0.5)
183    ret = ret & (clis[1].receive().find('Password accepted') != -1)
184    return ret
185
186def identify_nonexist_test(clis):
187    clis[1].send_priv_msg("&bitlbee", "register "+clis[1].pwd)
188    time.sleep(0.5)
189    return (clis[1].receive().find('The nick is (probably) not registered') != -1)
190
[51f144a]191def add_buddy_test(clis):
192    clis[0].add_jabber_buddy(clis[1].nick)
[2c7f522]193    clis[1].send_priv_msg("&bitlbee", "yes")
[51f144a]194
[2c7f522]195    clis[1].add_jabber_buddy(clis[0].nick)
[51f144a]196    clis[0].send_priv_msg("&bitlbee", "yes")
197
198    clis[0].send_priv_msg("&bitlbee", "blist")
[8d7cc55]199    junk = clis[0].receive()
200    ret = junk.find(clis[1].nick) != -1
201    ret = ret & (junk.find("1 available") != -1)
[50efbf4]202    return ret
203
204def remove_buddy_test(clis):
205    ret = add_buddy_test(clis)
206
[4c87970]207    clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick)
[50efbf4]208    clis[0].send_priv_msg("&bitlbee", "blist all")
209    time.sleep(0.5)
[51f144a]210    ret = ret & (clis[0].receive().find(clis[1].nick) == -1)
211
212    clis[0].add_jabber_buddy(clis[1].nick)
213    clis[1].send_priv_msg("&bitlbee", "yes")
[663f018]214    time.sleep(1)
[50efbf4]215    clis[0].send_priv_msg("&bitlbee", "blist all")
216    time.sleep(0.5)
[8d7cc55]217    junk = clis[0].receive()
[542b7d5]218    #ret = ret & (junk.find("1 available") != -1)
[8d7cc55]219    ret = ret & (junk.find(clis[1].nick) != -1)
[da215ef]220    return ret
[51f144a]221
[a4623f4]222def message_test(clis):
223    ret = msg_comes_thru(clis[0], clis[1], 'ohai <3')
224    ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*')
225    return ret
226
227def block_test(clis):
228    clis[0].block_jabber_buddy(clis[1].nick)
229    ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?')
230    clis[0].unblock_jabber_buddy(clis[1].nick)
231    ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*')
232    return ret
233
234def rename_test(clis):
235    newname = "xXx_pup_LINKENPARK4EVA"
236    message = "rawr meanmz i luv<3 u in dinosaur"
237
238    clis[0].rename_jabber_buddy(clis[1].nick, newname)
239    clis[0].send_priv_msg(newname, message)
240    ret = clis[1].receive().find(message) != -1
241
242    clis[0].rename_jabber_buddy("-del", newname)
243    ret = ret & msg_comes_thru(clis[0], clis[1], "rawr")
244    return ret
[43a257d]245
[0e2c8b4]246def status_test(clis):
[76f0fd1]247    status = "get out of my room mom"
248    clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'")
[0e2c8b4]249    clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick)
[35c72b7]250    ret = (clis[0].receive().find("jabber - Status message: "+status) != -1)
[0e2c8b4]251
[3d4cb51]252    clis[1].send_priv_msg("&bitlbee", "set")
253    ret = ret & (clis[1].receive().find(status) != -1)
254
[0e2c8b4]255    clis[1].send_priv_msg("&bitlbee", "set -del status")
256    clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick)
[2e9b39a]257    ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1)
[0e2c8b4]258    return ret
259
260def offline_test(clis):
[35c72b7]261    clis[0].send_priv_msg("&bitlbee", "account off")
262
263    junk = clis[0].receive()
264    ret = (junk.find(clis[1].nick) != -1)
[76f0fd1]265    ret = ret & (junk.find("QUIT") != -1)
[35c72b7]266
267    junk = clis[1].receive()
268    ret = ret & (junk.find(clis[0].nick) != -1)
[76f0fd1]269    ret = ret & (junk.find("QUIT") != -1)
[35c72b7]270
[2e9b39a]271    clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom")
[76f0fd1]272    ret = ret & (clis[0].receive().find("No such nick/channel") != -1)
[35c72b7]273
274    clis[0].send_priv_msg("&bitlbee", "account on")
275
276    junk = clis[0].receive()
277    ret = ret & (junk.find(clis[1].nick) != -1)
[76f0fd1]278    ret = ret & (junk.find("JOIN") != -1)
[35c72b7]279
280    junk = clis[1].receive()
281    ret = ret & (junk.find(clis[0].nick) != -1)
[76f0fd1]282    ret = ret & (junk.find("JOIN") != -1)
[35c72b7]283
284    return ret
[0e2c8b4]285
[7361a6a]286def default_target_test(clis):
287    clis[0].send_priv_msg("&bitlbee", "set default_target last")
[5552588]288    clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now")
289   
290    ret = (clis[1].receive().find("ur mah default now") != -1)
[7361a6a]291
[5552588]292    clis[0].send_priv_msg("&bitlbee", "mrow")
293    ret = ret & (clis[1].receive().find("mrow") != -1)
[7361a6a]294
295    clis[0].send_priv_msg("root", "set default_target root")
[68ba9f4]296    junk = clis[0].receive()
297    ret = ret & (junk.find("default_target") != -1)
298    ret = ret & (junk.find("root") != -1)
[7361a6a]299
300    clis[0].send_priv_msg("&bitlbee", "yes")
301    ret = ret & (clis[1].receive().find("yes") == -1)
302    return ret
303
[3d4cb51]304def help_test(clis):
305    clis[0].send_priv_msg("&bitlbee", "help")
306    ret = (clis[0].receive().find("identify_methods") != -1)
307    clis[0].send_priv_msg("&bitlbee", "help commands")
308    ret = ret & (clis[0].receive().find("qlist") != -1)
309    return ret
Note: See TracBrowser for help on using the repository browser.