source: .tests/test1.py @ 12886d4

Last change on this file since 12886d4 was 12886d4, checked in by / <>, at 2021-02-25T17:50:11Z

learn to write python howaboutit

  • 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    received = receiver.receive().find(message) != -1
74    if shouldreceive ^ received:
75        print('Test failed: Message from ' + sender.nick + ' to ' + receiver.nick)
76        print('Sender Log:' + a.log)
77        print('Receiver Log:' + b.log)
78        sys.exit(1)
79    else:
80
81def runTests():
82    clis = []
83    clis += [ircClient('test1', 'asd')]
84    clis += [ircClient('test2', 'asd')]
85    for cli in clis:
86        cli.connect()
87        cli.jabberLogin()
88
89    clis[0].addJabberBuddy(clis[1].nick)
90
91    print("Test: Send message")
92    testSendMessage(clis[0],clis[1]),'ohai <3')
93    testSendMessage(clis[1],clis[0]),'uwu *pounces*')
94    print("Test passed")
95
96    print("Test: Block/Unblock")
97    clis[0].blockJabberBuddy(clis[1].nick)
98    testSendMessage(clis[1],clis[0]),'m-meow?', shouldreceive = False)
99    clis[0].unblockJabberBuddy(clis[1].nick)
100    testSendMessage(clis[1],clis[0]),'*purrs*')
101    print("Test passed")
102   
103if __name__ == "__main__":
104    runTests()
Note: See TracBrowser for help on using the repository browser.