AtlatestRepositorysigil-quic
sigil-quic / tree / testtest-endpoint-network.sgl
1
(import (sigil test)2
(sigil io)3
(sigil socket)4
(sigil time)5
(sigil crypto)6
(sigil quic wire)7
(sigil quic bytes)8
(sigil quic frame)9
(sigil quic packet)10
(sigil quic connection)11
(sigil quic stream)12
(sigil quic endpoint))14
(define en-original #u8(1 2 3 4 5 6 7 8))15
(define en-client-id #u8(11 12 13 14 15 16 17 18))17
;; Public UDP fixture: client, endpoint, primary/alternate sockets, client18
;; stream, returned server/stream, factory calls/IDs, clock origin, signing key.19
;; All sockets and candidate connections remain owned even on assertion errors.20
(define (en-with-pair procedure21
(keys: (retry? #t) (pin? #f) (defer? #f) (lifetime 10000) (bad-factory? #f)))22
(let ((key (ecdsa-p256-generate-keypair)) (client #f) (endpoint #f)23
(primary #f) (alternate #f) (pair #f))24
(dynamic-wind25
(lambda () #t)26
(lambda ()27
(set! client28
(make-quic-client (list #u8(113 49)) "example.test"29
(append (stream-transport-parameters 'client)30
(list (cons 32 (encode-varint 1200))))31
(lambda (certificates name)32
(and (equal? certificates (list #u8(48 1 0))) (cdr key)))33
destination-id: en-original local-id: en-client-id defer-key-share?: defer?))34
(set! endpoint (open-quic-endpoint))35
(set! primary (udp-socket))36
(set! alternate (udp-socket))37
(for-each38
(lambda (socket)39
(unless (and socket (udp-bind socket 0 "127.0.0.1")40
(socket-set-non-blocking! socket #t))41
(error "endpoint Retry fixture: socket setup failed")))42
(list primary alternate))43
(set! pair (vector client endpoint primary alternate (make-quic-stream client)44
#f #f 0 #f (current-jiffy) key #f 0))45
(let ((factory46
(lambda (original source retry)47
(vector-set! pair 7 (+ 1 (vector-ref pair 7)))48
(vector-set! pair 8 (list (bytevector-copy original) (bytevector-copy source)49
(and retry (bytevector-copy retry))))50
(let ((server51
(make-quic-server (list #u8(113 49))52
(append (stream-transport-parameters 'server)53
(list (cons 32 (encode-varint 1200))))54
(list #u8(48 1 0)) (car key)55
original source retry-source-id: (and (not bad-factory?) retry))))56
(vector-set! pair 5 server)57
(vector-set! pair 6 (make-quic-stream server))58
server))))59
(if retry?60
(quic-endpoint-accept-retry! endpoint factory61
peer-port: (and pin? (cadr (socket-local-address primary))) token-lifetime: lifetime)62
(quic-endpoint-accept! endpoint63
(lambda (original source) (factory original source #f)))))64
(quic-bind-peer-address! client (en-address (quic-endpoint-port endpoint)))65
(quic-start! client 0)66
(procedure pair))67
(lambda ()68
(when primary (socket-close primary))69
(when alternate (socket-close alternate))70
(when endpoint (quic-endpoint-close! endpoint))71
(when client (quic-abort! client))72
(when (and pair (vector-ref pair 5)) (quic-abort! (vector-ref pair 5)))73
(do ((index 0 (+ index 1))) ((= index (bytevector-length (car key))))74
(bytevector-u8-set! (car key) index 0))))))76
;; Fields 11/12: active alternate socket?, received alternate datagram count.77
(define (en-address port)78
(bytevector 127 0 0 1 (quotient port 256) (modulo port 256)))80
(define (en-now pair)81
(quotient (* 1000 (- (current-jiffy) (vector-ref pair 9)))82
(jiffies-per-second)))84
(define (en-step! pair)85
(let ((client (vector-ref pair 0))86
(endpoint (vector-ref pair 1))87
(socket (vector-ref pair (if (vector-ref pair 11) 3 2))))88
(quic-tick! client (en-now pair))89
(for-each90
(lambda (output)91
(when (quic-output-valid? client output)92
(let* ((bytes (quic-output-bytes output))93
(sent (udp-send socket bytes "127.0.0.1"94
(quic-endpoint-port endpoint))))95
(unless (and sent (= sent (bytevector-length bytes)))96
(error "endpoint network fixture: UDP send failed")))))97
(quic-take-addressed-datagrams! client (en-now pair)))98
(quic-endpoint-step! endpoint wait-ms: 1)99
;; The old port is unreachable to this simulated NAT-rebound client;100
;; its socket remains owned only for deterministic cleanup.101
(let drain ((count 0))102
(when (< count 64)103
(let ((received (udp-receive socket 65528)))104
(when received105
(unless (and (equal? (cadr received) "127.0.0.1")106
(= (caddr received) (quic-endpoint-port endpoint)))107
(error "endpoint network fixture: unexpected UDP source"))108
(when (vector-ref pair 11)109
(vector-set! pair 12 (+ 1 (vector-ref pair 12))))110
(quic-receive! client (string->utf8 (car received)) (en-now pair)111
address: (en-address (quic-endpoint-port endpoint)))112
(drain (+ count 1))))))))114
(define (en-await! pair predicate)115
(let loop ((round 0))116
(cond117
((predicate) #t)118
((= round 2000) #f)119
(else120
(en-step! pair)121
(loop (+ round 1))))))123
(define (en-ready! pair)124
(unless (en-await! pair125
(lambda ()126
(and (quic-confirmed? (vector-ref pair 0))127
(vector-ref pair 5)128
(quic-confirmed? (vector-ref pair 5)))))129
(error "endpoint network fixture: handshake precondition failed"))130
;; Deliver and ACK connection-owned spare IDs before rebinding.131
(do ((round 0 (+ round 1))) ((= round 12))132
(en-step! pair)))134
(test-group "Addressed loopback UDP endpoint"135
(test "endpoint-network: confirmed endpoint exposes an owned peer address and drains path events"136
(en-with-pair137
(lambda (pair)138
(en-ready! pair)139
(let* ((endpoint (vector-ref pair 1))140
(expected (en-address (cadr (socket-local-address (vector-ref pair 2)))))141
(address (quic-endpoint-peer-address endpoint)))142
(assert-true (bytes=? expected address))143
(bytevector-u8-set! address 0 0)144
(assert-true (bytes=? expected (quic-endpoint-peer-address endpoint)))145
(assert-true (null? (quic-endpoint-take-path-events! endpoint)))))))146
(test "endpoint-network: actual client port rebind preserves stream and DATAGRAM traffic"147
(en-with-pair148
(lambda (pair)149
(en-ready! pair)150
(let* ((endpoint (vector-ref pair 1))151
(client (vector-ref pair 0))152
(server (vector-ref pair 5))153
(old (quic-endpoint-peer-address endpoint))154
(next (en-address (cadr (socket-local-address (vector-ref pair 3)))))155
(request #u8(0 255 1))156
(response #u8(2 0 254))157
(request-read #u8())158
(response-read #u8())159
(request-datagram #f)160
(response-datagram #f))161
(assert-false (bytes=? old next))162
(vector-set! pair 11 #t)163
;; PING selects the candidate; a separate bounded padding packet164
;; supplies actual UDP credit for its1200-byte validation probe.165
(quic-send-frame! client (make-quic-frame 1 '()))166
(quic-send-frame! client167
(make-quic-frame 0 (list (cons 'length 400))))168
(assert-true169
(and170
(en-await! pair171
(lambda () (bytes=? next (quic-endpoint-peer-address endpoint))))172
(= 3 (quic-stream-write! (vector-ref pair 4) request))173
(quic-send-datagram! client #u8(7 0 255))174
(en-await! pair175
(lambda ()176
(set! request-read177
(bytes-append request-read (quic-stream-read! (vector-ref pair 6))))178
(unless request-datagram179
(set! request-datagram (quic-read-datagram! server)))180
(and (bytes=? request request-read) request-datagram)))181
(= 3 (quic-stream-write! (vector-ref pair 6) response))182
(quic-send-datagram! server #u8(8 255 0))183
(en-await! pair184
(lambda ()185
(set! response-read186
(bytes-append response-read (quic-stream-read! (vector-ref pair 4))))187
(unless response-datagram188
(set! response-datagram (quic-read-datagram! client)))189
(and (bytes=? response response-read) response-datagram)))190
(let ((events (quic-endpoint-take-path-events! endpoint)))191
(and (bytes=? #u8(7 0 255) request-datagram)192
(bytes=? #u8(8 255 0) response-datagram)193
(> (vector-ref pair 12) 0)194
(= 1 (vector-ref pair 7))195
(not (quic-closed? client))196
(not (quic-closed? server))197
(any (lambda (event)198
(and (eq? (vector-ref event 0) 'path-changed)199
(bytes=? next (vector-ref event 1))))200
events)201
(any (lambda (event)202
(and (eq? (vector-ref event 0) 'path-validated)203
(bytes=? next (vector-ref event 1))))204
events)205
(null? (quic-endpoint-take-path-events! endpoint)))))))))))