source: .tests/btlib.py @ 6066443

Last change on this file since 6066443 was 2806b5e, checked in by / <>, at 2021-03-29T11:32:17Z

rf3

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