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
|
;; 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 (srfi srfi-26)
(gnu gnunet netstruct procedural)
(gnu gnunet utils bv-slice)
(rnrs bytevectors)
(prefix (gnu gnunet netstruct syntactic) s@))
(test-begin "netstruct procedural")
(define struct/a
(make-netstructure
(vector (make-field 'field u8))))
(define struct/b
(make-netstructure
(vector (make-field 'field u8)
(make-field 'other-field u8))))
(test-eqv "first field, offset (struct)"
0
(offsetof struct/a '(field)))
(test-equal "writing to a u16, big endian"
(cons #xde #xad)
(let ((s (make-slice/read-write 2)))
(set%! u16/big '() s #xdead)
(cons (slice-u8-ref s 0)
(slice-u8-ref s 1))))
(test-eqv "writing to (smaller) field"
#xbe
(let ((s (bv-slice/read-write (u8-list->bytevector '(#xbe #xef)))))
(set%! struct/b '(field) s #xbe)
(slice-u8-ref s 0)))
(test-eqv "writing to (smaller) field, at an offset"
#xbe
(let ((s (bv-slice/read-write (u8-list->bytevector '(#xbe #xef)))))
(set%! struct/b '(other-field) s #xbe)
(slice-u8-ref s 1)))
(test-eqv "reading from (smaller) field"
#xbe
(let ((s (bv-slice/read-write #u8(#xbe #xef))))
(read% struct/b '(field) s)))
(test-eqv "reading from (smaller) field, at an offset"
#xef
(let ((s (bv-slice/read-write #u8(#xbe #xef))))
(read% struct/b '(other-field) s)))
(test-eqv "writing to a u8"
#xde
(let ((s (make-slice/read-write 1)))
(set%! u8 '() s #xde)
(slice-u8-ref s 0)))
;; While the individual fields can be written / read,
;; the parts cannot!
(test-error "writing a structure -> error"
&unwritable
(set%! struct/b '() (make-slice/read-write 2) 'anything))
(test-error "reading a structure -> error"
&unreadable
(read% struct/b '() (make-slice/read-write 2)))
(test-eq "recursive part"
u8
(part (make-netstructure
(vector (make-field 'recurse struct/a)))
'(recurse field)))
(test-end "netstruct procedural")
(test-begin "nestruct syntactic")
(test-eqv "total size (syntactic, constant, one field)"
1 (eval '(s@sizeof struct/a '())
(current-module)))
(test-eqv "field size (syntactic, constant, one field)"
1 (eval '(s@sizeof struct/a '(field))
(current-module)))
(test-end "nestruct syntactic")
|