openpgp: Add 'string->openpgp-packet'.
authorLudovic Courtès <ludo@gnu.org>
Thu, 30 Apr 2020 13:56:54 +0000 (15:56 +0200)
committerLudovic Courtès <ludo@gnu.org>
Mon, 4 May 2020 07:56:13 +0000 (09:56 +0200)
* guix/openpgp.scm (string->openpgp-packet): New procedure.
* tests/openpgp.scm ("verify-openpgp-signature, missing key")
("verify-openpgp-signature, good signatures")
("verify-openpgp-signature, bad signature"): Use it.

guix/openpgp.scm
tests/openpgp.scm

index 987660f..2b2997d 100644 (file)
@@ -55,7 +55,8 @@
             lookup-key-by-fingerprint
             get-openpgp-keyring
 
-            read-radix-64)
+            read-radix-64
+            string->openpgp-packet)
   #:use-module (rnrs bytevectors)
   #:use-module (rnrs io ports)
   #:use-module (srfi srfi-1)
@@ -1067,3 +1068,9 @@ end-of-file object if the Radix-64 sequence was truncated."
                         (values #f kind)))
                      (loop (cons str lines))))))))
         (values #f #f))))
+
+(define (string->openpgp-packet str)
+  "Read STR, an ASCII-armored OpenPGP packet, and return the corresponding
+OpenPGP record."
+  (get-packet
+   (open-bytevector-input-port (call-with-input-string str read-radix-64))))
index a85fe6a..0beab6f 100644 (file)
@@ -194,10 +194,7 @@ Pz7oopeN72xgggYUNT37ezqN3MeCqw0=
 (test-equal "verify-openpgp-signature, missing key"
   `(missing-key ,%rsa-key-fingerprint)
   (let* ((keyring   (get-openpgp-keyring (%make-void-port "r")))
-         (signature (get-openpgp-packet
-                     (open-bytevector-input-port
-                      (call-with-input-string %hello-signature/rsa
-                        read-radix-64)))))
+         (signature (string->openpgp-packet %hello-signature/rsa)))
     (let-values (((status key)
                   (verify-openpgp-signature signature keyring
                                             (open-input-string "Hello!\n"))))
@@ -214,10 +211,7 @@ Pz7oopeN72xgggYUNT37ezqN3MeCqw0=
                 (keyring   (get-openpgp-keyring
                             (open-bytevector-input-port
                              (call-with-input-file key read-radix-64))))
-                (signature (get-openpgp-packet
-                            (open-bytevector-input-port
-                             (call-with-input-string signature
-                               read-radix-64)))))
+                (signature (string->openpgp-packet signature)))
            (let-values (((status key)
                          (verify-openpgp-signature signature keyring
                                                    (open-input-string "Hello!\n"))))
@@ -246,10 +240,7 @@ Pz7oopeN72xgggYUNT37ezqN3MeCqw0=
                          "tests/ed25519.key" "tests/ed25519.key"
                          "tests/ed25519.key"))))
     (map (lambda (signature)
-           (let ((signature (get-openpgp-packet
-                             (open-bytevector-input-port
-                              (call-with-input-string signature
-                                read-radix-64)))))
+           (let ((signature (string->openpgp-packet signature)))
              (let-values (((status key)
                            (verify-openpgp-signature signature keyring
                                                      (open-input-string "What?!"))))