source: .tests/test1.py @ 5552588

Last change on this file since 5552588 was 5552588, checked in by / <>, at 2021-03-17T16:20:58Z

proper send

  • Property mode set to 100644
File size: 7.8 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    clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now")
206   
207    ret = (clis[1].receive().find("ur mah default now") != -1)
208
209    clis[0].send_priv_msg("&bitlbee", "mrow")
210    ret = ret & (clis[1].receive().find("mrow") != -1)
211
212    clis[0].send_priv_msg("root", "set default_target root")
213    ret = ret & (clis[0].receive().find("default target") != -1)
214
215    clis[0].send_priv_msg("&bitlbee", "yes")
216    ret = ret & (clis[1].receive().find("yes") == -1)
217    return ret
218
219def run_tests(failed):
220    clis = []
221    clis += [IrcClient('test1', 'asd')]
222    clis += [IrcClient('test2', 'asd')]
223    for cli in clis:
224        cli.connect()
225
226    if YESTEST:
227        perform_test(failed, clis, yes_test, "Yes")
228
229    print("")
230    for cli in clis:
231        cli.jabber_login()
232
233    clis[0].add_jabber_buddy(clis[1].nick)
234    clis[1].add_jabber_buddy(clis[0].nick)
235    clis[0].send_priv_msg("&bitlbee", "yes")
236    clis[1].send_priv_msg("&bitlbee", "yes")
237
238    if MESSAGETEST:
239        perform_test(failed, clis, message_test, "Send message")
240
241    if BLOCKTEST:
242        perform_test(failed, clis, block_test, "Block user")
243
244    if RENAMETEST:
245        perform_test(failed, clis, rename_test, "Rename user")
246
247    if STATUSTEST:
248        perform_test(failed, clis, status_test, "Change status")
249
250    if OFFLINETEST:
251        perform_test(failed, clis, offline_test, "Go offline")
252
253    if DEFAULTTARGETTEST:
254        perform_test(failed, clis, default_target_test, "Default target")
255
256    if failed or SHOWLOG:
257        print("")
258        for cli in clis:
259            print(SMOLPARATOR)
260            print("Log "+ cli.nick+":")
261            print(cli.log)
262        print(SMOLPARATOR)
263
264    if failed:
265        print("\n" + SEPARATOR + "\nSome test have failed:")
266        for fail in failed:
267            print(fail)
268    else:
269        print("\n" + SEPARATOR + "\nAll tests have passed")
270   
271if __name__ == "__main__":
272    failed = []
273    run_tests(failed)
274    if failed:
275        sys.exit(1)
Note: See TracBrowser for help on using the repository browser.