github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/integrationtests/self/conn_id_test.go (about) 1 package self_test 2 3 import ( 4 "context" 5 "crypto/rand" 6 "fmt" 7 "io" 8 mrand "math/rand" 9 "net" 10 11 "github.com/TugasAkhir-QUIC/quic-go" 12 "github.com/TugasAkhir-QUIC/quic-go/internal/protocol" 13 14 . "github.com/onsi/ginkgo/v2" 15 . "github.com/onsi/gomega" 16 ) 17 18 type connIDGenerator struct { 19 length int 20 } 21 22 func (c *connIDGenerator) GenerateConnectionID() (quic.ConnectionID, error) { 23 b := make([]byte, c.length) 24 if _, err := rand.Read(b); err != nil { 25 fmt.Fprintf(GinkgoWriter, "generating conn ID failed: %s", err) 26 } 27 return protocol.ParseConnectionID(b), nil 28 } 29 30 func (c *connIDGenerator) ConnectionIDLen() int { 31 return c.length 32 } 33 34 var _ = Describe("Connection ID lengths tests", func() { 35 randomConnIDLen := func() int { return 4 + int(mrand.Int31n(15)) } 36 37 // connIDLen is ignored when connIDGenerator is set 38 runServer := func(connIDLen int, connIDGenerator quic.ConnectionIDGenerator) (*quic.Listener, func()) { 39 if connIDGenerator != nil { 40 GinkgoWriter.Write([]byte(fmt.Sprintf("Using %d byte connection ID generator for the server\n", connIDGenerator.ConnectionIDLen()))) 41 } else { 42 GinkgoWriter.Write([]byte(fmt.Sprintf("Using %d byte connection ID for the server\n", connIDLen))) 43 } 44 addr, err := net.ResolveUDPAddr("udp", "localhost:0") 45 Expect(err).ToNot(HaveOccurred()) 46 conn, err := net.ListenUDP("udp", addr) 47 Expect(err).ToNot(HaveOccurred()) 48 tr := &quic.Transport{ 49 Conn: conn, 50 ConnectionIDLength: connIDLen, 51 ConnectionIDGenerator: connIDGenerator, 52 } 53 ln, err := tr.Listen(getTLSConfig(), getQuicConfig(nil)) 54 Expect(err).ToNot(HaveOccurred()) 55 go func() { 56 defer GinkgoRecover() 57 for { 58 conn, err := ln.Accept(context.Background()) 59 if err != nil { 60 return 61 } 62 go func() { 63 defer GinkgoRecover() 64 str, err := conn.OpenStream() 65 Expect(err).ToNot(HaveOccurred()) 66 defer str.Close() 67 _, err = str.Write(PRData) 68 Expect(err).ToNot(HaveOccurred()) 69 }() 70 } 71 }() 72 return ln, func() { 73 ln.Close() 74 tr.Close() 75 } 76 } 77 78 // connIDLen is ignored when connIDGenerator is set 79 runClient := func(addr net.Addr, connIDLen int, connIDGenerator quic.ConnectionIDGenerator) { 80 if connIDGenerator != nil { 81 GinkgoWriter.Write([]byte(fmt.Sprintf("Using %d byte connection ID generator for the client\n", connIDGenerator.ConnectionIDLen()))) 82 } else { 83 GinkgoWriter.Write([]byte(fmt.Sprintf("Using %d byte connection ID for the client\n", connIDLen))) 84 } 85 laddr, err := net.ResolveUDPAddr("udp", "localhost:0") 86 Expect(err).ToNot(HaveOccurred()) 87 conn, err := net.ListenUDP("udp", laddr) 88 Expect(err).ToNot(HaveOccurred()) 89 defer conn.Close() 90 tr := &quic.Transport{ 91 Conn: conn, 92 ConnectionIDLength: connIDLen, 93 ConnectionIDGenerator: connIDGenerator, 94 } 95 defer tr.Close() 96 cl, err := tr.Dial( 97 context.Background(), 98 &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: addr.(*net.UDPAddr).Port}, 99 getTLSClientConfig(), 100 getQuicConfig(nil), 101 ) 102 Expect(err).ToNot(HaveOccurred()) 103 defer cl.CloseWithError(0, "") 104 str, err := cl.AcceptStream(context.Background()) 105 Expect(err).ToNot(HaveOccurred()) 106 data, err := io.ReadAll(str) 107 Expect(err).ToNot(HaveOccurred()) 108 Expect(data).To(Equal(PRData)) 109 } 110 111 It("downloads a file using a 0-byte connection ID for the client", func() { 112 ln, closeFn := runServer(randomConnIDLen(), nil) 113 defer closeFn() 114 runClient(ln.Addr(), 0, nil) 115 }) 116 117 It("downloads a file when both client and server use a random connection ID length", func() { 118 ln, closeFn := runServer(randomConnIDLen(), nil) 119 defer closeFn() 120 runClient(ln.Addr(), randomConnIDLen(), nil) 121 }) 122 123 It("downloads a file when both client and server use a custom connection ID generator", func() { 124 ln, closeFn := runServer(0, &connIDGenerator{length: randomConnIDLen()}) 125 defer closeFn() 126 runClient(ln.Addr(), 0, &connIDGenerator{length: randomConnIDLen()}) 127 }) 128 })