github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/packets_test.go (about)

     1  package sshfx
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestRawPacket(t *testing.T) {
     9  	const (
    10  		id      = 42
    11  		errMsg  = "eof"
    12  		langTag = "en"
    13  	)
    14  
    15  	p := &RawPacket{
    16  		PacketType: PacketTypeStatus,
    17  		RequestID:  id,
    18  		Data: Buffer{
    19  			b: []byte{
    20  				0x00, 0x00, 0x00, 0x01,
    21  				0x00, 0x00, 0x00, 0x03, 'e', 'o', 'f',
    22  				0x00, 0x00, 0x00, 0x02, 'e', 'n',
    23  			},
    24  		},
    25  	}
    26  
    27  	buf, err := p.MarshalBinary()
    28  	if err != nil {
    29  		t.Fatal("unexpected error:", err)
    30  	}
    31  
    32  	want := []byte{
    33  		0x00, 0x00, 0x00, 22,
    34  		101,
    35  		0x00, 0x00, 0x00, 42,
    36  		0x00, 0x00, 0x00, 0x01,
    37  		0x00, 0x00, 0x00, 3, 'e', 'o', 'f',
    38  		0x00, 0x00, 0x00, 2, 'e', 'n',
    39  	}
    40  
    41  	if !bytes.Equal(buf, want) {
    42  		t.Errorf("RawPacket.MarshalBinary() = %X, but wanted %X", buf, want)
    43  	}
    44  
    45  	*p = RawPacket{}
    46  
    47  	if err := p.ReadFrom(bytes.NewReader(buf), nil, DefaultMaxPacketLength); err != nil {
    48  		t.Fatal("unexpected error:", err)
    49  	}
    50  
    51  	if p.PacketType != PacketTypeStatus {
    52  		t.Errorf("RawPacket.UnmarshalBinary(): Type was %v, but expected %v", p.PacketType, PacketTypeStat)
    53  	}
    54  
    55  	if p.RequestID != uint32(id) {
    56  		t.Errorf("RawPacket.UnmarshalBinary(): RequestID was %d, but expected %d", p.RequestID, id)
    57  	}
    58  
    59  	want = []byte{
    60  		0x00, 0x00, 0x00, 0x01,
    61  		0x00, 0x00, 0x00, 3, 'e', 'o', 'f',
    62  		0x00, 0x00, 0x00, 2, 'e', 'n',
    63  	}
    64  
    65  	if !bytes.Equal(p.Data.Bytes(), want) {
    66  		t.Fatalf("RawPacket.UnmarshalBinary(): Data was %X, but expected %X", p.Data, want)
    67  	}
    68  
    69  	var resp StatusPacket
    70  	resp.UnmarshalPacketBody(&p.Data)
    71  
    72  	if resp.StatusCode != StatusEOF {
    73  		t.Errorf("UnmarshalPacketBody(): StatusCode was %v, but expected %v", resp.StatusCode, StatusEOF)
    74  	}
    75  
    76  	if resp.ErrorMessage != errMsg {
    77  		t.Errorf("UnmarshalPacketBody(): ErrorMessage was %q, but expected %q", resp.ErrorMessage, errMsg)
    78  	}
    79  
    80  	if resp.LanguageTag != langTag {
    81  		t.Errorf("UnmarshalPacketBody(): LanguageTag was %q, but expected %q", resp.LanguageTag, langTag)
    82  	}
    83  }
    84  
    85  func TestRequestPacket(t *testing.T) {
    86  	const (
    87  		id   = 42
    88  		path = "foo"
    89  	)
    90  
    91  	p := &RequestPacket{
    92  		RequestID: id,
    93  		Request: &StatPacket{
    94  			Path: path,
    95  		},
    96  	}
    97  
    98  	buf, err := p.MarshalBinary()
    99  	if err != nil {
   100  		t.Fatal("unexpected error:", err)
   101  	}
   102  
   103  	want := []byte{
   104  		0x00, 0x00, 0x00, 12,
   105  		17,
   106  		0x00, 0x00, 0x00, 42,
   107  		0x00, 0x00, 0x00, 3, 'f', 'o', 'o',
   108  	}
   109  
   110  	if !bytes.Equal(buf, want) {
   111  		t.Errorf("RequestPacket.MarshalBinary() = %X, but wanted %X", buf, want)
   112  	}
   113  
   114  	*p = RequestPacket{}
   115  
   116  	if err := p.ReadFrom(bytes.NewReader(buf), nil, DefaultMaxPacketLength); err != nil {
   117  		t.Fatal("unexpected error:", err)
   118  	}
   119  
   120  	if p.RequestID != uint32(id) {
   121  		t.Errorf("RequestPacket.UnmarshalBinary(): RequestID was %d, but expected %d", p.RequestID, id)
   122  	}
   123  
   124  	req, ok := p.Request.(*StatPacket)
   125  	if !ok {
   126  		t.Fatalf("unexpected Request type was %T, but expected %T", p.Request, req)
   127  	}
   128  
   129  	if req.Path != path {
   130  		t.Errorf("RequestPacket.UnmarshalBinary(): Request.Path was %q, but expected %q", req.Path, path)
   131  	}
   132  }