| 1 |
;;; |
| 2 |
;;; Test zlib |
| 3 |
;;; |
| 4 |
|
| 5 |
#!no-fold-case |
| 6 |
|
| 7 |
(use gauche.test) |
| 8 |
(use gauche.uvector) |
| 9 |
(use gauche.vport) |
| 10 |
(use gauche.process) |
| 11 |
|
| 12 |
(test-start "rfc.zlib") |
| 13 |
|
| 14 |
;; bail out if we aren't configured to build zlib |
| 15 |
(unless (file-exists? (string-append "zlib." (gauche-dso-suffix))) |
| 16 |
(test-end) |
| 17 |
(exit 0)) |
| 18 |
|
| 19 |
(load "./zlib") |
| 20 |
(import rfc.zlib) |
| 21 |
(test-module 'rfc.zlib) |
| 22 |
|
| 23 |
(test* "zlib-version" #t (string? (zlib-version))) |
| 24 |
|
| 25 |
(test-section "inflate port") |
| 26 |
|
| 27 |
;;------------------------------------------------------------------ |
| 28 |
(test-section "checksum procedures") |
| 29 |
|
| 30 |
(test* "crc32 (string)" 0 (crc32 "")) |
| 31 |
(test* "crc32 (string)" 2666930069 (crc32 "foobar")) |
| 32 |
(test* "crc32 (string)" 4010574376 (crc32 "abc" 8563)) |
| 33 |
(test* "crc32 (u8vector)" 0 (crc32 #u8())) |
| 34 |
(test* "crc32 (u8vector)" 2666930069 (crc32 (string->u8vector "foobar"))) |
| 35 |
(test* "crc32 (u8vector)" 4010574376 (crc32 (string->u8vector "abc") 8563)) |
| 36 |
(test* "crc32 (error)" (test-error) (crc32 'foo)) |
| 37 |
|
| 38 |
(test* "adler32 (string)" 1 (adler32 "")) |
| 39 |
(test* "adler32 (string)" 145425018 (adler32 "foobar")) |
| 40 |
(test* "adler32 (string)" 1721967257 (adler32 "abc" 8563)) |
| 41 |
(test* "adler32 (u8vector)" 1 (adler32 #u8())) |
| 42 |
(test* "adler32 (u8vector)" 145425018 (adler32 (string->u8vector "foobar"))) |
| 43 |
(test* "adler32 (string)" 1721967257 (adler32 (string->u8vector "abc") 8563)) |
| 44 |
(test* "adler32 (error)" (test-error) (adler32 'foo)) |
| 45 |
|
| 46 |
;;------------------------------------------------------------------ |
| 47 |
(test-section "constant values") |
| 48 |
|
| 49 |
(test* "Z_NO_COMPRESSION" 0 Z_NO_COMPRESSION) |
| 50 |
(test* "Z_BEST_SPEED" 1 Z_BEST_SPEED) |
| 51 |
(test* "Z_BEST_COMPRESSION" 9 Z_BEST_COMPRESSION) |
| 52 |
(test* "Z_DEFAULT_COMPRESSION" -1 Z_DEFAULT_COMPRESSION) |
| 53 |
(test* "Z_FILTERED" 1 Z_FILTERED) |
| 54 |
(test* "Z_HUFFMAN_ONLY" 2 Z_HUFFMAN_ONLY) |
| 55 |
(test* "Z_RLE" 3 Z_RLE) |
| 56 |
(test* "Z_FIXED" 4 Z_FIXED) |
| 57 |
(test* "Z_DEFAULT_STRATEGY" 0 Z_DEFAULT_STRATEGY) |
| 58 |
(test* "Z_BINARY" 0 Z_BINARY) |
| 59 |
(test* "Z_TEXT" 1 Z_TEXT) |
| 60 |
(test* "Z_ASCII" 1 Z_ASCII) |
| 61 |
(test* "Z_UNKNOWN" 2 Z_UNKNOWN) |
| 62 |
|
| 63 |
;;------------------------------------------------------------------ |
| 64 |
(test-section "zlib condition type") |
| 65 |
|
| 66 |
(test* "condition-type? <zlib-error>" #t |
| 67 |
(condition-type? <zlib-error>)) |
| 68 |
(test* "condition-type? <zlib-need-dict-error>" #t |
| 69 |
(condition-type? <zlib-need-dict-error>)) |
| 70 |
(test* "condition-type? <zlib-stream-error>" #t |
| 71 |
(condition-type? <zlib-stream-error>)) |
| 72 |
(test* "condition-type? <zlib-data-error>" #t |
| 73 |
(condition-type? <zlib-data-error>)) |
| 74 |
(test* "condition-type? <zlib-memory-error>" #t |
| 75 |
(condition-type? <zlib-memory-error>)) |
| 76 |
(test* "condition-type? <zlib-version-error>" #t |
| 77 |
(condition-type? <zlib-version-error>)) |
| 78 |
|
| 79 |
(test* "class-precedence-list <zlib-error>" |
| 80 |
'(<zlib-error> <error> <message-condition> <serious-condition> <condition> <top>) |
| 81 |
(map class-name (class-precedence-list <zlib-error>))) |
| 82 |
|
| 83 |
;;------------------------------------------------------------------ |
| 84 |
(test-section "deflate port") |
| 85 |
|
| 86 |
(test* "<deflating-port>" <deflating-port> |
| 87 |
(class-of (open-deflating-port (open-output-string)))) |
| 88 |
(test* "<deflating-port>'s CPL" '(<deflating-port> <port> <top>) |
| 89 |
(map class-name |
| 90 |
(class-precedence-list |
| 91 |
(class-of (open-deflating-port (open-output-string)))))) |
| 92 |
|
| 93 |
(test* "open-deflating-port" #t |
| 94 |
(port? (open-deflating-port (open-output-string)))) |
| 95 |
|
| 96 |
(test* "open-deflating-port" (test-error) |
| 97 |
(open-deflating-port (standard-input-port))) |
| 98 |
|
| 99 |
(test* "open-deflating-port :compression-level 9" #t |
| 100 |
(port? (open-deflating-port (open-output-string) :compression-level 9))) |
| 101 |
(test* "open-deflating-port :compression-level 10" 'OK |
| 102 |
(guard (e ((<zlib-stream-error> e) 'OK) |
| 103 |
(else 'error)) |
| 104 |
(open-deflating-port (open-output-string) :compression-level 10) |
| 105 |
'error)) |
| 106 |
|
| 107 |
(test* "open-deflating-port :buffer-size 512" #t |
| 108 |
(port? (open-deflating-port (open-output-string) :buffer-size 512))) |
| 109 |
|
| 110 |
(test* "open-deflating-port :window-bits 15" #t |
| 111 |
(port? (open-deflating-port (open-output-string) :window-bits 15))) |
| 112 |
(test* "open-deflating-port :window-bits -1" 'OK |
| 113 |
(guard (e ((<zlib-stream-error> e) 'OK)) |
| 114 |
(open-deflating-port (open-output-string) :window-bits -1))) |
| 115 |
|
| 116 |
(test* "open-deflating-port :memory-level 1" #t |
| 117 |
(port? (open-deflating-port (open-output-string) :memory-level 1))) |
| 118 |
(test* "open-deflating-port :memory-level -1" 'OK |
| 119 |
(guard (e ((<zlib-stream-error> e) 'OK)) |
| 120 |
(open-deflating-port (open-output-string) :memory-level -1))) |
| 121 |
|
| 122 |
(test* "open-deflating-port :strategy Z_HUFFMAN_ONLY" #t |
| 123 |
(port? (open-deflating-port (open-output-string) :strategy Z_HUFFMAN_ONLY))) |
| 124 |
(test* "open-deflating-port :strategy 100" 'OK |
| 125 |
(guard (e ((<zlib-stream-error> e) 'OK)) |
| 126 |
(open-deflating-port (open-output-string) :strategy 100))) |
| 127 |
|
| 128 |
(test* "open-deflating-port :dictionary \"abc\"" #t |
| 129 |
(port? (open-deflating-port (open-output-string) :dictionary "abc"))) |
| 130 |
(test* "open-deflating-port :dictionary '()" (test-error) |
| 131 |
(open-deflating-port (open-output-string) :dictionary '())) |
| 132 |
|
| 133 |
(test* "zstream-dictionary-adler32" #f |
| 134 |
(zstream-dictionary-adler32 |
| 135 |
(open-deflating-port (open-output-string)))) |
| 136 |
(test* "zstream-dictionary-adler32" (adler32 "abc") |
| 137 |
(zstream-dictionary-adler32 |
| 138 |
(open-deflating-port (open-output-string) :dictionary "abc"))) |
| 139 |
|
| 140 |
(test* "deflate-string" #*"x\x9c\x03\0\0\0\0\x01" |
| 141 |
(deflate-string "")) |
| 142 |
|
| 143 |
(test* "deflate-string" #*"x\x9cK\xcb\xcfOJ,\x02\0\x08\xab\x02z" |
| 144 |
(deflate-string "foobar")) |
| 145 |
|
| 146 |
(test* "deflate-string" #*"x\x9c\xed\xc1\x81\0\0\0\0\xc3 \xed\xe1\x9f\xe0\x06U\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x1c\x03%eI{" |
| 147 |
(deflate-string (make-string 16537 #\x))) |
| 148 |
|
| 149 |
(test* "owner? keyword" #f |
| 150 |
(let1 p (open-output-string) |
| 151 |
(close-output-port (open-deflating-port p)) |
| 152 |
(port-closed? p))) |
| 153 |
|
| 154 |
(test* "owner? keyword" #t |
| 155 |
(let1 p (open-output-string) |
| 156 |
(close-output-port (open-deflating-port p :owner? #t)) |
| 157 |
(port-closed? p))) |
| 158 |
|
| 159 |
(test* "port-file-number" #f |
| 160 |
(port-file-number (open-deflating-port (open-output-string)))) |
| 161 |
|
| 162 |
(test* "port-file-number" 'OK |
| 163 |
(call-with-output-file "test.o" |
| 164 |
(lambda (in) |
| 165 |
(let1 dp (open-deflating-port in) |
| 166 |
(begin0 (and (number? (port-file-number dp)) 'OK) |
| 167 |
(close-output-port dp)))))) |
| 168 |
(sys-unlink "test.o") |
| 169 |
|
| 170 |
(test* "port-name" "[deflating (output string port)]" |
| 171 |
(port-name (open-deflating-port (open-output-string)))) |
| 172 |
|
| 173 |
(test* "zstream-total-in" 3 |
| 174 |
(let1 p (open-deflating-port (open-output-string)) |
| 175 |
(display "foo" p) |
| 176 |
(close-output-port p) |
| 177 |
(zstream-total-in p))) |
| 178 |
|
| 179 |
(test* "zstream-total-out" 8 |
| 180 |
(let1 p (open-deflating-port (open-output-string)) |
| 181 |
(close-output-port p) |
| 182 |
(zstream-total-out p))) |
| 183 |
|
| 184 |
(test* "zstream-params-set!" #*"x\x01\x01\x06\0\xf9\xfffoobar\x08\xab\x02z" |
| 185 |
(call-with-output-string |
| 186 |
(lambda (out) |
| 187 |
(let1 out2 (open-deflating-port out) |
| 188 |
(zstream-params-set! out2 |
| 189 |
:compression-level 0 :strategy Z_DEFAULT_STRATEGY) |
| 190 |
(display "foobar" out2) |
| 191 |
(close-output-port out2))))) |
| 192 |
|
| 193 |
(test* "deflating-port-full-flush" #*"x\x9cJLJ\x06\0\0\0\xff\xffKIM\x03\0\x08\x1e\x02V" |
| 194 |
(call-with-output-string |
| 195 |
(lambda (p1) |
| 196 |
(let1 p2 (open-deflating-port p1) |
| 197 |
(display "abc" p2) |
| 198 |
(deflating-port-full-flush p2) |
| 199 |
(display "def" p2) |
| 200 |
(close-output-port p2))))) |
| 201 |
|
| 202 |
(test* "zstream-adler32" 1 |
| 203 |
(zstream-adler32 (open-deflating-port (open-output-string)))) |
| 204 |
|
| 205 |
(test* "zstream-data-type" #t |
| 206 |
(let1 p (open-deflating-port (open-output-string)) |
| 207 |
(close-output-port p) |
| 208 |
(not (not (memq (zstream-data-type p) `(,Z_BINARY ,Z_UNKNOWN)))))) |
| 209 |
|
| 210 |
(test* "zstream-data-type" Z_TEXT |
| 211 |
(let1 p (open-deflating-port (open-output-string)) |
| 212 |
(display "foo" p) |
| 213 |
(close-output-port p) |
| 214 |
(zstream-data-type p))) |
| 215 |
|
| 216 |
(test* "zstream-data-type" Z_BINARY |
| 217 |
(let1 p (open-deflating-port (open-output-string)) |
| 218 |
(display #*"\0" p) |
| 219 |
(close-output-port p) |
| 220 |
(zstream-data-type p))) |
| 221 |
|
| 222 |
;;------------------------------------------------------------------ |
| 223 |
(test-section "inflate port") |
| 224 |
|
| 225 |
(test* "<inflating-port>" <inflating-port> |
| 226 |
(class-of (open-inflating-port (open-input-string "")))) |
| 227 |
(test* "<inflating-port>'s CPL" '(<inflating-port> <port> <top>) |
| 228 |
(map class-name |
| 229 |
(class-precedence-list |
| 230 |
(class-of (open-inflating-port (open-input-string "")))))) |
| 231 |
|
| 232 |
(test* "open-inflating-port" #t |
| 233 |
(port? (open-inflating-port (open-input-string "")))) |
| 234 |
|
| 235 |
(test* "open-inflating-port" (test-error) |
| 236 |
(open-inflating-port (open-output-string))) |
| 237 |
|
| 238 |
(test* "open-inflating-port" #t |
| 239 |
(port? (open-inflating-port (open-input-string "") :buffer-size 512))) |
| 240 |
|
| 241 |
(test* "open-inflating-port :window-bits 8" #t |
| 242 |
(port? (open-inflating-port (open-input-string "") :window-bits 8))) |
| 243 |
(test* "open-inflating-port :window-bits -1" 'OK |
| 244 |
(guard (e ((<zlib-stream-error> e) 'OK)) |
| 245 |
(open-inflating-port (open-input-string "") :window-bits -1))) |
| 246 |
|
| 247 |
(test* "open-inflating-port :dictionary \"abc\"" "abc" |
| 248 |
(let1 s (call-with-output-string |
| 249 |
(lambda (p) |
| 250 |
(let1 p2 (open-deflating-port p :dictionary "abc") |
| 251 |
(display "abc" p2) |
| 252 |
(close-output-port p2)))) |
| 253 |
(port->string |
| 254 |
(open-inflating-port (open-input-string s) :dictionary "abc")))) |
| 255 |
|
| 256 |
(test* "inflate-string" "" |
| 257 |
(inflate-string (deflate-string ""))) |
| 258 |
|
| 259 |
(test* "inflate-string" "foobar" |
| 260 |
(inflate-string (deflate-string "foobar"))) |
| 261 |
|
| 262 |
(test* "EOF" (read-from-string "") |
| 263 |
(let1 p (open-inflating-port |
| 264 |
(open-input-string (deflate-string "foobar"))) |
| 265 |
(port->string p) |
| 266 |
(read-char p))) |
| 267 |
|
| 268 |
(test* "zstream-total-in" 14 |
| 269 |
(let1 p (open-inflating-port (open-input-string #*"x\x9cK\xcb\xcfOJ,\x02\0\x08\xab\x02z")) |
| 270 |
(port->string p) |
| 271 |
(zstream-total-in p))) |
| 272 |
|
| 273 |
(test* "zstream-total-out" 6 |
| 274 |
(let1 p (open-inflating-port (open-input-string #*"x\x9cK\xcb\xcfOJ,\x02\0\x08\xab\x02z")) |
| 275 |
(port->string p) |
| 276 |
(zstream-total-out p))) |
| 277 |
|
| 278 |
(test* "read gzip'ed stream" "foobar" |
| 279 |
(let ((str #*"\x1f\x8b\x08\0:c\xc3C\x02\x03K\xcb\xcfOJ,\x02\0\x95\x1f\xf6\x9e\x06\0\0\0")) |
| 280 |
(port->string (open-inflating-port (open-input-string str) |
| 281 |
:window-bits (+ 15 16))))) |
| 282 |
|
| 283 |
(test* "gzip-encode-string / gzip-decode-string" "foobar" |
| 284 |
(gzip-decode-string |
| 285 |
(gzip-encode-string "foobar" :compression-level 9))) |
| 286 |
|
| 287 |
(test* "owner? keyword" #t |
| 288 |
(let1 p (open-input-string (deflate-string "foo")) |
| 289 |
(close-input-port (open-inflating-port p :owner? #t)) |
| 290 |
(port-closed? p))) |
| 291 |
|
| 292 |
(test* "owner? keyword" #f |
| 293 |
(let1 p (open-input-string (deflate-string "foo")) |
| 294 |
(close-input-port (open-inflating-port p)) |
| 295 |
(port-closed? p))) |
| 296 |
|
| 297 |
(test* "read/write" #t |
| 298 |
(let* ((s1 (make-string 16537 #\X)) |
| 299 |
(s2 (inflate-string (deflate-string s1)))) |
| 300 |
(equal? s1 s2))) |
| 301 |
|
| 302 |
(test* "port-name" "[inflating (input string port)]" |
| 303 |
(port-name (open-inflating-port (open-input-string "")))) |
| 304 |
|
| 305 |
(test* "port-file-number" #f |
| 306 |
(port-file-number (open-inflating-port (open-input-string "")))) |
| 307 |
|
| 308 |
(call-with-output-file "test.o" |
| 309 |
(lambda (o) |
| 310 |
(close-output-port (open-deflating-port o)))) |
| 311 |
|
| 312 |
(test* "port-file-number" 'OK |
| 313 |
(call-with-input-file "test.o" |
| 314 |
(lambda (in) |
| 315 |
(let1 p (open-inflating-port in) |
| 316 |
(when (number? (port-file-number p)) |
| 317 |
'OK))))) |
| 318 |
|
| 319 |
(sys-unlink "test.o") |
| 320 |
|
| 321 |
(test* "broken data" 'OK |
| 322 |
(guard (e ((and (<zlib-data-error> e) |
| 323 |
(<io-read-error> e)) |
| 324 |
'OK)) |
| 325 |
(port->string (open-inflating-port (open-input-string "abc"))) |
| 326 |
'error)) |
| 327 |
|
| 328 |
(test* "need-dict-error" 'OK |
| 329 |
(guard (e ((and (<zlib-need-dict-error> e) |
| 330 |
(<io-read-error> e)) |
| 331 |
'OK)) |
| 332 |
(port->string |
| 333 |
(open-inflating-port |
| 334 |
(open-input-string |
| 335 |
(deflate-string "abcdefg" :dictionary "abc")))))) |
| 336 |
|
| 337 |
(test* "inflate-sync" '("abc" 9 "abc") |
| 338 |
(let ((in (open-inflating-port |
| 339 |
(open-input-string |
| 340 |
#*"x\x9cJLJ\x06\0\0\0\xff\0JLJ\x06\0\0\0\xff\xffJLJ\x06\0\0\0\xff\xff"))) |
| 341 |
(out (open-output-string))) |
| 342 |
(guard (e ((and (<io-read-error> e) |
| 343 |
(<zlib-data-error> e)) |
| 344 |
(let1 v (inflate-sync in) |
| 345 |
(list (get-output-string out) |
| 346 |
v |
| 347 |
(port->string in))))) |
| 348 |
(let loop () |
| 349 |
(write-char (read-char in) out) |
| 350 |
(loop)) |
| 351 |
'error))) |
| 352 |
|
| 353 |
(test* "inflate-sync" '(#f #t) |
| 354 |
(let* ((in (open-inflating-port |
| 355 |
(open-input-string |
| 356 |
(deflate-string "abc")))) |
| 357 |
(v (inflate-sync in))) |
| 358 |
(list v (eof-object? (read-char in))))) |
| 359 |
|
| 360 |
(test-end) |