xref: /aosp_15_r20/external/toybox/tests/dd.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3# Copyright 2013 Robin Mittal <[email protected]>
4# Copyright 2013 Divya Kothari <[email protected]>
5
6# TODO test skip= for seekable and nonseekable input.
7# TODO bs overrides ibs= and obs=, and disables block aggregation
8
9[ -f testing.sh ] && . testing.sh
10
11# 'dd' command, stderr prints redirecting to /dev/null
12exec 2>/dev/null
13
14#testing "name" "command" "result" "infile" "stdin"
15
16# Test suffixed number parsing; `count` is representative.
17testcmd "count=2" "if=input count=2 ibs=1" "hi" "high\n" ""
18testcmd "count= 2" "if=input 'count= 2' ibs=1" "hi" "high\n" ""
19toyonly testcmd "count=0x2" "if=input 'count=0x2' ibs=1" "hi" "high\n" ""
20testcmd 'posix 2x3x4' "if=input count=2x3x4 ibs=1" \
21  'abcdefghijklmnopqrstuvwx' 'abcdefghijklmnopqrstuvwxyz' ''
22testcmd "count=-2" "if=input 'count=-2' ibs=1 || echo errored" "errored\n" "" ""
23
24testcmd "if=(file)" "if=input" "I WANT\n" "I WANT\n" ""
25testcmd "of=(file)" "of=file && cat file" "I WANT\n" "" "I WANT\n"
26testcmd "if=file of=file" "if=input of=foo && cat foo && rm -f foo" \
27  "I WANT\n" "I WANT\n" ""
28testcmd "if=file | dd of=file" "if=input | dd of=foo && cat foo && rm -f foo" \
29  "I WANT\n" "I WANT\n" ""
30testcmd "(stdout)" '' "I WANT\n" "" "I WANT\n"
31testcmd "sync,noerror" \
32  "if=input of=outFile seek=8860 bs=1M conv=sync,noerror &&
33   stat -c \"%s\" outFile && rm -f outFile" "9291431936\n" "I WANT\n" ""
34testcmd "if=file of=(null)" "if=input of=/dev/null && echo 'yes'" "yes\n" \
35  "I WANT\n" ""
36testcmd "with if of bs" \
37  "if=/dev/zero of=sda.txt bs=512 count=1 &&
38   stat -c '%s' sda.txt && rm -f sda.txt" "512\n" "" ""
39testcmd "with if of ibs obs" \
40  "if=/dev/zero of=sda.txt ibs=512 obs=256 count=1 &&
41   stat -c '%s' sda.txt && rm -f sda.txt" "512\n" "" ""
42// TODO check block size of transactions
43testcmd "with if of ibs obs count" \
44  "if=/dev/zero of=sda.txt ibs=512 obs=256 count=3 &&
45   stat -c '%s' sda.txt && rm -f sda.txt" "1536\n" "" ""
46
47ln -s input softlink
48testcmd "if=softlink" "if=softlink" "I WANT\n" "I WANT\n" ""
49rm -f softlink
50
51ln -s file softlink
52testcmd "if=file of=softlink" "if=input of=softlink &&
53   [ -f file -a -L softlink ] && cat softlink" "I WANT\n" "I WANT\n" ""
54rm -f softlink file
55
56testcmd "if=file of=file (same file)" "if=input of=input &&
57   [ -f input ] && cat input && echo 'yes'" "yes\n" "I WANT\n" ""
58testcmd "same file notrunc" \
59  "if=input of=input conv=notrunc && cat input" "I WANT\n" "I WANT\n" ""
60testcmd "seek truncate" \
61  'of=input bs=3 count=2 seek=7 oflag=seek_bytes && cat input' \
62  '1234567ABCDEF' '1234567890abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJK'
63testcmd "seek notrunc" \
64  'of=input bs=3 count=2 seek=7 conv=notrunc oflag=seek_bytes && cat input' \
65  '1234567ABCDEFdefghijklmnopqrstuvwxyz' '1234567890abcdefghijklmnopqrstuvwxyz'\
66  'ABCDEFGHIJK'
67
68testcmd "with ibs obs bs" "ibs=2 obs=5 bs=9" "I WANT\n" "" "I WANT\n"
69testcmd "with ibs obs bs if" "ibs=2 obs=5 bs=9 if=input" "I WANT\n" "I WANT\n"\
70  ""
71
72testcmd "with ibs obs count" "ibs=1 obs=1 count=1" "I" "" "I WANT\n"
73testcmd "with ibs obs count if" "ibs=1 obs=1 count=3 if=input" "I W" "I WANT\n"\
74  ""
75
76testcmd "with count" "count=1" "I WANT\n" "" "I WANT\n"
77testcmd "with count if" "count=1 if=input" "I WANT\n" "I WANT\n" ""
78
79testcmd "with skip" "skip=0" "I WANT\n" "" "I WANT\n"
80testcmd "with skip if" "skip=0 if=input" "I WANT\n" "I WANT\n" ""
81
82testcmd "with seek" "seek=0" "I WANT\n" "" "I WANT\n"
83testcmd "with seek if" "seek=0 if=input" "I WANT\n" "I WANT\n" ""
84
85# Testing only 'notrunc', 'noerror', 'fsync', 'sync'
86
87testcmd "conv=notrunc" "conv=notrunc" "I WANT\n" "" "I WANT\n"
88testcmd "conv=notrunc with IF" "conv=notrunc if=input" "I WANT\n" \
89  "I WANT\n" ""
90
91testcmd "conv=noerror" "conv=noerror" "I WANT\n" "" "I WANT\n"
92testcmd "conv=noerror with IF" "conv=noerror if=input" "I WANT\n" \
93  "I WANT\n" ""
94
95testcmd "conv=fsync" "conv=fsync" "I WANT\n" "" "I WANT\n"
96testcmd "conv=fsync with IF" "conv=fsync if=input" "I WANT\n" \
97  "I WANT\n" ""
98
99testcmd "conv=sync" "conv=sync | head -n 1" "I WANT\n" "" "I WANT\n"
100testcmd "conv=sync with IF" "conv=sync if=input | head -n 1" "I WANT\n" \
101  "I WANT\n" ""
102
103# status=noxfer|none
104testcmd "status=noxfer" "if=input status=noxfer ibs=1 2>&1" \
105  "input\n6+0 records in\n0+1 records out\n" "input\n" ""
106testcmd "status=none" "if=input status=none ibs=1 2>&1" \
107 "input\n" "input\n" ""
108
109testing "seek stdout" "yes | dd bs=8 seek=2 count=1 > out && xxd -p out" \
110  "00000000000000000000000000000000790a790a790a790a\n" "" ""
111
112# Duplicated options are fine.
113testcmd "conv=sync,sync" "conv=sync,sync | head -n 1" "I WANT\n" "" "I WANT\n"
114
115# _bytes options
116testcmd "iflag=count_bytes" "if=input count=2 ibs=4096 iflag=count_bytes" "hi"\
117  "high" ""
118testcmd "iflag=skip_bytes" "if=input skip=2 ibs=4096 iflag=skip_bytes" "gh" \
119  "high" ""
120testcmd "oflag=seek_bytes" \
121  "if=input of=output seek=2 obs=4096 oflag=seek_bytes status=none && \
122   xxd -p output" "000030313233\n" "0123" ""
123
124# The sleep decouples input blocks to force short reads for conv=sync to pad
125testing 'sync padding ticks down count' \
126  'for i in one two three four five "$(seq 1 100)"
127   do echo "$i"; sleep .1; done | dd bs=1024 count=5 conv=sync | sha1sum' \
128  '02dcf1f497ccbe940f57818dfc34f2d0def8b3f9  -\n' '' ''
129