source: .tests/test1.py @ 7361a6a

Last change on this file since 7361a6a was 7361a6a, checked in by / <>, at 2021-03-17T15:39:39Z

default target

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