#!/usr/bin/expect -f
# This file is part of Firejail project
# Copyright (C) 2014-2024 Firejail Authors
# License GPL v2

set timeout 10
spawn $env(SHELL)
match_max 100000
set appimage_id $spawn_id

send -- "firejail --name=test --appimage hello-x86_64.AppImage\r"
expect {
	timeout {puts "TESTING ERROR 1\n";exit}
	-re "Child process initialized in \[0-9\]+.\[0-9\]+ ms"
}
expect {
	timeout {puts "TESTING ERROR 1\n";exit}
	"Hello, World!"
}
expect {
	timeout {puts "TESTING ERROR 1\n";exit}
	"Operation not permitted" {puts "1"}
	"Permission denied" {puts "2"}
}
expect {
	timeout {puts "TESTING ERROR 1\n";exit}
	"Hello, again!"
}
sleep 5

spawn $env(SHELL)
send -- "firejail --list\r"
expect {
	timeout {puts "TESTING ERROR 3\n";exit}
	":firejail"
}
expect {
	timeout {puts "TESTING ERROR 3.1\n";exit}
	"appimage hello-x86_64"
}
after 100

send -- "firejail --name=blablabla\r"
expect {
	timeout {puts "TESTING ERROR 4\n";exit}
	-re "Child process initialized in \[0-9\]+.\[0-9\]+ ms"
}
sleep 2

spawn $env(SHELL)
send -- "firemon --seccomp\r"
expect {
	timeout {puts "TESTING ERROR 5\n";exit}
	"need to be root" {puts "/proc mounted as hidepid, exiting...\n"; exit}
	"appimage hello-x86_64"
}
expect {
	timeout {puts "TESTING ERROR 5.1 (seccomp)\n";exit}
	"Seccomp:	2"
}
expect {
	timeout {puts "TESTING ERROR 5.1\n";exit}
	"name=blablabla"
}
after 100
send -- "firemon --caps\r"
expect {
	timeout {puts "TESTING ERROR 6\n";exit}
	"appimage hello-x86_64"
}
expect {
	timeout {puts "TESTING ERROR 6.1\n";exit}
	"CapBnd:"
}
expect {
	timeout {puts "TESTING ERROR 6.2\n";exit}
	"0000000000000000"
}
expect {
	timeout {puts "TESTING ERROR 6.3\n";exit}
	"name=blablabla"
}
after 100

spawn $env(SHELL)
send -- "firejail --shutdown=test\r"
set spawn_id $appimage_id
expect {
	timeout {puts "shutdown\n"}
	"AppImage detached"
}

after 100

puts "\nall done\n"
