;; 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 version. ;; 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 Lesser General Public LIcense for more details. ;; You should have received a copy of the GNU Lesser General Public LIcense along with ;; eris-cl. If not, see . (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 (block ref) (declare (ignore ref)) block) :secret ,secret))) (stream-encode (read-capability-to-urn (with-octet-input-stream (stream ,data) (eris-encode stream ,block-size (lambda (block ref) (declare (ignore ref)) block) :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")) (test empty-stream (check-urn (serapeum:make-octet-vector 0) 1024 "urn:eris:BIADFUKDPYKJNLGCVSIIDI3FVKND7MO5AGOCXBK2C4ITT5MAL4LSCZF62B4PDOFQCLLNL7AXXSJFGINUYXVGVTDCQ2V7S7W5S234WFXCJ4") (check-urn (serapeum:make-octet-vector 0) eris:32kib "urn:eris:B4AC3MKL2BYR3E2WPMY2QRA6QZBLY4VNWJEBTSK5KWD66BRIT2EXVQVWY6TWVKJCZLC66RE3T2PKWDU3TBAKZZZIZRBTMP6BSOPE4CRXII")) ;; simple gray stream class for this particular construction. (defclass null-stream (fundamental-binary-input-stream) ((counter :initform 0 :accessor counter) (max-counter :initarg :max-counter))) (defmethod stream-read-sequence ((stream null-stream) seq start end &key) (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 (block ref) (declare (ignore ref)) block)))) (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)) (defmacro encode-consensus-test (tmpdir data block-size &key (secret (random-data 32))) "Test if all the eris-encode methods give the same results." `(let ((pathname-encode (let ((pathname (merge-pathnames (crypto:byte-array-to-hex-string (crypto:random-data 16)) ,tmpdir))) (with-open-file (f pathname :direction :output :element-type 'serapeum:octet :if-does-not-exist :create) (write-sequence ,data f)) (read-capability-to-urn (eris-encode pathname ,block-size (lambda (block ref) (declare (ignore ref)) block) :secret ,secret)))) (file-stream-encode (let ((pathname (merge-pathnames (crypto:byte-array-to-hex-string (crypto:random-data 16)) ,tmpdir))) (with-open-file (f pathname :direction :output :element-type 'serapeum:octet :if-does-not-exist :create) (write-sequence ,data f)) (read-capability-to-urn (with-open-file (f pathname :direction :input :element-type 'serapeum:octet) (eris-encode f ,block-size (lambda (block ref) (declare (ignore ref)) block) :secret ,secret))))) (vector-encode (read-capability-to-urn (eris-encode ,data ,block-size (lambda (block ref) (declare (ignore ref)) block) :secret ,secret))) (stream-encode (read-capability-to-urn (with-octet-input-stream (stream ,data) (eris-encode stream ,block-size (lambda (block ref) (declare (ignore ref)) block) :secret ,secret))))) (is (serapeum:equalp* vector-encode stream-encode pathname-encode file-stream-encode)))) (test encoding-consensus-tests (with-temporary-dir tdir (encode-consensus-test tdir (make-octets 1 :element 2) 1024) (encode-consensus-test tdir (make-octets 512 :element 2) 1024) (encode-consensus-test tdir (make-octets 1023 :element 2) 1024) (encode-consensus-test tdir (make-octets 1024 :element 2) 1024) (encode-consensus-test tdir (make-octets 16383 :element 2) 1024) (encode-consensus-test tdir (make-octets 16384 :element 2) 1024) (encode-consensus-test tdir (make-octets 1024 :element 2) 32kib) (encode-consensus-test tdir (make-octets 32767 :element 2) 32kib) (encode-consensus-test tdir (make-octets 32768 :element 2) 32kib) (encode-consensus-test tdir (make-octets 64000 :element 2) 32kib) (for-all ((buffer (gen-buffer :length (gen-integer :min 1 :max 70000)))) (encode-consensus-test tdir buffer 1024) (encode-consensus-test tdir buffer 32kib)))) (test encoding-nothing (with-temporary-dir tdir (encode-consensus-test tdir (make-octets 0) 1024) (encode-consensus-test tdir (make-octets 0) 32kib))) (defmacro encoding-file-pos (tmpdir data pos block-size &key (secret null-secret)) `(let ((vector-encode (read-capability-to-urn (eris-encode (subseq ,data ,pos) ,block-size (lambda (block ref) (declare (ignore ref)) block) :secret ,secret))) (file-stream-encode (let ((pathname (merge-pathnames (crypto:byte-array-to-hex-string (crypto:random-data 16)) ,tmpdir))) (with-open-file (f pathname :direction :output :element-type 'serapeum:octet :if-does-not-exist :create) (write-sequence ,data f)) (read-capability-to-urn (with-open-file (f pathname :direction :input :element-type 'serapeum:octet) (file-position f ,pos) (eris-encode f ,block-size (lambda (block ref) (declare (ignore ref)) block) :secret ,secret)))))) (is (equalp vector-encode file-stream-encode)))) (test encoding-file-position-tests (with-temporary-dir tdir (encoding-file-pos tdir (make-octets 1024 :element 2) 512 1024) (encoding-file-pos tdir (make-octets 1024 :element 2) 1023 1024) (encoding-file-pos tdir (make-octets 1024 :element 2) 1 1024) (encoding-file-pos tdir (make-octets 32000 :element 2) 1673 32kib) (encoding-file-pos tdir (make-octets 32000 :element 2) 31999 32kib)))