build: ensure make-prime-list doesn't access out of bounds memory
[platform/upstream/coreutils.git] / tests / misc / sort-compress-proc.sh
1 #!/bin/sh
2 # Test use of compression subprocesses by sort
3
4 # Copyright (C) 2010-2013 Free Software Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ sort
21 expensive_
22
23 seq -w 2000 > exp || fail=1
24 tac exp > in || fail=1
25 insize=$(stat -c %s - <in) || fail=1
26
27 # This compressor's behavior is adjustable via environment variables.
28 export PRE_COMPRESS=
29 export POST_COMPRESS=
30 cat <<\EOF >compress || framework_failure_
31 #!/bin/sh
32 eval "$PRE_COMPRESS"
33 tr 41 14 || exit
34 eval "$POST_COMPRESS"
35 EOF
36
37 chmod +x compress
38
39 # "Impatient exit" tests
40 #
41 # In these test cases, the biggest compressor (or decompressor) exits
42 # with nonzero status, after sleeping a bit.  Until coreutils 8.7
43 # 'sort' impatiently exited without waiting for its decompression
44 # subprocesses in these cases.  Check compression too, while we're here.
45 #
46 for compress_arg in '' '-d'
47 do
48   POST_COMPRESS='
49     test "X$1" != "X'$compress_arg'" || {
50       test "X$1" = "X" && exec <&1
51       size=$(stat -c %s -)
52       exec >/dev/null 2>&1 <&1 || exit
53       expr $size "<" '"$insize"' / 2 || { sleep 1; exit 1; }
54     }
55   ' sort --compress-program=./compress -S 1k --batch-size=2 in > out && fail=1
56 done
57
58 # "Pre-exec child" test
59 #
60 # Ignore a random child process created before 'sort' was exec'ed.
61 # This bug was also present in coreutils 8.7.
62 #
63 ( (sleep 1; exec false) &
64   PRE_COMPRESS='test -f ok || sleep 2'
65   POST_COMPRESS='touch ok'
66   exec sort --compress-program=./compress -S 1k in >out
67 ) || fail=1
68 compare exp out || fail=1
69 test -f ok || fail=1
70 rm -f ok
71
72 rm -f compress
73
74 # If $TMPDIR is relative, give subprocesses time to react when 'sort' exits.
75 # Otherwise, under NFS, when 'sort' unlinks the temp files and they
76 # are renamed to .nfsXXXX instead of being removed, the parent cleanup
77 # of this directory will fail because the files are still open.
78 case $TMPDIR in
79 /*) ;;
80 *) sleep 1;;
81 esac
82
83 Exit $fail