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