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 | |
---|
11 | class Test(unittest.TestCase): |
---|
12 | def openssl(self, args): |
---|
13 | with open(os.devnull, "w") as devnull: |
---|
14 | proc = subprocess.Popen(['openssl'] + args, stdin=subprocess.PIPE, stderr=devnull) |
---|
15 | for i in range(6): |
---|
16 | proc.stdin.write("\n") |
---|
17 | proc.stdin.close() |
---|
18 | proc.communicate() |
---|
19 | def mock(self, name): |
---|
20 | skyped_log = open("t/skyped.log", "w") |
---|
21 | skyped = subprocess.Popen([sys.executable, "skyped.py", "-c", "t/skyped/skyped.conf", "-n", "-d", "-m", "t/%s-skyped.mock" % name], |
---|
22 | stdout=skyped_log, stderr=subprocess.STDOUT) |
---|
23 | |
---|
24 | try: |
---|
25 | bitlbee = pexpect.spawn('../../bitlbee', ['-d', 't/bitlbee']) |
---|
26 | bitlbee_mock = open("t/%s-bitlbee.mock" % name) |
---|
27 | for i in bitlbee_mock.readlines(): |
---|
28 | line = i.strip() |
---|
29 | if line.startswith(">> "): |
---|
30 | bitlbee.expect_exact(line[3:], timeout=10) |
---|
31 | elif line.startswith("<< "): |
---|
32 | bitlbee.sendline(line[3:]) |
---|
33 | bitlbee_mock.close() |
---|
34 | bitlbee.close() |
---|
35 | finally: |
---|
36 | skyped.terminate() |
---|
37 | skyped.communicate() |
---|
38 | skyped_log.close() |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | try: |
---|
42 | shutil.rmtree("t/bitlbee") |
---|
43 | except OSError: |
---|
44 | pass |
---|
45 | os.makedirs("t/bitlbee") |
---|
46 | |
---|
47 | try: |
---|
48 | shutil.rmtree("t/skyped") |
---|
49 | except OSError: |
---|
50 | pass |
---|
51 | os.makedirs("t/skyped") |
---|
52 | cwd = os.getcwd() |
---|
53 | os.chdir("t/skyped") |
---|
54 | shutil.copyfile("../../skyped.cnf", "skyped.cnf") |
---|
55 | self.openssl(['req', '-new', '-x509', '-days', '365', '-nodes', '-config', 'skyped.cnf', '-out', 'skyped.cert.pem', '-keyout', 'skyped.key.pem']) |
---|
56 | with open("skyped.conf", "w") as sock: |
---|
57 | sock.write("[skyped]\n") |
---|
58 | sock.write("username = alice\n") |
---|
59 | sock.write("password = %s\n" % hashlib.sha1("foo").hexdigest()) |
---|
60 | sock.write("cert = %s/skyped.cert.pem\n" % os.getcwd()) |
---|
61 | sock.write("key = %s/skyped.key.pem\n" % os.getcwd()) |
---|
62 | sock.write("port = 2727\n") |
---|
63 | os.chdir(cwd) |
---|
64 | |
---|
65 | |
---|
66 | def testMsg(self): |
---|
67 | self.mock("msg") |
---|
68 | |
---|
69 | def testLogin(self): |
---|
70 | self.mock("login") |
---|
71 | |
---|
72 | def testInfo(self): |
---|
73 | self.mock("info") |
---|
74 | |
---|
75 | def testCall(self): |
---|
76 | self.mock("call") |
---|
77 | |
---|
78 | def testCallFailed(self): |
---|
79 | self.mock("call-failed") |
---|
80 | |
---|
81 | def testAddYes(self): |
---|
82 | self.mock("add-yes") |
---|
83 | |
---|
84 | def testAddNo(self): |
---|
85 | self.mock("add-no") |
---|
86 | |
---|
87 | def testGroupchatInvited(self): |
---|
88 | self.mock("groupchat-invited") |
---|
89 | |
---|
90 | def testGroupchatInvite(self): |
---|
91 | self.mock("groupchat-invite") |
---|
92 | |
---|
93 | def testCalledYes(self): |
---|
94 | self.mock("called-yes") |
---|
95 | |
---|
96 | def testCalledNo(self): |
---|
97 | self.mock("called-no") |
---|
98 | |
---|
99 | if __name__ == '__main__': |
---|
100 | unittest.main() |
---|