summaryrefslogtreecommitdiff
path: root/tests/encode-tests.lisp
diff options
context:
space:
mode:
authorPiotr Szarmanski2022-09-21 22:50:14 +0200
committerPiotr Szarmanski2022-09-21 22:50:14 +0200
commit56d3ca4cf14ac1b2bac9c866daa98cdb803915fa (patch)
treef97d64c7ba5903491db5ae48612507a4ae216859 /tests/encode-tests.lisp
Initial commit.
Diffstat (limited to 'tests/encode-tests.lisp')
-rw-r--r--tests/encode-tests.lisp81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/encode-tests.lisp b/tests/encode-tests.lisp
new file mode 100644
index 0000000..c6b50d4
--- /dev/null
+++ b/tests/encode-tests.lisp
@@ -0,0 +1,81 @@
+;; This file is part of eris-cl.
+;; Copyright (C) 2022 Piotr SzarmaƄski
+
+;; eris-cl is free software: you can redistribute it and/or modify it under the
+;; terms of the GNU Lesser General Public License as published by the Free
+;; Software Foundation, either version 3 of the License, or (at your option) any
+;; later versqion.
+
+;; eris-cl 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 General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License along with
+;; eris-cl. If not, see <https://www.gnu.org/licenses/>.
+
+(in-package :eris/test)
+(def-suite* encoding-tests :in eris-tests)
+
+(defmacro check-urn (data block-size urn &key (secret null-secret))
+ `(let ((urn ,urn)
+ (vector-encode (read-capability-to-urn
+ (eris-encode ,data
+ ,block-size
+ (lambda (&rest args) (declare (ignore args)))
+ :secret ,secret)))
+ (stream-encode (read-capability-to-urn
+ (with-octet-input-stream (stream ,data)
+ (eris-encode stream
+ ,block-size
+ (lambda (&rest args) (declare (ignore args)))
+ :secret ,secret)))))
+ (is (equalp vector-encode urn))
+ (is (equalp stream-encode urn))))
+
+(test hello-world
+ (check-urn (base32-to-bytes-unpadded "JBSWY3DPEB3W64TMMQQQ") 1024
+ "urn:eris:BIAD77QDJMFAKZYH2DXBUZYAP3MXZ3DJZVFYQ5DFWC6T65WSFCU5S2IT4YZGJ7AC4SYQMP2DM2ANS2ZTCP3DJJIRV733CRAAHOSWIYZM3M"))
+
+;; simple gray stream class for this particular construction.
+(defclass null-stream (fundamental-binary-stream)
+ ((counter :initform 0 :accessor counter)
+ (max-counter :initarg :max-counter)))
+
+(defmethod stream-read-sequence ((stream null-stream) seq &optional start end)
+ (with-slots (counter max-counter) stream
+ (if (eql counter max-counter)
+ 0
+ (let ((bytes (if end (- end start) (- (length seq) start))))
+ (replace seq (make-array bytes :element-type '(unsigned-byte 8) :initial-element 0) :start1 start :end1 end)
+ (setf counter (+ bytes counter))
+ bytes))))
+
+(defmacro large-content-test (key block-size urn length)
+ `(let* ((stream (make-instance 'null-stream :max-counter ,length))
+ (chacha-stream (make-encrypting-stream stream
+ :chacha
+ :stream
+ (ironclad:digest-sequence
+ :blake2/256
+ ,key)
+ :initialization-vector (make-array 8 :element-type '(unsigned-byte 8) :initial-element 0)
+ :direction :input)))
+ (let ((read-capability (eris-encode chacha-stream ,block-size (lambda (&rest args) (declare (ignore args))))))
+ (is (equalp (read-capability-to-urn read-capability)
+ ,urn)))))
+
+(test 100MiB
+ (large-content-test (make-array 24 :element-type '(unsigned-byte 8)
+ :initial-contents
+ #(49 48 48 77 105 66 32 40 98 108 111 99 107 32 115 105 122 101 32 49 75 105 66 41))
+ 1024
+ "urn:eris:BIC6F5EKY2PMXS2VNOKPD3AJGKTQBD3EXSCSLZIENXAXBM7PCTH2TCMF5OKJWAN36N4DFO6JPFZBR3MS7ECOGDYDERIJJ4N5KAQSZS67YY"
+ 104857600))
+
+(test 1GiB
+ (large-content-test (make-array 23 :element-type '(unsigned-byte 8)
+ :initial-contents
+ #(49 71 105 66 32 40 98 108 111 99 107 32 115 105 122 101 32 51 50 75 105 66 41))
+ 32kib
+ "urn:eris:B4BL4DKSEOPGMYS2CU2OFNYCH4BGQT774GXKGURLFO5FDXAQQPJGJ35AZR3PEK6CVCV74FVTAXHRSWLUUNYYA46ZPOPDOV2M5NVLBETWVI"
+ 1073741824))