github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/internal/utils/byteoder_big_endian_test.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "io" 6 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("Big Endian encoding / decoding", func() { 12 Context("converting", func() { 13 It("Uint16", func() { 14 b := []byte{0x13, 0x37} 15 Expect(BigEndian.Uint16(b)).To(Equal(uint16(0x1337))) 16 }) 17 18 It("Uint24", func() { 19 b := []byte{0x13, 0x99, 0x37} 20 Expect(BigEndian.Uint24(b)).To(Equal(uint32(0x139937))) 21 }) 22 23 It("Uint32", func() { 24 b := []byte{0xde, 0xad, 0xbe, 0xef} 25 Expect(BigEndian.Uint32(b)).To(Equal(uint32(0xdeadbeef))) 26 }) 27 }) 28 29 Context("ReadUint16", func() { 30 It("reads a big endian", func() { 31 b := []byte{0x13, 0xEF} 32 val, err := BigEndian.ReadUint16(bytes.NewReader(b)) 33 Expect(err).ToNot(HaveOccurred()) 34 Expect(val).To(Equal(uint16(0x13EF))) 35 }) 36 37 It("throws an error if less than 2 bytes are passed", func() { 38 b := []byte{0x13, 0xEF} 39 for i := 0; i < len(b); i++ { 40 _, err := BigEndian.ReadUint16(bytes.NewReader(b[:i])) 41 Expect(err).To(MatchError(io.EOF)) 42 } 43 }) 44 }) 45 46 Context("ReadUint24", func() { 47 It("reads a big endian", func() { 48 b := []byte{0x13, 0xbe, 0xef} 49 val, err := BigEndian.ReadUint24(bytes.NewReader(b)) 50 Expect(err).ToNot(HaveOccurred()) 51 Expect(val).To(Equal(uint32(0x13beef))) 52 }) 53 54 It("throws an error if less than 3 bytes are passed", func() { 55 b := []byte{0x13, 0xbe, 0xef} 56 for i := 0; i < len(b); i++ { 57 _, err := BigEndian.ReadUint24(bytes.NewReader(b[:i])) 58 Expect(err).To(MatchError(io.EOF)) 59 } 60 }) 61 }) 62 63 Context("ReadUint32", func() { 64 It("reads a big endian", func() { 65 b := []byte{0x12, 0x35, 0xAB, 0xFF} 66 val, err := BigEndian.ReadUint32(bytes.NewReader(b)) 67 Expect(err).ToNot(HaveOccurred()) 68 Expect(val).To(Equal(uint32(0x1235ABFF))) 69 }) 70 71 It("throws an error if less than 4 bytes are passed", func() { 72 b := []byte{0x12, 0x35, 0xAB, 0xFF} 73 for i := 0; i < len(b); i++ { 74 _, err := BigEndian.ReadUint32(bytes.NewReader(b[:i])) 75 Expect(err).To(MatchError(io.EOF)) 76 } 77 }) 78 }) 79 80 Context("WriteUint16", func() { 81 It("outputs 2 bytes", func() { 82 b := &bytes.Buffer{} 83 BigEndian.WriteUint16(b, uint16(1)) 84 Expect(b.Len()).To(Equal(2)) 85 }) 86 87 It("outputs a big endian", func() { 88 num := uint16(0xFF11) 89 b := &bytes.Buffer{} 90 BigEndian.WriteUint16(b, num) 91 Expect(b.Bytes()).To(Equal([]byte{0xFF, 0x11})) 92 }) 93 }) 94 95 Context("WriteUint24", func() { 96 It("outputs 3 bytes", func() { 97 b := &bytes.Buffer{} 98 BigEndian.WriteUint24(b, uint32(1)) 99 Expect(b.Len()).To(Equal(3)) 100 }) 101 102 It("outputs a big endian", func() { 103 num := uint32(0xff11aa) 104 b := &bytes.Buffer{} 105 BigEndian.WriteUint24(b, num) 106 Expect(b.Bytes()).To(Equal([]byte{0xff, 0x11, 0xaa})) 107 }) 108 }) 109 110 Context("WriteUint32", func() { 111 It("outputs 4 bytes", func() { 112 b := &bytes.Buffer{} 113 BigEndian.WriteUint32(b, uint32(1)) 114 Expect(b.Len()).To(Equal(4)) 115 }) 116 117 It("outputs a big endian", func() { 118 num := uint32(0xEFAC3512) 119 b := &bytes.Buffer{} 120 BigEndian.WriteUint32(b, num) 121 Expect(b.Bytes()).To(Equal([]byte{0xEF, 0xAC, 0x35, 0x12})) 122 }) 123 }) 124 })