#
# \brief  Test for the SSH terminal
#

assert_spec x86

if {[have_spec linux]} {
	puts "Run script is not supported on this platform."
	exit 0
}

# Build
#

set build_components {
	core init timer
	drivers/nic
	drivers/rtc
	server/ssh_terminal
	server/fs_rom
	server/vfs
	lib/vfs/jitterentropy
	lib/vfs/lxip
	lib/vfs/pipe
	test/libports/ncurses
	test/terminal_echo
	noux-pkg/bash
	server/nic_router
}

source ${genode_dir}/repos/base/run/platform_drv.inc
append_platform_drv_build_components

build $build_components

create_boot_directory

#
# Generate config
#

set config {
<config verbose="no">
	<parent-provides>
		<service name="ROM"/>
		<service name="RAM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>

	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Timer"/> </provides>
	</start>

	<start name="report_rom">
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config verbose="yes"/>
	</start>

	<start name="nic_drv">
		<binary name="ipxe_nic_drv"/>
		<resource name="RAM" quantum="8M"/>
		<config mode="uplink_client"/>
		<route>
			<service name="Uplink"> <child name="nic_router"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

	<start name="nic_router" caps="200">
		<resource name="RAM" quantum="10M"/>
		<provides>
			<service name="Nic"/>
			<service name="Uplink"/>
		</provides>
		<config verbose_domain_state="yes">

			<policy label_prefix="ssh_terminal" domain="downlink"/>
			<policy label_prefix="nic_drv"      domain="uplink"/>

			<domain name="uplink">

				<nat domain="downlink"
				     tcp-ports="16384"
				     udp-ports="16384"
				     icmp-ids="16384"/>

				<tcp-forward port="22" domain="downlink" to="10.0.3.2"/>

			</domain>

			<domain name="downlink" interface="10.0.3.1/24">

				<dhcp-server ip_first="10.0.3.2" ip_last="10.0.3.2"/>

				<tcp dst="0.0.0.0/0"><permit-any domain="uplink" /></tcp>
				<udp dst="0.0.0.0/0"><permit-any domain="uplink" /></udp>
				<icmp dst="0.0.0.0/0" domain="uplink"/>

			</domain>

		</config>
	</start>

	<start name="rtc_drv">
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Rtc"/> </provides>
	</start>

	<start name="ssh_terminal" caps="250">
		<resource name="RAM" quantum="32M"/>
		<provides> <service name="Terminal"/> </provides>
		<config ld_verbose="yes" port="22" allow_password="yes"
		        show_password="yes" ed25519_key="/etc/ssh/ed25519_key">
			<policy label_prefix="/bin/bash" user="genode" password="xuon" multi_login="yes" request_terminal="yes"/>
			<policy label_prefix="test-terminal_echo" user="charlie" password="echo"/>

			<vfs>
				<dir name="dev">
					<log/>
					<jitterentropy name="random"/>
					<jitterentropy name="urandom"/>
					<rtc/>
				</dir>
				<dir name="etc">
					<dir name="ssh">
						<rom name="ed25519_key"/>
					</dir>
				</dir>
				<dir name="socket"> <lxip dhcp="yes"/> </dir>
				<dir name="pipe"> <pipe/> </dir>
			</vfs>
			<libc stdout="/dev/log" stderr="/dev/log" socket="/socket" pipe="/pipe" rtc="/dev/rtc">
			</libc>
		</config>
		<route>
			<service name="Nic"> <child name="nic_router"/> </service>
			<service name="Report"> <child name="report_rom"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

	<start name="test-terminal_echo">
		<resource name="RAM" quantum="1M"/>
	</start>

	<start name="vfs">
		<resource name="RAM" quantum="10M"/>
		<provides><service name="File_system"/></provides>
		<config>
			<vfs> <tar name="bash.tar"/> </vfs>
			<default-policy root="/" writeable="yes"/>
		</config>
	</start>

	<start name="vfs_rom">
		<resource name="RAM" quantum="10M"/>
		<binary name="fs_rom"/>
		<provides> <service name="ROM"/> </provides>
		<config/>
		<route>
			<service name="File_system"> <child name="vfs"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>

	<start name="/bin/bash" caps="500">
		<resource name="RAM" quantum="64M"/>
		<config verbose="yes">
			<libc stdin="/dev/terminal" stdout="/dev/terminal" stderr="/dev/terminal"
			      rtc="/dev/null"/>
			<vfs>
				<dir name="dev"> <terminal/> <log/> <null/> </dir>
				<inline name=".bash_profile">
					echo Welcome to Genode! > /dev/log
				</inline>
			</vfs>
			<env key="TERM" value="screen"/>
			<env key="HOME" value="/"/>
			<env key="IGNOREEOF" value="3"/>
			<arg value="/bin/bash"/>
			<arg value="--login"/>
		</config>
		<route>
			<service name="ROM" label_suffix=".lib.so"> <parent/> </service>
			<service name="ROM" label_last="/bin/bash"> <child name="vfs_rom"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
}

append_platform_drv_config

append config {
</config>}

install_config $config

#
# Generate a new host key
#
if {![file exists bin/ed25519_key]} {
	exec ssh-keygen -t ed25519 -f bin/ed25519_key -q -N ""
}

#
# Boot modules
#

# generic modules
set boot_modules {
	core ld.lib.so init timer ipxe_nic_drv rtc_drv report_rom vfs fs_rom
	test-terminal_echo nic_router

	libc.lib.so libm.lib.so vfs.lib.so
	vfs_lxip.lib.so lxip.lib.so
	posix.lib.so libcrypto.lib.so libssh.lib.so zlib.lib.so ncurses.lib.so
	vfs_jitterentropy.lib.so vfs_pipe.lib.so ssh_terminal

	bash.tar ed25519_key
}

# platform-specific modules
append_platform_drv_boot_modules

build_boot_image $boot_modules

#
# Execute test
#

append qemu_args     " -nographic "
append_qemu_nic_args "hostfwd=tcp::5555-:22"

set lxip_match_string "ipaddr=(\[0-9\]+\.\[0-9\]+\.\[0-9\]+\.\[0-9\]+).*\n"

if {[get_cmd_switch --autopilot]} {
	run_genode_until $lxip_match_string 60
	set serial_id [output_spawn_id]

	if {[have_include "power_on/qemu"]} {
		set host "localhost"
		set port "5555"
	} else {
		regexp $lxip_match_string $output all host
		puts ""
		set port "22"
	}
	# wait for ssh_terminal to come up
	sleep 5
	spawn sshpass -p xuon ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l genode $host -p $port
	set ssh_id $spawn_id
	set spawn_id_list [list $ssh_id $serial_id]
	run_genode_until {.*\[init -> /bin/bash.*\] Welcome to Genode!.*\n} 15 $spawn_id_list
	puts ""
	puts ""
} else {
	run_genode_until forever
}

exec rm bin/ed25519_key bin/ed25519_key.pub

# vi: set ft=tcl :
