1272343Sngie# $NetBSD: t_dd.sh,v 1.1 2012/03/17 16:33:11 jruoho Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2007 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343Sngietest_dd_length() {
29272343Sngie	result=$1
30272343Sngie	cmd=$2
31272343Sngie	set -- x `eval $cmd | wc -c`
32272343Sngie	res=$2
33272343Sngie	if [ x"$res" != x"$result" ]; then
34272343Sngie		atf_fail "Expected $result bytes of output, got $res: $cmd"
35272343Sngie	fi
36272343Sngie}
37272343Sngie
38272343Sngieatf_test_case length
39272343Sngielength_head() {
40272343Sngie	# XXX The PR should be stored in a tag.
41272343Sngie	atf_set "descr" "Test for result messages accidentally pumped into" \
42272343Sngie	                "the output file if the standard IO descriptors are" \
43272343Sngie	                "closed.  The last of the three following tests is" \
44272343Sngie	                "the one expected to fail.  (NetBSD PR bin/8521)"
45272343Sngie}
46272343Sngielength_body() {
47289487Sngie	# Begin FreeBSD
48289487Sngie	if ! df /dev/fd | grep -q '^fdescfs'; then
49289487Sngie		atf_skip "fdescfs is not mounted on /dev/fd"
50289487Sngie	fi
51289487Sngie	# End FreeBSD
52289487Sngie
53272343Sngie	test_dd_length 512 \
54272343Sngie	    "dd if=/dev/zero of=/dev/fd/5 count=1 5>&1 >/dev/null 2>/dev/null"
55272343Sngie	test_dd_length 512 \
56272343Sngie	    "dd if=/dev/zero of=/dev/fd/5 count=1 5>&1 >&- 2>/dev/null"
57272343Sngie	test_dd_length 512 \
58272343Sngie	    "dd if=/dev/zero of=/dev/fd/5 count=1 5>&1 >&- 2>&-"
59272343Sngie}
60272343Sngie
61272343Sngietest_dd_io() {
62272343Sngie	res="`echo -n "$2" | eval $1`"
63272343Sngie        if [ x"$res" != x"$3" ]; then
64272343Sngie		atf_fail "Expected \"$3\", got \"$res\": $1"
65272343Sngie	fi
66272343Sngie}
67272343Sngie
68272343Sngieallbits1="\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
69272343Sngie
70272343Sngieebcdicbits1="\000\001\002\003\067\055\056\057\026\005\045\013\014\015\016\017\020\021\022\023\074\075\062\046\030\031\077\047\034\035\036\037\100\132\177\173\133\154\120\175\115\135\134\116\153\140\113\141\360\361\362\363\364\365\366\367\370\371\172\136\114\176\156\157\174\301\302\303\304\305\306\307\310\311\321\322\323\324\325\326\327\330\331\342\343\344\345\346\347\350\351\255\340\275\232\155\171\201\202\203\204\205\206\207\210\211\221\222\223\224\225\226\227\230\231\242\243\244\245\246\247\250\251\300\117\320\137\007\040\041\042\043\044\025\006\027\050\051\052\053\054\011\012\033\060\061\032\063\064\065\066\010\070\071\072\073\004\024\076\341\101\102\103\104\105\106\107\110\111\121\122\123\124\125\126\127\130\131\142\143\144\145\146\147\150\151\160\161\162\163\164\165\166\167\170\200\212\213\214\215\216\217\220\152\233\234\235\236\237\240\252\253\254\112\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\241\276\277\312\313\314\315\316\317\332\333\334\335\336\337\352\353\354\355\356\357\372\373\374\375\376\377"
71272343Sngie
72272343Sngieallvisbits=`echo -n "$allbits1" | unvis | vis`
73272343Sngieebcdicvisbits=`echo -n "$ebcdicbits1" | unvis | vis`
74272343Sngie
75272343Sngieatf_test_case io
76272343Sngieio_head() {
77272343Sngie	atf_set "descr" "This checks the combination of bs= with" \
78272343Sngie	                "conv=ebcdic.  Prior to revision 1.24 of dd's" \
79272343Sngie	                "args.c, the conv option would be ignored."
80272343Sngie}
81272343Sngieio_body() {
82272343Sngie	test_dd_io "unvis | dd 2>/dev/null | vis" \
83272343Sngie	           "$allvisbits" "$allvisbits"
84272343Sngie	test_dd_io "unvis | dd ibs=1 2>/dev/null | vis" \
85272343Sngie	           "$allvisbits" "$allvisbits"
86272343Sngie	test_dd_io "unvis | dd obs=1 2>/dev/null | vis" \
87272343Sngie	           "$allvisbits" "$allvisbits"
88272343Sngie	test_dd_io "unvis | dd bs=1 2>/dev/null | vis" \
89272343Sngie	           "$allvisbits" "$allvisbits"
90272343Sngie
91272343Sngie	test_dd_io "unvis | dd conv=ebcdic 2>/dev/null | vis" \
92272343Sngie	           "$allvisbits" "$ebcdicvisbits"
93272343Sngie	test_dd_io "unvis | dd conv=ebcdic ibs=512 2>/dev/null | vis" \
94272343Sngie	           "$allvisbits" "$ebcdicvisbits"
95272343Sngie	test_dd_io "unvis | dd conv=ebcdic obs=512 2>/dev/null | vis" \
96272343Sngie	           "$allvisbits" "$ebcdicvisbits"
97272343Sngie	test_dd_io "unvis | dd conv=ebcdic bs=512 2>/dev/null | vis" \
98272343Sngie	           "$allvisbits" "$ebcdicvisbits"
99272343Sngie
100272343Sngie	test_dd_io "unvis | dd conv=ebcdic 2>/dev/null | vis" \
101272343Sngie	           "$allvisbits" "$ebcdicvisbits"
102272343Sngie	test_dd_io "unvis | dd conv=ebcdic ibs=1 2>/dev/null | vis" \
103272343Sngie	           "$allvisbits" "$ebcdicvisbits"
104272343Sngie	test_dd_io "unvis | dd conv=ebcdic obs=1 2>/dev/null | vis" \
105272343Sngie	           "$allvisbits" "$ebcdicvisbits"
106272343Sngie	test_dd_io "unvis | dd conv=ebcdic bs=1 2>/dev/null | vis" \
107272343Sngie	           "$allvisbits" "$ebcdicvisbits"
108272343Sngie}
109272343Sngie
110272343Sngieatf_test_case seek
111272343Sngieseek_head() {
112272343Sngie	atf_set "descr" "Tests output file seeking"
113272343Sngie}
114272343Sngie
115272343Sngieseek_body() {
116272343Sngie	echo TEST1234 > testfile
117272343Sngie	atf_check -s exit:0 -e ignore \
118272343Sngie	    dd if=/dev/zero of=testfile seek=1 bs=8k count=1
119272343Sngie	atf_check -s exit:0 -e ignore -o match:'^TEST1234$' dd if=testfile
120272343Sngie	eval $(stat -s testfile)
121272343Sngie	atf_check_equal $st_size $((2*8192))
122272343Sngie
123272343Sngie	echo -n TEST1234 > tf2
124272343Sngie	atf_check -s exit:0 -e ignore -x \
125272343Sngie	    'dd bs=4 if=/dev/zero count=1 | tr \\0 \\n | dd of=tf2 bs=4 seek=1'
126272343Sngie	atf_check -s exit:0 -e ignore -o match:'^TEST$' dd if=tf2
127272343Sngie	eval $(stat -s tf2)
128272343Sngie	atf_check_equal $st_size 8
129272343Sngie}
130272343Sngie
131272343Sngieatf_init_test_cases()
132272343Sngie{
133272343Sngie	atf_add_test_case length
134272343Sngie	atf_add_test_case io
135272343Sngie	atf_add_test_case seek
136272343Sngie}
137