Changeset 946c6da
- Timestamp:
- 2021-03-15T13:45:09Z (4 years ago)
- Children:
- e98404d
- Parents:
- 484e460
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
.tests/test1.py
r484e460 r946c6da 1 import socket, sys, time, select 1 import socket 2 import sys 3 import time 4 import select 2 5 3 class ircClient: 4 def __init__(self, nick, pw): 6 MESSAGETEST = True 7 BLOCKTEST = False 8 OFFLINETEST = False 9 RENAMETEST = True 10 11 FAILED = False 12 13 class IrcClient: 14 def __init__(self, nick, pwd): 5 15 self.nick = nick 6 self.pw = pw16 self.pwd = pwd 7 17 self.log = '' 8 18 self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 19 10 def send Raw(self, msg, loud = True):20 def send_raw(self, msg, loud = True): 11 21 self.receive() 12 22 if loud: … … 15 25 self.sck.send((msg+'\r\n').encode()) 16 26 17 def send PrivMsg(self, recip, msg, loud = True):18 self.send Raw('PRIVMSG '+recip+' :'+msg, loud)27 def send_priv_msg(self, recip, msg, loud = True): 28 self.send_raw('PRIVMSG '+recip+' :'+msg, loud) 19 29 20 30 def connect(self): … … 27 37 print("IRC connection established for " + self.nick) 28 38 29 self.send Raw('USER ' + (self.nick + " ")*3)30 self.send Raw('NICK ' + self.nick)31 self.send Raw('JOIN &bitlbee')39 self.send_raw('USER ' + (self.nick + " ")*3) 40 self.send_raw('NICK ' + self.nick) 41 self.send_raw('JOIN &bitlbee') 32 42 33 def jabber Login(self):34 self.send PrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw)43 def jabber_login(self): 44 self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) 35 45 time.sleep(0.3) 36 self.send PrivMsg("&bitlbee", "account on")46 self.send_priv_msg("&bitlbee", "account on") 37 47 time.sleep(1) 38 48 self.receive() … … 51 61 for line in text.split('\n'): 52 62 if line.find('PING') != -1: 53 self.send Raw('PONG ' + line.split()[1])63 self.send_raw('PONG ' + line.split()[1]) 54 64 else: 55 65 break … … 57 67 return text 58 68 59 def add JabberBuddy(self, nick):60 self.send PrivMsg("&bitlbee", "add 0 " + nick+"@localhost")69 def add_jabber_buddy(self, nick): 70 self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") 61 71 62 def block JabberBuddy(self, nick):63 self.send PrivMsg("&bitlbee", "block " + nick)72 def block_jabber_buddy(self, nick): 73 self.send_priv_msg("&bitlbee", "block " + nick) 64 74 65 def unblock JabberBuddy(self, nick):66 self.send PrivMsg("&bitlbee", "allow " + nick)75 def unblock_jabber_buddy(self, nick): 76 self.send_priv_msg("&bitlbee", "allow " + nick) 67 77 68 def rename JabberBuddy(self, oldnick, newnick):69 self.send PrivMsg("&bitlbee", "rename " + oldnick + " " + newnick)78 def rename_jabber_buddy(self, oldnick, newnick): 79 self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) 70 80 71 def test SendMessage(sender, receiver, message, shouldreceive = True):72 sender.send PrivMsg(receiver.nick, message)81 def test_send_message(sender, receiver, message): 82 sender.send_priv_msg(receiver.nick, message) 73 83 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:\n' + sender.log) 77 print('Receiver Log:\n' + receiver.log) 78 sys.exit(1) 84 return received; 79 85 80 def runTests(): 86 def run_tests(): 87 global FAILED 81 88 clis = [] 82 clis += [ ircClient('test1', 'asd')]83 clis += [ ircClient('test2', 'asd')]89 clis += [IrcClient('test1', 'asd')] 90 clis += [IrcClient('test2', 'asd')] 84 91 for cli in clis: 85 92 cli.connect() 86 cli.jabber Login()93 cli.jabber_login() 87 94 88 clis[0].add JabberBuddy(clis[1].nick)95 clis[0].add_jabber_buddy(clis[1].nick) 89 96 90 print("Test: Send message") 91 testSendMessage(clis[0], clis[1], 'ohai <3') 92 testSendMessage(clis[1], clis[0], 'uwu *pounces*') 93 print("Test passed") 97 if MESSAGETEST: 98 print("Test: Send message") 99 ret = test_send_message(clis[0], clis[1], 'ohai <3') 100 ret = ret & test_send_message(clis[1], clis[0], 'uwu *pounces*') 101 if ret: 102 print("Test passed") 103 else: 104 print("Test failed") 105 FAILED = True; 106 94 107 95 print("Test: Block/Unblock") 96 clis[0].blockJabberBuddy(clis[1].nick) 97 testSendMessage(clis[1], clis[0], 'm-meow?', shouldreceive = False) 98 clis[0].unblockJabberBuddy(clis[1].nick) 99 testSendMessage(clis[1], clis[0], '*purrs*') 100 print("Test passed") 108 if BLOCKTEST: 109 print("Test: Block/Unblock") 110 clis[0].block_jabber_buddy(clis[1].nick) 111 ret = not test_send_message(clis[1], clis[0], 'm-meow?') 112 clis[0].unblock_jabber_buddy(clis[1].nick) 113 ret = ret & test_send_message(clis[1], clis[0], '*purrs*') 114 if ret: 115 print("Test passed") 116 else: 117 print("Test failed") 118 FAILED = True; 119 120 if RENAMETEST: 121 print("Test: Rename buddy") 122 newname = "xXx_pup_LINKENPARK4EVA<3" 123 message = "rawr meanmz i luv u in dinosaur" 124 125 clis[0].rename_jabber_buddy(clis[1].nick, newname) 126 clis[0].send_priv_msg(newname, message) 127 ret = clis[1].receive().find(message) != -1 128 129 clis[0].rename_jabber_buddy(newname, clis[1].nick) 130 ret = ret & test_send_message(clis[0], clis[1], "rawr") 131 if ret: 132 print("Test passed") 133 else: 134 print("Test failed") 135 FAILED = True; 101 136 102 137 if __name__ == "__main__": 103 runTests() 138 run_tests() 139 if FAILED: 140 sys.exit(1)
Note: See TracChangeset
for help on using the changeset viewer.