Debian Codegraph @

guile/tests/base64.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS
2 ;;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This program is free software: you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation, either version 3 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 ;; Check the base64 encoding and decoding
20
21 (use-modules (gnutls)
22 (gnutls build tests)
23 (rnrs bytevectors))
24
25 (define hello
26 "Hello, world!")
27
28 (run-test
29 (lambda ()
30 (unless (equal? (base64-encode hello)
31 "SGVsbG8sIHdvcmxkIQ==")
32 (error "encoding failed"))
33 (unless (equal? (base64-encode (string->utf8 hello))
34 (base64-encode hello))
35 (error "encoding a bytevector failed"))
36 (unless (equal? (base64-decode "SGVsbG8sIHdvcmxkIQ==")
37 (string->utf8 hello))
38 (error "decoding failed"))
39 (unless (equal? (base64-decode (string->utf8 "SGVsbG8sIHdvcmxkIQ=="))
40 (base64-decode "SGVsbG8sIHdvcmxkIQ=="))
41 (error "decoding a bytevector failed"))
42 (unless (equal? (with-output-to-string
43 (lambda ()
44 (load-from-path "base16-example.scm")))
45 "\
46 The base16 encoding is: \"48656c6c6f2c20776f726c6421\"\n\
47 Decoding it back gives: \"Hello, world!\"\n")
48 (error "base16 failed"))
49 (unless (equal? (with-output-to-string
50 (lambda ()
51 (load-from-path "base64-example.scm")))
52 "\
53 The base64 encoding is: \"SGVsbG8sIHdvcmxkIQ==\"\n\
54 Decoding it back gives: \"Hello, world!\"\n")
55 (error "base64 failed"))
56 (unless (equal? (with-output-to-string
57 (lambda ()
58 (load-from-path "base64url-example.scm")))
59 "\
60 The base64-url encoding is: \"fn4gSGVsbG8sIHdvcmxkISB-fg\"\n\
61 Decoding it back gives: \"~~ Hello, world! ~~\"\n")
62 (error "base64-url failed"))))