frame_parser_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package wire
  2. import (
  3. "bytes"
  4. "github.com/lucas-clemente/quic-go/internal/protocol"
  5. "github.com/lucas-clemente/quic-go/qerr"
  6. . "github.com/onsi/ginkgo"
  7. . "github.com/onsi/gomega"
  8. )
  9. var _ = Describe("Frame parsing", func() {
  10. var buf *bytes.Buffer
  11. BeforeEach(func() {
  12. buf = &bytes.Buffer{}
  13. })
  14. It("returns nil if there's nothing more to read", func() {
  15. f, err := ParseNextFrame(bytes.NewReader(nil), nil, protocol.VersionWhatever)
  16. Expect(err).ToNot(HaveOccurred())
  17. Expect(f).To(BeNil())
  18. })
  19. It("skips PADDING frames", func() {
  20. buf.Write([]byte{0}) // PADDING frame
  21. (&PingFrame{}).Write(buf, versionIETFFrames)
  22. f, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  23. Expect(err).ToNot(HaveOccurred())
  24. Expect(f).To(Equal(&PingFrame{}))
  25. })
  26. It("handles PADDING at the end", func() {
  27. r := bytes.NewReader([]byte{0, 0, 0})
  28. f, err := ParseNextFrame(r, nil, versionIETFFrames)
  29. Expect(err).ToNot(HaveOccurred())
  30. Expect(f).To(BeNil())
  31. Expect(r.Len()).To(BeZero())
  32. })
  33. Context("for gQUIC frames", func() {
  34. It("unpacks RST_STREAM frames", func() {
  35. f := &RstStreamFrame{
  36. StreamID: 0xdeadbeef,
  37. ByteOffset: 0xdecafbad11223344,
  38. ErrorCode: 0x1337,
  39. }
  40. err := f.Write(buf, versionBigEndian)
  41. Expect(err).ToNot(HaveOccurred())
  42. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  43. Expect(err).ToNot(HaveOccurred())
  44. Expect(frame).To(Equal(f))
  45. })
  46. It("unpacks CONNECTION_CLOSE frames", func() {
  47. f := &ConnectionCloseFrame{ReasonPhrase: "foo"}
  48. err := f.Write(buf, versionBigEndian)
  49. Expect(err).ToNot(HaveOccurred())
  50. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  51. Expect(err).ToNot(HaveOccurred())
  52. Expect(frame).To(Equal(f))
  53. })
  54. It("unpacks GOAWAY frames", func() {
  55. f := &GoawayFrame{
  56. ErrorCode: 1,
  57. LastGoodStream: 2,
  58. ReasonPhrase: "foo",
  59. }
  60. err := f.Write(buf, 0)
  61. Expect(err).ToNot(HaveOccurred())
  62. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  63. Expect(err).ToNot(HaveOccurred())
  64. Expect(frame).To(Equal(f))
  65. })
  66. It("unpacks a stream-level WINDOW_UPDATE frame", func() {
  67. f := &MaxStreamDataFrame{
  68. StreamID: 0xdeadbeef,
  69. ByteOffset: 0xcafe000000001337,
  70. }
  71. buf := &bytes.Buffer{}
  72. err := f.Write(buf, versionBigEndian)
  73. Expect(err).ToNot(HaveOccurred())
  74. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  75. Expect(err).ToNot(HaveOccurred())
  76. Expect(frame).To(Equal(f))
  77. })
  78. It("unpacks a connection-level WINDOW_UPDATE frame", func() {
  79. f := &MaxDataFrame{
  80. ByteOffset: 0xcafe000000001337,
  81. }
  82. err := f.Write(buf, versionBigEndian)
  83. Expect(err).ToNot(HaveOccurred())
  84. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  85. Expect(err).ToNot(HaveOccurred())
  86. Expect(frame).To(Equal(f))
  87. })
  88. It("unpacks connection-level BLOCKED frames", func() {
  89. f := &BlockedFrame{}
  90. err := f.Write(buf, versionBigEndian)
  91. Expect(err).ToNot(HaveOccurred())
  92. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  93. Expect(err).ToNot(HaveOccurred())
  94. Expect(frame).To(Equal(f))
  95. })
  96. It("unpacks stream-level BLOCKED frames", func() {
  97. f := &StreamBlockedFrame{StreamID: 0xdeadbeef}
  98. err := f.Write(buf, versionBigEndian)
  99. Expect(err).ToNot(HaveOccurred())
  100. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  101. Expect(err).ToNot(HaveOccurred())
  102. Expect(frame).To(Equal(f))
  103. })
  104. It("unpacks STOP_WAITING frames", func() {
  105. hdr := &Header{
  106. PacketNumber: 0x1338,
  107. PacketNumberLen: protocol.PacketNumberLen4,
  108. }
  109. f := &StopWaitingFrame{
  110. LeastUnacked: 0x1337,
  111. PacketNumber: hdr.PacketNumber,
  112. PacketNumberLen: hdr.PacketNumberLen,
  113. }
  114. err := f.Write(buf, versionBigEndian)
  115. Expect(err).ToNot(HaveOccurred())
  116. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), hdr, versionBigEndian)
  117. Expect(err).ToNot(HaveOccurred())
  118. Expect(frame).To(BeAssignableToTypeOf(f))
  119. Expect(frame.(*StopWaitingFrame).LeastUnacked).To(Equal(protocol.PacketNumber(0x1337)))
  120. })
  121. It("errors on STOP_WAITING frames in QUIC 44", func() {
  122. Expect(protocol.Version44.UsesStopWaitingFrames()).To(BeFalse())
  123. hdr := &Header{
  124. PacketNumber: 0x1338,
  125. PacketNumberLen: protocol.PacketNumberLen4,
  126. }
  127. f := &StopWaitingFrame{
  128. LeastUnacked: 0x1337,
  129. PacketNumber: hdr.PacketNumber,
  130. PacketNumberLen: hdr.PacketNumberLen,
  131. }
  132. err := f.Write(buf, versionBigEndian)
  133. Expect(err).ToNot(HaveOccurred())
  134. _, err = ParseNextFrame(bytes.NewReader(buf.Bytes()), hdr, protocol.Version44)
  135. Expect(err).To(MatchError("STOP_WAITING frames not supported by this QUIC version"))
  136. })
  137. It("unpacks PING frames", func() {
  138. f := &PingFrame{}
  139. err := f.Write(buf, versionBigEndian)
  140. Expect(err).ToNot(HaveOccurred())
  141. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  142. Expect(err).ToNot(HaveOccurred())
  143. Expect(frame).To(Equal(f))
  144. })
  145. It("unpacks ACK frames", func() {
  146. f := &AckFrame{AckRanges: []AckRange{{Smallest: 1, Largest: 0x13}}}
  147. err := f.Write(buf, versionBigEndian)
  148. Expect(err).ToNot(HaveOccurred())
  149. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionBigEndian)
  150. Expect(err).ToNot(HaveOccurred())
  151. Expect(frame).ToNot(BeNil())
  152. Expect(frame).To(BeAssignableToTypeOf(f))
  153. Expect(frame.(*AckFrame).LargestAcked()).To(Equal(protocol.PacketNumber(0x13)))
  154. })
  155. It("errors on invalid type", func() {
  156. _, err := ParseNextFrame(bytes.NewReader([]byte{0xf}), nil, versionBigEndian)
  157. Expect(err).To(MatchError("InvalidFrameData: unknown type byte 0xf"))
  158. })
  159. It("errors on invalid frames", func() {
  160. for b, e := range map[byte]qerr.ErrorCode{
  161. 0x80: qerr.InvalidStreamData,
  162. 0x40: qerr.InvalidAckData,
  163. 0x01: qerr.InvalidRstStreamData,
  164. 0x02: qerr.InvalidConnectionCloseData,
  165. 0x03: qerr.InvalidGoawayData,
  166. 0x04: qerr.InvalidWindowUpdateData,
  167. 0x05: qerr.InvalidBlockedData,
  168. 0x06: qerr.InvalidStopWaitingData,
  169. } {
  170. _, err := ParseNextFrame(bytes.NewReader([]byte{b}), &Header{PacketNumberLen: 2}, versionBigEndian)
  171. Expect(err).To(HaveOccurred())
  172. Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(e))
  173. }
  174. })
  175. })
  176. Context("for IETF draft frames", func() {
  177. It("unpacks RST_STREAM frames", func() {
  178. f := &RstStreamFrame{
  179. StreamID: 0xdeadbeef,
  180. ByteOffset: 0xdecafbad1234,
  181. ErrorCode: 0x1337,
  182. }
  183. err := f.Write(buf, versionIETFFrames)
  184. Expect(err).ToNot(HaveOccurred())
  185. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  186. Expect(err).ToNot(HaveOccurred())
  187. Expect(frame).To(Equal(f))
  188. })
  189. It("unpacks CONNECTION_CLOSE frames", func() {
  190. f := &ConnectionCloseFrame{ReasonPhrase: "foo"}
  191. err := f.Write(buf, versionIETFFrames)
  192. Expect(err).ToNot(HaveOccurred())
  193. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  194. Expect(err).ToNot(HaveOccurred())
  195. Expect(frame).To(Equal(f))
  196. })
  197. It("unpacks MAX_DATA frames", func() {
  198. f := &MaxDataFrame{
  199. ByteOffset: 0xcafe,
  200. }
  201. buf := &bytes.Buffer{}
  202. err := f.Write(buf, versionIETFFrames)
  203. Expect(err).ToNot(HaveOccurred())
  204. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  205. Expect(err).ToNot(HaveOccurred())
  206. Expect(frame).To(Equal(f))
  207. })
  208. It("unpacks MAX_STREAM_DATA frames", func() {
  209. f := &MaxStreamDataFrame{
  210. StreamID: 0xdeadbeef,
  211. ByteOffset: 0xdecafbad,
  212. }
  213. buf := &bytes.Buffer{}
  214. err := f.Write(buf, versionIETFFrames)
  215. Expect(err).ToNot(HaveOccurred())
  216. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  217. Expect(err).ToNot(HaveOccurred())
  218. Expect(frame).To(Equal(f))
  219. })
  220. It("unpacks MAX_STREAM_ID frames", func() {
  221. f := &MaxStreamIDFrame{StreamID: 0x1337}
  222. buf := &bytes.Buffer{}
  223. err := f.Write(buf, versionIETFFrames)
  224. Expect(err).ToNot(HaveOccurred())
  225. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  226. Expect(err).ToNot(HaveOccurred())
  227. Expect(frame).To(Equal(f))
  228. })
  229. It("unpacks connection-level BLOCKED frames", func() {
  230. f := &BlockedFrame{Offset: 0x1234}
  231. buf := &bytes.Buffer{}
  232. err := f.Write(buf, versionIETFFrames)
  233. Expect(err).ToNot(HaveOccurred())
  234. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  235. Expect(err).ToNot(HaveOccurred())
  236. Expect(frame).To(Equal(f))
  237. })
  238. It("unpacks stream-level BLOCKED frames", func() {
  239. f := &StreamBlockedFrame{
  240. StreamID: 0xdeadbeef,
  241. Offset: 0xdead,
  242. }
  243. buf := &bytes.Buffer{}
  244. err := f.Write(buf, versionIETFFrames)
  245. Expect(err).ToNot(HaveOccurred())
  246. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  247. Expect(err).ToNot(HaveOccurred())
  248. Expect(frame).To(Equal(f))
  249. })
  250. It("unpacks STREAM_ID_BLOCKED frames", func() {
  251. f := &StreamIDBlockedFrame{StreamID: 0x1234567}
  252. buf := &bytes.Buffer{}
  253. err := f.Write(buf, versionIETFFrames)
  254. Expect(err).ToNot(HaveOccurred())
  255. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  256. Expect(err).ToNot(HaveOccurred())
  257. Expect(frame).To(Equal(f))
  258. })
  259. It("unpacks STOP_SENDING frames", func() {
  260. f := &StopSendingFrame{StreamID: 0x42}
  261. buf := &bytes.Buffer{}
  262. err := f.Write(buf, versionIETFFrames)
  263. Expect(err).ToNot(HaveOccurred())
  264. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  265. Expect(err).ToNot(HaveOccurred())
  266. Expect(frame).To(Equal(f))
  267. })
  268. It("unpacks ACK frames", func() {
  269. f := &AckFrame{AckRanges: []AckRange{{Smallest: 1, Largest: 0x13}}}
  270. err := f.Write(buf, versionIETFFrames)
  271. Expect(err).ToNot(HaveOccurred())
  272. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  273. Expect(err).ToNot(HaveOccurred())
  274. Expect(frame).ToNot(BeNil())
  275. Expect(frame).To(BeAssignableToTypeOf(f))
  276. Expect(frame.(*AckFrame).LargestAcked()).To(Equal(protocol.PacketNumber(0x13)))
  277. })
  278. It("unpacks PATH_CHALLENGE frames", func() {
  279. f := &PathChallengeFrame{Data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}}
  280. err := f.Write(buf, versionIETFFrames)
  281. Expect(err).ToNot(HaveOccurred())
  282. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  283. Expect(err).ToNot(HaveOccurred())
  284. Expect(frame).ToNot(BeNil())
  285. Expect(frame).To(BeAssignableToTypeOf(f))
  286. Expect(frame.(*PathChallengeFrame).Data).To(Equal([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
  287. })
  288. It("unpacks PATH_RESPONSE frames", func() {
  289. f := &PathResponseFrame{Data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}}
  290. err := f.Write(buf, versionIETFFrames)
  291. Expect(err).ToNot(HaveOccurred())
  292. frame, err := ParseNextFrame(bytes.NewReader(buf.Bytes()), nil, versionIETFFrames)
  293. Expect(err).ToNot(HaveOccurred())
  294. Expect(frame).ToNot(BeNil())
  295. Expect(frame).To(BeAssignableToTypeOf(f))
  296. Expect(frame.(*PathResponseFrame).Data).To(Equal([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
  297. })
  298. It("errors on invalid type", func() {
  299. _, err := ParseNextFrame(bytes.NewReader([]byte{0x42}), nil, versionIETFFrames)
  300. Expect(err).To(MatchError("InvalidFrameData: unknown type byte 0x42"))
  301. })
  302. It("errors on invalid frames", func() {
  303. for b, e := range map[byte]qerr.ErrorCode{
  304. 0x01: qerr.InvalidRstStreamData,
  305. 0x02: qerr.InvalidConnectionCloseData,
  306. 0x04: qerr.InvalidWindowUpdateData,
  307. 0x05: qerr.InvalidWindowUpdateData,
  308. 0x06: qerr.InvalidFrameData,
  309. 0x08: qerr.InvalidBlockedData,
  310. 0x09: qerr.InvalidBlockedData,
  311. 0x0a: qerr.InvalidFrameData,
  312. 0x0c: qerr.InvalidFrameData,
  313. 0x0d: qerr.InvalidAckData,
  314. 0x0e: qerr.InvalidFrameData,
  315. 0x0f: qerr.InvalidFrameData,
  316. 0x10: qerr.InvalidStreamData,
  317. 0x1a: qerr.InvalidAckData,
  318. } {
  319. _, err := ParseNextFrame(bytes.NewReader([]byte{b}), nil, versionIETFFrames)
  320. Expect(err).To(HaveOccurred())
  321. Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(e))
  322. }
  323. })
  324. })
  325. })