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
Line 
1import socket
2import sys
3import time
4import select
5
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
15SEPARATOR = "="*60
16
17ALWAYSSHOWLOG = False
18
19class IrcClient:
20    def __init__(self, nick, pwd):
21        self.nick = nick
22        self.pwd = pwd
23        self.log = ''
24        self.tmplog = ''
25        self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
26
27    def send_raw(self, msg, loud = False, log = True):
28        self.receive()
29        if loud:
30            print('FROM '+ self.nick + '|| ' + msg)
31        if log:
32            self.log += msg+'\r\n'
33            self.tmplog += msg+'\r\n'
34        self.sck.send((msg+'\r\n').encode())
35
36    def send_priv_msg(self, recip, msg, loud = False):
37        self.send_raw('PRIVMSG '+recip+' :'+msg, loud)
38
39    def connect(self):
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:
50            return False
51
52        self.send_raw('USER ' + (self.nick + " ")*3)
53        self.send_raw('NICK ' + self.nick)
54        self.send_raw('JOIN &bitlbee')
55        return True
56
57    def jabber_login(self):
58        self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd)
59        time.sleep(0.3)
60        self.send_priv_msg("&bitlbee", "account on")
61        time.sleep(1)
62        self.receive()
63        return (self.log.find('Logged in') != -1)
64
65    def receive(self, wait = 1):
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:
73                        self.send_raw('PONG ' + line.split()[1])
74            else:
75                time.sleep(wait)
76                if self.sck not in readable:
77                    break
78        self.log += text
79        self.tmplog += text
80        return text
81
82    def add_jabber_buddy(self, nick):
83        self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost")
84   
85    def block_jabber_buddy(self, nick):
86        self.send_priv_msg("&bitlbee", "block " + nick)
87
88    def unblock_jabber_buddy(self, nick):
89        self.send_priv_msg("&bitlbee", "allow " + nick)
90
91    def rename_jabber_buddy(self, oldnick, newnick):
92        self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick)
93       
94def msg_comes_thru(sender, receiver, message):
95    sender.send_priv_msg(receiver.nick, message)
96    received = receiver.receive().find(message) != -1
97    return received
98
99def perform_test(test_function):
100    clis = []
101    clis += [IrcClient('test1', 'asd')]
102    clis += [IrcClient('test2', 'asd')]
103
104    fail = not test_function(clis)
105
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)
112        print(SEPARATOR)
113   
114    if fail:
115        sys.exit(1)
116
117def connect_test(clis):
118    ret = True
119    for cli in clis:
120        ret = ret & cli.connect()
121    return ret
122
123def yes_test(clis):
124    ret = False
125    clis[0].send_priv_msg("&bitlbee", "yes")
126    clis[0].receive()
127    for x, fun in enumerate(FUN):
128        if (clis[0].log.find(fun) != -1):
129            ret = True
130            if x:
131                print("The RNG gods smile upon us")
132            break
133    return ret
134
135def jabber_login_test(clis):
136    ret = True
137    for cli in clis:
138        ret = ret & cli.jabber_login()
139    return ret
140
141def jabber_delete_account_test(clis):
142    ret = True
143    clis[1].receive()
144    clis[1].send_priv_msg("&bitlbee", "account list")
145    time.sleep(0.5)
146    ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1)
147
148    clis[1].send_priv_msg("&bitlbee", "account on")
149    clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del")
150    clis[1].receive()
151    clis[1].send_priv_msg("&bitlbee", "account list")
152    time.sleep(0.5)
153    ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1)
154
155    clis[1].send_priv_msg("&bitlbee", "account off")
156    clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del")
157    clis[1].receive()
158    clis[1].send_priv_msg("&bitlbee", "account list")
159    time.sleep(0.5)
160    ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') == -1)
161    return ret
162
163def register_test(clis):
164    clis[1].send_priv_msg("&bitlbee", "register "+clis[1].pwd*2)
165    ret = (clis[1].receive(wait = 10).find('Account successfully created') != -1)
166    print("What is this"+ ret)
167    return ret
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
180    clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd)
181    time.sleep(0.5)
182    ret = ret & (clis[1].receive().find('Incorrect password') != -1)
183
184    clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd*2)
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
194def add_buddy_test(clis):
195    clis[0].add_jabber_buddy(clis[1].nick)
196    clis[1].send_priv_msg("&bitlbee", "yes")
197
198    clis[1].add_jabber_buddy(clis[0].nick)
199    clis[0].send_priv_msg("&bitlbee", "yes")
200
201    clis[0].send_priv_msg("&bitlbee", "blist")
202    junk = clis[0].receive()
203    ret = junk.find(clis[1].nick) != -1
204    ret = ret & (junk.find("1 available") != -1)
205    return ret
206
207def remove_buddy_test(clis):
208    ret = add_buddy_test(clis)
209
210    clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick)
211    clis[0].send_priv_msg("&bitlbee", "blist all")
212    time.sleep(0.5)
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")
217    time.sleep(1)
218    clis[0].send_priv_msg("&bitlbee", "blist all")
219    time.sleep(0.5)
220    junk = clis[0].receive()
221    #ret = ret & (junk.find("1 available") != -1)
222    ret = ret & (junk.find(clis[1].nick) != -1)
223    return ret
224
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
248
249def status_test(clis):
250    status = "get out of my room mom"
251    clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'")
252    clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick)
253    ret = (clis[0].receive().find("jabber - Status message: "+status) != -1)
254
255    clis[1].send_priv_msg("&bitlbee", "set")
256    ret = ret & (clis[1].receive().find(status) != -1)
257
258    clis[1].send_priv_msg("&bitlbee", "set -del status")
259    clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick)
260    ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1)
261    return ret
262
263def offline_test(clis):
264    clis[0].send_priv_msg("&bitlbee", "account off")
265
266    junk = clis[0].receive()
267    ret = (junk.find(clis[1].nick) != -1)
268    ret = ret & (junk.find("QUIT") != -1)
269
270    junk = clis[1].receive()
271    ret = ret & (junk.find(clis[0].nick) != -1)
272    ret = ret & (junk.find("QUIT") != -1)
273
274    clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom")
275    ret = ret & (clis[0].receive().find("No such nick/channel") != -1)
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)
281    ret = ret & (junk.find("JOIN") != -1)
282
283    junk = clis[1].receive()
284    ret = ret & (junk.find(clis[0].nick) != -1)
285    ret = ret & (junk.find("JOIN") != -1)
286
287    return ret
288
289def default_target_test(clis):
290    clis[0].send_priv_msg("&bitlbee", "set default_target last")
291    clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now")
292   
293    ret = (clis[1].receive().find("ur mah default now") != -1)
294
295    clis[0].send_priv_msg("&bitlbee", "mrow")
296    ret = ret & (clis[1].receive().find("mrow") != -1)
297
298    clis[0].send_priv_msg("root", "set default_target root")
299    junk = clis[0].receive()
300    ret = ret & (junk.find("default_target") != -1)
301    ret = ret & (junk.find("root") != -1)
302
303    clis[0].send_priv_msg("&bitlbee", "yes")
304    ret = ret & (clis[1].receive().find("yes") == -1)
305    return ret
306
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.