[946c6da] | 1 | import socket |
---|
| 2 | import sys |
---|
| 3 | import time |
---|
| 4 | import select |
---|
[90417ce] | 5 | |
---|
[a4623f4] | 6 | YESTEST = True |
---|
[946c6da] | 7 | MESSAGETEST = True |
---|
[c248e37] | 8 | BLOCKTEST = False |
---|
[946c6da] | 9 | OFFLINETEST = False |
---|
| 10 | RENAMETEST = True |
---|
[d28ca33] | 11 | SHOWLOG = False |
---|
| 12 | SHOWTESTLOG = True |
---|
[a4623f4] | 13 | |
---|
[c248e37] | 14 | SEPARATOR = "="*60 |
---|
| 15 | SMOLPARATOR = "-"*60 |
---|
[946c6da] | 16 | |
---|
| 17 | class IrcClient: |
---|
| 18 | def __init__(self, nick, pwd): |
---|
[90417ce] | 19 | self.nick = nick |
---|
[946c6da] | 20 | self.pwd = pwd |
---|
[90417ce] | 21 | self.log = '' |
---|
[23fd63d] | 22 | self.tmplog = '' |
---|
[90417ce] | 23 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 24 | |
---|
[0167270] | 25 | def send_raw(self, msg, loud = False, log = True): |
---|
[d24e6f7] | 26 | self.receive() |
---|
[ef2cbca] | 27 | if loud: |
---|
| 28 | print('FROM '+ self.nick + '|| ' + msg) |
---|
[0167270] | 29 | if log: |
---|
| 30 | self.log += msg+'\r\n' |
---|
| 31 | self.tmplog += msg+'\r\n' |
---|
[90417ce] | 32 | self.sck.send((msg+'\r\n').encode()) |
---|
| 33 | |
---|
[d28ca33] | 34 | def send_priv_msg(self, recip, msg, loud = False): |
---|
[946c6da] | 35 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
[90417ce] | 36 | |
---|
| 37 | def connect(self): |
---|
[314c3f8] | 38 | try: |
---|
| 39 | self.sck.connect(('127.0.0.1', 6667)) |
---|
| 40 | except: |
---|
[ef2cbca] | 41 | print("IRC connection failed for " + self.nick) |
---|
[314c3f8] | 42 | sys.exit(1) |
---|
[ef2cbca] | 43 | |
---|
| 44 | print("IRC connection established for " + self.nick) |
---|
[314c3f8] | 45 | |
---|
[946c6da] | 46 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
| 47 | self.send_raw('NICK ' + self.nick) |
---|
| 48 | self.send_raw('JOIN &bitlbee') |
---|
[90417ce] | 49 | |
---|
[946c6da] | 50 | def jabber_login(self): |
---|
| 51 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
[90417ce] | 52 | time.sleep(0.3) |
---|
[946c6da] | 53 | self.send_priv_msg("&bitlbee", "account on") |
---|
[314c3f8] | 54 | time.sleep(1) |
---|
| 55 | self.receive() |
---|
| 56 | if self.log.find('Logged in') == -1: |
---|
[ef2cbca] | 57 | print("Jabber login failed for " + self.nick) |
---|
[314c3f8] | 58 | sys.exit(1) |
---|
[ef2cbca] | 59 | else: |
---|
| 60 | print("Jabber login successful for " + self.nick) |
---|
[90417ce] | 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: |
---|
[946c6da] | 70 | self.send_raw('PONG ' + line.split()[1]) |
---|
[90417ce] | 71 | else: |
---|
| 72 | break |
---|
| 73 | self.log += text |
---|
[23fd63d] | 74 | self.tmplog += text |
---|
[90417ce] | 75 | return text |
---|
| 76 | |
---|
[946c6da] | 77 | def add_jabber_buddy(self, nick): |
---|
| 78 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
[ef2cbca] | 79 | |
---|
[946c6da] | 80 | def block_jabber_buddy(self, nick): |
---|
| 81 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
[43a257d] | 82 | |
---|
[946c6da] | 83 | def unblock_jabber_buddy(self, nick): |
---|
| 84 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
[ef2cbca] | 85 | |
---|
[946c6da] | 86 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
| 87 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
[ef2cbca] | 88 | |
---|
[a4623f4] | 89 | def msg_comes_thru(sender, receiver, message): |
---|
[946c6da] | 90 | sender.send_priv_msg(receiver.nick, message) |
---|
[12886d4] | 91 | received = receiver.receive().find(message) != -1 |
---|
[a4623f4] | 92 | return received |
---|
| 93 | |
---|
[d28ca33] | 94 | def perform_test(failed, clis, test_function, test_name): |
---|
| 95 | fail = False |
---|
[a4623f4] | 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") |
---|
[d28ca33] | 106 | failed += [test_name] |
---|
| 107 | fail = True |
---|
[a4623f4] | 108 | |
---|
[d28ca33] | 109 | if fail or SHOWTESTLOG: |
---|
[a4623f4] | 110 | for cli in clis: |
---|
| 111 | if cli.tmplog != "": |
---|
| 112 | print(SMOLPARATOR) |
---|
| 113 | print("Test Log "+ cli.nick+":") |
---|
| 114 | print(cli.tmplog) |
---|
[c248e37] | 115 | print(SEPARATOR) |
---|
[a4623f4] | 116 | |
---|
| 117 | def yes_test(clis): |
---|
| 118 | ret = False |
---|
| 119 | for _ in range(100): |
---|
[0167270] | 120 | clis[0].send_raw("PRIVMSG yes", loud = False, log = False) |
---|
[a4623f4] | 121 | clis[0].receive() |
---|
[0167270] | 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: |
---|
[a4623f4] | 125 | ret = True |
---|
| 126 | print("The RNG gods smile upon us") |
---|
| 127 | break |
---|
| 128 | return ret |
---|
| 129 | |
---|
| 130 | def 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 | |
---|
| 135 | def 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 | |
---|
| 142 | def 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 |
---|
[43a257d] | 153 | |
---|
[d28ca33] | 154 | def run_tests(failed): |
---|
[90417ce] | 155 | clis = [] |
---|
[946c6da] | 156 | clis += [IrcClient('test1', 'asd')] |
---|
| 157 | clis += [IrcClient('test2', 'asd')] |
---|
[90417ce] | 158 | for cli in clis: |
---|
| 159 | cli.connect() |
---|
[946c6da] | 160 | |
---|
[a4623f4] | 161 | if YESTEST: |
---|
[d28ca33] | 162 | perform_test(failed, clis, yes_test, "Yes") |
---|
[23fd63d] | 163 | |
---|
[0167270] | 164 | print("") |
---|
[23fd63d] | 165 | for cli in clis: |
---|
| 166 | cli.jabber_login() |
---|
[d28ca33] | 167 | |
---|
[946c6da] | 168 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
| 169 | |
---|
| 170 | if MESSAGETEST: |
---|
[d28ca33] | 171 | perform_test(failed, clis, message_test, "Send message") |
---|
[946c6da] | 172 | |
---|
| 173 | if BLOCKTEST: |
---|
[d28ca33] | 174 | perform_test(failed, clis, block_test, "Block user") |
---|
[d24e6f7] | 175 | |
---|
[946c6da] | 176 | if RENAMETEST: |
---|
[d28ca33] | 177 | perform_test(failed, clis, rename_test, "Rename user") |
---|
[a4623f4] | 178 | |
---|
[d28ca33] | 179 | if failed or SHOWLOG: |
---|
[a4623f4] | 180 | print("") |
---|
| 181 | for cli in clis: |
---|
| 182 | print(SMOLPARATOR) |
---|
| 183 | print("Log "+ cli.nick+":") |
---|
| 184 | print(cli.log) |
---|
| 185 | print(SMOLPARATOR) |
---|
| 186 | |
---|
[d28ca33] | 187 | if failed: |
---|
[a4623f4] | 188 | print("\n" + SEPARATOR + "\nSome test have failed:") |
---|
[d28ca33] | 189 | for fail in failed: |
---|
[a4623f4] | 190 | print(fail) |
---|
| 191 | else: |
---|
| 192 | print("\n" + SEPARATOR + "\nAll tests have passed") |
---|
[ef2cbca] | 193 | |
---|
[90417ce] | 194 | if __name__ == "__main__": |
---|
[d28ca33] | 195 | failed = [] |
---|
| 196 | run_tests(failed) |
---|
| 197 | if failed: |
---|
[946c6da] | 198 | sys.exit(1) |
---|