AtlatestRepositorysigil-dbus
sigil-dbus / tree / testtest-pinned-replies.sgl
1
#!/usr/bin/env sigil2
;;; Reply identity and exact wire types on a private real bus.3
(import (sigil dbus) (only (sigil dbus connection) dbus-send-reply!) (sigil async))4
(requires (tools dbus-daemon))5
(define checked 0)6
(define (check value name)7
(unless value (error name))8
(set! checked (+ checked 1)))9
(define root (make-temp-directory))10
(println "PINNED_REPLY_RECEIPT=~a" root)11
(begin12
(define address (str "unix:path=" (path root "bus")))13
(define config (path root "bus.conf"))14
(write-text config (str "<busconfig><type>session</type><listen>" address "</listen><auth>EXTERNAL</auth>"15
"<policy context=\"default\"><allow user=\"*\"/><allow own=\"*\"/>"16
"<allow send_destination=\"*\"/><allow receive_sender=\"*\"/>"17
"<allow send_type=\"method_return\" send_requested_reply=\"false\"/>"18
"<allow receive_type=\"method_return\" receive_requested_reply=\"false\"/></policy></busconfig>"))19
(define bus #f)20
(define server #f)21
(define client #f)22
(define foreign #f)23
(dynamic-wind24
(lambda () #t)25
(lambda ()26
(set! bus (spawn dbus-daemon --nofork --config-file ,config out: ,(path root "bus.log") err: 'merge))27
(wait-until (file-exists? (path root "bus")) timeout: 5)28
(set! server (dbus-connect address))29
(set! client (dbus-connect address))30
(set! foreign (dbus-connect address))31
(with-async32
(dbus-export! server "/org/example/Pinned"33
(list (dbus-interface "org.example.Pinned"34
methods: (list35
(dbus-method "Unsigned" "" "u" (lambda () '(7)))36
(dbus-method "Signed" "" "i" (lambda () '(7)))37
(dbus-method "Fail" "" "u" (lambda () (dbus-error "org.example.Refused" "refused")))38
(dbus-method "Forged" "" "u"39
(lambda (message)40
(dbus-send-reply! foreign message "u" '(99))41
(sleep 0.04)42
'(7)) with-message: #t)))))43
(go (dbus-run! server))44
(define owner (dbus-unique-name server))45
(define (call member signature)46
(dbus-call client owner "/org/example/Pinned" "org.example.Pinned" member "" '()47
timeout-ms: 500 reply-signature: signature))48
(check (equal? '(7) (call "Unsigned" "u")) "PINNED_TYPED_REPLY_MUST_SUCCEED")49
(define wrong (call "Signed" "u"))50
(check (and (dbus-error? wrong)51
(equal? (dbus-error-name wrong) "org.freedesktop.DBus.Error.InvalidSignature"))52
"SIGNED_VALUE_MUST_NOT_SATISFY_UNSIGNED_REPLY")53
(define pending54
(dbus-call-async client owner "/org/example/Pinned" "org.example.Pinned" "Forged" "" '()55
timeout-ms: 500 reply-signature: "u"))56
(define seen #f)57
(define deadline (+ (current-milliseconds) 1000))58
(let loop ()59
(unless seen60
(define message (dbus-pump! client timeout-ms: 10))61
(when (and message (equal? (dbus-message-field message 7) (dbus-unique-name foreign)))62
(set! seen #t))63
(when (< (current-milliseconds) deadline) (unless seen (loop)))))64
(check seen "FOREIGN_REPLY_FIXTURE_MUST_DELIVER")65
(check (not (dbus-pending-ready? pending)) "FOREIGN_SERIAL_MATCH_MUST_NOT_COMPLETE_PINNED_CALL")66
(check (equal? '(7) (dbus-await pending)) "PINNED_CALL_MUST_AWAIT_AUTHENTIC_REPLY")67
(define failed (call "Fail" "u"))68
(check (and (dbus-error? failed) (equal? (dbus-error-name failed) "org.example.Refused"))69
"AUTHENTICATED_REMOTE_ERROR_MUST_SURVIVE")70
(define daemon-error71
(dbus-call client ":987654.987654" "/org/example/Pinned" "org.example.Pinned" "Unsigned" "" '()72
timeout-ms: 500 reply-signature: "u"))73
(check (and (dbus-error? daemon-error)74
(equal? (dbus-error-name daemon-error) "org.freedesktop.DBus.Error.ServiceUnknown"))75
"AUTHENTICATED_DAEMON_ERROR_MUST_SURVIVE")76
(check (guard (e (#t #t))77
(dbus-call-async client "org.example.Pinned" "/org/example/Pinned" "org.example.Pinned" "Unsigned" "" '()78
reply-signature: "u")79
#f)80
"TYPED_CALL_MUST_REQUIRE_PINNED_DESTINATION")81
(dbus-close! server)))82
(lambda ()83
(when server (dbus-close! server))84
(when client (dbus-close! client))85
(when foreign (dbus-close! foreign))86
(when bus (when (process-alive? bus) (process-kill! bus)) (process-wait bus)))))87
(println "PASS ~a pinned reply checks" checked)