source: .tests/test1.py @ 0167270

Last change on this file since 0167270 was 0167270, checked in by / <>, at 2021-03-16T15:03:06Z

asdasd

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