source: .tests/test1.py @ 56c9fbf

Last change on this file since 56c9fbf was 56c9fbf, checked in by / <>, at 2021-02-25T17:42:56Z

aaaand more typos

  • Property mode set to 100644
File size: 3.3 KB
Line 
1import socket, sys, time, select
2
3class ircClient:
4    def __init__(self, nick, pw):
5        self.nick = nick
6        self.pw = pw
7        self.log = ''
8        self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9
10    def sendRaw(self, msg, loud = True):
11        self.receive()
12        if loud:
13            print('FROM '+ self.nick + '|| ' + msg)
14        self.log += msg+'\r\n'
15        self.sck.send((msg+'\r\n').encode())
16
17    def sendPrivMsg(self, recip, msg, loud = True):
18        self.sendRaw('PRIVMSG '+recip+' :'+msg, loud)
19
20    def connect(self):
21        try:
22            self.sck.connect(('127.0.0.1', 6667))
23        except:
24            print("IRC connection failed for " + self.nick)
25            sys.exit(1)
26       
27        print("IRC connection established for " + self.nick)
28
29        self.sendRaw('USER ' + (self.nick + " ")*3)
30        self.sendRaw('NICK ' + self.nick)
31        self.sendRaw('JOIN &bitlbee')
32
33    def jabberLogin(self):
34        self.sendPrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw)
35        time.sleep(0.3)
36        self.sendPrivMsg("&bitlbee", "account on")
37        time.sleep(1)
38        self.receive()
39        if self.log.find('Logged in') == -1:
40            print("Jabber login failed for " + self.nick)
41            sys.exit(1)
42        else:
43            print("Jabber login successful for " + self.nick)
44
45    def receive(self):
46        text = ''
47        while True:
48            readable, _, _ = select.select([self.sck], [], [], 5)
49            if self.sck in readable:
50                text += self.sck.recv(2040).decode()
51                for line in text.split('\n'):
52                    if line.find('PING') != -1:
53                        self.sendRaw('PONG ' + line.split()[1])
54            else:
55                break
56        self.log += text
57        return text
58
59    def addJabberBuddy(self, nick):
60        self.sendPrivMsg("&bitlbee", "add 0 " + nick+"@localhost")
61   
62    def blockJabberBuddy(self, nick):
63        self.sendPrivMsg("&bitlbee", "block " + nick)
64
65    def unblockJabberBuddy(self, nick):
66        self.sendPrivMsg("&bitlbee", "allow " + nick)
67
68    def renameJabberBuddy(self, oldnick, newnick):
69        self.sendPrivMsg("&bitlbee", "rename " + oldnick + " " + newnick)
70       
71def testSendMessage(sender, receiver, message, shouldreceive = True):
72    sender.sendPrivMsg(b.nick, message)
73    msginhere = receiver.receive()
74    received = !(msginhere.find(message) == -1)
75    if shouldreceive ^ received:
76        print('Test failed: Message from ' + sender.nick + ' to ' + receiver.nick)
77        print('Sender Log:' + a.log)
78        print('Receiver Log:' + b.log)
79        sys.exit(1)
80    else:
81
82def runTests():
83    clis = []
84    clis += [ircClient('test1', 'asd')]
85    clis += [ircClient('test2', 'asd')]
86    for cli in clis:
87        cli.connect()
88        cli.jabberLogin()
89
90    clis[0].addJabberBuddy(clis[1].nick)
91
92    print("Test: Send message")
93    testSendMessage(clis[0],clis[1]),'ohai <3')
94    testSendMessage(clis[1],clis[0]),'uwu *pounces*')
95    print("Test passed")
96
97    print("Test: Block/Unblock")
98    clis[0].blockJabberBuddy(clis[1].nick)
99    testSendMessage(clis[1],clis[0]),'m-meow?', shouldreceive = False)
100    clis[0].unblockJabberBuddy(clis[1].nick)
101    testSendMessage(clis[1],clis[0]),'*purrs*')
102    print("Test passed")
103   
104if __name__ == "__main__":
105    runTests()
Note: See TracBrowser for help on using the repository browser.