[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 |
---|
[35c72b7] | 9 | OFFLINETEST = True |
---|
[946c6da] | 10 | RENAMETEST = True |
---|
[0e2c8b4] | 11 | STATUSTEST= True |
---|
[d28ca33] | 12 | SHOWLOG = False |
---|
| 13 | SHOWTESTLOG = True |
---|
[a4623f4] | 14 | |
---|
[da75c3d] | 15 | FUN = [ |
---|
| 16 | "Did I ask you something?", |
---|
| 17 | "Oh yeah, that's right.", |
---|
| 18 | "Alright, alright. Now go back to work.", |
---|
| 19 | "Buuuuuuuuuuuuuuuurp... Excuse me!", |
---|
| 20 | "Yes?", |
---|
| 21 | "No?", |
---|
| 22 | ] |
---|
| 23 | |
---|
[c248e37] | 24 | SEPARATOR = "="*60 |
---|
| 25 | SMOLPARATOR = "-"*60 |
---|
[946c6da] | 26 | |
---|
| 27 | class IrcClient: |
---|
| 28 | def __init__(self, nick, pwd): |
---|
[90417ce] | 29 | self.nick = nick |
---|
[946c6da] | 30 | self.pwd = pwd |
---|
[90417ce] | 31 | self.log = '' |
---|
[23fd63d] | 32 | self.tmplog = '' |
---|
[90417ce] | 33 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 34 | |
---|
[0167270] | 35 | def send_raw(self, msg, loud = False, log = True): |
---|
[d24e6f7] | 36 | self.receive() |
---|
[ef2cbca] | 37 | if loud: |
---|
| 38 | print('FROM '+ self.nick + '|| ' + msg) |
---|
[0167270] | 39 | if log: |
---|
| 40 | self.log += msg+'\r\n' |
---|
| 41 | self.tmplog += msg+'\r\n' |
---|
[90417ce] | 42 | self.sck.send((msg+'\r\n').encode()) |
---|
| 43 | |
---|
[d28ca33] | 44 | def send_priv_msg(self, recip, msg, loud = False): |
---|
[946c6da] | 45 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
[90417ce] | 46 | |
---|
| 47 | def connect(self): |
---|
[314c3f8] | 48 | try: |
---|
| 49 | self.sck.connect(('127.0.0.1', 6667)) |
---|
| 50 | except: |
---|
[ef2cbca] | 51 | print("IRC connection failed for " + self.nick) |
---|
[314c3f8] | 52 | sys.exit(1) |
---|
[ef2cbca] | 53 | |
---|
| 54 | print("IRC connection established for " + self.nick) |
---|
[314c3f8] | 55 | |
---|
[946c6da] | 56 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
| 57 | self.send_raw('NICK ' + self.nick) |
---|
| 58 | self.send_raw('JOIN &bitlbee') |
---|
[90417ce] | 59 | |
---|
[946c6da] | 60 | def jabber_login(self): |
---|
| 61 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
[90417ce] | 62 | time.sleep(0.3) |
---|
[946c6da] | 63 | self.send_priv_msg("&bitlbee", "account on") |
---|
[314c3f8] | 64 | time.sleep(1) |
---|
| 65 | self.receive() |
---|
| 66 | if self.log.find('Logged in') == -1: |
---|
[ef2cbca] | 67 | print("Jabber login failed for " + self.nick) |
---|
[314c3f8] | 68 | sys.exit(1) |
---|
[ef2cbca] | 69 | else: |
---|
| 70 | print("Jabber login successful for " + self.nick) |
---|
[90417ce] | 71 | |
---|
| 72 | def receive(self): |
---|
| 73 | text = '' |
---|
| 74 | while True: |
---|
| 75 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
| 76 | if self.sck in readable: |
---|
| 77 | text += self.sck.recv(2040).decode() |
---|
| 78 | for line in text.split('\n'): |
---|
| 79 | if line.find('PING') != -1: |
---|
[946c6da] | 80 | self.send_raw('PONG ' + line.split()[1]) |
---|
[90417ce] | 81 | else: |
---|
| 82 | break |
---|
| 83 | self.log += text |
---|
[23fd63d] | 84 | self.tmplog += text |
---|
[90417ce] | 85 | return text |
---|
| 86 | |
---|
[946c6da] | 87 | def add_jabber_buddy(self, nick): |
---|
| 88 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
[ef2cbca] | 89 | |
---|
[946c6da] | 90 | def block_jabber_buddy(self, nick): |
---|
| 91 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
[43a257d] | 92 | |
---|
[946c6da] | 93 | def unblock_jabber_buddy(self, nick): |
---|
| 94 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
[ef2cbca] | 95 | |
---|
[946c6da] | 96 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
| 97 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
[ef2cbca] | 98 | |
---|
[a4623f4] | 99 | def msg_comes_thru(sender, receiver, message): |
---|
[946c6da] | 100 | sender.send_priv_msg(receiver.nick, message) |
---|
[12886d4] | 101 | received = receiver.receive().find(message) != -1 |
---|
[a4623f4] | 102 | return received |
---|
| 103 | |
---|
[d28ca33] | 104 | def perform_test(failed, clis, test_function, test_name): |
---|
| 105 | fail = False |
---|
[a4623f4] | 106 | for cli in clis: |
---|
| 107 | cli.tmplog="" |
---|
| 108 | |
---|
| 109 | print("\n"+SEPARATOR) |
---|
| 110 | print("Test: "+test_name) |
---|
| 111 | |
---|
| 112 | if test_function(clis): |
---|
| 113 | print("Test passed") |
---|
| 114 | else: |
---|
| 115 | print("Test failed") |
---|
[d28ca33] | 116 | failed += [test_name] |
---|
| 117 | fail = True |
---|
[f1ba258] | 118 | for cli in clis: |
---|
| 119 | cli.receive() |
---|
[a4623f4] | 120 | |
---|
[d28ca33] | 121 | if fail or SHOWTESTLOG: |
---|
[a4623f4] | 122 | for cli in clis: |
---|
| 123 | if cli.tmplog != "": |
---|
| 124 | print(SMOLPARATOR) |
---|
| 125 | print("Test Log "+ cli.nick+":") |
---|
| 126 | print(cli.tmplog) |
---|
[c248e37] | 127 | print(SEPARATOR) |
---|
[a4623f4] | 128 | |
---|
| 129 | def yes_test(clis): |
---|
| 130 | ret = False |
---|
[a3d01fb] | 131 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
[da75c3d] | 132 | clis[0].receive() |
---|
| 133 | for x, fun in enumerate(FUN): |
---|
| 134 | if (clis[0].log.find(fun) != -1): |
---|
[a4623f4] | 135 | ret = True |
---|
[da75c3d] | 136 | if x: |
---|
| 137 | print("The RNG gods smile upon us") |
---|
[a4623f4] | 138 | break |
---|
| 139 | return ret |
---|
| 140 | |
---|
| 141 | def message_test(clis): |
---|
| 142 | ret = msg_comes_thru(clis[0], clis[1], 'ohai <3') |
---|
| 143 | ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*') |
---|
| 144 | return ret |
---|
| 145 | |
---|
| 146 | def block_test(clis): |
---|
| 147 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
| 148 | ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?') |
---|
| 149 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
| 150 | ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*') |
---|
| 151 | return ret |
---|
| 152 | |
---|
| 153 | def rename_test(clis): |
---|
| 154 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
| 155 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
| 156 | |
---|
| 157 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
| 158 | clis[0].send_priv_msg(newname, message) |
---|
| 159 | ret = clis[1].receive().find(message) != -1 |
---|
| 160 | |
---|
| 161 | clis[0].rename_jabber_buddy("-del", newname) |
---|
| 162 | ret = ret & msg_comes_thru(clis[0], clis[1], "rawr") |
---|
| 163 | return ret |
---|
[43a257d] | 164 | |
---|
[0e2c8b4] | 165 | def status_test(clis): |
---|
[76f0fd1] | 166 | status = "get out of my room mom" |
---|
| 167 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
[0e2c8b4] | 168 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
[35c72b7] | 169 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
[0e2c8b4] | 170 | |
---|
| 171 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
| 172 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
[2e9b39a] | 173 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
[0e2c8b4] | 174 | return ret |
---|
| 175 | |
---|
| 176 | def offline_test(clis): |
---|
[35c72b7] | 177 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
| 178 | |
---|
| 179 | junk = clis[0].receive() |
---|
| 180 | ret = (junk.find(clis[1].nick) != -1) |
---|
[76f0fd1] | 181 | ret = ret & (junk.find("QUIT") != -1) |
---|
[35c72b7] | 182 | |
---|
| 183 | junk = clis[1].receive() |
---|
| 184 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
[76f0fd1] | 185 | ret = ret & (junk.find("QUIT") != -1) |
---|
[35c72b7] | 186 | |
---|
[2e9b39a] | 187 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
[76f0fd1] | 188 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
[35c72b7] | 189 | |
---|
| 190 | clis[0].send_priv_msg("&bitlbee", "account on") |
---|
| 191 | |
---|
| 192 | junk = clis[0].receive() |
---|
| 193 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
[76f0fd1] | 194 | ret = ret & (junk.find("JOIN") != -1) |
---|
[35c72b7] | 195 | |
---|
| 196 | junk = clis[1].receive() |
---|
| 197 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
[76f0fd1] | 198 | ret = ret & (junk.find("JOIN") != -1) |
---|
[35c72b7] | 199 | |
---|
| 200 | return ret |
---|
[0e2c8b4] | 201 | |
---|
[d28ca33] | 202 | def run_tests(failed): |
---|
[90417ce] | 203 | clis = [] |
---|
[946c6da] | 204 | clis += [IrcClient('test1', 'asd')] |
---|
| 205 | clis += [IrcClient('test2', 'asd')] |
---|
[90417ce] | 206 | for cli in clis: |
---|
| 207 | cli.connect() |
---|
[946c6da] | 208 | |
---|
[a4623f4] | 209 | if YESTEST: |
---|
[d28ca33] | 210 | perform_test(failed, clis, yes_test, "Yes") |
---|
[23fd63d] | 211 | |
---|
[0167270] | 212 | print("") |
---|
[23fd63d] | 213 | for cli in clis: |
---|
| 214 | cli.jabber_login() |
---|
[d28ca33] | 215 | |
---|
[946c6da] | 216 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
[04d7229] | 217 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
| 218 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
| 219 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
[946c6da] | 220 | |
---|
| 221 | if MESSAGETEST: |
---|
[d28ca33] | 222 | perform_test(failed, clis, message_test, "Send message") |
---|
[946c6da] | 223 | |
---|
| 224 | if BLOCKTEST: |
---|
[d28ca33] | 225 | perform_test(failed, clis, block_test, "Block user") |
---|
[d24e6f7] | 226 | |
---|
[946c6da] | 227 | if RENAMETEST: |
---|
[d28ca33] | 228 | perform_test(failed, clis, rename_test, "Rename user") |
---|
[a4623f4] | 229 | |
---|
[0e2c8b4] | 230 | if STATUSTEST: |
---|
| 231 | perform_test(failed, clis, status_test, "Change status") |
---|
| 232 | |
---|
[35c72b7] | 233 | if OFFLINETEST: |
---|
[04d7229] | 234 | perform_test(failed, clis, offline_test, "Go offline") |
---|
[35c72b7] | 235 | |
---|
[d28ca33] | 236 | if failed or SHOWLOG: |
---|
[a4623f4] | 237 | print("") |
---|
| 238 | for cli in clis: |
---|
| 239 | print(SMOLPARATOR) |
---|
| 240 | print("Log "+ cli.nick+":") |
---|
| 241 | print(cli.log) |
---|
| 242 | print(SMOLPARATOR) |
---|
| 243 | |
---|
[d28ca33] | 244 | if failed: |
---|
[a4623f4] | 245 | print("\n" + SEPARATOR + "\nSome test have failed:") |
---|
[d28ca33] | 246 | for fail in failed: |
---|
[a4623f4] | 247 | print(fail) |
---|
| 248 | else: |
---|
| 249 | print("\n" + SEPARATOR + "\nAll tests have passed") |
---|
[ef2cbca] | 250 | |
---|
[90417ce] | 251 | if __name__ == "__main__": |
---|
[d28ca33] | 252 | failed = [] |
---|
| 253 | run_tests(failed) |
---|
| 254 | if failed: |
---|
[946c6da] | 255 | sys.exit(1) |
---|