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# $FreeBSD: stable/11/share/examples/tests/tests/tap/cp_test.sh 346920 2019-04-29 19:36:46Z ngie $
27#
28
29#
30# INTRODUCTION
31#
32# This TAP test program mimics the structure and contents of its
33# ATF-based counterpart.  It attempts to represent various test cases
34# in different separate functions and just calls them all from main.
35#
36
37test_num=1
38TEST_COUNT=4
39
40result()
41{
42	local result=$1; shift
43	local result_string
44
45	result_string="$result $test_num"
46	if [ $# -gt 0 ]; then
47		result_string="$result_string - $@"
48	fi
49	echo "$result_string"
50	: $(( test_num += 1 ))
51}
52
53# Auxiliary function to compare two files for equality.
54verify_copy() {
55	if cmp -s "${1}" "${2}"; then
56		result "ok"
57	else
58		result "not ok" "${1} and ${2} differ, but they should be equal"
59		diff -u "${1}" "${2}"
60	fi
61}
62
63simple_test() {
64	cp "$(dirname "${0}")/file1" .
65	if cp file1 file2; then
66		result "ok"
67		verify_copy file1 file2
68	else
69		result "not ok" "cp failed"
70		result "not ok" "# SKIP"
71	fi
72}
73
74force_test() {
75	echo 'File 3' >file3
76	chmod 400 file3
77	if cp -f file1 file3; then
78		result "ok"
79		verify_copy file1 file3
80	else
81		result "not ok" "cp -f failed"
82		result "not ok" "# SKIP"
83	fi
84}
85
86# If you have read the cp_test.sh counterpart in the atf/ directory, you
87# may think that the sequencing of tests below and the exposed behavior
88# to the user is very similar.  But you'd be wrong.
89#
90# There are two major differences with this and the ATF version. First off,
91# the TAP test doesn't isolate simple_test from force_test, whereas the ATF
92# version does. Secondly, the test script accepts arbitrary command line
93# inputs.
94echo "1..$TEST_COUNT"
95
96simple_test
97force_test
98exit 0
99