Add first unit test; also fix sha256sum

This commit is contained in:
Jaidyn Ann 2024-06-10 22:16:54 -05:00
parent ee06e1e80d
commit 4287ff4cd3
4 changed files with 54 additions and 9 deletions

View File

@ -21,5 +21,26 @@
:author "Jaidyn Ann <jadedctrl@posteo.at>" :author "Jaidyn Ann <jadedctrl@posteo.at>"
:homepage "https://hak.xwx.moe/jadedctrl/activitypub-servist" :homepage "https://hak.xwx.moe/jadedctrl/activitypub-servist"
:depends-on ("cl-base64" "inferior-shell" "ironclad" "str") :depends-on ("cl-base64" "flexi-streams" "inferior-shell" "ironclad" "str")
:components ((:file "src/signatures"))) :components ((:file "src/signatures")))
;;; Tests
;;; —————————————————————————————————————
(asdf:defsystem "activitypub-servist/tests/signatures"
:version "0.0"
:license "AGPLv3"
:author "Jaidyn Ann <jadedctrl@posteo.at>"
:description "Tests for the the activitypub-servist/signatures package."
:depends-on (:activitypub-servist/signatures :lisp-unit2)
:components ((:file "t/signatures")))
;; Following method tweaked from lisp-unit2s documentation:
;; https://github.com/AccelerationNet/lisp-unit2/blob/master/README.md#asdf
(defmethod asdf:perform
((o asdf:test-op) (c (eql (asdf:find-system :activitypub-servist/tests/signatures))))
(eval (read-from-string
"(lisp-unit2:with-summary ()
(lisp-unit2:run-tests :package :activitypub-servist/tests/signatures))")))

View File

@ -17,6 +17,7 @@
(defpackage #:activitypub-servist (defpackage #:activitypub-servist
(:use #:cl #:activitypub-servist/signatures) (:use #:cl #:activitypub-servist/signatures)
(:nicknames "AP-S")
(:export :server :start-server)) (:export :server :start-server))
(in-package #:activitypub-servist) (in-package #:activitypub-servist)

View File

@ -17,6 +17,7 @@
(defpackage #:activitypub-servist/signatures (defpackage #:activitypub-servist/signatures
(:use #:cl) (:use #:cl)
(:nicknames "AP-S/S")
(:export :openssl-shell-generate-key-pair (:export :openssl-shell-generate-key-pair
:openssl-shell-sign-string :openssl-shell-import-key-pair :openssl-shell-sign-string :openssl-shell-import-key-pair
:digest-string :string-sha256sum)) :digest-string :string-sha256sum))
@ -154,16 +155,11 @@ private key."
;;; ———————————————————————————————————————— ;;; ————————————————————————————————————————
(defun digest-string (digest-spec string) (defun digest-string (digest-spec string)
"Compute the digest of a STRING, given an Ironclad DIGEST-SPEC." "Compute the digest of a STRING, given an Ironclad DIGEST-SPEC."
(ironclad:digest-sequence digest-spec (string-to-ub8-vector string))) (ironclad:digest-sequence
digest-spec
(flexi-streams:string-to-octets string :external-format 'utf-8)))
(defun string-sha256sum (string) (defun string-sha256sum (string)
"Compute the sha256 checksum of a STRING, in hexadecimal string-format." "Compute the sha256 checksum of a STRING, in hexadecimal string-format."
(base64:usb8-array-to-base64-string (base64:usb8-array-to-base64-string
(digest-string (ironclad:make-digest :sha256) string))) (digest-string (ironclad:make-digest :sha256) string)))
(defun string-to-ub8-vector (string)
"Convert the given STRING into an unsigned 8-bit vector."
(coerce
(loop for char across string
collect (char-code char))
'(vector (unsigned-byte 8))))

27
t/signatures.lisp Normal file
View File

@ -0,0 +1,27 @@
;;;; activitypub-servist/tests/signatures: Testing activitypub-servist/signatures.
;; Copyright © 2023-2024 Jaidyn Levesque <jadedctrl@posteo.at>
;;
;; This program 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.
;;
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
(defpackage :activitypub-servist/tests/signatures
(:use :cl :lisp-unit2))
(in-package :activitypub-servist/tests/signatures)
(define-test string-sha256sum (:tags '(misc))
(assert-equal
"erws/VxJ7XO5xQBqpwHIUwG0P4q1Ek2D4N053+E2Ib8="
(ap-s/s::string-sha256sum "This is a testing string~! ♥ ĉu ne?~")))