|
| 1 | +import std/[random, sets] |
| 2 | +import chronos |
| 3 | +import quic/transport/[quicconnection, quicclientserver, tlsbackend] |
| 4 | +import quic/helpers/[asyncloop, rand] |
| 5 | +import ./certificate |
| 6 | +import ./addresses |
| 7 | + |
| 8 | +proc networkLoop*(source, destination: QuicConnectionection) {.async.} = |
| 9 | + proc transfer() {.async: (raises: [CancelledError]).} = |
| 10 | + let datagram = await source.outgoing.get() |
| 11 | + try: |
| 12 | + destination.receive(datagram) |
| 13 | + except CatchableError as e: |
| 14 | + # this is used in tests so it's fine to raise defect |
| 15 | + raise newException(Defect, e.msg) |
| 16 | + |
| 17 | + await asyncLoop(transfer) |
| 18 | + |
| 19 | +proc simulateNetwork*(a, b: QuicConnectionection) {.async.} = |
| 20 | + let loop1 = networkLoop(a, b) |
| 21 | + let loop2 = networkLoop(b, a) |
| 22 | + try: |
| 23 | + await allFutures(loop1, loop2) |
| 24 | + except CancelledError: |
| 25 | + await allFutures(loop1.cancelAndWait(), loop2.cancelAndWait()) |
| 26 | + |
| 27 | +proc lossyNetworkLoop*(source, destination: QuicConnectionection) {.async.} = |
| 28 | + proc transfer() {.async: (raises: [CancelledError]).} = |
| 29 | + let datagram = await source.outgoing.get() |
| 30 | + if rand(1.0) < 0.2: |
| 31 | + try: |
| 32 | + destination.receive(datagram) |
| 33 | + except CatchableError as e: |
| 34 | + # this is used in tests so it's fine to raise defect |
| 35 | + raise newException(Defect, e.msg) |
| 36 | + |
| 37 | + await asyncLoop(transfer) |
| 38 | + |
| 39 | +proc simulateLossyNetwork*(a, b: QuicConnectionection) {.async.} = |
| 40 | + let loop1 = lossyNetworkLoop(a, b) |
| 41 | + let loop2 = lossyNetworkLoop(b, a) |
| 42 | + try: |
| 43 | + await allFutures(loop1, loop2) |
| 44 | + except CancelledError: |
| 45 | + await allFutures(loop1.cancelAndWait(), loop2.cancelAndWait()) |
| 46 | + |
| 47 | +proc setupConnection*(): Future[tuple[client, server: QuicConnectionection]] {.async.} = |
| 48 | + let rng = newRng() |
| 49 | + let clientTLSBackend = |
| 50 | + newClientTLSBackend(@[], @[], toHashSet(@["test"]), Opt.none(CertificateVerifier)) |
| 51 | + let client = newQuicClientConnection(clientTLSBackend, zeroAddress, zeroAddress, rng) |
| 52 | + |
| 53 | + client.send() # Start Handshake |
| 54 | + let datagram = await client.outgoing.get() |
| 55 | + let serverTLSBackend = newServerTLSBackend( |
| 56 | + testCertificate(), |
| 57 | + testPrivateKey(), |
| 58 | + toHashSet(@["test"]), |
| 59 | + Opt.none(CertificateVerifier), |
| 60 | + ) |
| 61 | + let server = |
| 62 | + newQuicServerConnection(serverTLSBackend, zeroAddress, zeroAddress, datagram, rng) |
| 63 | + |
| 64 | + result = (client, server) |
| 65 | + |
| 66 | +proc performHandshake*(): Future[tuple[client, server: QuicConnectionection]] {.async.} = |
| 67 | + let (client, server) = await setupConnection() |
| 68 | + let clientLoop = networkLoop(client, server) |
| 69 | + let serverLoop = networkLoop(server, client) |
| 70 | + await allFutures(client.handshake.wait(), server.handshake.wait()) |
| 71 | + await serverLoop.cancelAndWait() |
| 72 | + await clientLoop.cancelAndWait() |
| 73 | + result = (client, server) |
0 commit comments