AtlatestRepositorysigil-dbus
sigil-dbus / tree / testtest-wire.sgl
1
(import (sigil dbus) (sigil dbus types) (sigil dbus transport) (sigil dbus buffers)2
(only (sigil error) exception-message)3
(sigil test) (sigil process))5
(test-group "wire round trips"6
(for-each7
(lambda (order)8
(for-each9
(lambda (entry)10
(let ((signature (car entry)) (values (cdr entry)))11
(test (string-append signature " / " (symbol->string order))12
(assert-equal values13
(dbus-unmarshal signature (dbus-marshal signature values endianness: order)14
endianness: order)))))15
(list16
(cons "" '())17
(cons "y" '(255)) (cons "b" '(#t)) (cons "b" '(#f))18
(cons "nqiu" (list -32768 65535 -2147483648 4294967295))19
(cons "xt" (list -9223372036854775808 18446744073709551615))20
(cons "xt" (list 9223372036854775807 0))21
(cons "d" '(1.25)) (cons "d" '(-0.5))22
(cons "sog" '("héλ🙂" "/org/example/Test" "a{sv}(it)"))23
(cons "h" '(123))24
(cons "ay" (list (bytevector 0 128 255)))25
(cons "ai" '((-1 0 1)))26
(cons "aai" '((() (1 2) (3))))27
(cons "(y(is)at)" (list (list 1 (list -4 "text") (list 0 18446744073709551615))))28
(cons "a{sv}" (list (list (cons "title" (dbus-variant "s" "hi"))29
(cons "urgent" (dbus-variant "b" #t)))))30
(cons "a{iv}" (list (list (cons -2 (dbus-variant "(si)" (list "two" 2))))))31
(cons "v" (list (dbus-variant "v" (dbus-variant "s" "nested")) ))32
(cons "yat" (list 9 '()))33
(cons "ya(xt)" (list 9 '())))))34
'(little big)))36
;; Golden bytes derived from specification sections Marshalling Basic Types37
;; and Marshalling Containers (not produced by the implementation under test).38
(test-group "golden vectors"39
(test "UINT32 0x01020304 little"40
(assert-equal (bytevector 4 3 2 1) (dbus-marshal "u" (list 16909060) endianness: 'little)))41
(test "UINT32 0x01020304 big"42
(assert-equal (bytevector 1 2 3 4) (dbus-marshal "u" (list 16909060) endianness: 'big)))43
(test "BOOLEAN alignment after BYTE"44
(assert-equal (bytevector 7 0 0 0 1 0 0 0)45
(dbus-marshal "yb" (list 7 #t) endianness: 'little)))46
(test "STRING foo"47
(assert-equal (bytevector 3 0 0 0 102 111 111 0)48
(dbus-marshal "s" (list "foo") endianness: 'little)))49
(test "SIGNATURE a{sv}"50
(assert-equal (bytevector 5 97 123 115 118 125 0)51
(dbus-marshal "g" (list "a{sv}"))))52
(test "VARIANT containing UINT32"53
(assert-equal (bytevector 1 117 0 0 4 3 2 1)54
(dbus-marshal "v" (list (dbus-variant "u" 16909060)) endianness: 'little)))55
(test "empty UINT64 array includes element alignment"56
(assert-equal (bytevector 0 0 0 0 0 0 0 0) (dbus-marshal "at" (list '()))))57
(test "array length excludes initial alignment"58
(assert-equal (bytevector 8 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0)59
(dbus-marshal "at" (list (list 1)) endianness: 'little)))60
(test "DOUBLE 1.0 big"61
(assert-equal (bytevector 63 240 0 0 0 0 0 0)62
(dbus-marshal "d" '(1.0) endianness: 'big))))64
(test-group "malformed and limits"65
(for-each66
(lambda (signature)67
(test (string-append "signature " signature)68
(assert-error (dbus-parse-signature signature))))69
'("a" "(" "()" "{ss}" "a{vs}" "a{sss}" "a{(i)s}" "a{}" "a{sa}" "r" "e" "m" "i)" "(i" "a{ss"))70
(test "signature length limit" (assert-error (dbus-parse-signature (make-string 256 #\i))))71
(test "array depth limit" (assert-error (dbus-parse-signature (string-append (make-string 33 #\a) "i"))))72
(test "struct depth limit"73
(assert-error (dbus-parse-signature (string-append (make-string 33 #\() "i" (make-string 33 #\))))))74
(test "dynamic variant depth"75
(let ((value (dbus-variant "i" 1)))76
(do ((i 0 (+ i 1))) ((= i 65)) (set! value (dbus-variant "v" value)))77
(assert-error (dbus-marshal "v" (list value)))))78
(for-each79
(lambda (entry)80
(test (string-append "range " (car entry))81
(assert-error (dbus-marshal (car entry) (list (cdr entry))))))82
(list (cons "y" 256) (cons "q" -1) (cons "n" 32768) (cons "i" -2147483649)83
(cons "t" 18446744073709551616) (cons "x" -9223372036854775809)84
(cons "b" 1) (cons "u" 1.0)))85
(test "body arity" (assert-error (dbus-marshal "ii" '(1))))86
(test "struct arity" (assert-error (dbus-marshal "(ii)" '((1)))))87
(test "missing boolean bytes" (assert-error (dbus-unmarshal "b" (bytevector 1))))88
(test "invalid boolean" (assert-error (dbus-unmarshal "b" (bytevector 2 0 0 0) endianness: 'little)))89
(test "nonzero alignment" (assert-error (dbus-unmarshal "yu" (bytevector 1 0 1 0 0 0 0 0))))90
(test "trailing data" (assert-error (dbus-unmarshal "y" (bytevector 0 0))))91
(test "oversized string before allocation"92
(assert-error (dbus-unmarshal "s" (bytevector 255 255 255 127) endianness: 'little)))93
(test "oversized array before allocation"94
(assert-error (dbus-unmarshal "ay" (bytevector 1 0 0 4) endianness: 'little)))95
(test "array length ends inside element"96
(assert-error (dbus-unmarshal "ai" (bytevector 3 0 0 0 1 0 0) endianness: 'little)))97
(test "string terminator" (assert-error (dbus-unmarshal "s" (bytevector 1 0 0 0 97 1))))98
(test "embedded NUL" (assert-error (dbus-unmarshal "s" (bytevector 1 0 0 0 0 0))))99
(test "empty variant signature" (assert-error (dbus-unmarshal "v" (bytevector 0 0))))100
(test "multiple variant types" (assert-error (dbus-unmarshal "v" (bytevector 2 105 105 0))))101
(test "invalid object path" (assert-error (dbus-marshal "o" '("/bad//path"))))102
(test "value budget" (assert-error (dbus-unmarshal "ai" (dbus-marshal "ai" '((1 2 3))) max-values: 3)))103
(test "byte budget" (assert-error (dbus-marshal "s" '("a long string") max-bytes: 8)))104
(for-each105
(lambda (bytes)106
(test "invalid UTF-8"107
(let ((n (bytevector-length bytes)))108
(assert-error109
(dbus-unmarshal "s"110
(bytevector-append (bytevector n 0 0 0) bytes (bytevector 0)) endianness: 'little)))))111
(list (bytevector 128) (bytevector 192 128) (bytevector 224 128 128)112
(bytevector 237 160 128) (bytevector 244 144 128 128) (bytevector 240 159)113
(bytevector 194 65))))115
(define (call-message)116
(dbus-message 1 123 (list (cons 1 (dbus-variant "o" "/org/example/Test"))117
(cons 2 (dbus-variant "s" "org.example.Test"))118
(cons 3 (dbus-variant "s" "Echo"))119
(cons 42 (dbus-variant "(is)" (list 9 "extension")) ))120
"s" '("hello")))122
(test-group "messages and names"123
(for-each124
(lambda (order)125
(test "complete message round trip"126
(assert-equal (call-message)127
(dbus-decode-message (dbus-encode-message (call-message) endianness: order)))))128
'(little big))129
(for-each130
(lambda (change)131
(test "invalid fixed prefix"132
(let ((bytes (dbus-encode-message (call-message) endianness: 'little)))133
(bytevector-u8-set! bytes (car change) (cdr change))134
(assert-error (dbus-decode-message bytes)))))135
'((0 . 0) (1 . 0) (3 . 2) (4 . 255) (8 . 0) (15 . 127)))136
(test "message size limit"137
(assert-error (dbus-frame-size (dbus-encode-message (call-message)) max-bytes: 16)))138
(test "duplicate header fields"139
(assert-error (dbus-encode-message140
(dbus-message 2 1 (list (cons 5 (dbus-variant "u" 1)) (cons 5 (dbus-variant "u" 2))) "" '()))))141
(test "wrong header type"142
(assert-error (dbus-encode-message (dbus-message 2 1 (list (cons 5 (dbus-variant "s" "one"))) "" '()))))143
(test "missing required header" (assert-error (dbus-encode-message (dbus-message 1 1 '() "" '()))))144
(test "names"145
(assert-true (dbus-bus-name? ":1.0"))146
(assert-false (dbus-interface-name? "1.bad"))147
(assert-false (dbus-member-name? "bad.name"))148
(assert-true (dbus-object-path? "/")))149
(test "escaped addresses"150
(assert-equal '(("unix" ("path" . "/tmp/a,b")))151
(dbus-parse-address "unix:path=/tmp/a%2cb")))152
(test "bad address escape" (assert-error (dbus-parse-address "unix:path=%xx")))153
(test "address escape requires two hex digits" (assert-error (dbus-parse-address "unix:path=%+a")))154
(test "address NUL" (assert-error (dbus-parse-address "unix:path=%00"))))156
(test "string clearing preserves all UTF-8 widths"157
(let* ((text (utf8->string (string->utf8 "aé中🙂")))158
(size (bytevector-length (string->utf8 text))))159
(assert-true (dbus-clear-string! text))160
(assert-equal size (bytevector-length (string->utf8 text)))161
(assert-equal '(0 128 2048 65536) (map char->integer (string->list text)))))163
(test "string clearing guards values it cannot overwrite"164
(assert-false (dbus-clear-string! #f)))166
(test "multibyte rejected object path keeps its original error"167
(let ((message168
(guard (e (else (exception-message e)))169
(dbus-unmarshal "o" (dbus-marshal "s" (list "é/bad")))170
#f)))171
(assert-equal "Invalid D-Bus object path" message)))173
(unless (run-tests) (exit 1))