[650d2b4] | 1 | #!/usr/bin/env python2.7 |
---|
| 2 | |
---|
| 3 | import subprocess |
---|
| 4 | import sys |
---|
| 5 | import pexpect |
---|
| 6 | import unittest |
---|
| 7 | import shutil |
---|
| 8 | import os |
---|
| 9 | import hashlib |
---|
| 10 | |
---|
[94d9511] | 11 | def openssl(args): |
---|
| 12 | with open(os.devnull, "w") as devnull: |
---|
| 13 | proc = subprocess.Popen(['openssl'] + args, stdin=subprocess.PIPE, stderr=devnull) |
---|
| 14 | for i in range(6): |
---|
| 15 | proc.stdin.write("\n") |
---|
| 16 | proc.stdin.close() |
---|
| 17 | proc.communicate() |
---|
| 18 | def setupSkyped(): |
---|
| 19 | try: |
---|
| 20 | shutil.rmtree("t/skyped") |
---|
| 21 | except OSError: |
---|
| 22 | pass |
---|
| 23 | os.makedirs("t/skyped") |
---|
| 24 | cwd = os.getcwd() |
---|
| 25 | os.chdir("t/skyped") |
---|
| 26 | try: |
---|
| 27 | shutil.copyfile("../../skyped.cnf", "skyped.cnf") |
---|
| 28 | openssl(['req', '-new', '-x509', '-days', '365', '-nodes', '-config', 'skyped.cnf', '-out', 'skyped.cert.pem', '-keyout', 'skyped.key.pem']) |
---|
| 29 | with open("skyped.conf", "w") as sock: |
---|
| 30 | sock.write("[skyped]\n") |
---|
| 31 | sock.write("username = alice\n") |
---|
| 32 | sock.write("password = %s\n" % hashlib.sha1("foo").hexdigest()) |
---|
| 33 | sock.write("cert = %s/skyped.cert.pem\n" % os.getcwd()) |
---|
| 34 | sock.write("key = %s/skyped.key.pem\n" % os.getcwd()) |
---|
| 35 | sock.write("port = 2727\n") |
---|
| 36 | finally: |
---|
| 37 | os.chdir(cwd) |
---|
| 38 | |
---|
[650d2b4] | 39 | class Test(unittest.TestCase): |
---|
| 40 | def mock(self, name): |
---|
[1146e21] | 41 | with open("t/skyped.log", "w") as skyped_log,\ |
---|
| 42 | open("t/pexpect.log", "w") as pexpect_log: |
---|
| 43 | skyped = subprocess.Popen([sys.executable, "skyped.py", |
---|
| 44 | "-c", "t/skyped/skyped.conf", "-n", "-d", "-m", "t/%s-skyped.mock" % name], |
---|
[650d2b4] | 45 | stdout=skyped_log, stderr=subprocess.STDOUT) |
---|
[1146e21] | 46 | try: |
---|
| 47 | bitlbee = pexpect.spawn('../../bitlbee', ['-d', 't/bitlbee'], logfile=pexpect_log) |
---|
[3e23850] | 48 | if os.environ.get('ATTACH_GDB'): |
---|
| 49 | subprocess.Popen(['gdb', '-batch-silent', |
---|
| 50 | '-ex', 'set logging overwrite on', |
---|
| 51 | '-ex', 'set logging file t/gdb-%s.log' % bitlbee.pid, |
---|
| 52 | '-ex', 'set logging on', |
---|
| 53 | '-ex', 'handle all pass nostop noprint', |
---|
| 54 | '-ex', 'handle SIGSEGV pass stop print', |
---|
| 55 | '-ex', 'set pagination 0', |
---|
| 56 | '-ex', 'continue', |
---|
| 57 | '-ex', 'backtrace full', |
---|
| 58 | '-ex', 'info registers', |
---|
| 59 | '-ex', 'thread apply all backtrace', |
---|
| 60 | '-ex', 'quit', |
---|
| 61 | '../../bitlbee', str(bitlbee.pid) ]) |
---|
[1146e21] | 62 | bitlbee_mock = open("t/%s-bitlbee.mock" % name) |
---|
| 63 | for i in bitlbee_mock.readlines(): |
---|
| 64 | line = i.strip() |
---|
| 65 | if line.startswith(">> "): |
---|
| 66 | bitlbee.expect_exact(line[3:], timeout=10) |
---|
| 67 | elif line.startswith("<< "): |
---|
| 68 | bitlbee.sendline(line[3:]) |
---|
| 69 | bitlbee_mock.close() |
---|
| 70 | bitlbee.close() |
---|
| 71 | finally: |
---|
| 72 | skyped.terminate() |
---|
| 73 | skyped.communicate() |
---|
[650d2b4] | 74 | |
---|
| 75 | def setUp(self): |
---|
| 76 | try: |
---|
| 77 | shutil.rmtree("t/bitlbee") |
---|
| 78 | except OSError: |
---|
| 79 | pass |
---|
| 80 | os.makedirs("t/bitlbee") |
---|
| 81 | |
---|
| 82 | def testMsg(self): |
---|
| 83 | self.mock("msg") |
---|
| 84 | |
---|
| 85 | def testLogin(self): |
---|
| 86 | self.mock("login") |
---|
| 87 | |
---|
| 88 | def testInfo(self): |
---|
| 89 | self.mock("info") |
---|
[ee3bccd] | 90 | |
---|
[650d2b4] | 91 | def testCall(self): |
---|
| 92 | self.mock("call") |
---|
[ee3bccd] | 93 | |
---|
[69ae6f2] | 94 | def testCallFailed(self): |
---|
| 95 | self.mock("call-failed") |
---|
[ee3bccd] | 96 | |
---|
[78e103e] | 97 | def testAddYes(self): |
---|
| 98 | self.mock("add-yes") |
---|
| 99 | |
---|
[33ed455] | 100 | def testAddedYes(self): |
---|
| 101 | self.mock("added-yes") |
---|
[650d2b4] | 102 | |
---|
[33ed455] | 103 | def testAddedNo(self): |
---|
| 104 | self.mock("added-no") |
---|
[db0d979] | 105 | |
---|
[d4b5e5b] | 106 | def testGroupchatInvited(self): |
---|
| 107 | self.mock("groupchat-invited") |
---|
| 108 | |
---|
[a6a2d81] | 109 | def testGroupchatInvite(self): |
---|
| 110 | self.mock("groupchat-invite") |
---|
[ee3bccd] | 111 | |
---|
[daac755] | 112 | def testCalledYes(self): |
---|
| 113 | self.mock("called-yes") |
---|
[a6a2d81] | 114 | |
---|
[f6cab7c] | 115 | def testCalledNo(self): |
---|
| 116 | self.mock("called-no") |
---|
| 117 | |
---|
[db34b06] | 118 | def testFiletransfer(self): |
---|
| 119 | self.mock("filetransfer") |
---|
| 120 | |
---|
[ef64837] | 121 | def testGroupRead(self): |
---|
| 122 | self.mock("group-read") |
---|
| 123 | |
---|
[3a92020] | 124 | def testCtcpHelp(self): |
---|
| 125 | self.mock("ctcp-help") |
---|
| 126 | |
---|
[130f7b8] | 127 | def testSetMoodText(self): |
---|
| 128 | self.mock("set-mood-text") |
---|
| 129 | |
---|
[650d2b4] | 130 | if __name__ == '__main__': |
---|
[94d9511] | 131 | setupSkyped() |
---|
[650d2b4] | 132 | unittest.main() |
---|