guile-fediverse/tests/webfinger.scm

webfinger.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 webfinger)
19
  #:use-module (ice-9 format)
20
  #:use-module (ice-9 match)
21
  #:use-module (json)
22
  #:use-module (rnrs bytevectors)
23
  #:use-module (srfi srfi-1)
24
  #:use-module (srfi srfi-64)
25
  #:use-module (webfinger webfinger))
26
27
(test-begin "webfinger")
28
29
(define (json-has-key? json key)
30
  (match json
31
    (((k . v) json ...)
32
     (or (equal? key k) (json-has-key? json key)))
33
    (_ #f)))
34
35
(define (alist-include? j1 j2)
36
  (fold
37
    (lambda (elem acc)
38
      (if acc
39
          (match elem
40
            ((key . value)
41
             (and
42
               (json-has-key? j2 key)
43
               (json-equal? value (assoc-ref j2 key))))
44
            (_ #f))
45
          #f))
46
    #t
47
    j1))
48
49
(define (json-equal? j1 j2)
50
  (cond
51
    ((list? j1)
52
     (and (list? j2)
53
          (alist-include? j1 j2)
54
          (alist-include? j2 j1)))
55
    ((string? j1)
56
     (equal? j1 j2))
57
    ((array? j1)
58
     (let loop ((j1 j1) (j2 j2))
59
       (match (cons j1 j2)
60
         ((#() . #())
61
          #t)
62
         ((#(v1 j1 ...) . #(v2 j2 ...))
63
          (and (json-equal? v1 v2)
64
               (loop `#(,@j1) `#(,@j2))))
65
         (_ #f))))
66
    (else (equal? j1 j2))))
67
68
(define simple-record-scm
69
  '(("subject" . "acct:carol@example.com")
70
    ("links" . #((("rel" . "http://openid.net/specs/connect/1.0/issuer")
71
                  ("href" . "https://openid.example.com"))))))
72
(define simple-record
73
  (make-jrd-record
74
    "acct:carol@example.com"
75
    '()
76
    *unspecified*
77
    (list
78
      (make-link-record
79
        "http://openid.net/specs/connect/1.0/issuer"
80
        *unspecified*
81
        "https://openid.example.com"
82
        *unspecified*
83
        *unspecified*))))
84
85
(test-assert "json-equality"
86
  (and (json-equal? #t #t)
87
       (json-equal? #f #f)
88
       (json-equal? "abc" "abc")
89
       (not (json-equal? "abc" "cba"))
90
       (json-equal? #() #())
91
       (json-equal? #(1 2) #(1 2))
92
       (not (json-equal? #(1 2) #(2 1)))
93
       (json-equal? '(("a" . 1) ("b" . 2))
94
                    '(("a" . 1) ("b" . 2)))
95
       (json-equal? '(("a" . 1) ("b" . 2))
96
                    '(("b" . 2) ("a" . 1)))))
97
98
(test-assert "simple example decode"
99
  (let ((converted-simple-record (json->jrd-record simple-record-scm)))
100
    (if (equal? converted-simple-record simple-record)
101
        #t
102
        (pk 'fail converted-simple-record #f))))
103
104
(test-assert "simple example encode"
105
  (let ((converted-simple-record-scm (jrd-record->json simple-record)))
106
    (if (json-equal? simple-record-scm
107
                     (json-string->scm converted-simple-record-scm))
108
        #t
109
        (pk 'fail converted-simple-record-scm
110
            (scm->json-string simple-record-scm) #f))))
111
112
(test-end "webfinger")
113