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

     1  package sshfx
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"testing"
     7  )
     8  
     9  func TestStatusPacketIs(t *testing.T) {
    10  	status := &StatusPacket{
    11  		StatusCode:   StatusFailure,
    12  		ErrorMessage: "error message",
    13  		LanguageTag:  "language tag",
    14  	}
    15  
    16  	if !errors.Is(status, StatusFailure) {
    17  		t.Error("errors.Is(StatusFailure, StatusFailure) != true")
    18  	}
    19  	if !errors.Is(status, &StatusPacket{StatusCode: StatusFailure}) {
    20  		t.Error("errors.Is(StatusFailure, StatusPacket{StatusFailure}) != true")
    21  	}
    22  	if errors.Is(status, StatusOK) {
    23  		t.Error("errors.Is(StatusFailure, StatusFailure) == true")
    24  	}
    25  	if errors.Is(status, &StatusPacket{StatusCode: StatusOK}) {
    26  		t.Error("errors.Is(StatusFailure, StatusPacket{StatusFailure}) == true")
    27  	}
    28  }
    29  
    30  var _ Packet = &StatusPacket{}
    31  
    32  func TestStatusPacket(t *testing.T) {
    33  	const (
    34  		id           = 42
    35  		statusCode   = StatusBadMessage
    36  		errorMessage = "foo"
    37  		languageTag  = "x-example"
    38  	)
    39  
    40  	p := &StatusPacket{
    41  		StatusCode:   statusCode,
    42  		ErrorMessage: errorMessage,
    43  		LanguageTag:  languageTag,
    44  	}
    45  
    46  	buf, err := ComposePacket(p.MarshalPacket(id, nil))
    47  	if err != nil {
    48  		t.Fatal("unexpected error:", err)
    49  	}
    50  
    51  	want := []byte{
    52  		0x00, 0x00, 0x00, 29,
    53  		101,
    54  		0x00, 0x00, 0x00, 42,
    55  		0x00, 0x00, 0x00, 5,
    56  		0x00, 0x00, 0x00, 3, 'f', 'o', 'o',
    57  		0x00, 0x00, 0x00, 9, 'x', '-', 'e', 'x', 'a', 'm', 'p', 'l', 'e',
    58  	}
    59  
    60  	if !bytes.Equal(buf, want) {
    61  		t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want)
    62  	}
    63  
    64  	*p = StatusPacket{}
    65  
    66  	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
    67  	if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil {
    68  		t.Fatal("unexpected error:", err)
    69  	}
    70  
    71  	if p.StatusCode != statusCode {
    72  		t.Errorf("UnmarshalBinary(): StatusCode was %v, but expected %v", p.StatusCode, statusCode)
    73  	}
    74  
    75  	if p.ErrorMessage != errorMessage {
    76  		t.Errorf("UnmarshalBinary(): ErrorMessage was %q, but expected %q", p.ErrorMessage, errorMessage)
    77  	}
    78  
    79  	if p.LanguageTag != languageTag {
    80  		t.Errorf("UnmarshalBinary(): LanguageTag was %q, but expected %q", p.LanguageTag, languageTag)
    81  	}
    82  }
    83  
    84  var _ Packet = &HandlePacket{}
    85  
    86  func TestHandlePacket(t *testing.T) {
    87  	const (
    88  		id     = 42
    89  		handle = "somehandle"
    90  	)
    91  
    92  	p := &HandlePacket{
    93  		Handle: "somehandle",
    94  	}
    95  
    96  	buf, err := ComposePacket(p.MarshalPacket(id, nil))
    97  	if err != nil {
    98  		t.Fatal("unexpected error:", err)
    99  	}
   100  
   101  	want := []byte{
   102  		0x00, 0x00, 0x00, 19,
   103  		102,
   104  		0x00, 0x00, 0x00, 42,
   105  		0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e',
   106  	}
   107  
   108  	if !bytes.Equal(buf, want) {
   109  		t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want)
   110  	}
   111  
   112  	*p = HandlePacket{}
   113  
   114  	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
   115  	if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil {
   116  		t.Fatal("unexpected error:", err)
   117  	}
   118  
   119  	if p.Handle != handle {
   120  		t.Errorf("UnmarshalBinary(): Handle was %q, but expected %q", p.Handle, handle)
   121  	}
   122  }
   123  
   124  var _ Packet = &DataPacket{}
   125  
   126  func TestDataPacket(t *testing.T) {
   127  	const (
   128  		id = 42
   129  	)
   130  
   131  	var payload = []byte(`foobar`)
   132  
   133  	p := &DataPacket{
   134  		Data: payload,
   135  	}
   136  
   137  	buf, err := ComposePacket(p.MarshalPacket(id, nil))
   138  	if err != nil {
   139  		t.Fatal("unexpected error:", err)
   140  	}
   141  
   142  	want := []byte{
   143  		0x00, 0x00, 0x00, 15,
   144  		103,
   145  		0x00, 0x00, 0x00, 42,
   146  		0x00, 0x00, 0x00, 6, 'f', 'o', 'o', 'b', 'a', 'r',
   147  	}
   148  
   149  	if !bytes.Equal(buf, want) {
   150  		t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want)
   151  	}
   152  
   153  	*p = DataPacket{}
   154  
   155  	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
   156  	if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil {
   157  		t.Fatal("unexpected error:", err)
   158  	}
   159  
   160  	if !bytes.Equal(p.Data, payload) {
   161  		t.Errorf("UnmarshalBinary(): Data was %X, but expected %X", p.Data, payload)
   162  	}
   163  }
   164  
   165  var _ Packet = &NamePacket{}
   166  
   167  func TestNamePacket(t *testing.T) {
   168  	const (
   169  		id                = 42
   170  		filename          = "foo"
   171  		longname          = "bar"
   172  		perms    FileMode = 0x87654300
   173  	)
   174  
   175  	p := &NamePacket{
   176  		Entries: []*NameEntry{
   177  			{
   178  				Filename: filename + "1",
   179  				Longname: longname + "1",
   180  				Attrs: Attributes{
   181  					Flags:       AttrPermissions | (1 << 8),
   182  					Permissions: perms | 1,
   183  				},
   184  			},
   185  			{
   186  				Filename: filename + "2",
   187  				Longname: longname + "2",
   188  				Attrs: Attributes{
   189  					Flags:       AttrPermissions | (2 << 8),
   190  					Permissions: perms | 2,
   191  				},
   192  			},
   193  		},
   194  	}
   195  
   196  	buf, err := ComposePacket(p.MarshalPacket(id, nil))
   197  	if err != nil {
   198  		t.Fatal("unexpected error:", err)
   199  	}
   200  
   201  	want := []byte{
   202  		0x00, 0x00, 0x00, 57,
   203  		104,
   204  		0x00, 0x00, 0x00, 42,
   205  		0x00, 0x00, 0x00, 0x02,
   206  		0x00, 0x00, 0x00, 4, 'f', 'o', 'o', '1',
   207  		0x00, 0x00, 0x00, 4, 'b', 'a', 'r', '1',
   208  		0x00, 0x00, 0x01, 0x04,
   209  		0x87, 0x65, 0x43, 0x01,
   210  		0x00, 0x00, 0x00, 4, 'f', 'o', 'o', '2',
   211  		0x00, 0x00, 0x00, 4, 'b', 'a', 'r', '2',
   212  		0x00, 0x00, 0x02, 0x04,
   213  		0x87, 0x65, 0x43, 0x02,
   214  	}
   215  
   216  	if !bytes.Equal(buf, want) {
   217  		t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want)
   218  	}
   219  
   220  	*p = NamePacket{}
   221  
   222  	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
   223  	if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil {
   224  		t.Fatal("unexpected error:", err)
   225  	}
   226  
   227  	if count := len(p.Entries); count != 2 {
   228  		t.Fatalf("UnmarshalBinary(): len(NameEntries) was %d, but expected %d", count, 2)
   229  	}
   230  
   231  	for i, e := range p.Entries {
   232  		if got, want := e.Filename, filename+string('1'+rune(i)); got != want {
   233  			t.Errorf("UnmarshalBinary(): Entries[%d].Filename was %q, but expected %q", i, got, want)
   234  		}
   235  
   236  		if got, want := e.Longname, longname+string('1'+rune(i)); got != want {
   237  			t.Errorf("UnmarshalBinary(): Entries[%d].Longname was %q, but expected %q", i, got, want)
   238  		}
   239  
   240  		if got, want := e.Attrs.Flags, AttrPermissions|((i+1)<<8); got != uint32(want) {
   241  			t.Errorf("UnmarshalBinary(): Entries[%d].Attrs.Flags was %#x, but expected %#x", i, got, want)
   242  		}
   243  
   244  		if got, want := e.Attrs.Permissions, perms|FileMode(i+1); got != want {
   245  			t.Errorf("UnmarshalBinary(): Entries[%d].Attrs.Permissions was %#v, but expected %#v", i, got, want)
   246  		}
   247  	}
   248  }
   249  
   250  var _ Packet = &AttrsPacket{}
   251  
   252  func TestAttrsPacket(t *testing.T) {
   253  	const (
   254  		id             = 42
   255  		perms FileMode = 0x87654321
   256  	)
   257  
   258  	p := &AttrsPacket{
   259  		Attrs: Attributes{
   260  			Flags:       AttrPermissions,
   261  			Permissions: perms,
   262  		},
   263  	}
   264  
   265  	buf, err := ComposePacket(p.MarshalPacket(id, nil))
   266  	if err != nil {
   267  		t.Fatal("unexpected error:", err)
   268  	}
   269  
   270  	want := []byte{
   271  		0x00, 0x00, 0x00, 13,
   272  		105,
   273  		0x00, 0x00, 0x00, 42,
   274  		0x00, 0x00, 0x00, 0x04,
   275  		0x87, 0x65, 0x43, 0x21,
   276  	}
   277  
   278  	if !bytes.Equal(buf, want) {
   279  		t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want)
   280  	}
   281  
   282  	*p = AttrsPacket{}
   283  
   284  	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
   285  	if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil {
   286  		t.Fatal("unexpected error:", err)
   287  	}
   288  
   289  	if p.Attrs.Flags != AttrPermissions {
   290  		t.Errorf("UnmarshalBinary(): Attrs.Flags was %#x, but expected %#x", p.Attrs.Flags, AttrPermissions)
   291  	}
   292  
   293  	if p.Attrs.Permissions != perms {
   294  		t.Errorf("UnmarshalBinary(): Attrs.Permissions was %#v, but expected %#v", p.Attrs.Permissions, perms)
   295  	}
   296  }