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 | 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 | |
---|
39 | class Test(unittest.TestCase): |
---|
40 | def mock(self, name): |
---|
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], |
---|
45 | stdout=skyped_log, stderr=subprocess.STDOUT) |
---|
46 | try: |
---|
47 | bitlbee = pexpect.spawn('../../bitlbee', ['-d', 't/bitlbee'], logfile=pexpect_log) |
---|
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) ]) |
---|
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() |
---|
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") |
---|
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 testGroupchatLeave(self): |
---|
113 | self.mock("groupchat-leave") |
---|
114 | |
---|
115 | def testGroupchatMsg(self): |
---|
116 | self.mock("groupchat-msg") |
---|
117 | |
---|
118 | def testGroupchatTopic(self): |
---|
119 | self.mock("groupchat-topic") |
---|
120 | |
---|
121 | def testCalledYes(self): |
---|
122 | self.mock("called-yes") |
---|
123 | |
---|
124 | def testCalledNo(self): |
---|
125 | self.mock("called-no") |
---|
126 | |
---|
127 | def testFiletransfer(self): |
---|
128 | self.mock("filetransfer") |
---|
129 | |
---|
130 | def testGroupRead(self): |
---|
131 | self.mock("group-read") |
---|
132 | |
---|
133 | def testGroupAdd(self): |
---|
134 | self.mock("group-add") |
---|
135 | |
---|
136 | def testCtcpHelp(self): |
---|
137 | self.mock("ctcp-help") |
---|
138 | |
---|
139 | def testSetMoodText(self): |
---|
140 | self.mock("set-mood-text") |
---|
141 | |
---|
142 | def testAwaySet(self): |
---|
143 | self.mock("away-set") |
---|
144 | |
---|
145 | if __name__ == '__main__': |
---|
146 | setupSkyped() |
---|
147 | unittest.main() |
---|