[946c6da] | 1 | import socket |
---|
| 2 | import sys |
---|
| 3 | import time |
---|
| 4 | import select |
---|
[90417ce] | 5 | |
---|
[c386390] | 6 | FUN = [ |
---|
| 7 | "Did I ask you something?", |
---|
| 8 | "Oh yeah, that's right.", |
---|
| 9 | "Alright, alright. Now go back to work.", |
---|
| 10 | "Buuuuuuuuuuuuuuuurp... Excuse me!", |
---|
| 11 | "Yes?", |
---|
| 12 | "No?", |
---|
| 13 | ] |
---|
| 14 | |
---|
[c248e37] | 15 | SEPARATOR = "="*60 |
---|
[946c6da] | 16 | |
---|
[50efbf4] | 17 | ALWAYSSHOWLOG = False |
---|
| 18 | |
---|
[946c6da] | 19 | class IrcClient: |
---|
| 20 | def __init__(self, nick, pwd): |
---|
[90417ce] | 21 | self.nick = nick |
---|
[946c6da] | 22 | self.pwd = pwd |
---|
[90417ce] | 23 | self.log = '' |
---|
[23fd63d] | 24 | self.tmplog = '' |
---|
[90417ce] | 25 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 26 | |
---|
[0167270] | 27 | def send_raw(self, msg, loud = False, log = True): |
---|
[d24e6f7] | 28 | self.receive() |
---|
[ef2cbca] | 29 | if loud: |
---|
| 30 | print('FROM '+ self.nick + '|| ' + msg) |
---|
[0167270] | 31 | if log: |
---|
| 32 | self.log += msg+'\r\n' |
---|
| 33 | self.tmplog += msg+'\r\n' |
---|
[90417ce] | 34 | self.sck.send((msg+'\r\n').encode()) |
---|
| 35 | |
---|
[d28ca33] | 36 | def send_priv_msg(self, recip, msg, loud = False): |
---|
[946c6da] | 37 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
[90417ce] | 38 | |
---|
| 39 | def connect(self): |
---|
[a18cf1b] | 40 | connected = False |
---|
| 41 | for x in range(5): |
---|
| 42 | try: |
---|
| 43 | self.sck.connect(('127.0.0.1', 6667)) |
---|
| 44 | connected = True |
---|
| 45 | break |
---|
| 46 | except: |
---|
| 47 | time.sleep(1) |
---|
| 48 | |
---|
| 49 | if not connected: |
---|
[c386390] | 50 | return False |
---|
[314c3f8] | 51 | |
---|
[946c6da] | 52 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
| 53 | self.send_raw('NICK ' + self.nick) |
---|
| 54 | self.send_raw('JOIN &bitlbee') |
---|
[c386390] | 55 | return True |
---|
[90417ce] | 56 | |
---|
[946c6da] | 57 | def jabber_login(self): |
---|
| 58 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
[90417ce] | 59 | time.sleep(0.3) |
---|
[946c6da] | 60 | self.send_priv_msg("&bitlbee", "account on") |
---|
[314c3f8] | 61 | time.sleep(1) |
---|
| 62 | self.receive() |
---|
[b9b29f3] | 63 | return (self.log.find('Logged in') != -1) |
---|
[90417ce] | 64 | |
---|
[bda5012] | 65 | def receive(self, wait = 1): |
---|
[90417ce] | 66 | text = '' |
---|
| 67 | while True: |
---|
| 68 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
| 69 | if self.sck in readable: |
---|
| 70 | text += self.sck.recv(2040).decode() |
---|
| 71 | for line in text.split('\n'): |
---|
| 72 | if line.find('PING') != -1: |
---|
[946c6da] | 73 | self.send_raw('PONG ' + line.split()[1]) |
---|
[90417ce] | 74 | else: |
---|
[bda5012] | 75 | time.sleep(wait) |
---|
| 76 | if self.sck not in readable: |
---|
| 77 | break |
---|
[90417ce] | 78 | self.log += text |
---|
[23fd63d] | 79 | self.tmplog += text |
---|
[90417ce] | 80 | return text |
---|
| 81 | |
---|
[946c6da] | 82 | def add_jabber_buddy(self, nick): |
---|
| 83 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
[ef2cbca] | 84 | |
---|
[946c6da] | 85 | def block_jabber_buddy(self, nick): |
---|
| 86 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
[43a257d] | 87 | |
---|
[946c6da] | 88 | def unblock_jabber_buddy(self, nick): |
---|
| 89 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
[ef2cbca] | 90 | |
---|
[946c6da] | 91 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
| 92 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
[ef2cbca] | 93 | |
---|
[a4623f4] | 94 | def msg_comes_thru(sender, receiver, message): |
---|
[946c6da] | 95 | sender.send_priv_msg(receiver.nick, message) |
---|
[12886d4] | 96 | received = receiver.receive().find(message) != -1 |
---|
[a4623f4] | 97 | return received |
---|
| 98 | |
---|
[e7434db] | 99 | def perform_test(test_function): |
---|
[2806b5e] | 100 | clis = [] |
---|
[25b84f06] | 101 | clis += [IrcClient('test1', 'asd')] |
---|
| 102 | clis += [IrcClient('test2', 'asd')] |
---|
[a4623f4] | 103 | |
---|
[f0e9ee1] | 104 | fail = not test_function(clis) |
---|
| 105 | |
---|
[50efbf4] | 106 | if ALWAYSSHOWLOG or fail: |
---|
[b2d7fdd] | 107 | print(test_function.__name__ + ' logs:') |
---|
[50efbf4] | 108 | for cli in clis: |
---|
| 109 | cli.receive() |
---|
| 110 | print(SEPARATOR) |
---|
| 111 | print("Test Log "+ cli.nick+":") |
---|
| 112 | print(cli.tmplog) |
---|
[2806b5e] | 113 | print(SEPARATOR) |
---|
[6083ea9] | 114 | else: |
---|
[b2d7fdd] | 115 | print(test_function.__name__ + ' succeeded') |
---|
[2806b5e] | 116 | |
---|
| 117 | if fail: |
---|
| 118 | sys.exit(1) |
---|
[f0e9ee1] | 119 | |
---|
[c386390] | 120 | def connect_test(clis): |
---|
| 121 | ret = True |
---|
| 122 | for cli in clis: |
---|
| 123 | ret = ret & cli.connect() |
---|
| 124 | return ret |
---|
[a4623f4] | 125 | |
---|
| 126 | def yes_test(clis): |
---|
| 127 | ret = False |
---|
[a3d01fb] | 128 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
[da75c3d] | 129 | clis[0].receive() |
---|
| 130 | for x, fun in enumerate(FUN): |
---|
| 131 | if (clis[0].log.find(fun) != -1): |
---|
[a4623f4] | 132 | ret = True |
---|
[da75c3d] | 133 | if x: |
---|
| 134 | print("The RNG gods smile upon us") |
---|
[a4623f4] | 135 | break |
---|
| 136 | return ret |
---|
| 137 | |
---|
[c386390] | 138 | def jabber_login_test(clis): |
---|
| 139 | ret = True |
---|
| 140 | for cli in clis: |
---|
| 141 | ret = ret & cli.jabber_login() |
---|
| 142 | return ret |
---|
| 143 | |
---|
[542b7d5] | 144 | def jabber_delete_account_test(clis): |
---|
| 145 | ret = True |
---|
[0a98f92] | 146 | clis[1].receive() |
---|
[542b7d5] | 147 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
[0a98f92] | 148 | ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1) |
---|
[542b7d5] | 149 | |
---|
| 150 | clis[1].send_priv_msg("&bitlbee", "account on") |
---|
| 151 | clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del") |
---|
[0a98f92] | 152 | clis[1].receive() |
---|
[542b7d5] | 153 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
[0a98f92] | 154 | ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1) |
---|
[542b7d5] | 155 | |
---|
| 156 | clis[1].send_priv_msg("&bitlbee", "account off") |
---|
| 157 | clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del") |
---|
[0a98f92] | 158 | clis[1].receive() |
---|
[542b7d5] | 159 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
[0a98f92] | 160 | ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') == -1) |
---|
[542b7d5] | 161 | return ret |
---|
| 162 | |
---|
[5d927631] | 163 | def register_test(clis): |
---|
| 164 | clis[1].send_priv_msg("&bitlbee", "register "+clis[1].pwd*2) |
---|
[bda5012] | 165 | ret = (clis[1].receive(wait = 10).find('Account successfully created') != -1) |
---|
[b77884c] | 166 | return ret |
---|
[5d927631] | 167 | |
---|
| 168 | def unregister_test(clis): |
---|
| 169 | clis[1].send_priv_msg("&bitlbee", "drop "+clis[1].pwd*2) |
---|
| 170 | ret = (clis[1].receive().find('removed') != -1) |
---|
| 171 | clis[1].send_priv_msg("&bitlbee", "drop "+clis[1].pwd*2) |
---|
| 172 | ret = ret & (clis[1].receive().find('That account does not exist') != -1) |
---|
| 173 | return ret |
---|
| 174 | |
---|
| 175 | def identify_test(clis): |
---|
| 176 | ret = True |
---|
[478a5f6] | 177 | clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd) |
---|
[5d927631] | 178 | ret = ret & (clis[1].receive().find('Incorrect password') != -1) |
---|
| 179 | |
---|
[478a5f6] | 180 | clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd*2) |
---|
[5d927631] | 181 | ret = ret & (clis[1].receive().find('Password accepted') != -1) |
---|
| 182 | return ret |
---|
| 183 | |
---|
| 184 | def identify_nonexist_test(clis): |
---|
[3b6a6e5] | 185 | clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd) |
---|
[5d927631] | 186 | return (clis[1].receive().find('The nick is (probably) not registered') != -1) |
---|
| 187 | |
---|
[51f144a] | 188 | def add_buddy_test(clis): |
---|
| 189 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
[2c7f522] | 190 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
[51f144a] | 191 | |
---|
[2c7f522] | 192 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
[51f144a] | 193 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
| 194 | |
---|
| 195 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
[8d7cc55] | 196 | junk = clis[0].receive() |
---|
| 197 | ret = junk.find(clis[1].nick) != -1 |
---|
| 198 | ret = ret & (junk.find("1 available") != -1) |
---|
[50efbf4] | 199 | return ret |
---|
| 200 | |
---|
| 201 | def remove_buddy_test(clis): |
---|
| 202 | ret = add_buddy_test(clis) |
---|
| 203 | |
---|
[4c87970] | 204 | clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick) |
---|
[50efbf4] | 205 | clis[0].send_priv_msg("&bitlbee", "blist all") |
---|
| 206 | time.sleep(0.5) |
---|
[51f144a] | 207 | ret = ret & (clis[0].receive().find(clis[1].nick) == -1) |
---|
| 208 | |
---|
| 209 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
| 210 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
[50efbf4] | 211 | clis[0].send_priv_msg("&bitlbee", "blist all") |
---|
[3b6a6e5] | 212 | junk = clis[0].receive(wait=5) |
---|
[542b7d5] | 213 | #ret = ret & (junk.find("1 available") != -1) |
---|
[8d7cc55] | 214 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
[da215ef] | 215 | return ret |
---|
[51f144a] | 216 | |
---|
[a4623f4] | 217 | def message_test(clis): |
---|
| 218 | ret = msg_comes_thru(clis[0], clis[1], 'ohai <3') |
---|
| 219 | ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*') |
---|
| 220 | return ret |
---|
| 221 | |
---|
| 222 | def block_test(clis): |
---|
| 223 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
| 224 | ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?') |
---|
| 225 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
| 226 | ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*') |
---|
| 227 | return ret |
---|
| 228 | |
---|
| 229 | def rename_test(clis): |
---|
| 230 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
| 231 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
| 232 | |
---|
| 233 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
| 234 | clis[0].send_priv_msg(newname, message) |
---|
| 235 | ret = clis[1].receive().find(message) != -1 |
---|
| 236 | |
---|
| 237 | clis[0].rename_jabber_buddy("-del", newname) |
---|
| 238 | ret = ret & msg_comes_thru(clis[0], clis[1], "rawr") |
---|
| 239 | return ret |
---|
[43a257d] | 240 | |
---|
[0e2c8b4] | 241 | def status_test(clis): |
---|
[76f0fd1] | 242 | status = "get out of my room mom" |
---|
| 243 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
[0e2c8b4] | 244 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
[35c72b7] | 245 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
[0e2c8b4] | 246 | |
---|
[3d4cb51] | 247 | clis[1].send_priv_msg("&bitlbee", "set") |
---|
| 248 | ret = ret & (clis[1].receive().find(status) != -1) |
---|
| 249 | |
---|
[0e2c8b4] | 250 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
| 251 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
[2e9b39a] | 252 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
[0e2c8b4] | 253 | return ret |
---|
| 254 | |
---|
| 255 | def offline_test(clis): |
---|
[35c72b7] | 256 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
| 257 | |
---|
| 258 | junk = clis[0].receive() |
---|
| 259 | ret = (junk.find(clis[1].nick) != -1) |
---|
[76f0fd1] | 260 | ret = ret & (junk.find("QUIT") != -1) |
---|
[35c72b7] | 261 | |
---|
| 262 | junk = clis[1].receive() |
---|
| 263 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
[76f0fd1] | 264 | ret = ret & (junk.find("QUIT") != -1) |
---|
[35c72b7] | 265 | |
---|
[2e9b39a] | 266 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
[76f0fd1] | 267 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
[35c72b7] | 268 | |
---|
| 269 | clis[0].send_priv_msg("&bitlbee", "account on") |
---|
| 270 | |
---|
| 271 | junk = clis[0].receive() |
---|
| 272 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
[76f0fd1] | 273 | ret = ret & (junk.find("JOIN") != -1) |
---|
[35c72b7] | 274 | |
---|
| 275 | junk = clis[1].receive() |
---|
| 276 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
[76f0fd1] | 277 | ret = ret & (junk.find("JOIN") != -1) |
---|
[35c72b7] | 278 | |
---|
| 279 | return ret |
---|
[0e2c8b4] | 280 | |
---|
[7361a6a] | 281 | def default_target_test(clis): |
---|
| 282 | clis[0].send_priv_msg("&bitlbee", "set default_target last") |
---|
[5552588] | 283 | clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now") |
---|
| 284 | |
---|
| 285 | ret = (clis[1].receive().find("ur mah default now") != -1) |
---|
[7361a6a] | 286 | |
---|
[5552588] | 287 | clis[0].send_priv_msg("&bitlbee", "mrow") |
---|
| 288 | ret = ret & (clis[1].receive().find("mrow") != -1) |
---|
[7361a6a] | 289 | |
---|
| 290 | clis[0].send_priv_msg("root", "set default_target root") |
---|
[68ba9f4] | 291 | junk = clis[0].receive() |
---|
| 292 | ret = ret & (junk.find("default_target") != -1) |
---|
| 293 | ret = ret & (junk.find("root") != -1) |
---|
[7361a6a] | 294 | |
---|
| 295 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
| 296 | ret = ret & (clis[1].receive().find("yes") == -1) |
---|
| 297 | return ret |
---|
| 298 | |
---|
[3d4cb51] | 299 | def help_test(clis): |
---|
| 300 | clis[0].send_priv_msg("&bitlbee", "help") |
---|
| 301 | ret = (clis[0].receive().find("identify_methods") != -1) |
---|
| 302 | clis[0].send_priv_msg("&bitlbee", "help commands") |
---|
| 303 | ret = ret & (clis[0].receive().find("qlist") != -1) |
---|
| 304 | return ret |
---|