xref: /spdk/test/dd/posix.sh (revision 7192849ed24874f3e9cc31e8a33a9b32c49b9506)
1#!/usr/bin/env bash
2testdir=$(readlink -f "$(dirname "$0")")
3rootdir=$(readlink -f "$testdir/../../")
4source "$testdir/common.sh"
5
6cleanup() {
7	rm -f "$test_file0"{,.link}
8	rm -f "$test_file1"{,.link}
9}
10
11append() {
12	local dump0
13	local dump1
14
15	dump0=$(gen_bytes 32)
16	dump1=$(gen_bytes 32)
17
18	printf '%s' "$dump0" > "$test_file0"
19	printf '%s' "$dump1" > "$test_file1"
20
21	"${DD_APP[@]}" --if="$test_file0" --of="$test_file1" --oflag=append
22
23	[[ $(< "$test_file1") == "${dump1}${dump0}" ]]
24}
25
26directory() {
27	NOT "${DD_APP[@]}" --if="$test_file0" --iflag=directory --of="$test_file0"
28	NOT "${DD_APP[@]}" --if="$test_file0" --of="$test_file0" --oflag=directory
29}
30
31nofollow() {
32	local test_file0_link=$test_file0.link
33	local test_file1_link=$test_file1.link
34
35	ln -fs "$test_file0" "$test_file0_link"
36	ln -fs "$test_file1" "$test_file1_link"
37
38	NOT "${DD_APP[@]}" --if="$test_file0_link" --iflag=nofollow --of="$test_file1"
39	NOT "${DD_APP[@]}" --if="$test_file0" --of="$test_file1_link" --oflag=nofollow
40
41	# Do an extra step of checking if we actually can follow symlinks
42	gen_bytes 512 > "$test_file0"
43
44	"${DD_APP[@]}" --if="$test_file0_link" --of="$test_file1"
45	[[ $(< "$test_file0") == "$(< "$test_file1")" ]]
46}
47
48noatime() {
49	local atime_if
50	local atime_of
51
52	# It seems like spdk_dd doesn't update the atime in case 0 bytes are copied.
53	# This differs from how standard dd works for instance
54	gen_bytes 512 > "$test_file0"
55
56	atime_if=$(stat --printf="%X" "$test_file0")
57	atime_of=$(stat --printf="%X" "$test_file1")
58
59	"${DD_APP[@]}" --if="$test_file0" --iflag=noatime --of="$test_file1"
60	((atime_if == $(stat --printf="%X" "$test_file0")))
61	((atime_of == $(stat --printf="%X" "$test_file1")))
62
63	"${DD_APP[@]}" --if="$test_file0" --of="$test_file1"
64	((atime_if < $(stat --printf="%X" "$test_file0")))
65}
66
67io() {
68	local flags_ro flags_rw flag_ro flag_rw
69
70	# O_NONBLOCK is actually a no-op, from a functional perspective, while
71	# open()ing a regular file, but let's keep it just to test its usage.
72	flags_ro=(direct nonblock)
73	flags_rw=("${flags_ro[@]}" sync dsync)
74
75	# simply check if data was correctly copied between files
76	for flag_ro in "${flags_ro[@]}"; do
77		gen_bytes 512 > "$test_file0"
78		for flag_rw in "${flags_rw[@]}"; do
79			"${DD_APP[@]}" \
80				--if="$test_file0" \
81				--iflag="$flag_ro" \
82				--of="$test_file1" \
83				--oflag="$flag_rw"
84			[[ $(< "$test_file0") == "$(< "$test_file1")" ]]
85		done
86	done
87}
88
89trap "cleanup" EXIT
90
91test_file0=$SPDK_TEST_STORAGE/dd.dump0
92test_file1=$SPDK_TEST_STORAGE/dd.dump1
93
94run_test "dd_flag_append" append
95run_test "dd_flag_directory" directory
96run_test "dd_flag_nofollow" nofollow
97run_test "dd_flag_noatime" noatime
98run_test "dd_flags_misc" io
99