netstruct.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;; This file is part of scheme-GNUnet.
  2. ;; Copyright (C) 2021 GNUnet e.V.
  3. ;;
  4. ;; scheme-GNUnet is free software: you can redistribute it and/or modify it
  5. ;; under the terms of the GNU Affero General Public License as published
  6. ;; by the Free Software Foundation, either version 3 of the License,
  7. ;; or (at your option) any later version.
  8. ;;
  9. ;; scheme-GNUnet is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Affero General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Affero General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;
  17. ;; SPDX-License-Identifier: AGPL-3.0-or-later
  18. (import (srfi srfi-26)
  19. (gnu gnunet netstruct procedural)
  20. (gnu gnunet utils bv-slice)
  21. (rnrs bytevectors)
  22. (prefix (gnu gnunet netstruct syntactic) s@))
  23. (test-begin "netstruct procedural")
  24. (define struct/a
  25. (make-netstructure
  26. (vector (make-field 'field u8))))
  27. (define struct/b
  28. (make-netstructure
  29. (vector (make-field 'field u8)
  30. (make-field 'other-field u8))))
  31. (test-eqv "first field, offset (struct)"
  32. 0
  33. (offsetof struct/a '(field)))
  34. (test-equal "writing to a u16, big endian"
  35. (cons #xde #xad)
  36. (let ((s (make-slice/read-write 2)))
  37. (set%! u16/big '() s #xdead)
  38. (cons (slice-u8-ref s 0)
  39. (slice-u8-ref s 1))))
  40. (test-eqv "writing to (smaller) field"
  41. #xbe
  42. (let ((s (bv-slice/read-write (u8-list->bytevector '(#xbe #xef)))))
  43. (set%! struct/b '(field) s #xbe)
  44. (slice-u8-ref s 0)))
  45. (test-eqv "writing to (smaller) field, at an offset"
  46. #xbe
  47. (let ((s (bv-slice/read-write (u8-list->bytevector '(#xbe #xef)))))
  48. (set%! struct/b '(other-field) s #xbe)
  49. (slice-u8-ref s 1)))
  50. (test-eqv "reading from (smaller) field"
  51. #xbe
  52. (let ((s (bv-slice/read-write #u8(#xbe #xef))))
  53. (read% struct/b '(field) s)))
  54. (test-eqv "reading from (smaller) field, at an offset"
  55. #xef
  56. (let ((s (bv-slice/read-write #u8(#xbe #xef))))
  57. (read% struct/b '(other-field) s)))
  58. (test-eqv "writing to a u8"
  59. #xde
  60. (let ((s (make-slice/read-write 1)))
  61. (set%! u8 '() s #xde)
  62. (slice-u8-ref s 0)))
  63. ;; While the individual fields can be written / read,
  64. ;; the parts cannot!
  65. (test-error "writing a structure -> error"
  66. &unwritable
  67. (set%! struct/b '() (make-slice/read-write 2) 'anything))
  68. (test-error "reading a structure -> error"
  69. &unreadable
  70. (read% struct/b '() (make-slice/read-write 2)))
  71. (test-eq "recursive part"
  72. u8
  73. (part (make-netstructure
  74. (vector (make-field 'recurse struct/a)))
  75. '(recurse field)))
  76. (test-end "netstruct procedural")
  77. (test-begin "nestruct syntactic")
  78. (test-eqv "total size (syntactic, constant, one field)"
  79. 1 (eval '(s@sizeof struct/a '())
  80. (current-module)))
  81. (test-eqv "field size (syntactic, constant, one field)"
  82. 1 (eval '(s@sizeof struct/a '(field))
  83. (current-module)))
  84. (test-end "nestruct syntactic")