package bt import ( "bytes" "testing" ) func TestMsg(t *testing.T) { tests := []struct { name string msg *Msg }{ { name: "choke", msg: &Msg{Kind: Choke}, }, { name: "unchoke", msg: &Msg{Kind: Unchoke}, }, { name: "interested", msg: &Msg{Kind: Interested}, }, { name: "not interested", msg: &Msg{Kind: NotInterested}, }, { name: "have", msg: &Msg{ Kind: Have, Index: 42, }, }, { name: "bitfield", msg: &Msg{ Kind: Bitfield, Bitfield: []byte{0b1111_1111, 0b0000_0000}, }, }, { name: "request", msg: &Msg{ Kind: Request, Index: 1, Begin: 16384, Length: 16384, }, }, { name: "piece", msg: &Msg{ Kind: Piece, Index: 1, Begin: 0, Block: []byte("data"), }, }, { name: "cancel", msg: &Msg{ Kind: Cancel, Index: 1, Begin: 0, Length: 16384, }, }, { name: "keep-alive", msg: nil, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var buf bytes.Buffer writeMsg(&buf, tt.msg) data := buf.Bytes() got, _, err := readMsg(bytes.NewReader(data)) if err != nil { t.Fatalf("readMsg: %v", err) } var buf2 bytes.Buffer writeMsg(&buf2, got) if !bytes.Equal(buf2.Bytes(), data) { t.Errorf("roundtrip mismatch: got %x, want %x", buf2.Bytes(), data) } }) } }