1 | #!/bin/sh |
---|
2 | # |
---|
3 | # check-utils - check that zzuf properly fuzzes common utilities |
---|
4 | # Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
5 | # All Rights Reserved |
---|
6 | # |
---|
7 | # This program is free software. It comes without any warranty, to |
---|
8 | # the extent permitted by applicable law. You can redistribute it |
---|
9 | # and/or modify it under the terms of the Do What The Fuck You Want |
---|
10 | # To Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | # http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | # |
---|
13 | |
---|
14 | . "$(dirname "$0")/functions.inc" |
---|
15 | |
---|
16 | HAVE_GETLINE=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^ getline(' && echo "y") |
---|
17 | HAVE_GETDELIM=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^ getdelim(' && echo "y") |
---|
18 | HAVE_GETC_UNLOCKED=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^ getc_unlocked(' && echo "y") |
---|
19 | HAVE_FGETC_UNLOCKED=$("$ZZCAT" -l | grep >/dev/null 2>&1 '^ fgetc_unlocked(' && echo "y") |
---|
20 | |
---|
21 | checkutils() |
---|
22 | { |
---|
23 | r=$1 |
---|
24 | for type in 00 ff text random; do |
---|
25 | file="$DIR/file-$type" |
---|
26 | ZZOPTS="-s $seed -r $r" |
---|
27 | case $file in |
---|
28 | *text*) ZZOPTS="$ZZOPTS -P '\\n' -R '\\000'" ;; |
---|
29 | esac |
---|
30 | echo "*** file $file, ratio $r ***" |
---|
31 | REFMD5="" |
---|
32 | if [ $r = 0.0 -a $type = 00 ]; then |
---|
33 | check="bb7df04e1b0a2570657527a7e108ae23" |
---|
34 | echo "*** should be $check ***" |
---|
35 | check "$ZZOPTS" "< $file" "zzuf" "$check" |
---|
36 | else |
---|
37 | check "$ZZOPTS" "< $file" "zzuf" |
---|
38 | fi |
---|
39 | if [ "$STATIC_CAT" = "" ]; then |
---|
40 | check "$ZZOPTS" "cat $file" "cat" |
---|
41 | check "$ZZOPTS" "-i cat < $file" "|cat" |
---|
42 | fi |
---|
43 | if [ "$STATIC_DD" = "" ]; then |
---|
44 | check "$ZZOPTS" "dd bs=65536 if=$file" "dd(bs=65536)" |
---|
45 | check "$ZZOPTS" "dd bs=1111 if=$file" "dd(bs=1111)" |
---|
46 | check "$ZZOPTS" "dd bs=1024 if=$file" "dd(bs=1024)" |
---|
47 | check "$ZZOPTS" "dd bs=1 if=$file" "dd(bs=1)" |
---|
48 | fi |
---|
49 | case $file in |
---|
50 | *text*) |
---|
51 | # We don't include grep or sed when the input is not text, because |
---|
52 | # they put a newline at the end of their input if it was not there |
---|
53 | # initially. (Linux sed doesn't, but OS X sed does.) |
---|
54 | check "$ZZOPTS" "head -n 9999 $file" "head -n 9999" |
---|
55 | check "$ZZOPTS" "${TAILN}9999 $file" "${TAILN}9999" |
---|
56 | check "$ZZOPTS" "${TAILP}1 $file" "${TAILP}1" |
---|
57 | if grep -a '' /dev/null >/dev/null 2>&1; then |
---|
58 | check "$ZZOPTS" "grep -a '' $file" "grep -a ''" |
---|
59 | fi |
---|
60 | check "$ZZOPTS" "sed -e n $file" "sed -e n" |
---|
61 | #check "$ZZOPTS" "cut -b1- $file" "cut -b1-" |
---|
62 | check "$ZZOPTS" "-i head -n 9999 < $file" "|head -n 9999" |
---|
63 | check "$ZZOPTS" "-i ${TAILN}9999 < $file" "|${TAILN}9999" |
---|
64 | check "$ZZOPTS" "-i ${TAILP}1 < $file" "|${TAILP}1" |
---|
65 | if grep -a '' /dev/null >/dev/null 2>&1; then |
---|
66 | check "$ZZOPTS" "-i grep -a '' < $file" "|grep -a ''" |
---|
67 | fi |
---|
68 | check "$ZZOPTS" "-i sed -e n < $file" "|sed -e n" |
---|
69 | #check "$ZZOPTS" "-i cut -b1- < $file" "|cut -b1-" |
---|
70 | ;; |
---|
71 | esac |
---|
72 | # Regression tests for stuff that used to break |
---|
73 | check "$ZZOPTS" "$ZZCAT -x \"fread(1,33000) fseek(1,SEEK_SET) fread(1,1) fseek(4093,SEEK_CUR) fread(1,1) fseek(1,SEEK_CUR) fread(1,1)\" $file" \ |
---|
74 | "eglibc (2.9-21) bug regression" |
---|
75 | if [ "$HAVE_GET_UNLOCKED $HAVE_GETLINE" = "y y" ]; then |
---|
76 | check "$ZZOPTS" "$ZZCAT -x \"repeat(33000,getc_unlocked(),feof(10),ungetc(),getline()))\" $file" \ |
---|
77 | "sed getc_unlocked() bug regression" |
---|
78 | fi |
---|
79 | # Misc tests |
---|
80 | for n in \ |
---|
81 | "fread(1,33000)" \ |
---|
82 | "repeat(-1,fread(1,1000),feof(1))" \ |
---|
83 | "repeat(-1,fread(1,33),feof(1))" \ |
---|
84 | "getc() fread(1,33000)" \ |
---|
85 | "fgetc() fread(1,33000)" \ |
---|
86 | "getc() ungetc() fread(1,33000)" \ |
---|
87 | "fgetc() ungetc() fread(1,33000)" \ |
---|
88 | "repeat(-1,getc(),feof(10))" \ |
---|
89 | "repeat(-1,fgetc(),feof(10))" \ |
---|
90 | "repeat(8000,getc()) fread(1,33000)" \ |
---|
91 | "repeat(8000,fgetc()) fread(1,33000)" \ |
---|
92 | "fread(1,100) fseek(50,SEEK_SET) fread(1,33000)" \ |
---|
93 | "fread(1,100) rewind() fseek(50,SEEK_CUR) fread(1,33000)" \ |
---|
94 | "fread(1,33000) rewind() repeat(-1,fseek(1,SEEK_CUR),fread(1,1),feof(1))" \ |
---|
95 | "fread(1,33000) rewind() repeat(-1,fseek(2,SEEK_CUR),fread(1,2),feof(1))" \ |
---|
96 | "fread(1,33000) rewind() repeat(-1,fseek(3,SEEK_CUR),fread(1,3),feof(1))" \ |
---|
97 | "fread(1,33000) rewind() repeat(-1,fseek(4,SEEK_CUR),fread(1,4),feof(1))" \ |
---|
98 | "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,fread(1,2),feof(10))" \ |
---|
99 | "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,getc(),feof(10))" \ |
---|
100 | "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,fgetc(),feof(10))" \ |
---|
101 | ; do |
---|
102 | check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n" |
---|
103 | done |
---|
104 | # Platform-specific tests |
---|
105 | if [ "$HAVE_GETLINE" = "y" ]; then |
---|
106 | for n in \ |
---|
107 | "repeat(10,getline()) fread(1,33000)" \ |
---|
108 | "repeat(-1,getc(),feof(10),ungetc(),getline())" \ |
---|
109 | "repeat(-1,fgetc(),feof(10),ungetc(),getline())" \ |
---|
110 | ; do |
---|
111 | check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n" |
---|
112 | done |
---|
113 | fi |
---|
114 | if [ "$HAVE_GETDELIM" = "y" ]; then |
---|
115 | for n in \ |
---|
116 | "repeat(10,getdelim(0)) fread(1,33000)" \ |
---|
117 | "repeat(10,getdelim(' ')) fread(1,33000)" \ |
---|
118 | ; do |
---|
119 | check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n" |
---|
120 | done |
---|
121 | fi |
---|
122 | if [ "$HAVE_GETC_UNLOCKED" = "y" ]; then |
---|
123 | for n in \ |
---|
124 | "getc_unlocked() fread(1,33000)" \ |
---|
125 | "getc_unlocked() ungetc() fread(1,33000)" \ |
---|
126 | "repeat(-1,getc_unlocked(),feof(10))" \ |
---|
127 | "repeat(8000,getc_unlocked()) fread(1,33000)" \ |
---|
128 | "fread(1,33000) fseek(1000,SEEK_CUR) repeat(-1,getc_unlocked(),feof(10))" \ |
---|
129 | ; do |
---|
130 | check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n" |
---|
131 | done |
---|
132 | fi |
---|
133 | if [ "$HAVE_FGETC_UNLOCKED" = "y" ]; then |
---|
134 | for n in \ |
---|
135 | "fgetc_unlocked() fread(1,33000)" \ |
---|
136 | "fgetc_unlocked() ungetc() fread(1,33000)" \ |
---|
137 | "repeat(-1,fgetc_unlocked(),feof(10))" \ |
---|
138 | "repeat(8000,fgetc_unlocked()) fread(1,33000)" \ |
---|
139 | ; do |
---|
140 | check "$ZZOPTS" "$ZZCAT -x \"$n\" $file" "$n" |
---|
141 | done |
---|
142 | fi |
---|
143 | done |
---|
144 | } |
---|
145 | |
---|
146 | check() |
---|
147 | { |
---|
148 | ZZOPTS="$1" |
---|
149 | CMD="$2" |
---|
150 | ALIAS="$3" |
---|
151 | CHECK="$4" |
---|
152 | printf " $(echo "$ALIAS .................................." | cut -b1-38) " |
---|
153 | MD5="$(eval "$ZZUF -m $ZZOPTS $CMD" 2>/dev/null | cut -f2 -d' ')" |
---|
154 | if [ -n "$CHECK" ]; then |
---|
155 | REFMD5="$CHECK" |
---|
156 | fi |
---|
157 | if [ -z "$REFMD5" ]; then |
---|
158 | REFMD5="$MD5" |
---|
159 | echo "$MD5" |
---|
160 | elif [ "$MD5" != "$REFMD5" ]; then |
---|
161 | fail_test "$MD5 FAILED" |
---|
162 | else |
---|
163 | pass_test 'ok' |
---|
164 | fi |
---|
165 | } |
---|
166 | |
---|
167 | start_test "zzuf utils test suite" |
---|
168 | |
---|
169 | checkutils 0.0 |
---|
170 | checkutils 0.001 |
---|
171 | checkutils 1.0 |
---|
172 | |
---|
173 | stop_test |
---|
174 | |
---|