From d3ebe5a4920bf28c2c579078bb69e349776d5840 Mon Sep 17 00:00:00 2001
From: Arun Isaac
Date: Sat, 15 Sep 2018 11:18:42 +0530
Subject: tests: Add tests for quoted-printable.

* tests/quoted-printable.scm (random-bytevector,
each-line-has-a-maximum-of-76-characters?,
string-has-only-quoted-printable-valid-characters?): New functions.
(quoted-printable random bytevector: quoted-printable-encode and
quoted-printable-decode are inverses of each other, quoted-printable
random bytevector: encoded output should not be more than 76 columns
wide, quoted-printable random bytevector: encoded output must only
consist of printable ASCII characters): New tests.
---
 tests/quoted-printable.scm | 42 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)

(limited to 'tests')

diff --git a/tests/quoted-printable.scm b/tests/quoted-printable.scm
index f3dd5eb..e41eced 100755
--- a/tests/quoted-printable.scm
+++ b/tests/quoted-printable.scm
@@ -19,9 +19,28 @@
 
 (use-modules (email quoted-printable)
              (ice-9 iconv)
+             (rnrs bytevectors)
              (srfi srfi-1)
              (srfi srfi-64))
 
+(set! *random-state* (random-state-from-platform))
+
+(define (random-bytevector len)
+  "Return a random bytevector of length LEN."
+  (u8-list->bytevector
+   (map (lambda _ (random 256)) (iota len))))
+
+(define (each-line-has-a-maximum-of-76-characters? str)
+  (every (lambda (line)
+           (<= (string-length line) 76))
+         (string-split str #\newline)))
+
+(define (string-has-only-quoted-printable-valid-characters? str)
+  (string-every (char-set-union
+                 (char-set #\newline #\return #\space)
+                 (ucs-range->char-set 33 127))
+                str))
+
 (test-begin "quoted-printable")
 
 (let ((decoded-text
@@ -43,11 +62,9 @@ abriquent pour te la vendre une =C3=A2me vulgaire.")
     encoded-text)
 
   (test-assert "quoted-printable wikipedia example: encoded output should not be more than 76 columns wide"
-    (every (lambda (line)
-             (<= (string-length line) 76))
-           (string-split (quoted-printable-encode
-                          (string->bytevector decoded-text charset))
-                         #\newline))))
+    (each-line-has-a-maximum-of-76-characters?
+     (quoted-printable-encode
+      (string->bytevector decoded-text charset)))))
 
 (test-equal "quoted-printable encoding of ="
   (quoted-printable-encode
@@ -55,6 +72,21 @@ abriquent pour te la vendre une =C3=A2me vulgaire.")
   (string-append "=" (string-upcase (number->string
                                      (char->integer #\=) 16))))
 
+(let ((x (random-bytevector 1000)))
+  (test-equal "quoted-printable random bytevector: quoted-printable-encode and quoted-printable-decode are inverses of each other"
+    (quoted-printable-decode
+     (quoted-printable-encode x))
+    x))
+
+(test-assert "quoted-printable random bytevector: encoded output should not be more than 76 columns wide"
+  (each-line-has-a-maximum-of-76-characters?
+   (quoted-printable-encode
+    (random-bytevector 1000))))
+
+(test-assert "quoted-printable random bytevector: encoded output must only consist of printable ASCII characters"
+  (string-has-only-quoted-printable-valid-characters?
+   (quoted-printable-encode (random-bytevector 1000))))
+
 (let ((encoded-text "=A1Hola,_se=F1or!")
       (decoded-text "¡Hola, señor!")
       (charset "ISO-8859-1"))
-- 
cgit v1.2.3