forked from frowand/serio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
serio-test.sh
executable file
·308 lines (237 loc) · 6.04 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
#!/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]
-B, --basic *Use basic (minimal) remote system commands
--debug Print each shell line as it is executed
-F, --numfile <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, --time <seconds> *Delay in _safe_write()
-y, --port <serial port> *Serial port to use [/dev/ttyUSB0]
--remote-dir <dir> Directory on remote system to put files into [.]
-v Be verbose
* Sets a serio command line option
" >&2
}
#_______________________________________________________________________________
script_name=`basename $0`
#PARANOID="-P"
unset BASIC
unset SERIAL_DEV
unset PARANOID
unset SLEEP
unset TIME
do_cleanup=1
help=0
numfile=1
remote_dir="."
verbose=0
while [ $# -gt 0 ] ; do
case $1 in
-B | --basic )
shift
BASIC="-B"
;;
--debug )
shift
set -x
;;
-F | --numfile )
shift
numfile=$1
shift
;;
-h | -help | --help )
shift
help=1
;;
--nc )
shift
do_cleanup=0
;;
-P | --paranoid )
PARANOID="-P"
shift
;;
-s | --sleep )
shift
SLEEP="sleep $1"
shift
;;
-t | --time )
shift
TIME="-t $1"
shift
verbose=1
;;
-T | --remote-dir )
shift
remote_dir="$1"
shift
verbose=1
;;
-v )
shift
verbose=1
;;
-y | --port )
shift
SERIAL_DEV="-y $1"
shift
verbose=1
;;
* )
break
;;
esac
done
if [ $help -eq 1 ] ; then
usage
exit 1
fi
if [ $# -ne 0 ] ; then
echo "ERROR: unexpected parameter:" >&2
echo " $@" >&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="file_1"
for k in $( seq 2 $(( ${numfile} + 1 )) ) ; do
FILE_LIST="${FILE_LIST} file_${k}"
done
SERIO_ARGS="$SERIAL_DEV $PARANOID $BASIC $TIME"
# shell builtin 'time' does not recognize -f
TIME="/usr/bin/time"
# respect $PATH
SERIO=$( which serio )
vecho "remote-dir = ${remote_dir}"
vecho "FILE_LIST = ${FILE_LIST}"
vecho "SERIAL_DEV = ${SERIAL_DEV}"
vecho "SERIO_ARGS = ${SERIO_ARGS}"
vecho "SLEEP = ${SLEEP}"
vecho "SERIO = ${SERIO}"
#########################
# create test files
echo "Creating files..."
# make a super-small, super-safe file for file1
echo "this is the first test file for serio" >file1
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
#########################
# 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" $SERIO $SERIO_ARGS -p --source=$f_full --destination="${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" $SERIO $SERIO_ARGS -g --source="${remote_dir}/$f" --destination=$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
#$SERIO $SERIO_ARGS -c "echo hello there!"
echo "Executing some commands"
vecho " Execute 'ls -l $remote_dir'"
$SERIO $SERIO_ARGS -c "ls -l $remote_dir"
vecho " Execute 'echo hello there'"
res1=$($SERIO $SERIO_ARGS -c "echo hello there")
exp1=$'hello there'
echo "expected : [$exp1]"
echo "got result: [$res1]"
desc="$test_num - run 'echo hello there' on remote"
if [ "$res1" = "$exp1" ] ; then
echo "ok $desc"
else
echo "not ok $desc"
fi
#########################
# test cleanup
function cleanup {
# remove test files
$SERIO $SERIAL_DEV -c "rm ${remote_dir}/file_[1-9]*"
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