|
| 1 | +package y3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/yomorun/y3-codec-golang/pkg/spec" |
| 9 | +) |
| 10 | + |
| 11 | +func TestStreamDecode1(t *testing.T) { |
| 12 | + // testStreamDecode(t, []byte{0x01, 0x00}, []byte{0x01}, []byte{0x00}, []byte{}) |
| 13 | + data := []byte{0x01, 0x01, 0x03} //, []byte{0x01}, []byte{0x01}, []byte{0x03}, flag) |
| 14 | + // testStreamDecode(t, []byte{0x01, 0x02, 0x03, 0x04, 0x05}, []byte{0x01}, []byte{0x02}, []byte{0x03, 0x04}) |
| 15 | + // testStreamDecode(t, []byte{0x01, 0x03, 0x03, 0x04, 0x05}, []byte{0x01}, []byte{0x03}, []byte{0x03, 0x04, 0x05}) |
| 16 | + // testStreamDecode(t, []byte{0x01, 0x01}, []byte{0x02}, []byte{0x01}, []byte{}) |
| 17 | + // t.Errorf("---") |
| 18 | + |
| 19 | + expectTagbuf := []byte{0x01} |
| 20 | + expectLenbuf := []byte{0x01} |
| 21 | + expectValbuf := []byte{0x03} |
| 22 | + |
| 23 | + // as reader |
| 24 | + r := bytes.NewReader(data) |
| 25 | + // create steam decoder |
| 26 | + pr := NewStreamDecoder(r) |
| 27 | + |
| 28 | + // handler |
| 29 | + pr.OnPacket(func(p *spec.Packet) { |
| 30 | + t.Logf("[CALLBACK] p=%v", p) |
| 31 | + compareBytes(t, p.GetTagBuffer(), expectTagbuf, "T") |
| 32 | + compareBytes(t, p.GetLengthBuffer(), expectLenbuf, "L") |
| 33 | + compareBytes(t, p.GetValueBuffer(), expectValbuf, "V") |
| 34 | + }) |
| 35 | + pr.Start() |
| 36 | +} |
| 37 | + |
| 38 | +func TestStreamDecode2(t *testing.T) { |
| 39 | + data := []byte{0x01, 0x00} |
| 40 | + |
| 41 | + expectTagbuf := []byte{0x01} |
| 42 | + expectLenbuf := []byte{0x00} |
| 43 | + expectValbuf := []byte{} |
| 44 | + |
| 45 | + // as reader |
| 46 | + r := bytes.NewReader(data) |
| 47 | + // create steam decoder |
| 48 | + pr := NewStreamDecoder(r) |
| 49 | + |
| 50 | + // handler |
| 51 | + pr.OnPacket(func(p *spec.Packet) { |
| 52 | + t.Logf("[CALLBACK] p=%v", p) |
| 53 | + compareBytes(t, p.GetTagBuffer(), expectTagbuf, "T") |
| 54 | + compareBytes(t, p.GetLengthBuffer(), expectLenbuf, "L") |
| 55 | + compareBytes(t, p.GetValueBuffer(), expectValbuf, "V") |
| 56 | + }) |
| 57 | + pr.Start() |
| 58 | +} |
| 59 | + |
| 60 | +func TestStreamDecode3(t *testing.T) { |
| 61 | + data := []byte{0x01, 0x02, 0x03, 0x04} |
| 62 | + |
| 63 | + expectTagbuf := []byte{0x01} |
| 64 | + expectLenbuf := []byte{0x02} |
| 65 | + expectValbuf := []byte{0x03, 0x04} |
| 66 | + |
| 67 | + // as reader |
| 68 | + r := bytes.NewReader(data) |
| 69 | + // create steam decoder |
| 70 | + pr := NewStreamDecoder(r) |
| 71 | + |
| 72 | + // handler |
| 73 | + pr.OnPacket(func(p *spec.Packet) { |
| 74 | + t.Logf("[CALLBACK] p=%v", p) |
| 75 | + compareBytes(t, p.GetTagBuffer(), expectTagbuf, "T") |
| 76 | + compareBytes(t, p.GetLengthBuffer(), expectLenbuf, "L") |
| 77 | + compareBytes(t, p.GetValueBuffer(), expectValbuf, "V") |
| 78 | + }) |
| 79 | + pr.Start() |
| 80 | +} |
| 81 | + |
| 82 | +func TestStreamDecode4(t *testing.T) { |
| 83 | + data := []byte{0x01, 0x02, 0x03} |
| 84 | + |
| 85 | + // as reader |
| 86 | + r := bytes.NewReader(data) |
| 87 | + // create steam decoder |
| 88 | + pr := NewStreamDecoder(r) |
| 89 | + |
| 90 | + parsed := false |
| 91 | + |
| 92 | + // handler |
| 93 | + pr.OnPacket(func(p *spec.Packet) { |
| 94 | + t.Logf("[CALLBACK] p=%v", p) |
| 95 | + if p != nil { |
| 96 | + t.Error(p) |
| 97 | + } |
| 98 | + parsed = true |
| 99 | + }) |
| 100 | + pr.Start() |
| 101 | + |
| 102 | + if parsed == true { |
| 103 | + t.Errorf("Should not trigger callback") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestStreamDecode5(t *testing.T) { |
| 108 | + data := []byte{0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x02, 0x03, 0x03, 0x01, 0x02, 0x03} |
| 109 | + |
| 110 | + // as reader |
| 111 | + r := bytes.NewReader(data) |
| 112 | + // create steam decoder |
| 113 | + pr := NewStreamDecoder(r) |
| 114 | + |
| 115 | + times := 0 |
| 116 | + |
| 117 | + // handler |
| 118 | + pr.OnPacket(func(p *spec.Packet) { |
| 119 | + log.Printf("==>OnPacket: %v", p) |
| 120 | + if times == 0 { |
| 121 | + compareBytes(t, p.GetTagBuffer(), []byte{0x01}, "T") |
| 122 | + compareBytes(t, p.GetLengthBuffer(), []byte{0x01}, "L") |
| 123 | + compareBytes(t, p.GetValueBuffer(), []byte{0x01}, "V") |
| 124 | + } |
| 125 | + if times == 1 { |
| 126 | + compareBytes(t, p.GetTagBuffer(), []byte{0x02}, "T") |
| 127 | + compareBytes(t, p.GetLengthBuffer(), []byte{0x02}, "L") |
| 128 | + compareBytes(t, p.GetValueBuffer(), []byte{0x01, 0x02}, "V") |
| 129 | + } |
| 130 | + if times == 2 { |
| 131 | + compareBytes(t, p.GetTagBuffer(), []byte{0x03}, "T") |
| 132 | + compareBytes(t, p.GetLengthBuffer(), []byte{0x03}, "L") |
| 133 | + compareBytes(t, p.GetValueBuffer(), []byte{0x01, 0x02, 0x03}, "V") |
| 134 | + } |
| 135 | + times++ |
| 136 | + }) |
| 137 | + |
| 138 | + pr.Start() |
| 139 | +} |
| 140 | + |
| 141 | +func compareBytes(t *testing.T, result []byte, expected []byte, v string) { |
| 142 | + if len(result) != len(expected) { |
| 143 | + t.Errorf("\n[%s] expected:[% X]\n actual:[% X]\n", v, expected, result) |
| 144 | + } |
| 145 | + |
| 146 | + for i, p := range result { |
| 147 | + if p != expected[i] { |
| 148 | + t.Errorf("\n[%s] expected:[% X]\n actual:[% X]\n", v, expected, result) |
| 149 | + break |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments