summaryrefslogtreecommitdiffstats
path: root/source3/script/tests/test_virus_scanner.sh
blob: 83b50df915f89404c280de82c3d56d992d723447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
# Copyright (c) 2022      Pavel Filipenský <pfilipen@redhat.com>
# shellcheck disable=1091

if [ $# -lt 4 ]; then
	cat <<EOF
Usage: $0 SERVER_IP SHARE LOCAL_PATH SMBCLIENT
EOF
	exit 1
fi

SERVER_IP=${1}
SHARE=${2}
LOCAL_PATH=${3}
SMBCLIENT=${4}

SMBCLIENT="${VALGRIND} ${SMBCLIENT}"

failed=0
sharedir="${LOCAL_PATH}/${SHARE}"

incdir="$(dirname "$0")/../../../testprogs/blackbox"
. "${incdir}/subunit.sh"

check_infected_read()
{
	rm -rf "${sharedir:?}"/*

	if ! mkdir "${sharedir}/read1"; then
		echo "ERROR: Cannot create ${sharedir}/read1"
		return 1
	fi

	if ! mkdir "${sharedir}/read1/read2"; then
		echo "ERROR: Cannot create ${sharedir}/read1/read2"
		return 1
	fi

	if ! touch "${sharedir}/read1/read2/infected.txt"; then
		echo "ERROR: Cannot create ${sharedir}/read1/read2/infected.txt"
		return 1
	fi

	${SMBCLIENT} "//${SERVER_IP}/${SHARE}" -U"${USER}"%"${PASSWORD}" -c "get read1/read2/infected.txt ${sharedir}/read1/read2/infected.download.txt"

	# check that virusfilter:rename prefix/suffix was added
	if [ ! -f "${sharedir}/read1/read2/virusfilter.infected.txt.infected" ]; then
		echo "ERROR: ${sharedir}/read1/read2/virusfilter.infected.txt.infected is missing."
		return 1
	fi

	# check that file was not downloaded
	if [ -f "${sharedir}/read1/read2/infected.download.txt" ]; then
		echo "ERROR: {sharedir}/read1/read2/infected.download.txt should not exist."
		return 1
	fi

	rm -rf "${sharedir:?}"/*
	return 0
}

check_infected_write()
{
	rm -rf "${sharedir:?}"/*
	smbfile=infected.upload.txt
	smbfilerenamed="virusfilter.${smbfile}.infected"

	# non empty file is needed
	# vsf_virusfilter performs a scan only if fsp->fsp_flags.modified
	if ! echo "Hello Virus!" >"${sharedir}/infected.txt"; then
		echo "ERROR: Cannot create ${sharedir}/infected.txt"
		return 1
	fi

	${SMBCLIENT} "//${SERVER_IP}/${SHARE}" -U"${USER}"%"${PASSWORD}" -c "put ${sharedir}/infected.txt ${smbfile}"

	# check that virusfilter:rename prefix/suffix was added
	if [ ! -f "${sharedir}/${smbfilerenamed}" ]; then
		echo "ERROR: ${sharedir}/${smbfilerenamed} is missing."
		return 1
	fi

	# check that file was not uploaded
	if [ -f "${sharedir}/infected.upload.txt" ]; then
		echo "ERROR: {sharedir}/${smbfile} should not exist."
		return 1
	fi

	return 0
}

check_healthy_read()
{
	rm -rf "${sharedir:?}"/*

	if ! echo "Hello Samba!" >"${sharedir}/healthy.txt"; then
		echo "ERROR: Cannot create ${sharedir}/healthy.txt"
		return 1
	fi

	${SMBCLIENT} //"${SERVER_IP}"/"${SHARE}" -U"${USER}"%"${PASSWORD}" -c "get healthy.txt ${sharedir}/healthy.download.txt"

	if ! cmp "${sharedir}/healthy.txt" "${sharedir}/healthy.download.txt"; then
		echo "ERROR: cmp ${sharedir}/healthy.txt ${sharedir}/healthy.download.txt FAILED"
		return 1
	fi

	return 0
}

check_healthy_write()
{
	rm -rf "${sharedir:?}"/*

	if ! echo "Hello Samba!" >"${sharedir}/healthy.txt"; then
		echo "ERROR: Cannot create ${sharedir}/healthy.txt"
		return 1
	fi

	${SMBCLIENT} //"${SERVER_IP}"/"${SHARE}" -U"${USER}"%"${PASSWORD}" -c "put ${sharedir}/healthy.txt healthy.upload.txt"

	if ! cmp "${sharedir}/healthy.txt" "${sharedir}/healthy.upload.txt"; then
		echo "ERROR: cmp ${sharedir}/healthy.txt ${sharedir}/healthy.upload.txt FAILED"
		return 1
	fi

	return 0
}

testit "check_infected_read" check_infected_read || failed=$((failed + 1))
testit "check_infected_write" check_infected_write || failed=$((failed + 1))
testit "check_healthy_read" check_healthy_read || failed=$((failed + 1))
testit "check_healthy_write" check_healthy_write || failed=$((failed + 1))

testok "$0" "$failed"