From 02431261e07c3e069f14311e34faf2a5a7eefdd0 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Thu, 21 May 2020 04:00:56 +0530 Subject: tests: Add tests for (email base64). * tests/encoding.scm: Move common code for encoding tests to this new file. * tests/quoted-printable.scm: Load encoding.scm. * tests/base64.scm: New file. * Makefile.am (SCM_TESTS): Register tests/base64.scm. --- Makefile.am | 1 + tests/base64.scm | 76 ++++++++++++++++++++++++++++++++++++++++++++++ tests/encoding.scm | 33 ++++++++++++++++++++ tests/quoted-printable.scm | 12 +------- 4 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 tests/base64.scm create mode 100644 tests/encoding.scm diff --git a/Makefile.am b/Makefile.am index 4ea14af..f578418 100644 --- a/Makefile.am +++ b/Makefile.am @@ -50,6 +50,7 @@ SOURCES = \ TEST_EXTENSIONS = .scm SCM_TESTS = \ + tests/base64.scm \ tests/email.scm \ tests/quoted-printable.scm \ tests/utils.scm diff --git a/tests/base64.scm b/tests/base64.scm new file mode 100644 index 0000000..c200f81 --- /dev/null +++ b/tests/base64.scm @@ -0,0 +1,76 @@ +;;; guile-email --- Guile email parser +;;; Copyright © 2020 Arun Isaac +;;; +;;; This file is part of guile-email. +;;; +;;; guile-email is free software; you can redistribute it and/or modify +;;; it under the terms of the GNU Affero General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; guile-email is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; Affero General Public License for more details. +;;; +;;; You should have received a copy of the GNU Affero General Public +;;; License along with guile-email. If not, see +;;; . + +(use-modules (email base64) + (ice-9 iconv)) + +(load "encoding.scm") + +(define (string-has-only-valid-base64-characters? str) + (string-every (char-set-union + (char-set #\newline #\return #\space) + (ucs-range->char-set (char->integer #\A) (1+ (char->integer #\Z))) + (ucs-range->char-set (char->integer #\a) (1+ (char->integer #\z))) + (ucs-range->char-set (char->integer #\0) (1+ (char->integer #\9))) + (char-set #\+ #\/ #\=)) + str)) + +(test-begin "base64") + +(let ((decoded-text + "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.") + (encoded-text + "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz +IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg +dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu +dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo +ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=") + (charset "UTF-8")) + (test-equal "base64 wikipedia example: decoding" + (bytevector->string (base64-decode encoded-text) charset) + decoded-text) + + (test-equal "base64 wikipedia example: encoding" + (base64-encode + (string->bytevector decoded-text charset)) + encoded-text) + + (test-assert "base64 wikipedia example: encoded output should not be more than 76 columns wide" + (each-line-has-a-maximum-of-76-characters? + (base64-encode + (string->bytevector decoded-text charset))))) + +(let ((x (random-bytevector 1000))) + (test-equal "base64 random bytevector: base64-encode and base64-decode are inverses of each other" + (base64-decode (base64-encode x)) + x)) + +(test-assert "base64 random bytevector: encoded output should not be more than 76 columns wide" + (each-line-has-a-maximum-of-76-characters? + (base64-encode (random-bytevector 1000)))) + +(test-assert "base64 random bytevector: encoded output must only consist of characters from the base64 alphabet" + (string-has-only-valid-base64-characters? + (base64-encode (random-bytevector 1000)))) + +(test-equal "base64 decoding should ignore invalid characters" + (bytevector->string (base64-decode "..TWFu,") "utf-8") + "Man") + +(test-end "base64") diff --git a/tests/encoding.scm b/tests/encoding.scm new file mode 100644 index 0000000..d623d48 --- /dev/null +++ b/tests/encoding.scm @@ -0,0 +1,33 @@ +;;; guile-email --- Guile email parser +;;; Copyright © 2020 Arun Isaac +;;; +;;; This file is part of guile-email. +;;; +;;; guile-email is free software; you can redistribute it and/or modify +;;; it under the terms of the GNU Affero General Public License as +;;; published by the Free Software Foundation; either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; guile-email is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; Affero General Public License for more details. +;;; +;;; You should have received a copy of the GNU Affero General Public +;;; License along with guile-email. If not, see +;;; . + +(use-modules (rnrs bytevectors) + (srfi srfi-1)) + +(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))) diff --git a/tests/quoted-printable.scm b/tests/quoted-printable.scm index 267011a..0b5b462 100644 --- a/tests/quoted-printable.scm +++ b/tests/quoted-printable.scm @@ -23,17 +23,7 @@ (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))) +(load "encoding.scm") (define (string-has-only-quoted-printable-valid-characters? str) (string-every (char-set-union -- cgit v1.2.3