Skip to content

Commit e82a339

Browse files
add uart CI test
1 parent dc2caed commit e82a339

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
name: Test UART Communication with SWTPM
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ 'master', 'main', 'release/**' ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test-uart-swtpm:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Install dependencies
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y \
22+
automake \
23+
autotools-dev \
24+
libtool \
25+
pkg-config \
26+
gcc \
27+
make \
28+
git \
29+
socat \
30+
libglib2.0-dev \
31+
libtasn1-dev \
32+
expect \
33+
netcat-openbsd
34+
35+
- name: Install OpenSSL
36+
run: |
37+
sudo apt-get install -y libssl-dev
38+
39+
- name: Build and install libtpms
40+
run: |
41+
git clone https://github.com/stefanberger/libtpms.git
42+
cd libtpms
43+
./autogen.sh --with-tpm2 --with-openssl --prefix=/usr
44+
make -j$(nproc)
45+
sudo make install
46+
cd ..
47+
48+
- name: Build and install swtpm
49+
run: |
50+
git clone https://github.com/stefanberger/swtpm.git
51+
cd swtpm
52+
./autogen.sh
53+
./configure --with-openssl --with-tpm2 --prefix=/usr
54+
make -j$(nproc)
55+
sudo make install
56+
cd ..
57+
58+
- name: Build wolfSSL
59+
run: |
60+
git clone https://github.com/wolfSSL/wolfssl.git
61+
cd wolfssl
62+
./autogen.sh
63+
./configure --enable-wolftpm --prefix=$PWD/../wolfssl-install
64+
make -j$(nproc)
65+
make install
66+
cd ..
67+
68+
- name: Create virtual UART pair
69+
id: uart
70+
run: |
71+
# Create a PTY pair for UART simulation using socat
72+
# This creates two pseudo-terminals that are connected
73+
# One end will be used by swtpm (server side)
74+
# The other end will be used by wolfTPM (client side, as UART device)
75+
socat -d -d pty,raw,echo=0,link=/tmp/tpm-uart-server pty,raw,echo=0,link=/tmp/tpm-uart-client &
76+
SOCAT_PID=$!
77+
echo $SOCAT_PID > /tmp/socat.pid
78+
sleep 2
79+
80+
# Get the actual PTY device names
81+
SERVER_PTY=$(readlink -f /tmp/tpm-uart-server)
82+
CLIENT_PTY=$(readlink -f /tmp/tpm-uart-client)
83+
84+
echo "server_pty=$SERVER_PTY" >> $GITHUB_OUTPUT
85+
echo "client_pty=$CLIENT_PTY" >> $GITHUB_OUTPUT
86+
87+
echo "Server PTY (for swtpm): $SERVER_PTY"
88+
echo "Client PTY (for wolfTPM): $CLIENT_PTY"
89+
90+
# Verify PTYs exist
91+
ls -la $SERVER_PTY $CLIENT_PTY || exit 1
92+
93+
- name: Start swtpm with chardev (UART)
94+
run: |
95+
SERVER_PTY="${{ steps.uart.outputs.server_pty }}"
96+
mkdir -p /tmp/swtpm-state
97+
98+
# Start swtpm with chardev backend using the server PTY
99+
# This allows swtpm to communicate over the PTY as if it were a UART
100+
swtpm chardev \
101+
--tpm2 \
102+
--tpmstate dir=/tmp/swtpm-state \
103+
--chardev $SERVER_PTY \
104+
--flags not-need-init &
105+
SWTPM_PID=$!
106+
echo $SWTPM_PID > /tmp/swtpm.pid
107+
108+
# Give swtpm time to start
109+
sleep 3
110+
111+
# Verify swtpm is running
112+
ps aux | grep swtpm | grep -v grep || exit 1
113+
114+
- name: Build wolfTPM with UART support
115+
env:
116+
CLIENT_PTY: ${{ steps.uart.outputs.client_pty }}
117+
run: |
118+
cd ${{ github.workspace }}
119+
./autogen.sh
120+
# Set UART device path and baud rate via CFLAGS
121+
# The device path needs to be properly quoted in the C define
122+
export CFLAGS="-DTPM2_SWTPM_HOST=\\\"$CLIENT_PTY\\\" -DTPM2_SWTPM_PORT=115200"
123+
echo "Building with UART device: $CLIENT_PTY"
124+
./configure \
125+
--enable-swtpm=uart \
126+
--with-wolfcrypt=$PWD/../wolfssl-install
127+
make -j$(nproc)
128+
129+
- name: Verify UART setup
130+
env:
131+
CLIENT_PTY: ${{ steps.uart.outputs.client_pty }}
132+
SERVER_PTY: ${{ steps.uart.outputs.server_pty }}
133+
run: |
134+
echo "Verifying UART setup..."
135+
echo "Client PTY: $CLIENT_PTY"
136+
echo "Server PTY: $SERVER_PTY"
137+
138+
# Verify PTYs are still accessible
139+
[ -c "$CLIENT_PTY" ] || (echo "Client PTY not found!" && exit 1)
140+
[ -c "$SERVER_PTY" ] || (echo "Server PTY not found!" && exit 1)
141+
142+
# Verify swtpm is still running
143+
ps aux | grep swtpm | grep -v grep || (echo "swtpm not running!" && exit 1)
144+
145+
echo "UART setup verified successfully"
146+
147+
- name: Run UART communication test
148+
env:
149+
CLIENT_PTY: ${{ steps.uart.outputs.client_pty }}
150+
run: |
151+
cd ${{ github.workspace }}
152+
153+
# Build the caps example
154+
cd examples/wrap
155+
make caps
156+
157+
echo "Running UART communication test..."
158+
echo "Using UART device: $CLIENT_PTY"
159+
160+
# Run the test with a timeout
161+
# The test should connect to the PTY as if it were a UART device
162+
timeout 30 ./caps || {
163+
echo "Test failed!"
164+
echo "Checking if swtpm is still running..."
165+
ps aux | grep swtpm | grep -v grep || echo "swtpm is not running"
166+
exit 1
167+
}
168+
169+
echo "UART communication test passed!"
170+
171+
- name: Cleanup
172+
if: always()
173+
run: |
174+
# Kill swtpm
175+
if [ -f /tmp/swtpm.pid ]; then
176+
kill $(cat /tmp/swtpm.pid) 2>/dev/null || true
177+
fi
178+
179+
# Kill socat PTY pair
180+
if [ -f /tmp/socat.pid ]; then
181+
kill $(cat /tmp/socat.pid) 2>/dev/null || true
182+
fi
183+
184+
# Clean up PTY links
185+
rm -f /tmp/tpm-uart-server /tmp/tpm-uart-client

0 commit comments

Comments
 (0)