summaryrefslogtreecommitdiff
path: root/tests/common
blob: 3dd96284f5d3bd2d8b5fb96e6fb0959d85415fef (about) (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
BIN="$(cd ..; pwd)/ratox"
test -x $BIN || exit 1

echolor() {
	printf '[1;3%dm%s\n' "$1" "$2"
}

cleanup() {
	echo ":: cleaning"
	kill $(pgrep -f 'abduco -n test-u1')
	kill $(pgrep -f 'abduco -n test-u2')
	rm -rf u1 u2
}

trap cleanup INT

spawn_users() {
	for u in u1 u2; do
		echo ":: creating user $u"
		mkdir -p $u
		abduco -n test-$u sh -c "cd $u;$BIN 2>&1|tee LOG"
	done
	sleep 1
}

add_friends() {
	echo ":: send u2 a friend request from u1"
	id1=$(cut -b-64 < u1/id)
	id2=$(cut -b-64 < u2/id)
	cat u2/id > u1/request/in
	while [ ! -p u2/request/out/$id1 ]; do sleep 1; done
	echo ":: accepting request from $id1"
	echo 1 > u2/request/out/$id1
	while [ ! -d u2/$id1 ]; do sleep 1; done
	echo ":: u1: waiting for u2 to come online"
	while [ $(cat u1/$id2/online) -eq 0 ]; do sleep 1; done
}

change_name() {
	NAME=$(dd bs=64 count=1 if=/dev/urandom 2>/dev/null|tr -cd '\11\12\15\40-\176\n'|sed 's/$/\n/')
	id1=$(cut -b-64 < u1/id)
	echo ":: changing name of u1"
	echo "$NAME" >u1/name/in
	while ! grep -Fq -- "$NAME"  u2/$id1/name; do sleep 1; done
}

change_status() {
	STATUS=$(dd bs=128 count=1 if=/dev/urandom 2>/dev/null|tr -cd '\11\12\15\40-\176\n'|sed 's/$/\n/')
	id1=$(cut -b-64 < u1/id)
	echo ":: changing status of u1"
	echo "$STATUS" >u1/name/in
	while ! grep -Fq -- "$STATUS"  u2/$id1/name; do sleep 1; done
}

send_text() {
	RND=$(tr -cd 'A-F0-9' </dev/urandom|fold -w16|head -n1)
	id1=$(cut -b-64 < u1/id)
	id2=$(cut -b-64 < u2/id)
	echo ":: sending text to u2"
	echo "$RND" > u1/$id2/text_in
	while ! grep -q -- "$RND" u2/$id1/text_out; do sleep 1; done
}

send_file() {
	TMP1=$(mktemp)
	TMP2=$(mktemp)
	</dev/urandom base64 | dd bs=1K count=2048 of=$TMP1 2>/dev/null
	id1=$(cut -b-64 < u1/id)
	id2=$(cut -b-64 < u2/id)
	echo ":: u1: sending file $TMP1"
	cat $TMP1 > u1/$id2/file_in &
	while [ $(wc -c <u2/$id1/file_pending) -eq 0 ]; do sleep 1; done
	echo ":: u2: saving file to $TMP2"
	cat u2/$id1/file_out > $TMP2
	echo ":: verifying $TMP1 and $TMP2"
	sha1sum $TMP1 | sed "s,$TMP1,$TMP2," | sha1sum -c
}