Added regression test for bug #7234 - Symlink delete fails but incorrectly reports...
[nivanova/samba-autobuild/.git] / source3 / script / tests / test_smbclient_s3.sh
1 #!/bin/sh
2
3 # this runs the file serving tests that are expected to pass with samba3
4
5 if [ $# -lt 6 ]; then
6 cat <<EOF
7 Usage: test_smbclient_s3.sh SERVER SERVER_IP USERNAME PASSWORD USERID LOCAL_PATH
8 EOF
9 exit 1;
10 fi
11
12 SERVER="$1"
13 SERVER_IP="$2"
14 USERNAME="$3"
15 PASSWORD="$4"
16 USERID="$5"
17 LOCAL_PATH="$6"
18 SMBCLIENT="$VALGRIND ${SMBCLIENT:-$BINDIR/smbclient} $CONFIGURATION"
19 shift 6
20 ADDARGS="$*"
21
22 test x"$TEST_FUNCTIONS_SH" != x"INCLUDED" && {
23 incdir=`dirname $0`
24 . $incdir/test_functions.sh
25 }
26
27 failed=0
28
29 # Test that a noninteractive smbclient does not prompt
30 test_noninteractive_no_prompt()
31 {
32     prompt="smb"
33
34     cmd='echo du | $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS 2>&1'
35     eval echo "$cmd"
36     out=`eval $cmd`
37
38     if [ $? != 0 ] ; then
39         echo "$out"
40         echo "command failed"
41         false
42         return
43     fi
44
45     echo "$out" | grep $prompt >/dev/null 2>&1
46
47     if [ $? = 0 ] ; then
48         # got a prompt .. fail
49         echo matched interactive prompt in non-interactive mode
50         false
51     else
52         true
53     fi
54 }
55
56 # Test that an interactive smbclient prompts to stdout
57 test_interactive_prompt_stdout()
58 {
59     prompt="smb"
60     tmpfile=/tmp/smbclient.in.$$
61
62     cat > $tmpfile <<EOF
63 du
64 quit
65 EOF
66
67     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
68     eval echo "$cmd"
69     out=`eval $cmd`
70     ret=$?
71     rm -f $tmpfile
72
73     if [ $ret != 0 ] ; then
74         echo "$out"
75         echo "command failed"
76         false
77         return
78     fi
79
80     echo "$out" | grep $prompt >/dev/null 2>&1
81
82     if [ $? = 0 ] ; then
83         # got a prompt .. succeed
84         true
85     else
86         echo failed to match interactive prompt on stdout
87         false
88     fi
89 }
90
91 # Test creating a bad symlink and deleting it.
92 test_bad_symlink()
93 {
94     prompt="posix_unlink deleted file /newname"
95     tmpfile=/tmp/smbclient.in.$$
96
97     cat > $tmpfile <<EOF
98 posix
99 posix_unlink newname
100 symlink badname newname
101 posix_unlink newname
102 quit
103 EOF
104
105     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
106     eval echo "$cmd"
107     out=`eval $cmd`
108     ret=$?
109     rm -f $tmpfile
110
111     if [ $ret != 0 ] ; then
112         echo "$out"
113         echo "failed create then delete bad symlink with error $ret"
114         false
115         return
116     fi
117
118     echo "$out" | grep "$prompt" >/dev/null 2>&1
119
120     ret=$?
121     if [ $ret = 0 ] ; then
122         # got the correct prompt .. succeed
123         true
124     else
125         echo "$out"
126         echo "failed create then delete bad symlink - grep failed with $ret"
127         false
128     fi
129 }
130
131 # Test creating a good symlink and deleting it by path.
132 test_good_symlink()
133 {
134     tmpfile=/tmp/smbclient.in.$$
135
136     touch "$LOCAL_PATH/foo"
137     ln -s "$LOCAL_PATH/foo" "$LOCAL_PATH/bar"
138     cat > $tmpfile <<EOF
139 del bar
140 quit
141 EOF
142
143     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
144     eval echo "$cmd"
145     out=`eval $cmd`
146     ret=$?
147     rm -f $tmpfile
148
149     if [ $ret != 0 ] ; then
150         echo "$out"
151         echo "failed delete good symlink with error $ret"
152         false
153         return
154     fi
155
156     if [ -e "$LOCAL_PATH/bar" ] ; then
157         echo "failed delete good symlink - symlink still exists"
158         rm "$LOCAL_PATH/bar"
159         rm "$LOCAL_PATH/foo"
160         false
161     else
162         # got the correct prompt .. succeed
163         rm "$LOCAL_PATH/foo"
164         true
165     fi
166 }
167
168 # Test writing into a read-only directory (logon as guest) fails.
169 test_read_only_dir()
170 {
171     prompt="NT_STATUS_ACCESS_DENIED making remote directory"
172     tmpfile=/tmp/smbclient.in.$$
173
174 ##
175 ## We can't do this as non-root. We always have rights to
176 ## create the directory.
177 ##
178     if [ "$USERID" != 0 ] ; then
179         echo "skipping test_read_only_dir as non-root"
180         true
181         return
182     fi
183
184 ##
185 ## We can't do this with an encrypted connection. No credentials
186 ## to set up the channel.
187 ##
188     if [ "$ADDARGS" = "-e" ] ; then
189         echo "skipping test_read_only_dir with encrypted connection"
190         true
191         return
192     fi
193
194     cat > $tmpfile <<EOF
195 mkdir a_test_dir
196 quit
197 EOF
198
199     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
200     eval echo "$cmd"
201     out=`eval $cmd`
202     ret=$?
203     rm -f $tmpfile
204
205     if [ $ret != 0 ] ; then
206         echo "$out"
207         echo "failed writing into read-only directory with error $ret"
208         false
209         return
210     fi
211
212     echo "$out" | grep "$prompt" >/dev/null 2>&1
213
214     ret=$?
215     if [ $ret = 0 ] ; then
216         # got the correct prompt .. succeed
217         true
218     else
219         echo "$out"
220         echo "failed writing into read-only directory - grep failed with $ret"
221         false
222     fi
223 }
224
225 # Test reading an owner-only file (logon as guest) fails.
226 test_owner_only_file()
227 {
228     prompt="NT_STATUS_ACCESS_DENIED opening remote file"
229     tmpfile=/tmp/smbclient.in.$$
230
231 ##
232 ## We can't do this as non-root. We always have rights to
233 ## read the file.
234 ##
235     if [ "$USERID" != 0 ] ; then
236         echo "skipping test_owner_only_file as non-root"
237         true
238         return
239     fi
240
241 ##
242 ## We can't do this with an encrypted connection. No credentials
243 ## to set up the channel.
244 ##
245     if [ "$ADDARGS" = "-e" ] ; then
246         echo "skipping test_owner_only_file with encrypted connection"
247         true
248         return
249     fi
250
251     cat > $tmpfile <<EOF
252 get unreadable_file
253 quit
254 EOF
255
256     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
257     eval echo "$cmd"
258     out=`eval $cmd`
259     ret=$?
260     rm -f $tmpfile
261
262     if [ $ret != 0 ] ; then
263         echo "$out"
264         echo "failed reading owner-only file with error $ret"
265         false
266         return
267     fi
268
269     echo "$out" | grep "$prompt" >/dev/null 2>&1
270
271     ret=$?
272     if [ $ret = 0 ] ; then
273         # got the correct prompt .. succeed
274         true
275     else
276         echo "$out"
277         echo "failed reading owner-only file - grep failed with $ret"
278         false
279     fi
280 }
281
282 testit "smbclient -L $SERVER_IP" $SMBCLIENT $CONFIGURATION -L $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
283 testit "smbclient -L $SERVER -I $SERVER_IP" $SMBCLIENT $CONFIGURATION -L $SERVER -I $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
284
285 testit "noninteractive smbclient does not prompt" \
286     test_noninteractive_no_prompt || \
287     failed=`expr $failed + 1`
288
289 testit "noninteractive smbclient -l does not prompt" \
290    test_noninteractive_no_prompt -l /tmp || \
291     failed=`expr $failed + 1`
292
293 testit "interactive smbclient prompts on stdout" \
294    test_interactive_prompt_stdout || \
295     failed=`expr $failed + 1`
296
297 testit "interactive smbclient -l prompts on stdout" \
298    test_interactive_prompt_stdout -l /tmp || \
299     failed=`expr $failed + 1`
300
301 testit "creating a bad symlink and deleting it" \
302    test_bad_symlink || \
303    failed=`expr $failed + 1`
304
305 testit "creating a good symlink and deleting it by path" \
306    test_good_symlink || \
307    failed=`expr $failed + 1`
308
309 testit "writing into a read-only directory fails" \
310    test_read_only_dir || \
311    failed=`expr $failed + 1`
312
313 testit "Reading a owner-only file fails" \
314    test_owner_only_file || \
315    failed=`expr $failed + 1`
316
317 testok $0 $failed