From 4287ff4cd386895fd8108ce2819a2149a6227a8a Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Mon, 10 Jun 2024 22:16:54 -0500 Subject: [PATCH] Add first unit test; also fix sha256sum --- activitypub-servist.asd | 23 ++++++++++++++++++++++- src/activitypub-servist.lisp | 1 + src/signatures.lisp | 12 ++++-------- t/signatures.lisp | 27 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 t/signatures.lisp diff --git a/activitypub-servist.asd b/activitypub-servist.asd index d5e3668..9848eb3 100644 --- a/activitypub-servist.asd +++ b/activitypub-servist.asd @@ -21,5 +21,26 @@ :author "Jaidyn Ann " :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"))) + + + +;;; Tests +;;; ————————————————————————————————————— +(asdf:defsystem "activitypub-servist/tests/signatures" + :version "0.0" + :license "AGPLv3" + :author "Jaidyn Ann " + :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-unit2’s 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))"))) diff --git a/src/activitypub-servist.lisp b/src/activitypub-servist.lisp index 733c851..160ef9f 100644 --- a/src/activitypub-servist.lisp +++ b/src/activitypub-servist.lisp @@ -17,6 +17,7 @@ (defpackage #:activitypub-servist (:use #:cl #:activitypub-servist/signatures) + (:nicknames "AP-S") (:export :server :start-server)) (in-package #:activitypub-servist) diff --git a/src/signatures.lisp b/src/signatures.lisp index 26687ca..82cdad8 100644 --- a/src/signatures.lisp +++ b/src/signatures.lisp @@ -17,6 +17,7 @@ (defpackage #:activitypub-servist/signatures (:use #:cl) + (:nicknames "AP-S/S") (:export :openssl-shell-generate-key-pair :openssl-shell-sign-string :openssl-shell-import-key-pair :digest-string :string-sha256sum)) @@ -154,16 +155,11 @@ private key." ;;; ———————————————————————————————————————— (defun digest-string (digest-spec string) "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) "Compute the sha256 checksum of a STRING, in hexadecimal string-format." (base64:usb8-array-to-base64-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)))) diff --git a/t/signatures.lisp b/t/signatures.lisp new file mode 100644 index 0000000..d3b58fc --- /dev/null +++ b/t/signatures.lisp @@ -0,0 +1,27 @@ +;;;; activitypub-servist/tests/signatures: Testing activitypub-servist/signatures. + +;; Copyright © 2023-2024 Jaidyn Levesque +;; +;; 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 . + +(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?~")))