source: .tests/test1.py @ a3d01fb

Last change on this file since a3d01fb was a3d01fb, checked in by / <>, at 2021-03-17T12:48:20Z

yesyes

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