MCPcopy
hub / github.com/lonng/nano / TestPack

Function TestPack

internal/codec/codec_test.go:10–75  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8)
9
10func TestPack(t *testing.T) {
11 data := []byte("hello world")
12 p1 := &Packet{Type: Handshake, Data: data, Length: len(data)}
13 pp1, err := Encode(Handshake, data)
14 if err != nil {
15 t.Error(err.Error())
16 }
17
18 d1 := NewDecoder()
19 packets, err := d1.Decode(pp1)
20 if err != nil {
21 t.Fatal(err.Error())
22 }
23 if len(packets) < 1 {
24 t.Fatal("packets should not empty")
25 }
26 if !reflect.DeepEqual(p1, packets[0]) {
27 t.Fatalf("expect: %v, got: %v", p1, packets[0])
28 }
29
30 p2 := &Packet{Type: Type(5), Data: data, Length: len(data)}
31 pp2, err := Encode(Kick, data)
32 if err != nil {
33 t.Error(err.Error())
34 }
35
36 d2 := NewDecoder()
37 upp2, err := d2.Decode(pp2)
38 if err != nil {
39 t.Fatal(err.Error())
40 }
41 if len(upp2) < 1 {
42 t.Fatal("packets should not empty")
43 }
44 if !reflect.DeepEqual(p2, upp2[0]) {
45 t.Fatalf("expect: %v, got: %v", p2, upp2[0])
46 }
47
48 _ = &Packet{Type: Type(0), Data: data, Length: len(data)}
49 if _, err := Encode(Type(0), data); err == nil {
50 t.Error("should err")
51 }
52
53 _ = &Packet{Type: Type(6), Data: data, Length: len(data)}
54 if _, err = Encode(Type(6), data); err == nil {
55 t.Error("should err")
56 }
57
58 p5 := &Packet{Type: Type(5), Data: data, Length: len(data)}
59 pp5, err := Encode(Kick, data)
60 if err != nil {
61 t.Fatal(err.Error())
62 }
63 d3 := NewDecoder()
64 upp5, err := d3.Decode(append(pp5, []byte{0x01, 0x00, 0x00, 0x00}...))
65 if err != nil {
66 t.Fatal(err.Error())
67 }

Callers

nothing calls this directly

Calls 6

DecodeMethod · 0.95
NewDecoderFunction · 0.85
FatalMethod · 0.80
FatalfMethod · 0.80
EncodeFunction · 0.70
TypeTypeAlias · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…