1# $FreeBSD: stable/11/tests/sys/geom/class/gate/ggate_test.sh 361128 2020-05-17 02:40:49Z asomers $
2
3PIDFILE=ggated.pid
4PLAINFILES=plainfiles
5PORT=33080
6CONF=gg.exports
7
8atf_test_case ggated cleanup
9ggated_head()
10{
11	atf_set "descr" "ggated can proxy geoms"
12	atf_set "require.progs" "ggatec ggated"
13	atf_set "require.user" "root"
14	atf_set "timeout" 60
15}
16
17ggated_body()
18{
19	load_ggate
20
21	us=$(alloc_ggate_dev)
22	work=$(alloc_md)
23	src=$(alloc_md)
24
25	atf_check -e ignore -o ignore \
26	    dd if=/dev/random of=/dev/$work bs=1m count=1 conv=notrunc
27	atf_check -e ignore -o ignore \
28	    dd if=/dev/random of=/dev/$src bs=1m count=1 conv=notrunc
29
30	echo $CONF >> $PLAINFILES
31	echo "127.0.0.1 RW /dev/$work" > $CONF
32
33	atf_check ggated -p $PORT -F $PIDFILE $CONF
34	atf_check ggatec create -p $PORT -u $us 127.0.0.1 /dev/$work
35
36	ggate_dev=/dev/ggate${us}
37
38	wait_for_ggate_device ${ggate_dev}
39
40	atf_check -e ignore -o ignore \
41	    dd if=/dev/${src} of=${ggate_dev} bs=1m count=1 conv=notrunc
42
43	checksum /dev/$src /dev/$work
44}
45
46ggated_cleanup()
47{
48	common_cleanup
49}
50
51atf_test_case ggatel_file cleanup
52ggatel_file_head()
53{
54	atf_set "descr" "ggatel can proxy files"
55	atf_set "require.progs" "ggatel"
56	atf_set "require.user" "root"
57	atf_set "timeout" 15
58}
59
60ggatel_file_body()
61{
62	load_ggate
63
64	us=$(alloc_ggate_dev)
65
66	echo src work >> ${PLAINFILES}
67	dd if=/dev/random of=work bs=1m count=1
68	dd if=/dev/random of=src bs=1m count=1
69
70	atf_check ggatel create -u $us work
71
72	ggate_dev=/dev/ggate${us}
73
74	wait_for_ggate_device ${ggate_dev}
75
76	atf_check -e ignore -o ignore \
77	    dd if=src of=${ggate_dev} bs=1m count=1 conv=notrunc
78
79	checksum src work
80}
81
82ggatel_file_cleanup()
83{
84	common_cleanup
85}
86
87atf_test_case ggatel_md cleanup
88ggatel_md_head()
89{
90	atf_set "descr" "ggatel can proxy files"
91	atf_set "require.progs" "ggatel"
92	atf_set "require.user" "root"
93	atf_set "timeout" 15
94}
95
96ggatel_md_body()
97{
98	load_ggate
99
100	us=$(alloc_ggate_dev)
101	work=$(alloc_md)
102	src=$(alloc_md)
103
104	atf_check -e ignore -o ignore \
105	    dd if=/dev/random of=$work bs=1m count=1 conv=notrunc
106	atf_check -e ignore -o ignore \
107	    dd if=/dev/random of=$src bs=1m count=1 conv=notrunc
108
109	atf_check ggatel create -u $us /dev/$work
110
111	ggate_dev=/dev/ggate${us}
112
113	wait_for_ggate_device ${ggate_dev}
114
115	atf_check -e ignore -o ignore \
116	    dd if=/dev/$src of=${ggate_dev} bs=1m count=1 conv=notrunc
117
118	checksum /dev/$src /dev/$work
119}
120
121ggatel_md_cleanup()
122{
123	common_cleanup
124}
125
126atf_init_test_cases()
127{
128	atf_add_test_case ggated
129	atf_add_test_case ggatel_file
130	atf_add_test_case ggatel_md
131}
132
133alloc_ggate_dev()
134{
135	local us
136
137	us=0
138	while [ -c /dev/ggate${us} ]; do
139		: $(( us += 1 ))
140	done
141	echo ${us} > ggate.devs
142	echo ${us}
143}
144
145alloc_md()
146{
147	local md
148
149	md=$(mdconfig -a -t malloc -s 1M) || \
150		atf_fail "failed to allocate md device"
151	echo ${md} >> md.devs
152	echo ${md}
153}
154
155checksum()
156{
157	local src work
158	src=$1
159	work=$2
160
161	src_checksum=$(md5 -q $src)
162	work_checksum=$(md5 -q $work)
163
164	if [ "$work_checksum" != "$src_checksum" ]; then
165		atf_fail "work md5 checksum didn't match"
166	fi
167
168	ggate_checksum=$(md5 -q /dev/ggate${us})
169	if [ "$ggate_checksum" != "$src_checksum" ]; then
170		atf_fail "ggate md5 checksum didn't match"
171	fi
172}
173
174common_cleanup()
175{
176	if [ -f "ggate.devs" ]; then
177		while read test_ggate; do
178			ggatec destroy -f -u $test_ggate >/dev/null
179		done < ggate.devs
180		rm ggate.devs
181	fi
182
183	if [ -f "$PIDFILE" ]; then
184		pkill -F "$PIDFILE"
185		rm $PIDFILE
186	fi
187
188	if [ -f "PLAINFILES" ]; then
189		while read f; do
190			rm -f ${f}
191		done < ${PLAINFILES}
192		rm ${PLAINFILES}
193	fi
194
195	if [ -f "md.devs" ]; then
196		while read test_md; do
197			# ggatec destroy doesn't release the provider
198			# synchronously, so we may need to retry destroying it.
199			while ! mdconfig -d -u $test_md; do
200				sleep 0.1
201			done
202		done < md.devs
203		rm md.devs
204	fi
205	true
206}
207
208load_ggate()
209{
210	local class=gate
211
212	# If the geom class isn't already loaded, try loading it.
213	if ! kldstat -q -m g_${class}; then
214		if ! geom ${class} load; then
215			atf_skip "could not load module for geom class=${class}"
216		fi
217	fi
218}
219
220# Bug 204616: ggatel(8) creates /dev/ggate* asynchronously if `ggatel create`
221#             isn't called with `-v`.
222wait_for_ggate_device()
223{
224	ggate_device=$1
225
226	while [ ! -c $ggate_device ]; do
227		sleep 0.5
228	done
229}
230