AtlatestRepositorysigil-quic
sigil-quic / tree / scriptsconstruction-cleanup-tests.sgl
1
(import (sigil test)2
(sigil crypto)3
(sigil quic packet)4
(sigil quic connection)5
(sigil quic tls)6
(sigil quic tests construction spy))8
;; Explicit remote-only instrumented suite. Observe real owned buffers and9
;; check them before teardown, which also clears baseline's stranded copies.10
(define (construction-wipe! bytes)11
(when (bytevector? bytes)12
(do ((i 0 (+ i 1))) ((= i (bytevector-length bytes)))13
(bytevector-u8-set! bytes i 0))))15
(define (construction-zero? bytes)16
(let loop ((i 0))17
(or (= i (bytevector-length bytes))18
(and (= (bytevector-u8-ref bytes i) 0) (loop (+ i 1))))))20
(define (construction-with-key procedure)21
(let* ((key (ecdsa-p256-generate-keypair)) (snapshot (bytevector-copy (car key))))22
(dynamic-wind23
construction-spy-reset!24
(lambda () (procedure key snapshot))25
(lambda ()26
(for-each (lambda (entry) (tls-abort! (vector-ref entry 1)))27
(construction-spy-engines))28
(for-each29
(lambda (entry)30
(discard-packet-keys! (vector-ref entry 2))31
(for-each construction-wipe! (vector->list (vector-ref entry 3))))32
(construction-spy-materials))33
(for-each (lambda (entry) (construction-wipe! (vector-ref entry 1)))34
(construction-spy-keys))35
(construction-wipe! (car key))36
(construction-wipe! snapshot)37
(construction-spy-reset!)))))39
(define (construction-tls-server key)40
(make-tls-server (list #u8(113 49))41
(list (cons 0 #u8(9)) (cons 15 #u8(2))) (list #u8(48 1 0)) (car key)))43
(define (construction-quic role key defer?)44
(if (eq? role 'client)45
(make-quic-client (list #u8(113 49)) 'pinned-key '()46
(make-tls-pin-verifier (cdr key)) local-id: #u8(1)47
destination-id: #u8(10 11 12 13 14 15 16 17) defer-key-share?: defer?)48
(make-quic-server (list #u8(113 49)) '() (list #u8(48 1 0)) (car key)49
#u8(10 11 12 13 14 15 16 17) #u8(1) local-id: #u8(2))))51
(define (construction-check-keys! count key snapshot)52
(let ((entries (construction-spy-keys)))53
(assert-equal count (length entries))54
(assert-equal snapshot (car key))55
(for-each56
(lambda (entry)57
(assert-true (eq? (car key) (vector-ref entry 0)))58
(assert-equal 32 (bytevector-length (vector-ref entry 1)))59
(assert-true (vector-ref entry 2))60
(assert-true (vector-ref entry 3))) entries)61
entries))63
(define (construction-check-materials! role directions)64
(let ((entries (filter (lambda (entry) (eq? role (vector-ref entry 0)))65
(construction-spy-materials))))66
(assert-equal directions (map (lambda (entry) (vector-ref entry 1)) entries))67
(for-each68
(lambda (entry)69
(assert-equal '(32 16 12 16)70
(map bytevector-length (vector->list (vector-ref entry 3))))71
(assert-equal '(#t #t #t #t) (vector-ref entry 4))) entries)72
entries))74
(define (construction-owned-zero? keys materials)75
(and (let loop ((entries keys))76
(or (null? entries)77
(and (construction-zero? (vector-ref (car entries) 1))78
(loop (cdr entries)))))79
(let loop ((entries materials))80
(or (null? entries)81
(and (let inner ((bytes (vector->list (vector-ref (car entries) 3))))82
(or (null? bytes)83
(and (construction-zero? (car bytes)) (inner (cdr bytes)))))84
(loop (cdr entries)))))))86
(define (construction-check-error! failure role stage)87
(assert-true (error-object? failure))88
(assert-equal "QL7 injected construction failure" (error-object-message failure))89
(assert-equal (list role stage) (error-object-irritants failure)))91
(define (construction-tls-failure! stage)92
(construction-with-key93
(lambda (key snapshot)94
(construction-spy-fail! 'server stage)95
(let* ((failure96
(guard (exception (else exception))97
(construction-tls-server key)98
#f))99
(keys (construction-check-keys! 1 key snapshot)))100
(construction-check-error! failure 'server stage)101
(assert-equal '() (construction-spy-engines))102
(assert-equal '() (construction-spy-materials))103
(assert-true (construction-owned-zero? keys '()))))))105
(define (construction-quic-failure! role stage)106
(construction-with-key107
(lambda (key snapshot)108
(construction-spy-fail! role stage)109
(let* ((failure110
(guard (exception (else exception))111
(construction-quic role key #f)112
#f))113
(keys (construction-check-keys! (if (eq? role 'server) 1 0) key snapshot))114
(materials (construction-check-materials! role115
(if (eq? stage 'initial-write) '(read) '())))116
(engines (construction-spy-engines)))117
(construction-check-error! failure role stage)118
(assert-equal 1 (length engines))119
(assert-equal role (vector-ref (car engines) 0))120
(assert-equal (length materials) (length (construction-spy-materials)))121
;; Client read failure precedes any packet allocation, but the fresh122
;; unreturned engine must still close rather than remain usable.123
(let ((engine (vector-ref (car engines) 1)))124
(assert-false (tls-complete? engine))125
(assert-equal '() (tls-take-events! engine))126
(assert-true127
(and (construction-owned-zero? keys materials)128
(tls-failed? engine)129
(= 0 (tls-retained-input-bytes engine)))))))))131
(define (construction-normal! defer?)132
(construction-with-key133
(lambda (key snapshot)134
(let ((client #f) (server #f))135
(dynamic-wind136
(lambda () #t)137
(lambda ()138
(set! client (construction-quic 'client key defer?))139
(set! server (construction-quic 'server key #f))140
(let ((keys (construction-check-keys! 1 key snapshot))141
(client-materials (construction-check-materials! 'client '(read write)))142
(server-materials (construction-check-materials! 'server '(read write)))143
(engines (construction-spy-engines)))144
(assert-equal '(client server) (map (lambda (entry) (vector-ref entry 0)) engines))145
(assert-false (construction-zero? (vector-ref (car keys) 1)))146
(for-each147
(lambda (entry)148
(let ((references (vector-ref entry 3)))149
(assert-true (construction-zero? (vector-ref references 0)))150
(do ((i 1 (+ i 1))) ((= i 4))151
(assert-false (construction-zero? (vector-ref references i))))))152
(append client-materials server-materials))153
(for-each (lambda (entry) (assert-false (tls-failed? (vector-ref entry 1)))) engines)154
(quic-start! client 0)155
(do ((i 0 (+ i 1))) ((= i 8))156
(for-each (lambda (wire) (quic-receive! server wire 0))157
(quic-take-datagrams! client 0))158
(for-each (lambda (wire) (quic-receive! client wire 0))159
(quic-take-datagrams! server 0)))160
(for-each161
(lambda (connection)162
(assert-false (quic-failure connection))163
(assert-false (quic-closed? connection))164
(assert-true (quic-complete? connection))165
(assert-true (quic-confirmed? connection))166
(assert-equal #u8(113 49) (quic-negotiated-alpn connection)))167
(list client server))168
(assert-equal snapshot (car key))169
(assert-true (construction-owned-zero? keys (append client-materials server-materials)))))170
(lambda ()171
(when client (quic-abort! client))172
(when server (quic-abort! server))))))))174
(test-group "Unreturned TLS and QUIC constructor ownership"175
(test "ql7-construction: transcript failure clears unreturned signing key"176
(construction-tls-failure! 'tls-transcript))177
(test "ql7-construction: buffer failure clears unreturned signing key"178
(construction-tls-failure! 'tls-buffer))179
(test "ql7-construction: post-buffer failure clears unreturned signing key"180
(construction-tls-failure! 'tls-buffer-after))181
(test "ql7-construction: client Initial read failure aborts unreturned engine"182
(construction-quic-failure! 'client 'initial-read))183
(test "ql7-construction: client Initial write failure clears installed read keys"184
(construction-quic-failure! 'client 'initial-write))185
(test "ql7-construction: server Initial read failure clears signing key"186
(construction-quic-failure! 'server 'initial-read))187
(test "ql7-construction: server Initial write failure clears installed material"188
(construction-quic-failure! 'server 'initial-write))189
(test "ql7-construction: pre-binding failure clears unreturned signing key"190
(construction-quic-failure! 'server 'bind-before))191
(test "ql7-construction: post-binding failure clears unreturned signing key"192
(construction-quic-failure! 'server 'bind-after))193
(test "ql7-construction-control: returned TLS engine owns its distinct signing key"194
(construction-with-key195
(lambda (key snapshot)196
(let ((engine (construction-tls-server key)))197
(dynamic-wind198
(lambda () #t)199
(lambda ()200
(let ((keys (construction-check-keys! 1 key snapshot)))201
(assert-false (tls-failed? engine))202
(assert-false (construction-zero? (vector-ref (car keys) 1)))203
(tls-abort! engine)204
(assert-true (construction-owned-zero? keys '()))205
(assert-equal snapshot (car key))))206
(lambda () (tls-abort! engine)))))))207
(test "ql7-construction-control: invalid key fails before ownership begins"208
(construction-with-key209
(lambda (key snapshot)210
(let ((failure211
(guard (exception (else exception))212
(make-tls-server (list #u8(113 49)) '() (list #u8(48 1 0))213
(make-bytevector 31 7))214
#f)))215
(assert-true (error-object? failure))216
(assert-equal "TLS server: certificate chain and P-256 private key required"217
(error-object-message failure))218
(assert-equal '() (construction-spy-keys))219
(assert-equal '() (construction-spy-engines))220
(assert-equal '() (construction-spy-materials))221
(assert-equal snapshot (car key))))))222
(test "ql7-construction-control: successful connections retain usable returned ownership"223
(construction-normal! #f))224
(test "ql7-construction-control: HRR connections retain usable returned ownership"225
(construction-normal! #t)))