summaryrefslogtreecommitdiff
path: root/tests/encode-tests.lisp
blob: 214eaad9ba6e54c4b5f6029bd4cfe76e2fc611d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 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 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-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 (&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))