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