]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/examples/tests/tests/atf/cp_test.sh
Merge ^/vendor/llvm-project/release-10.x up to its last change (upstream
[FreeBSD/FreeBSD.git] / share / examples / tests / tests / atf / cp_test.sh
1 # $FreeBSD$
2 #
3 # SPDX-License-Identifier: BSD-3-Clause
4 #
5 # Copyright 2013 Google Inc.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 #   notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above copyright
15 #   notice, this list of conditions and the following disclaimer in the
16 #   documentation and/or other materials provided with the distribution.
17 # * Neither the name of Google Inc. nor the names of its contributors
18 #   may be used to endorse or promote products derived from this software
19 #   without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 #
34 # INTRODUCTION
35 #
36 # This sample test program implements various test cases for the cp(1)
37 # utility in order to demonstrate the usage of the ATF shell API (see
38 # atf-sh-api(3)).
39 #
40
41 #
42 # Auxiliary function to compare two files for equality.
43 #
44 verify_copy() {
45         if ! cmp -s "${1}" "${2}"; then
46                 echo "${1} and ${2} differ, but they should be equal"
47                 diff -u "${1}" "${2}"
48                 atf_fail "Original and copy do not match"
49         fi
50 }
51
52 #
53 # This is the simplest form of a test case definition: a test case
54 # without a header.
55 #
56 # In most cases, this is the definition you will want to use.  However,
57 # make absolutely sure that the test case name is descriptive enough.
58 # Multi-word test case names are encouraged.  Keep in mind that these
59 # are exposed to the reader in the test reports, and the goal is for
60 # the combination of the test program plus the name of the test case to
61 # give a pretty clear idea of what specific condition the test is
62 # validating.
63 #
64 atf_test_case simple
65 simple_body() {
66         cp $(atf_get_srcdir)/file1 .
67
68         # The atf_check function is a very powerful function of atf-sh.
69         # It allows you to define checkers for the exit status, the
70         # stdout and the stderr of any command you execute.  If the
71         # result of the command does not match the expectations defined
72         # in the checkers, the test fails and verbosely reports data
73         # behind the problem.
74         #
75         # See atf-check(1) for details.
76         atf_check -s exit:0 -o empty -e empty cp file1 file2
77
78         verify_copy file1 file2
79
80         # Of special note here is that we are NOT deleting the temporary
81         # files we created in this test.  Kyua takes care of this
82         # cleanup automatically and tests can (and should) rely on this
83         # behavior.
84 }
85
86 #
87 # This is a more complex form of a test case definition: a test case
88 # with a header and a body.  You should always favor the simpler
89 # definition above unless you have to override specific metadata
90 # variables.
91 #
92 # See atf-test-case(4) and kyua-atf-interface(1) for details on all
93 # available properties.
94 #
95 atf_test_case force
96 force_head() {
97         # In this specific case, we define a textual description for
98         # the test case, which is later exported to the reports for
99         # documentation purposes.
100         #
101         # However, note again that you should favor highly descriptive
102         # test case names to textual descriptions.
103         atf_set "descr" "Tests that the -f flag causes cp to forcibly" \
104             "override the destination file"
105 }
106 force_body() {
107         cp $(atf_get_srcdir)/file1 .
108         echo 'File 2' >file2
109         chmod 400 file2
110         atf_check cp -f file1 file2
111         verify_copy file1 file2
112 }
113
114 #
115 # Lastly, we tell ATF which test cases exist in this program.  This
116 # function should not do anything other than this registration.
117 #
118 atf_init_test_cases() {
119         atf_add_test_case simple
120         atf_add_test_case force
121 }