1#!/usr/bin/expect -f
2
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements.  See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership.  The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License.  You may obtain a copy of the License at
10#
11#   http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied.  See the License for the
17# specific language governing permissions and limitations
18# under the License.
19
20# Expect script for OP-TEE CI Test
21#
22# This script starts QEMU, loads and boots Linux/OP-TEE, then runs
23# Rust applications in the guest. The return code is 0 for success,
24# >0 for error.
25# It is invoked in OP-TEE/build/qemu_v8.mk and can be triggered by
26# "$ cd OPTEE_DIR/build && make CFG_TEE_CORE_LOG_LEVEL=0 check-rust"
27
28set bios "../out/bios-qemu/bios.bin"
29set quiet 0
30set timeout 900
31
32# Parse command line
33set myargs $argv
34while {[llength $myargs]} {
35	set myargs [lassign $myargs arg]
36	switch -exact -- $arg {
37		"--bios"	{set myargs [lassign $myargs ::bios]}
38		"--timeout"	{set myargs [lassign $myargs ::timeout]}
39		"-q"		{set ::quiet 1}
40	}
41}
42
43proc info arg {
44	if {$::quiet==1} { return }
45	puts -nonewline $arg
46	flush stdout
47}
48
49# Disable echoing of guest output
50log_user 0
51# Save guest console output to a file
52log_file -a -noappend "serial0.log"
53info "Starting QEMU...\n"
54open "serial1.log" "w+"
55spawn -open [open "|tail -f serial1.log"]
56set teecore $spawn_id
57if {[string first "aarch64" $::env(QEMU)] != -1} {
58	spawn $::env(QEMU) -nographic -serial mon:stdio -serial file:serial1.log -smp $::env(QEMU_SMP) -machine virt,secure=on,gic-version=$::env(QEMU_GIC) -cpu cortex-a57 -d unimp -semihosting-config enable=on,target=native -m $::env(QEMU_MEM) -bios bl1.bin -initrd rootfs.cpio.gz -kernel Image -no-acpi -append "console=ttyAMA0,38400 keep_bootcon root=/dev/vda2"
59} else {
60	spawn $::env(QEMU) -nographic -monitor none -machine virt -machine secure=on -cpu cortex-a15 -smp $::env(QEMU_SMP) -d unimp -semihosting-config enable=on,target=native -m 1057 -serial stdio -serial file:serial1.log -bios $bios
61}
62expect {
63	"Kernel panic" {
64		info "!!! Kernel panic\n"
65		exit 1
66	}
67	timeout {
68		info "!!! Timeout\n"
69		exit 1
70	}
71	"ogin:"
72}
73send -- "root\r\r"
74expect "# "
75info " done, guest is booted"
76info ".\n"
77
78# Test Rust applications
79info "Test Rust applications:\n"
80info "Running acipher-rs...\n"
81send -- "acipher-rs 256 teststring\r"
82expect {
83	-re "Success decrypt the above ciphertext as (\\d+) bytes plain text:" {
84		info "Test success\n"
85	}
86	timeout {
87		info "!!! Timeout: Test failed\n"
88		exit 1
89	}
90}
91
92expect "# "
93info "Running aes-rs...\n"
94send -- "aes-rs\r"
95expect {
96	-re "Prepare encode operation.*Encode buffer from TA.*Clear text and decoded text match" {
97		info "Test success\n"
98	}
99	timeout {
100		info "!!! Timeout: Test failed\n"
101		exit 1
102	}
103}
104
105expect "# "
106info "Running authentication-rs...\n"
107send -- "authentication-rs\r"
108expect {
109	-re "Clear text and decoded text match.*Success" {
110		info "Test success\n"
111	}
112	timeout {
113		info "!!! Timeout: Test failed\n"
114		exit 1
115	}
116}
117
118expect "# "
119info "Running big_int-rs...\n"
120send -- "big_int-rs\r"
121expect {
122	"Success" {
123		info "Test success\n"
124	}
125	timeout {
126		info "!!! Timeout: Test failed\n"
127		exit 1
128	}
129}
130
131expect "# "
132info "Running diffie_hellman-rs...\n"
133send -- "diffie_hellman-rs\r"
134expect {
135	-re "get key (\\d+) pair as public:.*private.*Derived share key as.*Success" {
136		info "Test success\n"
137	}
138	timeout {
139		info "!!! Timeout: Test failed\n"
140		exit 1
141	}
142}
143
144expect "# "
145info "Running digest-rs...\n"
146send -- "digest-rs message1 message2\r"
147expect {
148	-re "Get message hash as:.*Success" {
149		info "Test success\n"
150	}
151	timeout {
152		info "!!! Timeout: Test failed\n"
153		exit 1
154	}
155}
156
157expect "# "
158info "Running hello_world-rs...\n"
159send -- "hello_world-rs\r"
160expect {
161	-re "original value is 29.*inc value is 129.*dec value is 29.*Success" {
162		info "Test success\n"
163	}
164	timeout {
165		info "!!! Timeout: Test failed\n"
166		exit 1
167	}
168}
169
170expect "# "
171info "Running hotp-rs...\n"
172send -- "hotp-rs\r"
173expect {
174	-re "Get HOTP.*Success" {
175		info "Test success\n"
176	}
177	timeout {
178		info "!!! Timeout: Test failed\n"
179		exit 1
180	}
181}
182
183expect "# "
184info "Running message_passing_interface-rs...\n"
185send -- "message_passing_interface-rs\r"
186expect {
187	-re "Hello, World" {
188		info "Test success\n"
189	}
190	timeout {
191		info "!!! Timeout: Test failed\n"
192		exit 1
193	}
194}
195
196expect "# "
197info "Running random-rs...\n"
198send -- "random-rs\r"
199expect {
200	-re "Generate random UUID: \[a-z0-9]*-\[a-z0-9]*-\[a-z0-9]*-\[a-z0-9]*.*Success" {
201		info "Test success\n"
202	}
203	timeout {
204		info "!!! Timeout: Test failed\n"
205		exit 1
206	}
207}
208
209expect "# "
210info "Running secure_storage-rs...\n"
211send -- "secure_storage-rs\r"
212expect {
213	-re "We're done, close and release TEE resources" {
214		info "Test success\n"
215	}
216	timeout {
217		info "!!! Timeout: Test failed\n"
218		exit 1
219	}
220}
221
222expect "# "
223info "Running serde-rs...\n"
224send -- "serde-rs\r"
225expect {
226	"Success" {
227		info "Test success\n"
228	}
229	timeout {
230		info "!!! Timeout: Test failed\n"
231		exit 1
232	}
233}
234
235expect "# "
236info "Running supp_plugin-rs...\n"
237send -- "supp_plugin-rs\r"
238expect {
239	-re "invoke commmand finished" {
240		info "Test success\n"
241	}
242	timeout {
243		info "!!! Timeout: Test failed\n"
244		exit 1
245	}
246}
247
248expect "# "
249info "Running tcp_client-rs...\n"
250send -- "tcp_client-rs\r"
251expect {
252	"Success" {
253		info "Test success\n"
254	}
255	timeout {
256		info "!!! Timeout: Test failed\n"
257		exit 1
258	}
259}
260
261expect "# "
262info "Running time-rs...\n"
263send -- "time-rs\r"
264expect {
265	"Success" {
266		info "Test success\n"
267	}
268	timeout {
269		info "!!! Timeout: Test failed\n"
270		exit 1
271	}
272}
273
274expect "# "
275info "Running udp_socket-rs...\n"
276send -- "udp_socket-rs\r"
277expect {
278	"Success" {
279		info "Test success\n"
280	}
281	timeout {
282		info "!!! Timeout: Test failed\n"
283		exit 1
284	}
285}
286info "Test Rust application finished\n"
287