zlib.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;; Guile-zlib --- Functional package management for GNU
  3. ;;; Copyright © 2016, 2019, 2021, 2024 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2023 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  5. ;;;
  6. ;;; This file is part of Guile-zlib.
  7. ;;;
  8. ;;; Guile-zlib is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; Guile-zlib is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with Guile-zlib. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (test-zlib)
  21. #:use-module (zlib)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-11)
  24. #:use-module (srfi srfi-64)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 binary-ports)
  27. #:use-module (rnrs bytevectors)
  28. #:use-module (rnrs io ports)
  29. #:use-module (ice-9 match))
  30. (test-begin "zlib")
  31. (define (random-seed)
  32. (logxor (getpid) (car (gettimeofday))))
  33. (define %seed
  34. (let ((seed (random-seed)))
  35. (format (current-error-port) "random seed for tests: ~a~%"
  36. seed)
  37. (seed->random-state seed)))
  38. (define (random-bytevector n)
  39. "Return a random bytevector of N bytes."
  40. (let ((bv (make-bytevector n)))
  41. (let loop ((i 0))
  42. (if (< i n)
  43. (begin
  44. (bytevector-u8-set! bv i (random 256 %seed))
  45. (loop (1+ i)))
  46. bv))))
  47. (let ((str "αβγ"))
  48. ;; Guile 2.x lacks 'call-with-output-bytevector'.
  49. (unless (defined? 'call-with-output-bytevector) (test-skip 1))
  50. (test-equal "port encoding is inherited"
  51. str
  52. (let ((bv (call-with-output-bytevector
  53. (lambda (o)
  54. ; has to be set because call-with-output-bytevector ignores %default-port-encoding.
  55. (set-port-encoding! o "UTF-8")
  56. (call-with-zlib-output-port
  57. o
  58. (lambda (o)
  59. ; force encoding here to reveal a mismatch on read.
  60. (set-port-encoding! o "UTF-8")
  61. (write str o)))))))
  62. (with-fluids ((%default-port-encoding "UTF-8"))
  63. (call-with-input-bytevector
  64. bv
  65. (lambda (o)
  66. ; has to be set because call-with-input-bytevector ignores %default-port-encoding; see above.
  67. (set-port-encoding! o "UTF-8")
  68. (call-with-zlib-input-port o read)))))))
  69. (test-assert "compression/decompression pipe"
  70. (let ((data (random-bytevector (+ (random 10000)
  71. (* 20 1024)))))
  72. (match (pipe)
  73. ((parent . child)
  74. (match (primitive-fork)
  75. (0 ;compress
  76. (dynamic-wind
  77. (const #t)
  78. (lambda ()
  79. (close-port parent)
  80. (call-with-gzip-output-port child
  81. (lambda (port)
  82. (put-bytevector port data))))
  83. (lambda ()
  84. (primitive-exit 0))))
  85. (pid ;decompress
  86. (begin
  87. (close-port child)
  88. (let ((received (call-with-gzip-input-port parent
  89. (lambda (port)
  90. (get-bytevector-all port))
  91. #:buffer-size (* 64 1024))))
  92. (match (waitpid pid)
  93. ((_ . status)
  94. (and (zero? status)
  95. (port-closed? parent)
  96. (bytevector=? received data))))))))))))
  97. (unless (file-exists? "/dev/full") (test-skip 1))
  98. (test-assert "gzip output port, error"
  99. (catch 'zlib-error
  100. (lambda ()
  101. (call-with-output-file "/dev/full"
  102. (lambda (p)
  103. (let ((p (make-gzip-output-port p)))
  104. (put-bytevector p (random-bytevector 262140))
  105. (force-output p)
  106. (close-port p))))
  107. #f)
  108. (lambda (key ret code message . _)
  109. (and (= code Z_ERRNO)
  110. (string-contains message "No space left")
  111. (string-contains
  112. (call-with-output-string
  113. (lambda (port)
  114. (print-exception port #f key (list key ret code message))))
  115. "No space left")))))
  116. (test-assert "raw compress/decompress"
  117. (let* ((data (random-bytevector (+ (random 10000) (* 20 1024))))
  118. (cdata (compress data))
  119. (ucdata (uncompress cdata)))
  120. (equal? data ucdata)))
  121. (define (test-zlib n fmt level)
  122. (test-assert (format #f "zlib ports [size: ~a, format: ~a, level: ~a]"
  123. n fmt level)
  124. (let* ((size (pk 'size (+ (random n %seed) n)))
  125. (data (random-bytevector size)))
  126. (let*-values (((port get)
  127. (open-bytevector-output-port))
  128. ((compressed)
  129. (make-zlib-output-port port
  130. #:level level
  131. #:format fmt)))
  132. (put-bytevector compressed data)
  133. (close-port compressed)
  134. (let ((data2 (get-bytevector-all
  135. (make-zlib-input-port
  136. (open-bytevector-input-port (get))
  137. #:format fmt))))
  138. (pk 'sizes size 'vs (bytevector-length data2))
  139. (bytevector=? data2 data))))))
  140. (for-each (lambda (n)
  141. (for-each (lambda (format)
  142. (for-each (lambda (level)
  143. (test-zlib n format level))
  144. (iota 9 1)))
  145. '(deflate zlib gzip)))
  146. (list (expt 2 8) (expt 2 10) (expt 2 12)
  147. (expt 2 14) (expt 2 18)))
  148. ;; This test checks if "uncompress" is able to allocate enough memory for the
  149. ;; output when the input data has high compression ratio properly (e.g. when the
  150. ;; input data is but a lots of compressed zeroes.)
  151. ;;
  152. ;; See <https://notabug.org/guile-zlib/guile-zlib/issues/4>
  153. (let* ((data (make-bytevector 100000 0))
  154. (data-length (bytevector-length data)))
  155. (test-equal "uncompress: High compression ratio"
  156. data-length
  157. (let ((compressed-data (compress data)))
  158. (bytevector-length (uncompress compressed-data)))))
  159. (let ((bv (compress #vu8(0))))
  160. (unless (= 9 (bytevector-length bv)) (test-skip 1))
  161. (test-equal "uncompress: input size times 1.5 is a fraction"
  162. #vu8(0)
  163. ;; In 0.2.0, this would throw:
  164. ;; (wrong-type-arg #f "Wrong type (expecting ~A): ~S" ("exact integer" 27/2) (27/2))
  165. ;; where 27/2 is 9x1.5 and 9 the size of BV.
  166. (uncompress bv)))
  167. (test-end)