aboutsummaryrefslogtreecommitdiff
path: root/test.sh
blob: d7746be6c845006cfbc7852c0efa502b20c87305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash

set -e

PORT=12345
USER=$(whoami)
GROUP=$(groups | cut -d' ' -f 1)

SSD=/sbin/start-stop-daemon

RTFTPC=$(pwd)/target/release/rtftpc
RTFTPD=$(pwd)/target/release/rtftpd

ATFTPC=/usr/bin/atftp
ATFTPD=/usr/sbin/atftpd

TFTPC=/usr/bin/tftp

BUSYBOX=/bin/busybox

CLIENTDIR=$(mktemp -d)
SERVERDIR=$(mktemp -d)

cleanup() {
	atftpd_cleanup
	rtftpd_cleanup
	rm -f "$CLIENTDIR/testfile" "$SERVERDIR/testfile"
	rmdir "$CLIENTDIR" "$SERVERDIR"
}

compare_files() {
	cmp "$CLIENTDIR/testfile" "$SERVERDIR/testfile" 1>/dev/null
}

atftpd() {
	$ATFTPD --port $PORT --user "$USER" --group "$GROUP" --daemon "$SERVERDIR"
}

atftpc() {
	[ $TX -eq 1 ] && op="-p" || op="-g"
	if [ -n "$BLKSIZE" ]; then
		$ATFTPC $op -l testfile -r testfile --option "blksize $BLKSIZE" 127.0.0.1 $PORT 1>/dev/null 2>&1
	else
		$ATFTPC $op -l testfile -r testfile 127.0.0.1 $PORT 1>/dev/null
	fi
}

tftpc() {
	[ $TX -eq 1 ] && op="put" || op="get"
	printf "connect 127.0.0.1 $PORT\\nmode binary\\n$op testfile\\n" | $TFTPC 1>/dev/null
}

rtftpd() {
	$SSD --background --exec "$RTFTPD" --start -- -p $PORT -d "$SERVERDIR" 1>/dev/null
}

rtftpc() {
	[ $TX -eq 1 ] && op="-p" || op="-g"
	[ -n "$BLKSIZE" ] && opts="--blksize $BLKSIZE"
	$RTFTPC $op testfile $opts 127.0.0.1:$PORT 1>/dev/null
}

busybox_tftpc() {
	[ $TX -eq 1 ] && op="-p" || op="-g"
	[ -n "$BLKSIZE" ] && opts="-b $BLKSIZE"
	$BUSYBOX tftp $op -l testfile -r testfile $opts 127.0.0.1 $PORT 1>/dev/null 2>&1
}

atftpd_cleanup() {
	killall -q -9 $ATFTPD 2>/dev/null || true
}

rtftpd_cleanup() {
	killall -q -9 "$RTFTPD" 2>/dev/null || true
}


test_transfer() {
	client=$1
	server=$2

	$server

	dd if=/dev/urandom of="$CLIENTDIR/testfile" bs=1M count=100 2>/dev/null

	time (
		printf "$client TX (to $server): "
		TX=1
		${client}
		compare_files
		printf "ok"
	)
	rm -f "$CLIENTDIR/testfile"

	time (
		printf "$client RX (from $server): "
		TX=0
		${client}
		compare_files
		printf "ok"
	)
	rm -f "$SERVERDIR/testfile"

	${server}_cleanup
}

trap cleanup 0 1 2

if [ ! -x "$RTFTPC" ] || [ ! -x "$RTFTPD" ]; then
	cargo build --release
fi

cd "$CLIENTDIR"


# Defaults
printf "Testing with default configuration\\n"
test_transfer rtftpc rtftpd
[ -x $ATFTPD ]  && test_transfer rtftpc atftpd
[ -x $ATFTPC ]  && test_transfer atftpc rtftpd
[ -x $TFTPC ]   && test_transfer tftpc rtftpd
[ -x $BUSYBOX ] && test_transfer busybox_tftpc rtftpd

# different block size
printf "\\n\\nTesting larger block sizes\\n"
BLKSIZE=1500
test_transfer rtftpc rtftpd
[ -x $ATFTPD ]  && test_transfer rtftpc atftpd
[ -x $ATFTPC ]  && test_transfer atftpc rtftpd
[ -x $BUSYBOX ] && test_transfer busybox_tftpc rtftpd
unset BLKSIZE