Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

74 linhas
1.9 KiB

  1. // Copyright 2009 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 tls
  5. import (
  6. "bytes";
  7. "testing";
  8. "testing/iotest";
  9. )
  10. func matchRecord(r1, r2 *record) bool {
  11. if (r1 == nil) != (r2 == nil) {
  12. return false;
  13. }
  14. if r1 == nil {
  15. return true;
  16. }
  17. return r1.contentType == r2.contentType &&
  18. r1.major == r2.major &&
  19. r1.minor == r2.minor &&
  20. bytes.Compare(r1.payload, r2.payload) == 0;
  21. }
  22. type recordReaderTest struct {
  23. in []byte;
  24. out []*record;
  25. }
  26. var recordReaderTests = []recordReaderTest{
  27. recordReaderTest{nil, nil},
  28. recordReaderTest{fromHex("01"), nil},
  29. recordReaderTest{fromHex("0102"), nil},
  30. recordReaderTest{fromHex("010203"), nil},
  31. recordReaderTest{fromHex("01020300"), nil},
  32. recordReaderTest{fromHex("0102030000"), []*record{&record{1, 2, 3, nil}}},
  33. recordReaderTest{fromHex("01020300000102030000"), []*record{&record{1, 2, 3, nil}, &record{1, 2, 3, nil}}},
  34. recordReaderTest{fromHex("0102030001fe0102030002feff"), []*record{&record{1, 2, 3, []byte{0xfe}}, &record{1, 2, 3, []byte{0xfe, 0xff}}}},
  35. recordReaderTest{fromHex("010203000001020300"), []*record{&record{1, 2, 3, nil}}},
  36. }
  37. func TestRecordReader(t *testing.T) {
  38. for i, test := range recordReaderTests {
  39. buf := bytes.NewBuffer(test.in);
  40. c := make(chan *record);
  41. go recordReader(c, buf);
  42. matchRecordReaderOutput(t, i, test, c);
  43. buf = bytes.NewBuffer(test.in);
  44. buf2 := iotest.OneByteReader(buf);
  45. c = make(chan *record);
  46. go recordReader(c, buf2);
  47. matchRecordReaderOutput(t, i*2, test, c);
  48. }
  49. }
  50. func matchRecordReaderOutput(t *testing.T, i int, test recordReaderTest, c <-chan *record) {
  51. for j, r1 := range test.out {
  52. r2 := <-c;
  53. if r2 == nil {
  54. t.Errorf("#%d truncated after %d values", i, j);
  55. break;
  56. }
  57. if !matchRecord(r1, r2) {
  58. t.Errorf("#%d (%d) got:%#v want:%#v", i, j, r2, r1);
  59. }
  60. }
  61. <-c;
  62. if !closed(c) {
  63. t.Errorf("#%d: channel didn't close", i);
  64. }
  65. }