source: .tests/test1.py @ d28ca33

Last change on this file since d28ca33 was d28ca33, checked in by / <>, at 2021-03-16T14:49:57Z

less glob more verbose

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