refactor(expect): factor common Expect failure cases into `handle-arguments.inc`

This change factors common Expect failure cases like timeouts and
premature EOFs into the common `handle-arguments.inc` Expect script
fragment.

This fragment now implicitly inserts a timeout handler and an EOF
handler into every expectation introduced after the inclusion of this
fragment. These handlers have the lowest priority, so can be overridden
if necessary.

Signed-off-by: Chris Kay <chris.kay@arm.com>
Change-Id: Ia92f0046ea123d3ed213aa76dc5fce6163471451
diff --git a/expect/disable_dyn_auth_tftf.exp b/expect/disable_dyn_auth_tftf.exp
index 0985976..6a61112 100644
--- a/expect/disable_dyn_auth_tftf.exp
+++ b/expect/disable_dyn_auth_tftf.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2020 Arm Limited. All rights reserved.
+# Copyright (c) 2020-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -24,9 +24,6 @@
 		puts "<<TFTF Fail>>"
 		exit_uart -1
 	}
-	timeout {
-		exit_timeout
-	}
 }
 
 exit_uart -1
diff --git a/expect/ffa_test_driver.exp b/expect/ffa_test_driver.exp
index 28fd6da..eb98e4b 100644
--- a/expect/ffa_test_driver.exp
+++ b/expect/ffa_test_driver.exp
@@ -11,18 +11,12 @@
                 send "\n\nroot\n"
                 puts "<<Linux Booted>>"
         }
-        timeout {
-                exit_uart -1
-        }
 }
 
 expect {
         "#" {
                 send "ioctl 255\n"
         }
-        timeout {
-                exit_uart -1
-        }
 }
 
 expect_string "Test: Echo Message to SP"
@@ -40,9 +34,6 @@
         "Total Failures: 0" {
                 puts "<<SUCCESS>>"
         }
-        timeout {
-                exit_uart -1
-        }
 }
 
 exit_uart 0
diff --git a/expect/fvp-r-yocto.exp b/expect/fvp-r-yocto.exp
index ae1de8b..a7ac912 100644
--- a/expect/fvp-r-yocto.exp
+++ b/expect/fvp-r-yocto.exp
@@ -1,5 +1,5 @@
 #

-# Copyright (c) 2021 Arm Limited. All rights reserved.

+# Copyright (c) 2021-2022 Arm Limited. All rights reserved.

 #

 # SPDX-License-Identifier: BSD-3-Clause

 #

@@ -13,18 +13,12 @@
 	"Booting Trusted Firmware" {

 		puts "<<Booting Trusted Firmware>>"

 	}

-	timeout {

-		exit_uart -1

-	}

 }

 

 expect {

 	"BL1: Booting BL33" {

 		puts "<<Booting BL33>>"

 	}

-	timeout {

-		exit_uart -1

-	}

 }

 

 # Uboot

@@ -32,9 +26,6 @@
 	"U-Boot" {

 		puts "<<Entered Uboot>>"

 	}

-	timeout {

-		exit_uart -1

-	}

 }

 

 # Yocto

@@ -42,9 +33,6 @@
 	"Booting Linux on physical CPU" {

 		puts "<<Booting Linux>>"

 	}

-	timeout {

-		exit_uart -1

-	}

 }

 

 expect {

@@ -52,16 +40,10 @@
 		puts "<<Yocto Login Prompt Received>>"

 		send "root\n"

 	}

-	timeout {

-		exit_uart -1

-	}

 }

 

 expect {

 	"#" {

 		puts "<<Successfully Reached Yocto Shell>>"

 	}

-	timeout {

-		exit_uart -1

-	}

 }

diff --git a/expect/handle-arguments.inc b/expect/handle-arguments.inc
index 6a730a5..53ee95b 100644
--- a/expect/handle-arguments.inc
+++ b/expect/handle-arguments.inc
@@ -21,3 +21,14 @@
     set uart_log_file [get_param uart_log_file]
     set telnet_pid [spawn cat $uart_log_file]
 }
+
+expect_after {
+    eof {
+        puts "<<stream closed prematurely, exiting>>"
+        exit -1
+    }
+
+    timeout {
+        exit_timeout
+    }
+}
diff --git a/expect/linux-tpm-384.exp b/expect/linux-tpm-384.exp
index 5324d4e..57674ff 100644
--- a/expect/linux-tpm-384.exp
+++ b/expect/linux-tpm-384.exp
@@ -22,10 +22,6 @@
         "TCG_EfiSpecIDEvent:" {
                 set digest_log [open $TFA_DIGEST w]
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
@@ -42,20 +38,12 @@
         -exact "Booting BL31" {
                 close $digest_log
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
         "login" {
                 send "root\n"
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
@@ -63,18 +51,10 @@
                 # Load the fTPM driver
                 send "ftpm\n"
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
         "#" { }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 # Iterate over the rest of PCRs and check that they all are zeros.
@@ -87,10 +67,6 @@
                 -re $non_zero_pcr {
                         exit_uart -1
                 }
-
-                timeout {
-                        exit_timeout
-                }
         }
 }
 
@@ -101,10 +77,6 @@
         "#" {
                 spawn diff -s $TFA_DIGEST ftpm_event_log
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
diff --git a/expect/linux-tpm.exp b/expect/linux-tpm.exp
index ee8ae8a..6783503 100644
--- a/expect/linux-tpm.exp
+++ b/expect/linux-tpm.exp
@@ -21,10 +21,6 @@
         "TCG_EfiSpecIDEvent:" {
                 set digest_log [open $TFA_DIGEST w]
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
@@ -41,20 +37,12 @@
         -exact "Booting BL31" {
                 close $digest_log
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
         "login" {
                 send "root\n"
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
@@ -62,10 +50,6 @@
                 # Load the fTPM driver and retrieves PCR0
                 send "ftpm\n"
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
@@ -79,10 +63,6 @@
                 # get PCR1 value
                 send "pcrread -ha 1\n"
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
@@ -93,10 +73,6 @@
         }
 
         "#" { }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 # Iterate over the rest of PCRs and check that they all are zeros.
@@ -109,10 +85,6 @@
                 -re $non_zero_pcr {
                         exit_uart -1
                 }
-
-                timeout {
-                        exit_timeout
-                }
         }
 }
 
@@ -123,10 +95,6 @@
         "#" {
                 spawn diff -s $TFA_DIGEST ftpm_event_log
         }
-
-        timeout {
-                exit_timeout
-        }
 }
 
 expect {
diff --git a/expect/spm-cactus-sp-uart1.exp b/expect/spm-cactus-sp-uart1.exp
index 8a136b3..2ddefb9 100644
--- a/expect/spm-cactus-sp-uart1.exp
+++ b/expect/spm-cactus-sp-uart1.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2021, Arm Limited. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -13,9 +13,6 @@
 	"Booting Secure Partition"  {
 		puts "<<SP entry>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 # Hold UART until test completes
diff --git a/expect/spm-edk2-uart0.exp b/expect/spm-edk2-uart0.exp
index 72b0f11..ce854df 100644
--- a/expect/spm-edk2-uart0.exp
+++ b/expect/spm-edk2-uart0.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -18,36 +18,24 @@
 	"UEFI firmware" {
 		puts "<<EDK2 starting>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"UEFI Interactive Shell" {
 		puts "<<EDK2 shell starting>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"any other key to continue." {
 		send "\r"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"Shell>" {
 		send "fs0:\r"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
@@ -55,27 +43,18 @@
 		send "UefiInfo.efi\r"
 		puts "<<Loading UEFI application>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	-re "Loading driver at .* UefiInfo.efi" {
 		puts "<<UEFI application is being loaded>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"FS0:" {
 		puts "<<UEFI application loaded>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 exit_uart 0
diff --git a/expect/spm-edk2-uart2.exp b/expect/spm-edk2-uart2.exp
index a6a2536..74480bd 100644
--- a/expect/spm-edk2-uart2.exp
+++ b/expect/spm-edk2-uart2.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -14,36 +14,24 @@
 	"SPM Version" {
 		puts "<<Secure Partition booting>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"MmMain Done!" {
 		puts "<<Secure Partition booted>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"Received event - 0xC4000041 on cpu"  {
 		puts "<<Received event>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"MmEntryPoint Done"  {
 		puts "<<Secure Partition done>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 exit_uart 0
diff --git a/expect/spm-linux-uart0.exp b/expect/spm-linux-uart0.exp
index dc6aebd..1895bd8 100644
--- a/expect/spm-linux-uart0.exp
+++ b/expect/spm-linux-uart0.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -11,27 +11,18 @@
 		puts "<<Linux PVM booted>>"
 		send "\r"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"/ # " {
 		send "insmod hafnium.ko\n"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"Hafnium successfully loaded with 1 VMs:" {
 		puts "<<Hafnium driver started>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 exit_uart 0
diff --git a/expect/spm-optee-sp-uart1.exp b/expect/spm-optee-sp-uart1.exp
index 6908609..289c91d 100644
--- a/expect/spm-optee-sp-uart1.exp
+++ b/expect/spm-optee-sp-uart1.exp
@@ -10,18 +10,12 @@
 	"OP-TEE version: "  {
 		puts "<<OP-TEE version>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 expect {
 	"Primary CPU switching to normal world boot"  {
 		puts "<<OP-TEE initialized>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 # Hold UART until test completes
diff --git a/expect/spm-uart2.exp b/expect/spm-uart2.exp
index fa05f0c..f9b22eb 100644
--- a/expect/spm-uart2.exp
+++ b/expect/spm-uart2.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -10,9 +10,6 @@
 	"Booting Secure Partition"  {
 		puts "<<SP entry>>"
 	}
-	timeout {
-		exit_uart -1
-	}
 }
 
 # UART2 captures prints from SP0. If we exit from here, expect script will stop
diff --git a/expect/tftf-aarch32.exp b/expect/tftf-aarch32.exp
index 0b197b0..12a8bb3 100644
--- a/expect/tftf-aarch32.exp
+++ b/expect/tftf-aarch32.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -26,9 +26,6 @@
 		expect_string "Exiting tests." "<<TFTF Fail>>"
 		exit_uart -1
 	}
-	timeout {
-		exit_timeout
-	}
 }
 
 exit_uart -1
diff --git a/expect/tftf-non-primary.exp b/expect/tftf-non-primary.exp
index 38536d3..8bf23f8 100644
--- a/expect/tftf-non-primary.exp
+++ b/expect/tftf-non-primary.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2021 Arm Limited. All rights reserved.
+# Copyright (c) 2021-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -24,9 +24,6 @@
 		puts "<<TFTF Fail>>"
 		exit_uart -1
 	}
-	timeout {
-		exit_timeout
-	}
 }
 
 exit_uart -1
diff --git a/expect/tftf-sgi.exp b/expect/tftf-sgi.exp
index 56c28b8..63626e9 100644
--- a/expect/tftf-sgi.exp
+++ b/expect/tftf-sgi.exp
@@ -24,9 +24,6 @@
 		expect_string "Exiting tests." "<<TFTF Fail>>"
 		exit_uart -1
 	}
-	timeout {
-		exit_timeout
-	}
 }
 
 exit_uart -1
diff --git a/expect/tftf.exp b/expect/tftf.exp
index 87a5f33..2b1ed24 100644
--- a/expect/tftf.exp
+++ b/expect/tftf.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -26,9 +26,6 @@
 		expect_string "Exiting tests." "<<TFTF Fail>>"
 		exit_uart -1
 	}
-	timeout {
-		exit_timeout
-	}
 }
 
 exit_uart -1
diff --git a/expect/trusted-firmware-load-error.exp b/expect/trusted-firmware-load-error.exp
index 174c06e..3f326fa 100644
--- a/expect/trusted-firmware-load-error.exp
+++ b/expect/trusted-firmware-load-error.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -10,5 +10,4 @@
 expect {
 	"Loading of FW_CONFIG failed" { exit_uart 0 }
 	"BL1: Booting BL2"		{ exit_uart -1 }
-	timeout				{ exit_timeout }
 }
diff --git a/expect/tsp.exp b/expect/tsp.exp
index ce7fba4..f65b389 100644
--- a/expect/tsp.exp
+++ b/expect/tsp.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -39,9 +39,6 @@
 					break
 				}
 			}
-			timeout {
-				exit_timeout
-			}
 		}
 	}
 }
diff --git a/expect/ubsan-test-trap.exp b/expect/ubsan-test-trap.exp
index 6b30d0b..4ed5202 100644
--- a/expect/ubsan-test-trap.exp
+++ b/expect/ubsan-test-trap.exp
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -15,10 +15,6 @@
 	"Unexpected BRK instruction with value $trap_value" {
 		exit_uart 0
 	}
-
-	timeout {
-                exit_uart -1
-        }
 }
 
 exit_uart -1