1320443Sngie#!/bin/sh
2258299Sjmmv#
3346920Sngie# Copyright (c) 2017 Enji Cooper <ngie@FreeBSD.org>
4258299Sjmmv#
5258299Sjmmv# Redistribution and use in source and binary forms, with or without
6320443Sngie# modification, are permitted provided that the following conditions
7320443Sngie# are met:
8320443Sngie# 1. Redistributions of source code must retain the above copyright
9320443Sngie#    notice, this list of conditions and the following disclaimer.
10320443Sngie# 2. Redistributions in binary form must reproduce the above copyright
11320443Sngie#    notice, this list of conditions and the following disclaimer in the
12320443Sngie#    documentation and/or other materials provided with the distribution.
13258299Sjmmv#
14320443Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15320443Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16320443Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17320443Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18320443Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19320443Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20320443Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21320443Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22320443Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23320443Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24320443Sngie# POSSIBILITY OF SUCH DAMAGE.
25258299Sjmmv#
26320443Sngie# $FreeBSD: stable/11/share/examples/tests/tests/tap/cp_test.sh 346920 2019-04-29 19:36:46Z ngie $
27320443Sngie#
28258299Sjmmv
29258299Sjmmv#
30258299Sjmmv# INTRODUCTION
31258299Sjmmv#
32320443Sngie# This TAP test program mimics the structure and contents of its
33258299Sjmmv# ATF-based counterpart.  It attempts to represent various test cases
34258299Sjmmv# in different separate functions and just calls them all from main.
35258299Sjmmv#
36258299Sjmmv
37320443Sngietest_num=1
38320443SngieTEST_COUNT=4
39258299Sjmmv
40320443Sngieresult()
41320443Sngie{
42320443Sngie	local result=$1; shift
43320443Sngie	local result_string
44320443Sngie
45320443Sngie	result_string="$result $test_num"
46320443Sngie	if [ $# -gt 0 ]; then
47320443Sngie		result_string="$result_string - $@"
48320443Sngie	fi
49320443Sngie	echo "$result_string"
50320443Sngie	: $(( test_num += 1 ))
51258299Sjmmv}
52258299Sjmmv
53258299Sjmmv# Auxiliary function to compare two files for equality.
54258299Sjmmvverify_copy() {
55320443Sngie	if cmp -s "${1}" "${2}"; then
56320443Sngie		result "ok"
57320443Sngie	else
58320443Sngie		result "not ok" "${1} and ${2} differ, but they should be equal"
59258299Sjmmv		diff -u "${1}" "${2}"
60258299Sjmmv	fi
61258299Sjmmv}
62258299Sjmmv
63258299Sjmmvsimple_test() {
64299094Sngie	cp "$(dirname "${0}")/file1" .
65320443Sngie	if cp file1 file2; then
66320443Sngie		result "ok"
67320443Sngie		verify_copy file1 file2
68320443Sngie	else
69320443Sngie		result "not ok" "cp failed"
70320443Sngie		result "not ok" "# SKIP"
71320443Sngie	fi
72258299Sjmmv}
73258299Sjmmv
74258299Sjmmvforce_test() {
75299094Sngie	echo 'File 3' >file3
76299094Sngie	chmod 400 file3
77320443Sngie	if cp -f file1 file3; then
78320443Sngie		result "ok"
79320443Sngie		verify_copy file1 file3
80320443Sngie	else
81320443Sngie		result "not ok" "cp -f failed"
82320443Sngie		result "not ok" "# SKIP"
83320443Sngie	fi
84258299Sjmmv}
85258299Sjmmv
86258299Sjmmv# If you have read the cp_test.sh counterpart in the atf/ directory, you
87258299Sjmmv# may think that the sequencing of tests below and the exposed behavior
88258299Sjmmv# to the user is very similar.  But you'd be wrong.
89258299Sjmmv#
90320443Sngie# There are two major differences with this and the ATF version. First off,
91320443Sngie# the TAP test doesn't isolate simple_test from force_test, whereas the ATF
92320443Sngie# version does. Secondly, the test script accepts arbitrary command line
93320443Sngie# inputs.
94320443Sngieecho "1..$TEST_COUNT"
95320443Sngie
96258299Sjmmvsimple_test
97258299Sjmmvforce_test
98320443Sngieexit 0
99