summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/ar/artest.ksh
blob: 5854a95c3ae5d0fdc6c4ecb91c2662ae8c636c73 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/ksh
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2022 Oxide Computer Company
#

#
# This contains a number of basic tests for ar(1). When adding something
# to ar or fixing a bug, please expand this!
#

unalias -a
set -o pipefail

ar_arg0="$(basename $0)"
ar_data="$(dirname $0)"
ar_data0="$ar_data/ar_test0.o"
ar_data1="$ar_data/ar_test1.o"
ar_prog=/usr/bin/ar
ar_tmpdir=/tmp/ar.$$

ar_f01="$ar_tmpdir/test01.a"
ar_f10="$ar_tmpdir/test10.a"

ar_t01="ar_test0.o
ar_test1.o"
ar_t10="ar_test1.o
ar_test0.o"

strip_prog=/usr/bin/strip
dump_prog=/usr/bin/dump
nm_prog=/usr/bin/nm

ar_exit=0

function warn
{
	typeset msg="$*"
	[[ -z "$msg" ]] && msg="failed"
	print -u2 "TEST FAILED: $ar_arg0: $msg"
	ar_exit=1
}

function compare_files
{
	typeset base="$1"
	typeset comp="$2"
	typeset err=0

	if ! $dump_prog -g $comp | sed 1d > $comp.dump; then
		warn "failed to dump -g $comp"
		err=1
	fi

	if ! $nm_prog -P -tx $comp > $comp.nm; then
		warn "failed to nm $comp"
		err=1
	fi

	if ! diff $base.dump $comp.dump; then
		warn "$base.dump and $comp.dump don't match"
		err=1
	fi

	if ! diff $base.nm $comp.nm; then
		warn "$base.dump and $comp.dump don't match"
		err=1
	fi

	return $err
}

#
# To set things up, we first go and create two basic archives that we
# will then use as the basis for comaring various operations later.
#
function setup_baseline
{
	if ! $ar_prog cr $ar_f01 $ar_data0 $ar_data1; then
		warn "failed to create basic archive $ar_f01"
	fi

	if ! $ar_prog cr $ar_f10 $ar_data1 $ar_data0; then
		warn "failed to create basic archive $ar_f10"
	fi

	if ! $dump_prog -g $ar_f01 | sed 1d > $ar_f01.dump; then
		warn "failed to dump archive $ar_f01"
	fi

	if ! $dump_prog -g $ar_f10 | sed 1d > $ar_f10.dump; then
		warn "failed to dump archive $ar_f10"
	fi

	if ! $nm_prog -P -tx $ar_f01 > $ar_f01.nm; then
		warn "failed to nm archive $ar_f01"
	fi

	if ! $nm_prog -P -tx $ar_f10 > $ar_f10.nm; then
		warn "failed to nm archive $ar_f01"
	fi

	print "TEST PASSED: basic archive creation"
}

function strip_archive
{
	typeset file="$1"
	typeset output=

	if ! $strip_prog $file 2>/dev/null; then
		warn "failed to strip $alt"
		return 1
	fi

	output=$($dump_prog -g $file)
	if [[ -n "$output" ]]; then
		warn "stripped file $file somehow has an ar header"
		return 1
	fi

	return 0
}

#
# Validate that stripping and regenerating a symbol table actually
# works.
#
function test_s
{
	typeset alt="$ar_tmpdir/s.a"
	typeset output=

	if ! cp $ar_f01 $alt; then
		warn "failed to copy file"
		return
	fi

	if ! strip_archive $alt; then
		return
	fi

	if ! $ar_prog s $alt; then
		warn "restore symbol table with ar s"
	fi

	if compare_files "$ar_f01" "$alt"; then
		print "TEST PASSED: restoring stripped archive with -s"
	fi

	if ! strip_archive $alt; then
		return
	fi

	if ! $ar_prog st $alt >/dev/null; then
		warn "restore symbol table with ar st"
	fi

	if compare_files "$ar_f01" "$alt"; then
		print "TEST PASSED: restoring stripped archive with -st"
	fi

	if ! strip_archive $alt; then
		return
	fi

	output=$($ar_prog sv $alt 2>&1)
	if [[ "$output" == "ar: writing $alt" ]]; then
		print "TEST PASSED: ar -sv has proper output"
	else
		warn "ar -sv has unexpected output: $output"
	fi

	if compare_files "$ar_f01" "$alt"; then
		print "TEST PASSED: restoring stripped archive with -sv"
	fi
}

#
# Ensure that use of -s and -r still works. This is a regression test
# for the original integration of standalone -s support.
#
function test_rs
{
	typeset alt="$ar_tmpdir/rs.a"

	if ! $ar_prog rs $alt $ar_data1 $ar_data0; then
		warn "ar -rs: did not create an archive"
	fi

	if ! compare_files $ar_f10 $alt; then
		warn "ar -rs: did not create expected file"
	else
		print "TEST PASSED: ar -rs creates archives"
	fi

	rm -f $alt

	if ! $ar_prog crs $alt $ar_data1 $ar_data0; then
		warn "ar -crs: did not create an archive"
	fi

	if ! compare_files $ar_f10 $alt; then
		warn "ar -crs: did not create expected file"
	else
		print "TEST PASSED: ar -crs creates archives"
	fi
}

#
# Verify that basic ar -r invocations ultimately end up creating what
# we'd expect.
#
function test_incremental
{
	typeset alt="$ar_tmpdir/incr.a"

	if ! $ar_prog cr $alt $ar_data0; then
		warn "incremental archive: failed to create archive"
		return
	fi

	if ! $ar_prog cr $alt $ar_data1; then
		warn "incremental archive: failed to add to archive"
		return
	fi

	if ! compare_files $ar_f01 $alt; then
		warn "incremental archive: did not create expected file"
	else
		print "TEST PASSED: incremental archive creation"
	fi

}

#
# Validate that ar's various -a and -b variants work.
#
function test_pos
{
	typeset alt="$ar_tmpdir/pos.a"

	if ! $ar_prog cr $alt $ar_data1; then
		warn "positional tests: failed to create archive"
		return;
	fi

	if ! $ar_prog -cra ar_test1.o $alt $ar_data0; then
		warn "positional tests: -a append failed"
		return
	fi

	if ! compare_files $ar_f10 $alt; then
		warn "positional tests: -cra archive is incorrect"
	else
		print "TEST PASSED: positional tests: ar -cra"
	fi

	rm -f $alt

	if ! $ar_prog cr $alt $ar_data1; then
		warn "positional tests: failed to create archive"
		return;
	fi

	if ! $ar_prog -crb ar_test1.o $alt $ar_data0; then
		warn "positional tests: -b prepend failed"
		return
	fi

	if ! compare_files $ar_f01 $alt; then
		warn "positional tests: -crb archive is incorrect"
	else
		print "TEST PASSED: positional tests: ar -crb"
	fi

	rm -f $alt

	if ! $ar_prog cr $alt $ar_data1; then
		warn "positional tests: failed to create archive"
		return;
	fi

	if ! $ar_prog -cri ar_test1.o $alt $ar_data0; then
		warn "positional tests: -i prepend failed"
		return
	fi

	if ! compare_files $ar_f01 $alt; then
		warn "positional tests: -cri archive is incorrect"
	else
		print "TEST PASSED: positional tests: ar -cri"
	fi

}

#
# Go through and validate the various versions of ar x.
#
function test_x
{
	typeset out0="$ar_tmpdir/ar_test0.o"
	typeset out1="$ar_tmpdir/ar_test1.o"
	typeset output=

	rm -f $out0 $out1

	if ! $ar_prog x $ar_f01; then
		warn "ar -x: failed to extract files"
	fi

	if cmp -s $out0 $ar_data0 && cmp -s $out1 $ar_data1; then
		print "TEST PASSED: ar -x"
	else
		warn "ar -x: extracted files differs"
	fi

	rm -f $out0 $out1
	echo elberth > $out0

	#
	# For some reason, ar -Cx will actually fail if you have an
	# existing file. It seems a bit weird as it means you can't
	# extract existing files (depdendent on order), but that's how
	# it goes.
	#
	if $ar_prog Cx $ar_f01 ar_test0.o; then
		warn "ar -Cx: failed to extract file that wasn't in fs\n"
	fi

	output=$(cat $out0)
	if [[ "$output" != "elberth" ]]; then
		warn "ar -Cx: overwrote pre-existing file"
	else
		print "TEST PASSED: ar -Cx did not overwrite existing file"
	fi

	if ! $ar_prog Cx $ar_f01 ar_test1.o; then
		warn "ar -Cx: failed to extract file that wasn't in fs\n"
	fi

	if cmp -s $out1 $ar_data1; then
		print "TEST PASSED: ar -Cx extracted file that didn't exist"
	else
		warn "ar -Cx: failed to extract file that exists"
	fi
}

#
# Variant of -x that ensures we restore stripped archives.
#
function test_xs
{
	typeset alt="$ar_tmpdir/xs.a"
	typeset out0="$ar_tmpdir/ar_test0.o"
	typeset out1="$ar_tmpdir/ar_test1.o"

	rm -f $out0 $out1

	if ! cp $ar_f01 $alt; then
		warn "failed to copy file"
		return
	fi

	if ! strip_archive $alt; then
		return
	fi

	if ! $ar_prog xs $alt; then
		warn "ar -xs: failed to extract files"
	fi

	if cmp -s $out0 $ar_data0 && cmp -s $out1 $ar_data1 && \
	    compare_files "$ar_f01" "$alt"; then
		print "TEST PASSED: ar -xs"
	else
		warn "ar -xs: extracted and restore archive differ"
	fi
}

function test_m
{
	typeset alt="$ar_tmpdir/pos.a"

	if ! cp $ar_f01 $alt; then
		warn "failed to copy file"
		return
	fi

	if ! $ar_prog ma ar_test1.o $alt ar_test0.o; then
		warn "ar -ma: failed didn't work"
	fi

	if compare_files "$ar_f10" "$alt"; then
		print "TEST PASSED: ar -ma"
	else
		warn "ar -ma: did not create expected archive"
	fi

	if ! $ar_prog mb ar_test1.o $alt ar_test0.o; then
		warn "ar -ma: failed didn't work"
	fi

	if compare_files "$ar_f01" "$alt"; then
		print "TEST PASSED: ar -mb"
	else
		warn "ar -mb: did not create expected archive"
	fi
}

function test_t
{
	typeset output=

	output=$($ar_prog t $ar_f01)
	if [[ "$ar_t01" != "$output" ]]; then
		warn "ar t: mismatched output on $ar_t01, found $output"
	else
		print "TEST PASSED: ar -t (output 01)"
	fi

	output=$($ar_prog t $ar_f10)
	if [[ "$ar_t10" != "$output" ]]; then
		warn "ar t: mismatched output on $ar_f10, found $output"
	else
		print "TEST PASSED: ar -t (output 10)"
	fi
}

function test_q
{
	typeset alt="$ar_tmpdir/q.a"

	if ! $ar_prog q $alt $ar_data1 $ar_data0; then
		warn "ar -q: did not create an archive"
	fi

	if ! compare_files $ar_f10 $alt; then
		warn "ar -q: did not create expected file"
	else
		print "TEST PASSED: ar -q creates archives"
	fi

	rm -f $alt

	if ! $ar_prog cq $alt $ar_data1 $ar_data0; then
		warn "ar -rs: did not create an archive"
	fi

	if ! compare_files $ar_f10 $alt; then
		warn "ar -cq: did not create expected file"
	else
		print "TEST PASSED: ar -cq creates archives"
	fi

	rm -f $alt

	if ! $ar_prog cqs $alt $ar_data1 $ar_data0; then
		warn "ar -cqs: did not create an archive"
	fi

	if ! compare_files $ar_f10 $alt; then
		warn "ar -cqs: did not create expected file"
	else
		print "TEST PASSED: ar -cqs creates archives"
	fi

}

function test_err
{
	if $ar_prog $@ 2>/dev/null 1>/dev/null; then
		warn "should have failed with args "$@", but passed"
	else
		printf "TEST PASSED: invalid arguments %s\n" "$*"
	fi
}

#
# Before we begin execution, set up the environment such that we have a
# standard locale and that umem will help us catch mistakes.
#
export LC_ALL=C.UTF-8
export LD_PRELOAD=libumem.so
export UMEM_DEBUG=default

if ! mkdir "$ar_tmpdir"; then
	printf "failed to make output directory %s\n" "$ar_tmpdir" >&2
	exit 1
fi

if ! cd "$ar_tmpdir"; then
	printf "failed to cd into output directory %s\n" "$ar_tmpdir" >&2
	exit 1
fi

if [[ ! -d "$ar_data" ]]; then
	printf "failed to find data directory %s\n" "$ar_data" >&2
	exit 1
fi

if [[ -n $AR ]]; then
	echo overwrote AR as $AR
	ar_prog=$AR
fi

setup_baseline
test_s
test_rs
test_incremental
test_pos
test_x
test_xs
test_m
test_t
test_q

#
# Note, there are many cases here which probably should be failures and
# aren't (e.g. ar -mabi) because that's how the tool works today. With
# proper regression testing of building 3rd party packages this could be
# changed.
#
test_err ""
test_err "-z"
test_err "-d"
test_err "-d" "$ar_tmpdir/enoent"
test_err "-d" "$ar_f01" "foobar.exe"
test_err "-m" "$ar_tmpdir/enoent"
test_err "-ma" "foobar.exe" "$ar_tmpdir/enoent"
test_err "-mb" "foobar.exe" "$ar_tmpdir/enoent"
test_err "-mi" "foobar.exe" "$ar_tmpdir/enoent"
test_err "-p" "$ar_tmpdir/enoent"
test_err "-P" "$ar_tmpdir/enoent"
test_err "-q"
test_err "-qa" "foobar.exe" "$ar_f0.a"
test_err "-qb" "foobar.exe" "$ar_f0.a"
test_err "-qi" "foobar.exe" "$ar_f0.a"
test_err "-qa" "ar_test0.o" "$ar_f0.a"
test_err "-qb" "ar_test0.o" "$ar_f0.a"
test_err "-qi" "ar_test0.o" "$ar_f0.a"
test_err "-r"
test_err "-ra" "foobar.exe"
test_err "-ra" "foobar.exe" "$ar_tmpdir/enoent"
test_err "-rb" "foobar.exe"
test_err "-rb" "foobar.exe" "$ar_tmpdir/enoent"
test_err "-ri" "foobar.exe"
test_err "-ri" "foobar.exe" "$ar_tmpdir/enoent"
test_err "-t"
test_err "-t" "$ar_tmpdir/enoent"
test_err "-x"
test_err "-x" "$ar_tmpdir/enoent"
test_err "-s"
test_err "-s" "$ar_tmpdir/enoent"
test_err "-s" "$ar_f01" "$ar_f10"
test_err "-sz" "$ar_f01"
test_err "-rd"
test_err "-xd"
test_err "-qp"

if (( ar_exit == 0 )); then
	printf "All tests passed successfully!\n"
fi

cd - >/dev/null
rm -rf "$ar_tmpdir"
exit $ar_exit