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 | with open("t/skyped.log", "w") as skyped_log,\ |
---|
21 | open("t/pexpect.log", "w") as pexpect_log: |
---|
22 | skyped = subprocess.Popen([sys.executable, "skyped.py", |
---|
23 | "-c", "t/skyped/skyped.conf", "-n", "-d", "-m", "t/%s-skyped.mock" % name], |
---|
24 | stdout=skyped_log, stderr=subprocess.STDOUT) |
---|
25 | try: |
---|
26 | bitlbee = pexpect.spawn('../../bitlbee', ['-d', 't/bitlbee'], logfile=pexpect_log) |
---|
27 | if os.environ.get('ATTACH_GDB'): |
---|
28 | subprocess.Popen(['gdb', '-batch-silent', |
---|
29 | '-ex', 'set logging overwrite on', |
---|
30 | '-ex', 'set logging file t/gdb-%s.log' % bitlbee.pid, |
---|
31 | '-ex', 'set logging on', |
---|
32 | '-ex', 'handle all pass nostop noprint', |
---|
33 | '-ex', 'handle SIGSEGV pass stop print', |
---|
34 | '-ex', 'set pagination 0', |
---|
35 | '-ex', 'continue', |
---|
36 | '-ex', 'backtrace full', |
---|
37 | '-ex', 'info registers', |
---|
38 | '-ex', 'thread apply all backtrace', |
---|
39 | '-ex', 'quit', |
---|
40 | '../../bitlbee', str(bitlbee.pid) ]) |
---|
41 | bitlbee_mock = open("t/%s-bitlbee.mock" % name) |
---|
42 | for i in bitlbee_mock.readlines(): |
---|
43 | line = i.strip() |
---|
44 | if line.startswith(">> "): |
---|
45 | bitlbee.expect_exact(line[3:], timeout=10) |
---|
46 | elif line.startswith("<< "): |
---|
47 | bitlbee.sendline(line[3:]) |
---|
48 | bitlbee_mock.close() |
---|
49 | bitlbee.close() |
---|
50 | finally: |
---|
51 | skyped.terminate() |
---|
52 | skyped.communicate() |
---|
53 | |
---|
54 | def setUp(self): |
---|
55 | try: |
---|
56 | shutil.rmtree("t/bitlbee") |
---|
57 | except OSError: |
---|
58 | pass |
---|
59 | os.makedirs("t/bitlbee") |
---|
60 | |
---|
61 | try: |
---|
62 | shutil.rmtree("t/skyped") |
---|
63 | except OSError: |
---|
64 | pass |
---|
65 | os.makedirs("t/skyped") |
---|
66 | cwd = os.getcwd() |
---|
67 | os.chdir("t/skyped") |
---|
68 | try: |
---|
69 | shutil.copyfile("../../skyped.cnf", "skyped.cnf") |
---|
70 | self.openssl(['req', '-new', '-x509', '-days', '365', '-nodes', '-config', 'skyped.cnf', '-out', 'skyped.cert.pem', '-keyout', 'skyped.key.pem']) |
---|
71 | with open("skyped.conf", "w") as sock: |
---|
72 | sock.write("[skyped]\n") |
---|
73 | sock.write("username = alice\n") |
---|
74 | sock.write("password = %s\n" % hashlib.sha1("foo").hexdigest()) |
---|
75 | sock.write("cert = %s/skyped.cert.pem\n" % os.getcwd()) |
---|
76 | sock.write("key = %s/skyped.key.pem\n" % os.getcwd()) |
---|
77 | sock.write("port = 2727\n") |
---|
78 | finally: |
---|
79 | os.chdir(cwd) |
---|
80 | |
---|
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") |
---|
90 | |
---|
91 | def testCall(self): |
---|
92 | self.mock("call") |
---|
93 | |
---|
94 | def testCallFailed(self): |
---|
95 | self.mock("call-failed") |
---|
96 | |
---|
97 | def testAddYes(self): |
---|
98 | self.mock("add-yes") |
---|
99 | |
---|
100 | def testAddedYes(self): |
---|
101 | self.mock("added-yes") |
---|
102 | |
---|
103 | def testAddedNo(self): |
---|
104 | self.mock("added-no") |
---|
105 | |
---|
106 | def testGroupchatInvited(self): |
---|
107 | self.mock("groupchat-invited") |
---|
108 | |
---|
109 | def testGroupchatInvite(self): |
---|
110 | self.mock("groupchat-invite") |
---|
111 | |
---|
112 | def testCalledYes(self): |
---|
113 | self.mock("called-yes") |
---|
114 | |
---|
115 | def testCalledNo(self): |
---|
116 | self.mock("called-no") |
---|
117 | |
---|
118 | def testFiletransfer(self): |
---|
119 | self.mock("filetransfer") |
---|
120 | |
---|
121 | def testGroupRead(self): |
---|
122 | self.mock("group-read") |
---|
123 | |
---|
124 | def testCtcpHelp(self): |
---|
125 | self.mock("ctcp-help") |
---|
126 | |
---|
127 | if __name__ == '__main__': |
---|
128 | unittest.main() |
---|