message-handler.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. (use-modules (gnu gnunet mq handler)
  19. (gnu gnunet message protocols)
  20. (gnu extractor enum)
  21. (rnrs base))
  22. (test-begin "mq-handler")
  23. (test-assert "constructor docstring"
  24. (procedure-documentation make-message-handler))
  25. (define (bogus-verifier x)
  26. (throw 'what #:verifier))
  27. (define (bogus-interposition thunk)
  28. (thow 'what #:interposition))
  29. (define (bogus-handler x)
  30. (throw 'what #:handler))
  31. (define bogus-handler
  32. (cute make-message-handler <> bogus-interposition
  33. bogus-verifier bogus-handler))
  34. (define (uninteresting-interposition thunk)
  35. (thunk))
  36. ;; Message indices
  37. (test-equal "message handler index (0)"
  38. 0 (message-handler-index (bogus-handler 0)))
  39. (test-equal "message handler index (65535)"
  40. 65535 (message-handler-index (bogus-handler 65535)))
  41. (test-error "message handler OOB (< 0)" #t
  42. (message-handler-index (bogus-handler -1)))
  43. (test-error "message handler OOB (> 65535)" #t
  44. (message-handler-index (bogus-handler -1)))
  45. (test-error "message handler inexact" #t
  46. (message-handler-index (bogus-handler -1)))
  47. (test-equal "message handler enum value"
  48. 777
  49. (message-handler-index
  50. (bogus-handler (integer->value message-type 777))))
  51. (define %arbitrary-index #xdead)
  52. (define-syntax ^vals
  53. (syntax-rules ()
  54. ((_ exp) (call-with-values (lambda () exp) list))))
  55. ;; Handlers may return multiple values.
  56. ;;
  57. ;; (Currently, handle-message! is even tail-recursive,
  58. ;; but that's not guaranteed)
  59. (test-equal "handlers with zero values"
  60. '()
  61. (^vals (handle-message!
  62. (make-message-handler %arbitrary-index
  63. uninteresting-interposition
  64. bogus-verifier
  65. (lambda (message)
  66. (values)))
  67. 'message)))
  68. (test-equal "handlers with three values"
  69. '(x y z)
  70. (^vals (handle-message!
  71. (make-message-handler %arbitrary-index
  72. uninteresting-interposition
  73. bogus-verifier
  74. (lambda (message)
  75. (values 'x 'y 'z)))
  76. 'dont-care)))
  77. ;; Dynamic environment tests
  78. (let* ((nestedness (make-parameter 0))
  79. (is-set? #f)
  80. (return-nestedness
  81. (lambda (message)
  82. (nestedness)))
  83. (interposition (lambda (thunk)
  84. (assert (not is-set?))
  85. (set! is-set? #t)
  86. (parameterize ((nestedness (1+ (nestedness))))
  87. (thunk)))))
  88. (test-equal "dynamic environment adjusted exactly once (verifier)"
  89. 1
  90. (verify-message?
  91. (make-message-handler %arbitrary-index interposition
  92. return-nestedness bogus-handler)
  93. "message"))
  94. (set! is-set? #f)
  95. (test-equal "dynamic environment adjusted exactly once (handler)"
  96. 1
  97. (handle-message!
  98. (make-message-handler %arbitrary-index interposition
  99. bogus-verifier return-nestedness)
  100. 'anything))
  101. (set! is-set? #f))
  102. ;; Multiple handler tests
  103. (test-equal "message handler OOB (0,0)"
  104. #f
  105. (message-handler-for (message-handlers) 0))
  106. (test-equal "message handler OOB (0,high)"
  107. #f
  108. (message-handler-for (message-handlers) 9))
  109. ;; 0, 65535: two extreme values
  110. ;; 777: something else entirely
  111. (let* ((indices `(0 777 65535))
  112. (all-handlers (map bogus-handler indices))
  113. (handlers (apply message-handlers all-handlers)))
  114. (for-each
  115. (lambda (i handler)
  116. ;; Both numeric and typed value are acceptable.
  117. ;; Whatever's convenient to the caller.
  118. (test-eq "message handler (non-empty, numeric)"
  119. handler
  120. (message-handler-for handlers i))
  121. (test-eq "message handler (non-empty, enum value)"
  122. handler
  123. (message-handler-for handlers (integer->value message-type i))))
  124. indices all-handlers))
  125. (test-end "mq-handler")