guile-fediverse/tests/asn1.scm

asn1.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (tests asn1)
19
  #:use-module (srfi srfi-64)
20
  #:use-module (ice-9 format)
21
  #:use-module (rnrs bytevectors)
22
  #:use-module (http-signature asn1))
23
24
(define (bv-equal? bv1 bv2)
25
  (if (equal? (bytevector-length bv1) (bytevector-length bv2))
26
    (let loop ((pos 0))
27
      (if (= pos (bytevector-length bv1))
28
          #t
29
          (if (equal? (bytevector-u8-ref bv1 pos) (bytevector-u8-ref bv2 pos))
30
            (loop (+ pos 1))
31
            #f)))
32
    #f))
33
34
(test-begin "asn1")
35
36
(test-assert "asn1 bool decoding #f"
37
  (equal?
38
    (decode-asn1 #vu8(#x01 #x01 #x00) asn1:bool)
39
    #f))
40
41
(test-assert "asn1 bool decoding #t"
42
  (equal?
43
    (decode-asn1 #vu8(#x01 #x01 #x51) asn1:bool)
44
    #t))
45
46
(test-assert "asn1 integer decoding"
47
  (and
48
    (bv-equal? (decode-asn1 #vu8(#x02 #x00) asn1:int-bv) #vu8())
49
    (bv-equal? (decode-asn1 #vu8(#x02 #x02 #x01 #x50) asn1:int-bv) #vu8(#x01 #x50))))
50
51
(test-assert "asn1 bit string decoding"
52
  (and
53
    (bv-equal? (decode-asn1 #vu8(#x03 #x00) asn1:bitstring) #vu8())
54
    (bv-equal? (decode-asn1 #vu8(#x03 #x02 #x01 #x50) asn1:bitstring) #vu8(#x01 #x50))))
55
56
(test-assert "asn1 bit string constructed decoding"
57
  (and
58
    (bv-equal? (decode-asn1 #vu8(#x23 #x80 #x03 #x03 #x00 #x0a #x3b
59
                                           #x03 #x05 #x04 #x5f #x29 #x1c #xd0
60
                                           #x00 #x00)
61
                            asn1:bitstring)
62
               #vu8(#x00 #x0a #x3b #x04 #x5f #x29 #x1c #xd0))
63
    (bv-equal? (decode-asn1 #vu8(#x23 #x80 #x03 #x00
64
                                           #x23 #x80 #x03 #x01 #x5f
65
                                                     #x03 #x01 #x1e
66
                                                     #x00 #x00
67
                                           #x03 #x02 #x5e #x22
68
                                           #x00 #x00)
69
                            asn1:bitstring)
70
               #vu8(#x5f #x1e #x5e #x22))))
71
72
(test-assert "asn1 octet string decoding"
73
  (and
74
    (bv-equal? (decode-asn1 #vu8(#x04 #x00) asn1:octetstring) #vu8())
75
    (bv-equal? (decode-asn1 #vu8(#x04 #x02 #x01 #x50) asn1:octetstring) #vu8(#x01 #x50))))
76
77
(test-assert "asn1 octet string constructed decoding"
78
  (and
79
    (bv-equal? (decode-asn1 #vu8(#x24 #x80 #x04 #x03 #x00 #x0a #x3b
80
                                           #x04 #x05 #x04 #x5f #x29 #x1c #xd0
81
                                           #x00 #x00)
82
                            asn1:octetstring)
83
               #vu8(#x00 #x0a #x3b #x04 #x5f #x29 #x1c #xd0))
84
    (bv-equal? (decode-asn1 #vu8(#x24 #x80 #x04 #x00
85
                                           #x24 #x80 #x04 #x01 #x5f
86
                                                     #x04 #x01 #x1e
87
                                                     #x00 #x00
88
                                           #x04 #x02 #x5e #x22
89
                                           #x00 #x00)
90
                            asn1:octetstring)
91
               #vu8(#x5f #x1e #x5e #x22))))
92
93
(test-assert "asn1 null decoding"
94
  (equal?
95
    (decode-asn1 #vu8(#x05 #x00) asn1:null)
96
    #nil))
97
98
(test-assert "asn1 sequence decoding"
99
  (map equal?
100
       (decode-asn1 #vu8(#x30 #x0f #x01 #x01 #x50
101
                                   #x01 #x01 #x01
102
                                   #x01 #x01 #x00
103
                                   #x01 #x01 #xff
104
                                   #x01 #x01 #x00)
105
         (asn1:sequence asn1:bool asn1:bool asn1:bool asn1:bool asn1:bool))
106
       (list #t #t #f #t #f)))
107
108
(test-assert "asn1 object identifier decoding"
109
  (map equal?
110
       (decode-asn1 #vu8(#x06 #x03 #x88 #x37 #x03) asn1:object-identifier)
111
       '(2 999 3)))
112
113
(test-assert "asn1 bool encoding #t"
114
  (bv-equal? (encode-asn1 asn1:bool #t) #vu8(#x01 #x01 #xff)))
115
116
(test-assert "asn1 bool encoding #f"
117
  (bv-equal? (encode-asn1 asn1:bool #f) #vu8(#x01 #x01 #x00)))
118
119
(test-assert "asn1 int encoding"
120
  (and
121
    (bv-equal? (encode-asn1 asn1:int-bv #vu8()) #vu8(#x02 #x00))
122
    (bv-equal? (encode-asn1 asn1:int-bv #vu8(#x5e #x11)) #vu8(#x02 #x02 #x5e #x11))))
123
124
(test-assert "asn1 bit string encoding"
125
  (and
126
    (bv-equal? (encode-asn1 asn1:bitstring #vu8()) #vu8(#x03 #x00))
127
    (bv-equal? (encode-asn1 asn1:bitstring #vu8(#x5e #x11)) #vu8(#x03 #x02 #x5e #x11))))
128
129
(test-assert "asn1 octet string encoding"
130
  (and
131
    (bv-equal? (encode-asn1 asn1:octetstring #vu8()) #vu8(#x04 #x00))
132
    (bv-equal? (encode-asn1 asn1:octetstring #vu8(#x5e #x11)) #vu8(#x04 #x02 #x5e #x11))))
133
134
(test-assert "asn1 null encoding"
135
  (bv-equal? (encode-asn1 asn1:null #nil) #vu8(#x05 #x00)))
136
137
(test-assert "asn1 object identifier encoding"
138
  (bv-equal? (encode-asn1 asn1:object-identifier '(2 999 3))
139
             #vu8(#x06 #x03 #x88 #x37 #x03)))
140
141
(test-assert "asn1 sequence encoding"
142
  (bv-equal? (encode-asn1 (asn1:sequence asn1:bool asn1:int-bv asn1:bool)
143
                          '(#t #vu8(#x5e #x5e #x5e) #f))
144
             #vu8(#x30 #x0b #x01 #x01 #xff
145
                            #x02 #x3 #x5e #x5e #x5e
146
                            #x01 #x01 #x00)))
147
148
(test-end "asn1")
149