sftp/encoding/ssh/filexfer/packets_test.go

58 lines
1.2 KiB
Go
Raw Normal View History

2021-03-19 01:03:37 +08:00
package filexfer
import (
"bytes"
"testing"
)
func TestRawPacket(t *testing.T) {
var id byte = 42
p := &RawPacket{
Type: PacketTypeStat,
RequestID: uint32(id),
Data: Buffer{
b: []byte{0x00, 0x00, 0x00, 0x03, 'f', 'o', 'o'},
2021-03-19 01:03:37 +08:00
},
}
data, err := p.MarshalBinary()
if err != nil {
t.Fatal("unexpected error:", err)
}
want := []byte{
0x00, 0x00, 0x00, 12,
17,
0x00, 0x00, 0x00, id,
0x00, 0x00, 0x00, 3, 'f', 'o', 'o',
}
if !bytes.Equal(data, want) {
t.Errorf("RawPacket.Marshal() = %X, but wanted %X", data, want)
}
*p = RawPacket{}
// UnmarshalBinary assumes the length has already been consumed.
if err := p.UnmarshalBinary(data[4:]); err != nil {
t.Fatal("unexpected error:", err)
}
if p.Type != PacketTypeStat {
t.Errorf("RawPacket.UnmarshalBinary(): Type was %v, but expected %v", p.Type, PacketTypeStat)
}
if p.RequestID != uint32(id) {
t.Errorf("RawPacket.UnmarshalBinary(): RequestID was %d, but expected %d", p.RequestID, id)
}
want = []byte{
0x00, 0x00, 0x00, 3, 'f', 'o', 'o',
}
if !bytes.Equal(p.Data.Bytes(), want) {
t.Errorf("RawPacket.UnmarshalBinary(): Data was %X, but expected %X", p.Data, want)
2021-03-19 01:03:37 +08:00
}
}