aboutsummaryrefslogtreecommitdiff
path: root/tests/config-fs.scm
blob: b194a6b95944feae8cd8f25fd9b677b07f98094a (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
;; This file is part of scheme-GNUnet.
;; Copyright (C) 2021 Maxime Devos
;;
;; scheme-GNUnet is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; scheme-GNUnet 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
;; Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;; SPDX-License-Identifier: AGPL3.0-or-later

(import (gnu gnunet config db)
	(gnu gnunet config expand)
	(gnu gnunet config fs)
	(gnu gnunet config parser)
	(rnrs exceptions)
	(srfi srfi-1)
	(srfi srfi-64))
(test-begin "config-fs")

(define (alist->getenv alist)
  (lambda (x)
    (assoc-ref alist x)))

(test-equal "locate-user-configuraton, XDG_CONFIG_HOME + HOME"
  "/somewhere/unusual/gnunet.conf"
  (locate-user-configuration
   #:getenv
   (alist->getenv '(("HOME" . "/a/home")
		    ("XDG_CONFIG_HOME" . "/somewhere/unusual")))))

(test-equal "locate-user-configuraton, XDG_CONFIG_HOME without HOME"
  "/somewhere/unusual/gnunet.conf"
  (locate-user-configuration
   #:getenv
   (alist->getenv '(("XDG_CONFIG_HOME" . "/somewhere/unusual")))))

(test-equal "locate-user-configuration, no XDG_CONFIG_HOME"
  "/a/home/.config/gnunet.conf"
  (locate-user-configuration
   #:getenv
   (alist->getenv '(("HOME" . "/a/home")))))

(test-equal "locate-user-configuration, empty XDG_CONFIG_HOME"
  "/a/home/.config/gnunet.conf"
  (locate-user-configuration
   #:getenv
   (alist->getenv '(("HOME" . "/a/home")
		    ("XDG_CONFIG_HOME" . "")))))

(test-equal "locate-user-configuration, no XDG_CONFIG_HOME, no HOME"
  #false
  (locate-user-configuration
   #:getenv
   (alist->getenv '())))

(test-equal "locate-user-configuration, no XDG_CONFIG_HOME, empty HOME"
  #false
  (locate-user-configuration
   #:getenv
   (alist->getenv '(("HOME" . "")))))

(define (load-string->alist/unexpanded s)
  (call-with-input-string s
    (lambda (p)
      (define a '())
      (define (set-value! section key value)
	(pk 's section key value)
	(set! a `(((,section . ,key) . ,value) ,@a))
	(values))
      (load-configuration/port! set-value! p)
      a)))

;; TODO: better error reporting

(test-equal "load-configuration/port!, literal read-value"
  `((("section" . "VAR")
     . #("VAR = VALUE" 2 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "[section]\nVAR = VALUE"))

(test-equal "load-configuration/port!, empty"
  '()
  (load-string->alist/unexpanded ""))

(test-error "load-configuration/port!, assignment outside section"
  "assignment outside section"
  (load-string->alist/unexpanded "VAR = VALUE"))

(test-equal "load-configuration/port!, literal read-value after empty line"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "[section]\n\nVAR = VALUE"))

(test-equal "load-configuration/port!, section after empty line"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "\n[section]\nVAR = VALUE"))

(test-error "load-configuration/port!, bogus syntax before section"
  "unrecognised syntax at line ???"
  (load-string->alist/unexpanded "]\n[section]\n"))

(test-error "load-configuration/port!, bogus syntax after section"
  "unrecognised syntax at line ???"
  (load-string->alist/unexpanded "[section]\n]"))

(test-equal "load-configuration/port!, skip comment (#) after section"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "[section]\n#\nVAR = VALUE"))

(test-equal "load-configuration/port!, skip comment (%) after section"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "[section]\n#\nVAR = VALUE"))

(test-equal "load-configuration/port!, skip empty line after section"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "[section]\n\nVAR = VALUE"))

(test-equal "load-configuration/port!, skip comment (#) before section"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "#\n[section]\nVAR = VALUE"))

(test-equal "load-configuration/port!, skip comment (%) before section"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "%\n[section]\nVAR = VALUE"))

(test-equal "load-configuration/port!, skip empty line before section"
  `((("section" . "VAR")
     . #("VAR = VALUE" 3 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded "\n[section]\nVAR = VALUE"))

(test-equal "load-configuration/port!, two sections"
  `((("section2" . "VAR2")
     . #("VAR2 = VALUE2" 4 (,(make-literal-position 7 13))))
    (("section1" . "VAR")
     . #("VAR = VALUE" 2 (,(make-literal-position 6 11)))))
  (load-string->alist/unexpanded
   "[section1]\nVAR = VALUE\n[section2]\nVAR2 = VALUE2"))

(define (load-string->config/expanded text environment-variables)
  (make-expanded-configuration
   (lambda (set-value!)
     (call-with-input-string text
       (lambda (p)
	 (load-configuration/port! set-value! p))))
   #:getenv
   (alist->getenv environment-variables)))

(define (load-string->alist/expanded text interested environment-variables)
  (define config (load-string->config/expanded text environment-variables))
  (filter-map (lambda (section+key)
		`(,section+key
		  . ,(guard (c ((undefined-key-error? c) 'undefined))
		       (read-value identity config (car section+key)
				   (cdr section+key)))))
	      interested))

(test-equal "make-expanded-configuration, one variable"
  '((("sect" . "var") . "iable"))
  (load-string->alist/expanded "[sect]\nvar=iable"
			       '(("sect" . "var")) '()))

;; Detected a missing 'list'
(test-equal "make-expanded-configuration, expand variable (via getenv)"
  '((("sect" . "var") . "iable"))
  (load-string->alist/expanded "[sect]\nvar=i$a"
			       '(("sect" . "var")) '(("a" . "able"))))


(test-equal "make-expanded-configuration, expand variable (via getenv, fancyness)"
  '((("sect" . "var") . "i}\\$able%f'"))
  (load-string->alist/expanded "[sect]\nvar=i$a"
			       '(("sect" . "var"))
			       '(("a" . "}\\$able%f'"))))

(test-equal "make-expanded-configuration, expand variable (via PATHS)"
  '((("sect" . "var") . "iable")
    (("PATHS" . "something") . "able"))
  (load-string->alist/expanded
   "[sect]\nvar=i$something\n[PATHS]\nsomething=able"
   '(("sect" . "var")
     ("PATHS" . "something"))
   '()))

;; Detects incorrect implementation of substring=? (string=? was used instead)
(test-equal "make-expanded-configuration, expand variable (via PATHS + getenv)"
  '((("sect" . "var") . "iable")
    (("PATHS" . "something") . "able"))
  (load-string->alist/expanded
   "[sect]\nvar=i$something\n[PATHS]\nsomething=a$else"
   '(("sect" . "var")
     ("PATHS" . "something"))
   '(("else" . "ble"))))

;; Detect implementations of substring=? that always return #f.
(test-equal "make-expanded-configuration, loop detected"
  #t
  (guard (c ((expansion-loop-error? c) #t))
    (load-string->alist/expanded
     "[sect]\nvar=i${something}\n[PATHS]\nsomething=d${something}"
     '(("PATHS" . "something"))
     '(("something" . "unused")))))

(test-end "config-fs")