frame_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "bytes"
  7. "reflect"
  8. "testing"
  9. )
  10. func testFramer() (*Framer, *bytes.Buffer) {
  11. buf := new(bytes.Buffer)
  12. return NewFramer(buf, buf), buf
  13. }
  14. func TestWriteRST(t *testing.T) {
  15. fr, buf := testFramer()
  16. var streamID uint32 = 1<<24 + 2<<16 + 3<<8 + 4
  17. var errCode uint32 = 7<<24 + 6<<16 + 5<<8 + 4
  18. fr.WriteRSTStream(streamID, errCode)
  19. const wantEnc = "\x00\x00\x04\x03\x00\x01\x02\x03\x04\x07\x06\x05\x04"
  20. if buf.String() != wantEnc {
  21. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  22. }
  23. f, err := fr.ReadFrame()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. want := &RSTStreamFrame{
  28. FrameHeader: FrameHeader{
  29. valid: true,
  30. Type: 0x3,
  31. Flags: 0x0,
  32. Length: 0x4,
  33. StreamID: 0x1020304,
  34. },
  35. ErrCode: 0x7060504,
  36. }
  37. if !reflect.DeepEqual(f, want) {
  38. t.Errorf("parsed back %#v; want %#v", f, want)
  39. }
  40. }
  41. func TestWriteData(t *testing.T) {
  42. fr, buf := testFramer()
  43. var streamID uint32 = 1<<24 + 2<<16 + 3<<8 + 4
  44. data := []byte("ABC")
  45. fr.WriteData(streamID, true, data)
  46. const wantEnc = "\x00\x00\x03\x00\x01\x01\x02\x03\x04ABC"
  47. if buf.String() != wantEnc {
  48. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  49. }
  50. f, err := fr.ReadFrame()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. df, ok := f.(*DataFrame)
  55. if !ok {
  56. t.Fatalf("got %T; want *DataFrame", f)
  57. }
  58. if !bytes.Equal(df.Data(), data) {
  59. t.Errorf("got %q; want %q", df.Data, data)
  60. }
  61. if f.Header().Flags&1 == 0 {
  62. t.Errorf("didn't see END_STREAM flag")
  63. }
  64. }
  65. func TestWriteHeaders(t *testing.T) {
  66. tests := []struct {
  67. name string
  68. p HeadersFrameParam
  69. wantEnc string
  70. wantFrame *HeadersFrame
  71. }{
  72. {
  73. "basic",
  74. HeadersFrameParam{
  75. StreamID: 42,
  76. BlockFragment: []byte("abc"),
  77. Priority: PriorityParam{},
  78. },
  79. "\x00\x00\x03\x01\x00\x00\x00\x00*abc",
  80. &HeadersFrame{
  81. FrameHeader: FrameHeader{
  82. valid: true,
  83. StreamID: 42,
  84. Type: FrameHeaders,
  85. Length: uint32(len("abc")),
  86. },
  87. Priority: PriorityParam{},
  88. headerFragBuf: []byte("abc"),
  89. },
  90. },
  91. {
  92. "basic + end flags",
  93. HeadersFrameParam{
  94. StreamID: 42,
  95. BlockFragment: []byte("abc"),
  96. EndStream: true,
  97. EndHeaders: true,
  98. Priority: PriorityParam{},
  99. },
  100. "\x00\x00\x03\x01\x05\x00\x00\x00*abc",
  101. &HeadersFrame{
  102. FrameHeader: FrameHeader{
  103. valid: true,
  104. StreamID: 42,
  105. Type: FrameHeaders,
  106. Flags: FlagHeadersEndStream | FlagHeadersEndHeaders,
  107. Length: uint32(len("abc")),
  108. },
  109. Priority: PriorityParam{},
  110. headerFragBuf: []byte("abc"),
  111. },
  112. },
  113. {
  114. "with padding",
  115. HeadersFrameParam{
  116. StreamID: 42,
  117. BlockFragment: []byte("abc"),
  118. EndStream: true,
  119. EndHeaders: true,
  120. PadLength: 5,
  121. Priority: PriorityParam{},
  122. },
  123. "\x00\x00\t\x01\r\x00\x00\x00*\x05abc\x00\x00\x00\x00\x00",
  124. &HeadersFrame{
  125. FrameHeader: FrameHeader{
  126. valid: true,
  127. StreamID: 42,
  128. Type: FrameHeaders,
  129. Flags: FlagHeadersEndStream | FlagHeadersEndHeaders | FlagHeadersPadded,
  130. Length: uint32(1 + len("abc") + 5), // pad length + contents + padding
  131. },
  132. Priority: PriorityParam{},
  133. headerFragBuf: []byte("abc"),
  134. },
  135. },
  136. {
  137. "with priority",
  138. HeadersFrameParam{
  139. StreamID: 42,
  140. BlockFragment: []byte("abc"),
  141. EndStream: true,
  142. EndHeaders: true,
  143. PadLength: 2,
  144. Priority: PriorityParam{
  145. StreamDep: 15,
  146. Exclusive: true,
  147. Weight: 127,
  148. },
  149. },
  150. "\x00\x00\v\x01-\x00\x00\x00*\x02\x80\x00\x00\x0f\u007fabc\x00\x00",
  151. &HeadersFrame{
  152. FrameHeader: FrameHeader{
  153. valid: true,
  154. StreamID: 42,
  155. Type: FrameHeaders,
  156. Flags: FlagHeadersEndStream | FlagHeadersEndHeaders | FlagHeadersPadded | FlagHeadersPriority,
  157. Length: uint32(1 + 5 + len("abc") + 2), // pad length + priority + contents + padding
  158. },
  159. Priority: PriorityParam{
  160. StreamDep: 15,
  161. Exclusive: true,
  162. Weight: 127,
  163. },
  164. headerFragBuf: []byte("abc"),
  165. },
  166. },
  167. }
  168. for _, tt := range tests {
  169. fr, buf := testFramer()
  170. if err := fr.WriteHeaders(tt.p); err != nil {
  171. t.Errorf("test %q: %v", tt.name, err)
  172. continue
  173. }
  174. if buf.String() != tt.wantEnc {
  175. t.Errorf("test %q: encoded %q; want %q", tt.name, buf.Bytes(), tt.wantEnc)
  176. }
  177. f, err := fr.ReadFrame()
  178. if err != nil {
  179. t.Errorf("test %q: failed to read the frame back: %v", tt.name, err)
  180. continue
  181. }
  182. if !reflect.DeepEqual(f, tt.wantFrame) {
  183. t.Errorf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tt.name, f, tt.wantFrame)
  184. }
  185. }
  186. }
  187. func TestWriteContinuation(t *testing.T) {
  188. const streamID = 42
  189. tests := []struct {
  190. name string
  191. end bool
  192. frag []byte
  193. wantFrame *ContinuationFrame
  194. }{
  195. {
  196. "not end",
  197. false,
  198. []byte("abc"),
  199. &ContinuationFrame{
  200. FrameHeader: FrameHeader{
  201. valid: true,
  202. StreamID: streamID,
  203. Type: FrameContinuation,
  204. Length: uint32(len("abc")),
  205. },
  206. headerFragBuf: []byte("abc"),
  207. },
  208. },
  209. {
  210. "end",
  211. true,
  212. []byte("def"),
  213. &ContinuationFrame{
  214. FrameHeader: FrameHeader{
  215. valid: true,
  216. StreamID: streamID,
  217. Type: FrameContinuation,
  218. Flags: FlagContinuationEndHeaders,
  219. Length: uint32(len("def")),
  220. },
  221. headerFragBuf: []byte("def"),
  222. },
  223. },
  224. }
  225. for _, tt := range tests {
  226. fr, _ := testFramer()
  227. if err := fr.WriteContinuation(streamID, tt.end, tt.frag); err != nil {
  228. t.Errorf("test %q: %v", tt.name, err)
  229. continue
  230. }
  231. f, err := fr.ReadFrame()
  232. if err != nil {
  233. t.Errorf("test %q: failed to read the frame back: %v", tt.name, err)
  234. continue
  235. }
  236. if !reflect.DeepEqual(f, tt.wantFrame) {
  237. t.Errorf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tt.name, f, tt.wantFrame)
  238. }
  239. }
  240. }
  241. func TestWritePriority(t *testing.T) {
  242. const streamID = 42
  243. tests := []struct {
  244. name string
  245. priority PriorityParam
  246. wantFrame *PriorityFrame
  247. }{
  248. {
  249. "not exclusive",
  250. PriorityParam{
  251. StreamDep: 2,
  252. Exclusive: false,
  253. Weight: 127,
  254. },
  255. &PriorityFrame{
  256. FrameHeader{
  257. valid: true,
  258. StreamID: streamID,
  259. Type: FramePriority,
  260. Length: 5,
  261. },
  262. PriorityParam{
  263. StreamDep: 2,
  264. Exclusive: false,
  265. Weight: 127,
  266. },
  267. },
  268. },
  269. {
  270. "exclusive",
  271. PriorityParam{
  272. StreamDep: 3,
  273. Exclusive: true,
  274. Weight: 77,
  275. },
  276. &PriorityFrame{
  277. FrameHeader{
  278. valid: true,
  279. StreamID: streamID,
  280. Type: FramePriority,
  281. Length: 5,
  282. },
  283. PriorityParam{
  284. StreamDep: 3,
  285. Exclusive: true,
  286. Weight: 77,
  287. },
  288. },
  289. },
  290. }
  291. for _, tt := range tests {
  292. fr, _ := testFramer()
  293. if err := fr.WritePriority(streamID, tt.priority); err != nil {
  294. t.Errorf("test %q: %v", tt.name, err)
  295. continue
  296. }
  297. f, err := fr.ReadFrame()
  298. if err != nil {
  299. t.Errorf("test %q: failed to read the frame back: %v", tt.name, err)
  300. continue
  301. }
  302. if !reflect.DeepEqual(f, tt.wantFrame) {
  303. t.Errorf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tt.name, f, tt.wantFrame)
  304. }
  305. }
  306. }
  307. func TestWriteSettings(t *testing.T) {
  308. fr, buf := testFramer()
  309. fr.WriteSettings(Setting{1, 2}, Setting{3, 4})
  310. const wantEnc = "\x00\x00\f\x04\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00\x00\x04"
  311. if buf.String() != wantEnc {
  312. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  313. }
  314. }
  315. func TestWriteSettingsAck(t *testing.T) {
  316. fr, buf := testFramer()
  317. fr.WriteSettingsAck()
  318. const wantEnc = "\x00\x00\x00\x04\x01\x00\x00\x00\x00"
  319. if buf.String() != wantEnc {
  320. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  321. }
  322. }