1#!/bin/sh
2#
3# Copyright (c) 2017 Enji Cooper <ngie@FreeBSD.org>
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24# POSSIBILITY OF SUCH DAMAGE.
25#
26#
27
28#
29# INTRODUCTION
30#
31# This TAP test program mimics the structure and contents of its
32# ATF-based counterpart.  It attempts to represent various test cases
33# in different separate functions and just calls them all from main.
34#
35
36test_num=1
37TEST_COUNT=4
38
39result()
40{
41	local result=$1; shift
42	local result_string
43
44	result_string="$result $test_num"
45	if [ $# -gt 0 ]; then
46		result_string="$result_string - $@"
47	fi
48	echo "$result_string"
49	: $(( test_num += 1 ))
50}
51
52# Auxiliary function to compare two files for equality.
53verify_copy() {
54	if cmp -s "${1}" "${2}"; then
55		result "ok"
56	else
57		result "not ok" "${1} and ${2} differ, but they should be equal"
58		diff -u "${1}" "${2}"
59	fi
60}
61
62simple_test() {
63	cp "$(dirname "${0}")/file1" .
64	if cp file1 file2; then
65		result "ok"
66		verify_copy file1 file2
67	else
68		result "not ok" "cp failed"
69		result "not ok" "# SKIP"
70	fi
71}
72
73force_test() {
74	echo 'File 3' >file3
75	chmod 400 file3
76	if cp -f file1 file3; then
77		result "ok"
78		verify_copy file1 file3
79	else
80		result "not ok" "cp -f failed"
81		result "not ok" "# SKIP"
82	fi
83}
84
85# If you have read the cp_test.sh counterpart in the atf/ directory, you
86# may think that the sequencing of tests below and the exposed behavior
87# to the user is very similar.  But you'd be wrong.
88#
89# There are two major differences with this and the ATF version. First off,
90# the TAP test doesn't isolate simple_test from force_test, whereas the ATF
91# version does. Secondly, the test script accepts arbitrary command line
92# inputs.
93echo "1..$TEST_COUNT"
94
95simple_test
96force_test
97exit 0
98