]> git.proxmox.com Git - swtpm.git/blame - tests/test_clientfds.py
packaging: track dbgsym package for swtpm-libs and swtpm-tools
[swtpm.git] / tests / test_clientfds.py
CommitLineData
b4372fe5 1#!/usr/bin/env python3
93edca48 2
8d4d320b
SB
3import os
4import sys
93edca48
AV
5import socket
6import subprocess
7import time
8import struct
9
10child = None
11fd = -1
12ctrlfd = -1
13
8d4d320b 14
93edca48
AV
15def wait_for_pidfile(pidfile, timeout):
16 while timeout != 0:
17 if os.path.exists(pidfile):
18 return True
19 time.sleep(1)
20 timeout -= 1
21 return False
22
8d4d320b 23
93edca48
AV
24def spawn_swtpm():
25 global child, fd, ctrlfd
26
27 _fd, fd = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
28 _ctrlfd, ctrlfd = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
29
30 swtpm_exe = os.getenv('SWTPM_EXE')
31 tpmpath = os.getenv('TPMDIR')
32 pidfile = os.getenv('PID_FILE')
33
8d4d320b
SB
34 if not swtpm_exe or not tpmpath or not pidfile:
35 print("Missing test environment \n swtpm_exe=%s,\n"
36 " tpmpath=%s\n pidfile=%s" %
37 (swtpm_exe, tpmpath, pidfile))
93edca48
AV
38 return False
39
40 cmd = swtpm_exe + " socket --fd=" + str(_fd.fileno())
41 cmd += " --ctrl type=unixio,clientfd=" + str(_ctrlfd.fileno())
42 cmd += " --pid file=" + pidfile + " --tpmstate dir=" + tpmpath
930c7ba1
SB
43 if os.getenv('SWTPM_TEST_SECCOMP_OPT'):
44 cmd += " " + os.getenv('SWTPM_TEST_SECCOMP_OPT')
93edca48
AV
45 print("Running child cmd: %s" % cmd)
46 try:
610cd51e
SB
47 if sys.version_info[0] >= 3:
48 child = subprocess.Popen(cmd.split(),
49 pass_fds=[_fd.fileno(), _ctrlfd.fileno()])
50 else:
51 child = subprocess.Popen(cmd.split())
93edca48
AV
52 except OSError as err:
53 print("OS error: %d" % err.errno)
54 return False
55
56 print("Child PID: %d" % child.pid)
57
8d4d320b
SB
58 if not wait_for_pidfile(pidfile, 3):
59 print("waitpid timeout")
93edca48
AV
60 child.kill()
61 child = None
62 return False
63
64 return True
65
8d4d320b 66
93edca48
AV
67def test_get_caps():
68 global ctrlfd
69
70 # test get capabilities
71 # CMD_GET_CAPABILITY = 0x00 00 00 01
8d4d320b 72 cmd_get_caps = bytearray([0x00, 0x00, 0x00, 0x01])
6fbb219d 73 expected_caps = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7f, 0xff])
93edca48
AV
74
75 def toString(arr):
76 return ' '.join('{:02x}'.format(x) for x in arr)
77
78 try:
79 ctrlfd.sendall(cmd_get_caps)
80 except SocketError as e:
81 print("SocketError")
82 buf = ctrlfd.recv(8)
83 if buf:
84 caps = bytearray(buf)
85 if caps == expected_caps:
86 return True
87 else:
8d4d320b
SB
88 print("Unexpected reply for CMD_GET_CAPABILITY: \n"
89 " actual: %s\n expected: %s"
90 % (toString(caps), toString(expected_caps)))
93edca48
AV
91 return False
92 else:
93 print("Null reply from swtpm")
94 return False
95
96if __name__ == "__main__":
97 try:
8d4d320b 98 if not spawn_swtpm() or not test_get_caps():
93edca48
AV
99 res = 1
100 else:
101 res = 0
102 except:
610cd51e 103 print("__Exception: ", sys.exc_info())
93edca48
AV
104 res = -1
105
8d4d320b
SB
106 if child:
107 child.terminate()
93edca48
AV
108
109 sys.exit(res)