github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/open_packets_test.go (about) 1 package sshfx 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 var _ Packet = &OpenPacket{} 9 10 func TestOpenPacket(t *testing.T) { 11 const ( 12 id = 42 13 filename = "/foo" 14 perms FileMode = 0x87654321 15 ) 16 17 p := &OpenPacket{ 18 Filename: "/foo", 19 PFlags: FlagRead, 20 Attrs: Attributes{ 21 Flags: AttrPermissions, 22 Permissions: perms, 23 }, 24 } 25 26 buf, err := ComposePacket(p.MarshalPacket(id, nil)) 27 if err != nil { 28 t.Fatal("unexpected error:", err) 29 } 30 31 want := []byte{ 32 0x00, 0x00, 0x00, 25, 33 3, 34 0x00, 0x00, 0x00, 42, 35 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', 36 0x00, 0x00, 0x00, 1, 37 0x00, 0x00, 0x00, 0x04, 38 0x87, 0x65, 0x43, 0x21, 39 } 40 41 if !bytes.Equal(buf, want) { 42 t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) 43 } 44 45 *p = OpenPacket{} 46 47 // UnmarshalPacketBody assumes the (length, type, request-id) have already been consumed. 48 if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { 49 t.Fatal("unexpected error:", err) 50 } 51 52 if p.Filename != filename { 53 t.Errorf("UnmarshalPacketBody(): Filename was %q, but expected %q", p.Filename, filename) 54 } 55 56 if p.PFlags != FlagRead { 57 t.Errorf("UnmarshalPacketBody(): PFlags was %#x, but expected %#x", p.PFlags, FlagRead) 58 } 59 60 if p.Attrs.Flags != AttrPermissions { 61 t.Errorf("UnmarshalPacketBody(): Attrs.Flags was %#x, but expected %#x", p.Attrs.Flags, AttrPermissions) 62 } 63 64 if p.Attrs.Permissions != perms { 65 t.Errorf("UnmarshalPacketBody(): Attrs.Permissions was %#v, but expected %#v", p.Attrs.Permissions, perms) 66 } 67 } 68 69 var _ Packet = &OpenDirPacket{} 70 71 func TestOpenDirPacket(t *testing.T) { 72 const ( 73 id = 42 74 path = "/foo" 75 ) 76 77 p := &OpenDirPacket{ 78 Path: path, 79 } 80 81 buf, err := ComposePacket(p.MarshalPacket(id, nil)) 82 if err != nil { 83 t.Fatal("unexpected error:", err) 84 } 85 86 want := []byte{ 87 0x00, 0x00, 0x00, 13, 88 11, 89 0x00, 0x00, 0x00, 42, 90 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', 91 } 92 93 if !bytes.Equal(buf, want) { 94 t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) 95 } 96 97 *p = OpenDirPacket{} 98 99 // UnmarshalPacketBody assumes the (length, type, request-id) have already been consumed. 100 if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { 101 t.Fatal("unexpected error:", err) 102 } 103 104 if p.Path != path { 105 t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) 106 } 107 }