frame_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. settings := []Setting{{1, 2}, {3, 4}}
  310. fr.WriteSettings(settings...)
  311. const wantEnc = "\x00\x00\f\x04\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00\x00\x04"
  312. if buf.String() != wantEnc {
  313. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  314. }
  315. f, err := fr.ReadFrame()
  316. if err != nil {
  317. t.Fatal(err)
  318. }
  319. sf, ok := f.(*SettingsFrame)
  320. if !ok {
  321. t.Fatalf("Got a %T; want a SettingsFrame", f)
  322. }
  323. var got []Setting
  324. sf.ForeachSetting(func(s Setting) error {
  325. got = append(got, s)
  326. valBack, ok := sf.Value(s.ID)
  327. if !ok || valBack != s.Val {
  328. t.Errorf("Value(%d) = %v, %v; want %v, true", s.ID, valBack, ok)
  329. }
  330. return nil
  331. })
  332. if !reflect.DeepEqual(settings, got) {
  333. t.Errorf("Read settings %+v != written settings %+v", got, settings)
  334. }
  335. }
  336. func TestWriteSettingsAck(t *testing.T) {
  337. fr, buf := testFramer()
  338. fr.WriteSettingsAck()
  339. const wantEnc = "\x00\x00\x00\x04\x01\x00\x00\x00\x00"
  340. if buf.String() != wantEnc {
  341. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  342. }
  343. }
  344. func TestWriteWindowUpdate(t *testing.T) {
  345. fr, buf := testFramer()
  346. const streamID = 1<<24 + 2<<16 + 3<<8 + 4
  347. const incr = 7<<24 + 6<<16 + 5<<8 + 4
  348. if err := fr.WriteWindowUpdate(streamID, incr); err != nil {
  349. t.Fatal(err)
  350. }
  351. const wantEnc = "\x00\x00\x04\x08\x00\x01\x02\x03\x04\x07\x06\x05\x04"
  352. if buf.String() != wantEnc {
  353. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  354. }
  355. f, err := fr.ReadFrame()
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. want := &WindowUpdateFrame{
  360. FrameHeader: FrameHeader{
  361. valid: true,
  362. Type: 0x8,
  363. Flags: 0x0,
  364. Length: 0x4,
  365. StreamID: 0x1020304,
  366. },
  367. Increment: 0x7060504,
  368. }
  369. if !reflect.DeepEqual(f, want) {
  370. t.Errorf("parsed back %#v; want %#v", f, want)
  371. }
  372. }
  373. func TestWritePing(t *testing.T) { testWritePing(t, false) }
  374. func TestWritePingAck(t *testing.T) { testWritePing(t, true) }
  375. func testWritePing(t *testing.T, ack bool) {
  376. fr, buf := testFramer()
  377. if err := fr.WritePing(ack, [8]byte{1, 2, 3, 4, 5, 6, 7, 8}); err != nil {
  378. t.Fatal(err)
  379. }
  380. var wantFlags Flags
  381. if ack {
  382. wantFlags = FlagPingAck
  383. }
  384. var wantEnc = "\x00\x00\x08\x06" + string(wantFlags) + "\x00\x00\x00\x00" + "\x01\x02\x03\x04\x05\x06\x07\x08"
  385. if buf.String() != wantEnc {
  386. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  387. }
  388. f, err := fr.ReadFrame()
  389. if err != nil {
  390. t.Fatal(err)
  391. }
  392. want := &PingFrame{
  393. FrameHeader: FrameHeader{
  394. valid: true,
  395. Type: 0x6,
  396. Flags: wantFlags,
  397. Length: 0x8,
  398. StreamID: 0,
  399. },
  400. Data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
  401. }
  402. if !reflect.DeepEqual(f, want) {
  403. t.Errorf("parsed back %#v; want %#v", f, want)
  404. }
  405. }
  406. func TestReadFrameHeader(t *testing.T) {
  407. tests := []struct {
  408. len uint32
  409. typ FrameType
  410. flags Flags
  411. streamID uint32
  412. }{
  413. {len: 0, typ: 255, flags: 1, streamID: 0},
  414. {len: 0, typ: 255, flags: 1, streamID: 1},
  415. {len: 0, typ: 255, flags: 1, streamID: 255},
  416. {len: 0, typ: 255, flags: 1, streamID: 256},
  417. {len: 0, typ: 255, flags: 1, streamID: 65535},
  418. {len: 0, typ: 255, flags: 1, streamID: 65536},
  419. {len: 0, typ: 1, flags: 255, streamID: 1},
  420. {len: 255, typ: 1, flags: 255, streamID: 1},
  421. {len: 256, typ: 1, flags: 255, streamID: 1},
  422. {len: 65535, typ: 1, flags: 255, streamID: 1},
  423. {len: 65536, typ: 1, flags: 255, streamID: 1},
  424. {len: 16777215, typ: 1, flags: 255, streamID: 1},
  425. }
  426. for _, tt := range tests {
  427. fr, buf := testFramer()
  428. fr.startWrite(tt.typ, tt.flags, tt.streamID)
  429. fr.writeBytes(make([]byte, tt.len))
  430. fr.endWrite()
  431. fh, err := ReadFrameHeader(buf)
  432. if err != nil {
  433. t.Errorf("ReadFrameHeader(%+v) = %v", tt, err)
  434. continue
  435. }
  436. if fh.Type != tt.typ || fh.Flags != tt.flags || fh.Length != tt.len || fh.StreamID != tt.streamID {
  437. t.Errorf("ReadFrameHeader(%+v) = %+v; mismatch", tt, fh)
  438. }
  439. }
  440. }
  441. func TestWriteTooLargeFrame(t *testing.T) {
  442. fr, _ := testFramer()
  443. fr.startWrite(0, 1, 1)
  444. fr.writeBytes(make([]byte, 1<<24))
  445. err := fr.endWrite()
  446. if err != errFrameTooLarge {
  447. t.Errorf("endWrite = %v; want errFrameTooLarge", err)
  448. }
  449. }
  450. func TestWriteGoAway(t *testing.T) {
  451. const debug = "foo"
  452. fr, buf := testFramer()
  453. if err := fr.WriteGoAway(0x01020304, 0x05060708, []byte(debug)); err != nil {
  454. t.Fatal(err)
  455. }
  456. const wantEnc = "\x00\x00\v\a\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08" + debug
  457. if buf.String() != wantEnc {
  458. t.Errorf("encoded as %q; want %q", buf.Bytes(), wantEnc)
  459. }
  460. f, err := fr.ReadFrame()
  461. if err != nil {
  462. t.Fatal(err)
  463. }
  464. want := &GoAwayFrame{
  465. FrameHeader: FrameHeader{
  466. valid: true,
  467. Type: 0x7,
  468. Flags: 0,
  469. Length: uint32(4 + 4 + len(debug)),
  470. StreamID: 0,
  471. },
  472. LastStreamID: 0x01020304,
  473. ErrCode: 0x05060708,
  474. debugData: []byte(debug),
  475. }
  476. if !reflect.DeepEqual(f, want) {
  477. t.Fatalf("parsed back:\n%#v\nwant:\n%#v", f, want)
  478. }
  479. if got := string(f.(*GoAwayFrame).DebugData()); got != debug {
  480. t.Errorf("debug data = %q; want %q", got, debug)
  481. }
  482. }