summaryrefslogtreecommitdiff
blob: a2d15b4a1a9f1ce0b1e7bbd58704074896322910 (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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/bin/sh
# shellcheck disable=2015,2154,2164,2181,2317

# Requires mktemp(1), which is not a standard utility, but is commonly
# available. The implementations provided by GNU coreutils, busybox and toybox
# are known to be compatible.

bailout() {
	printf 'Bail out! %s.\n' "$1"
	cleanup_tmpdir
	exit 1
}

assign_tmpdir() {
	# shellcheck disable=1007
	dir=$(mktemp -d) \
	&& chdir "${dir}" \
	|| bailout "Couldn't create or change to the temp dir"
}

cleanup_tmpdir() {
	if [ -n "${dir}" ]; then
		rm -rf -- "${dir}"
	fi
}

test_chdir() {
	set -- \
		1  grandchild  \
		1         var  \
		0          -L  \
		0          -p  \
		0          -e  \
		0          -@  \
		0           -  \
		0       child

	if ! mkdir -p -- -L -p -e -@ - child child/grandchild; then
		bailout "Couldn't set up all test directories"
	fi

	callback() {
		shift
		test_description="chdir $(print_args "$@")"
		if [ "$BASH" ]; then
			# shellcheck disable=3044
			shopt -s cdable_vars
		fi
		CDPATH=child var=child chdir "$@" \
		&& test "$PWD" != "$OLDPWD" \
		&& cd - >/dev/null
	}

	iterate_tests 2 "$@"
}

test_chdir_noop() {
	set -- 0 ''

	callback() {
		shift
		test_description="chdir $(print_args "$@")"
		chdir "$@" \
		&& test "$PWD" = "$OLDPWD" \
		|| { cd - >/dev/null; false; }
	}

	iterate_tests 2 "$@"
}

test_ebegin() {
	_eprint() {
		shift
		_ends_with_newline "$*"
		ok=$(( $? == 0 ))
	}

	ok=0
	set -- "message"
	ebegin "$1"

	if [ "${ok}" -eq 0 ]; then
		printf 'not '
	fi
	printf 'ok %d - ebegin %s (expecting terminating newline)\n' "$((testnum + 1))" "$1"

	return "$(( ok ? 0 : 1 ))"
}

test_is_older_than() {
	set -- \
		1  N/A           N/A \
		1  newer         N/A \
		1  newer-empty   N/A \
		1  newer/file    N/A \
		1  non-existent  N/A \
		0  newer         newer \
		1  newer         newer-empty \
		0  newer         newer/file \
		1  newer         non-existent \
		1  newer         older \
		1  newer         older-empty \
		1  newer         older/file \
		0  newer-empty   newer \
		1  newer-empty   newer-empty \
		0  newer-empty   newer/file \
		1  newer-empty   non-existent \
		1  newer-empty   older \
		1  newer-empty   older-empty \
		1  newer-empty   older/file \
		1  newer/file    newer \
		1  newer/file    newer-empty \
		1  newer/file    newer/file \
		1  newer/file    non-existent \
		1  newer/file    older \
		1  newer/file    older-empty \
		1  newer/file    older/file \
		0  non-existent  newer \
		0  non-existent  newer-empty \
		0  non-existent  newer/file \
		1  non-existent  non-existent \
		0  non-existent  older \
		0  non-existent  older-empty \
		0  non-existent  older/file \
		0  older         newer \
		0  older         newer-empty \
		0  older         newer/file \
		1  older         non-existent \
		0  older         older \
		1  older         older-empty \
		0  older         older/file \
		0  older-empty   newer \
		0  older-empty   newer-empty \
		0  older-empty   newer/file \
		1  older-empty   non-existent \
		0  older-empty   older \
		1  older-empty   older-empty \
		0  older-empty   older/file \
		0  older/file    newer \
		0  older/file    newer-empty \
		0  older/file    newer/file \
		1  older/file    non-existent \
		1  older/file    older \
		1  older/file    older-empty \
		1  older/file    older/file

	# The mtimes need to be explicitly assigned. Empirical evidence has
	# shown that executing mkdir(1) sequentially, with a single operand
	# each time, does not guarantee the order of the resulting mtimes.
	tstamp=197001010000
	for age in older newer; do
		mkdir "${age}" "${age}-empty" \
		&& touch -m -t "${tstamp%0}1" "${age}"/file \
		&& touch -m -t "${tstamp}" "${age}" "${age}-empty" \
		|| bailout "Couldn't create or adjust the mtimes of the sample files"
		tstamp=197001010100 # add an hour
	done

	callback() {
		shift
		test_description="is_older_than $(print_args "$@")"
		is_older_than "$@"
	}

	iterate_tests 3 "$@"
}

test_get_bootparam() {
	cmdline="foo gentoo=bar,baz quux"
	set -- \
		1  "${cmdline}"                   N/A \
		1  "${cmdline}"                   '' \
		1  "gentoo="                      '' \
		1  "${cmdline}"                   foo \
		0  "${cmdline}"                   bar \
		0  "foo gentoo=gentoo=1,bar baz"  bar \
		0  "foo gentoo=bar,gentoo=1 baz"  bar \
		0  "${cmdline}"                   baz \
		1  "${cmdline}"                   bar,baz \
		0  "foo gentoo=bar,gentoo=1 baz"  gentoo=1 \
		0  "foo gentoo=gentoo=1,bar baz"  gentoo=1 \
		1  "${cmdline}"                   quux

	callback() {
		cmdline=$2
		shift 2
		test_description="get_bootparam $(print_args "$@")"
		printf '%s\n' "${cmdline}" | get_bootparam "$@"
	}

	iterate_tests 3 "$@"
}

test_esyslog() {
	set -- \
		1  0  N/A    N/A   N/A \
		1  0  debug  N/A   N/A \
		0  0  debug  user  N/A \
		0  0  debug  user  '' \
		0  1  debug  user  message

	logger() {
		# esyslog() ignores empty messages. By overriding logger(1), it
		# can be determined whether a message would have been logged.
		logged=$((logged + 1))
	}

	callback() {
		should_log=$2
		shift 2
		logged=0
		test_description="esyslog $(print_args "$@")"
		EINFO_LOG=1 esyslog "$@" 2>/dev/null
		case $? in
			0)
				test "${logged}" -eq "${should_log}"
				;;
			*)
				return "$?"
		esac
	}

	iterate_tests 5 "$@"
}

test_is_identifier() {
	set -- \
		1   ''   \
		1    _   \
		1    0   \
		1   0a   \
		1   0Z   \
		1    9   \
		1   9a   \
		1   9Z   \
		1   /a   \
		1   /Z   \
		1   .a   \
		1   .Z   \
		1   [a   \
		1   [Z   \
		1  '`a'  \
		1  '`Z'  \
		1  '{a'  \
		1  '{Z'  \
		1  '|a'  \
		1  '|Z'  \
		1   a/   \
		1   Z/   \
		1   a.   \
		1   Z.   \
		1   a[   \
		1   Z[   \
		1  'a`'  \
		1  'Z`'  \
		1  'a{'  \
		1  'Z{'  \
		1  'a|'  \
		1  'Z|'  \
		0    a   \
		0    Z   \
		0   __   \
		0   _a   \
		0   _Z   \
		0   a_   \
		0   Z_   \
		0  a_a   \
		0  a_Z   \
		0  Z_a   \
		0  Z_Z

		callback() {
			shift
			test_description="is_identifier $(print_args "$@")"
			is_identifier "$@"
		}

		iterate_tests 2 "$@"
}

test_is_int() {
	set -- \
		1  N/A \
		1  ' ' \
		1  ' 1 ' \
		1  '' \
		1  +1 \
		1  +008 \
		1  -008 \
		1  008 \
		1  x \
		0  0 \
		0  1 \
		0  -1 \
		0  123456789

		callback() {
			shift
			test_description="is_int $(print_args "$@")"
			is_int "$@"
		}

		iterate_tests 2 "$@"
}

test_is_visible() {
	set -- \
		1  '' \
		1  ' ' \
		1  "$(printf '\t')" \
		1  "$(printf '\a')" \
		0  . \
		0  ' . ' \
		0  "$(printf '\t.\t')" \
		0  "$(printf '\a.\a')"

		callback() {
			shift
			test_description="_is_visible $(print_args "$@")"
			_is_visible "$@"
		}

		iterate_tests 2 "$@"
}

test_yesno() {
	set -- \
		0  yes \
		0  YES \
		0  Yes \
		0  true \
		0  TRUE \
		0  true \
		0  on \
		0  ON \
		0  On \
		0  1 \
		1  no \
		1  NO \
		1  No \
		1  false \
		1  FALSE \
		1  False \
		1  off \
		1  OFF \
		1  Off \
		1  0 \
		1  not_a_nameref \
		1  not-a-valid-nameref \
		1  '_"; set -- yes # code injection' \
		0  truthful_nameref

		# shellcheck disable=2034
		truthful_nameref=yes

		callback() {
			shift
			test_description="yesno $(print_args "$@")"
			yesno "$@"
		}

		iterate_tests 2 "$@"
}

iterate_tests() {
	slice_width=$1
	shift

	total=$(( $# / slice_width ))
	passed=0
	i=0
	while [ "$((i += 1))" -le "${total}" ]; do
		code="callback"
		j=0
		while [ "$((j += 1))" -le "${slice_width}" ]; do
			if eval "[ \"\$${j}\" = N/A ]"; then
				break
			else
				code="${code} \"\$${j}\""
			fi
		done
		eval "${code}"
		case $? in
			0)
				test "$?" -eq "$1"
				;;
			*)
				test "$?" -ge "$1"
		esac
		if [ "$?" -eq 0 ]; then
			passed=$((passed + 1))
		else
			printf 'not '
		fi
		if [ "$1" -eq 0 ]; then
			expected=$1
		else
			expected=">=$1"
		fi
		printf 'ok %d - %s (expecting %s)\n' "$((testnum += 1))" "${test_description}" "${expected}"
		shift "${slice_width}"
	done
	return "$(( passed < total ))"
}

print_args() {
	i=0
	for arg; do
		if [ "$((i += 1))" -eq 1 ]; then
			set --
		fi
		case ${arg} in
			''|*[[:space:]]*)
				set -- "$@" "'${arg}'"
				;;
			*)
				set -- "$@" "${arg}"
		esac
	done
	printf '%s\n' "$*"
}

printf 'TAP version 13\n'

unset -v dir

if ! . ./functions.sh; then
	bailout "Couldn't source ./functions.sh"
fi

assign_tmpdir

export TEST_GENFUNCS=1
export TZ=UTC
testnum=0
rc=0

test_chdir || rc=1
test_chdir_noop || rc=1
( test_ebegin ) || rc=1; testnum=$((testnum + 1))
test_is_older_than || rc=1
test_get_bootparam || rc=1
test_esyslog || rc=1
test_is_identifier || rc=1
test_is_int || rc=1
test_is_visible || rc=1
test_yesno || rc=1

cleanup_tmpdir

printf '1..%d\n' "${testnum}"

exit "${rc}"