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

set timeout 10
spawn $env(SHELL)
match_max 100000

# create some test files in user home directory
send -- "touch ~/_firejail_test_file1\r"
after 100
send -- "touch ~/_firejail_test_file2\r"
after 100
send -- "mkdir ~/_firejail_test_dir1\r"
after 100
send -- "mkdir ~/_firejail_test_dir1/_firejail_test_dir2\r"
after 100
send -- "touch ~/_firejail_test_dir1/_firejail_test_dir2/_firejail_test_file3\r"
after 100
send -- "ln -s /etc ~/_firejail_test_link1\r"
after 100
send -- "ln -s ~/_firejail_test_dir1 ~/_firejail_test_link2\r"
after 100

send -- "firejail --private-home=_firejail_test_file1,_firejail_test_file2,_firejail_test_dir1\r"
expect {
	timeout {puts "TESTING ERROR 1\n";exit}
	-re "Child process initialized in \[0-9\]+.\[0-9\]+ ms"
}
after 100

# FIXME: Prints "TESTING ERROR 3" in CI since 2024-09-19.
#send -- "find ~\r"
#expect {
#	timeout {puts "TESTING ERROR 2\n";exit}
#	"_firejail_test_file3"
#}
#expect {
#	timeout {puts "TESTING ERROR 3\n";exit}
#	"_firejail_test_file2"
#}
#expect {
#	timeout {puts "TESTING ERROR 4\n";exit}
#	"_firejail_test_file1"
#}
#after 100

send -- "exit\r"
sleep 1

send -- "firejail --private-home=\"bla;bla\"\r"
expect {
	timeout {puts "TESTING ERROR 5\n";exit}
	"is an invalid filename"
}
after 100

send -- "firejail --private-home=/etc/shadow\r"
expect {
	timeout {puts "TESTING ERROR 6\n";exit}
	"invalid file"
}
after 100

send -- "firejail --private-home=/etc/passwd\r"
expect {
	timeout {puts "TESTING ERROR 7\n";exit}
	"invalid file"
}
after 100

send -- "firejail --private-home=../../etc/passwd\r"
expect {
	timeout {puts "TESTING ERROR 8\n";exit}
	"invalid file"
}
after 100

send -- "firejail --private-home=_firejail_test_link1\r"
expect {
	timeout {puts "TESTING ERROR 9\n";exit}
	"to file or directory not owned by the user"
}
after 100

send -- "firejail --private-home=_firejail_test_link2\r"
expect {
	timeout {puts "TESTING ERROR 10\n";exit}
	-re "Child process initialized in \[0-9\]+.\[0-9\]+ ms"
}
after 100
send -- "file ~/_firejail_test_link2\r"
expect {
	timeout {puts "TESTING ERROR 11\n";exit}
	"broken symbolic link"
}
send -- "exit\r"
sleep 1

send -- "echo cleanup\r"
after 100
send -- "rm -f ~/_firejail_test_file1\r"
after 100
send -- "rm -f ~/_firejail_test_file2\r"
after 100
send -- "rm -fr ~/_firejail_test_dir1\r"
after 100
send -- "rm -f ~/_firejail_test_link1\r"
after 100
send -- "rm -f ~/_firejail_test_link2\r"
after 100

puts "\nall done\n"
