-
Notifications
You must be signed in to change notification settings - Fork 6
/
serio-test.sh
executable file
·351 lines (273 loc) · 6.94 KB
/
serio-test.sh
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
# serio-test.sh - unit test for serio
#_______________________________________________________________________________
function vecho {
if [ $verbose -gt 0 ] ; then
echo "$@"
fi
}
function usage
{
echo "
usage: ${script_name} [-v] [--debug] [-h] [-nc] serial_port
-B, --basic *Use basic (minimal) remote system commands
--debug Print each shell line as it is executed
-F, --numfiles <count> Number of random data test files
-h, -help, --help Show help
--nc Don't clean up test files
-s, --sleep <seconds> Time to sleep between --get and --put tests [0.0]
-T, --timeout <seconds> *Read timout on the serial port (float)
-R, --remote-dir <dir> Directory on remote system to put files into [.]
-v Be verbose
" >&2
}
#_______________________________________________________________________________
script_name=`basename $0`
#PARANOID="-P"
unset BASIC
unset PARANOID
unset PORT
unset SERIAL_DEV
unset SLEEP
unset TIMEOUT
do_cleanup=1
help=0
numfiles=2
remote_dir="."
verbose=0
while [ $# -gt 0 ] ; do
case $1 in
-B | --basic )
shift
BASIC="-B"
;;
--debug )
shift
set -x
;;
-F | --numfiles )
shift
numfiles=$1
shift
;;
-h | -help | --help )
shift
help=1
;;
--nc )
shift
do_cleanup=0
;;
-s | --sleep )
shift
SLEEP="sleep $1"
shift
;;
-T | --timeout )
shift
TIMEOUT="-T $1"
shift
;;
-R | --remote-dir )
shift
remote_dir="$1"
shift
;;
-v )
shift
verbose=1
;;
-* )
echo "ERROR: unrecognized option: $1" >&2
exit 1
break
;;
* )
break
;;
esac
done
if [ $# -gt 0 ] ; then
PORT="$1"
SERIAL_DEV="-y /dev/$1"
shift
fi
if [ $help -eq 1 ] ; then
usage
exit 1
fi
if [ $# -ne 0 ] ; then
echo "ERROR: unexpected parameter: $@" >&2
exit 1
fi
if [ "${PORT}" == "" ] ; then
echo "ERROR: argument 'serial_port' is required" >&2
exit 1
fi
# FIXME - Should have an option to do something analagous to mktemp on the
# target, but do not want to add "mktemp" dependency on remote system, so
# can hand craft with something like:
# $remote_dir="${remote_dir}/tmp"
# $SERIO $SERIAL_DEV -c 'if [ -f tmp -o -d tmp ] ; then echo error ; else mkdir tmp ; echo ok ; fi'
#
# then modify cleanup() to: $SERIO $SERIAL_DEV -c 'rm -r ${remote_dir}'
tmp_dir=$( mktemp --tmpdir -d serio-test.XXXXXXXXXX )
FILE_LIST=""
for k in $(seq ${numfiles}) ; do
FILE_LIST="${FILE_LIST} file_${k}"
done
SERCP_ARGS="$TIMEOUT $BASIC"
SERSH_ARGS="$TIMEOUT"
# shell builtin 'time' does not recognize -f
TIME="/usr/bin/time"
# respect $PATH
SERCP=$( which sercp )
SERIO=$( which serio )
SERSH=$( which sersh )
# if serio is not on the PATH, then try using it from
# the current directory. This way, even if serio is
# not installed, there's a chance that serio-test.sh will work
if [ -z "${SERCP}" ] ; then
SERCP=./sercp
fi
if [ -z "${SERIO}" ] ; then
SERIO=./serio
fi
if [ -z "${SERSH}" ] ; then
SERSH=./sersh
fi
vecho "remote-dir = ${remote_dir}"
vecho "FILE_LIST = ${FILE_LIST}"
vecho "SERIAL_DEV = ${SERIAL_DEV}"
vecho "SERIO_ARGS = ${SERIO_ARGS}"
vecho "SLEEP = ${SLEEP}"
vecho "SERCP = ${SERCP}"
vecho "SERIO = ${SERIO}"
vecho "SERSH = ${SERSH}"
#########################
# create test files
echo "Creating files..."
# make a super-small, super-safe file for file1
echo "this is the first test file for serio" >${tmp_dir}/file_short
if [ $verbose -gt 0 ] ; then
ddout="/dev/stdout"
else
ddout="/dev/null"
fi
size=1
for f in ${FILE_LIST} ; do
f_full="${tmp_dir}/$f"
vecho "Creating file ${f}"
dd if=/dev/urandom of=${f_full} bs=1000 count=${size} >$ddout 2>&1
size=$(( $size * 10 ))
i=$(( $i + 1 ))
done
FILE_LIST="file_short ${FILE_LIST}"
#########################
# test put and get
# run some put and get tests, timing them
# FIXME- In the result messages:
# suggest "PASS" instead of "ok"
# suggest "FAIL" instead of "not ok"
#
# I would have made those changes myself, but we have had the discussion
# about the lack of consistency in test pass/fail messages across test
# projects, so I figured this might not be as obvious as what I am
# suggesting.
#
# FIXME - Suggest less verbosity and reordering for easier parsing of the messages,
# eg s/time for get of $f: %E/time | %E | get $f/
test_num=1
## put some files
echo "Putting files: $FILE_LIST"
for f in $FILE_LIST ; do
f_full="${tmp_dir}/$f"
vecho "Putting file ${f}"
${TIME} -f " time for put of $f: %E" \
$SERCP $SERCP_ARGS $f_full "${PORT}:${remote_dir}/$f"
$SLEEP
done
## get some files
echo "Getting files: $FILE_LIST"
for f in $FILE_LIST ; do
ret_f="${f}-return"
ret_f_full="${tmp_dir}/${ret_f}"
vecho " Getting file ${f} (into $ret_f)"
${TIME} -f " time for get of $f: %E" \
$SERCP $SERCP_ARGS "${PORT}:${remote_dir}/$f" $ret_f_full
$SLEEP
if [ -e "$ret_f_full" ] ; then
echo "ok $test_num - get of file $ret_f"
else
echo "not ok $test_num - get of file $ret_f"
fi
test_num=$(( $test_num + 1 ))
done
if [ $verbose -gt 0 ] ; then
echo "Here are the checksums of testfiles"
cksum file*
fi
function check_cksum {
local f="$1"
local ret_f="${f}-return"
local f_full="${tmp_dir}/${f}"
local ret_f_full="${tmp_dir}/${ret_f}"
local f_s=$(cksum $f_full | cut -d " " -f 1,2)
local ret_f_s=$(cksum $ret_f_full | cut -d " " -f 1,2)
local desc="$test_num - check ${ret_f} cksum with ${f} cksum"
if [ "${f_s}" == "${ret_f_s}" ] ; then
echo "ok $desc"
else
echo "not ok $desc"
fi
test_num=$(( $test_num + 1 ))
}
for f in $FILE_LIST ; do
check_cksum $f
done
#########################
# test some commands
#$SERSH $SERSH_ARGS -c "echo hello there!"
echo "Executing some commands"
vecho " Execute 'ls -l $remote_dir'"
$SERSH $SERSH_ARGS ${PORT} "ls -l $remote_dir"
vecho " Execute 'echo hello there'"
res=$($SERSH $SERSH_ARGS ${PORT} "echo hello there")
rcode=$?
exp=$'hello there'
echo "expected : [$exp], rcode=[0]"
echo "got result: [$res], rcode=[$rcode]"
desc="$test_num - run 'echo hello there' on remote"
if [ "$res" = "$exp" -a "$rcode" = "0" ] ; then
echo "ok $desc"
else
echo "not ok $desc"
fi
test_num=$(( $test_num + 1 ))
vecho " Execute 'echo foo ; false'"
res=$($SERSH $SERSH_ARGS ${PORT} "echo foo ; false")
rcode=$?
exp=$'foo'
expcode=1
echo "expected : [$exp], rcode=[$expcode]"
echo "got result: [$res], rcode=[$rcode]"
desc="$test_num - run 'echo foo ; false' on remote"
if [ "$res" = "$exp" -a "$rcode" = "$expcode" ] ; then
echo "ok $desc"
else
echo "not ok $desc"
fi
test_num=$(( $test_num + 1 ))
#########################
# test cleanup
function cleanup {
# remove test files
$SERSH ${PORT} "rm ${remote_dir}/file_[1-9]* ${remote_dir}/file_short"
rm -r ${tmp_dir}
}
if [ $do_cleanup -eq 1 ] ; then
echo "Doing cleanup"
cleanup
else
echo "Not doing cleanup, test files are in ${tmp_dir}"
fi