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