| 1 | package openssh |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | |
| 7 | sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer" |
| 8 | ) |
| 9 | |
| 10 | var _ sshfx.PacketMarshaller = &HardlinkExtendedPacket{} |
| 11 | |
| 12 | func init() { |
| 13 | RegisterExtensionHardlink() |
| 14 | } |
| 15 | |
| 16 | func TestHardlinkExtendedPacket(t *testing.T) { |
| 17 | const ( |
| 18 | id = 42 |
| 19 | oldpath = "/foo" |
| 20 | newpath = "/bar" |
| 21 | ) |
| 22 | |
| 23 | ep := &HardlinkExtendedPacket{ |
| 24 | OldPath: oldpath, |
| 25 | NewPath: newpath, |
| 26 | } |
| 27 | |
| 28 | data, err := sshfx.ComposePacket(ep.MarshalPacket(id, nil)) |
| 29 | if err != nil { |
| 30 | t.Fatal("unexpected error:", err) |
| 31 | } |
| 32 | |
| 33 | want := []byte{ |
| 34 | 0x00, 0x00, 0x00, 45, |
| 35 | 200, |
| 36 | 0x00, 0x00, 0x00, 42, |
| 37 | 0x00, 0x00, 0x00, 20, 'h', 'a', 'r', 'd', 'l', 'i', 'n', 'k', '@', 'o', 'p', 'e', 'n', 's', 's', 'h', '.', 'c', 'o', 'm', |
| 38 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 39 | 0x00, 0x00, 0x00, 4, '/', 'b', 'a', 'r', |
| 40 | } |
| 41 | |
| 42 | if !bytes.Equal(data, want) { |
| 43 | t.Fatalf("MarshalPacket() = %X, but wanted %X", data, want) |
| 44 | } |
| 45 | |
| 46 | var p sshfx.ExtendedPacket |
| 47 | |
| 48 | |
| 49 | if err := p.UnmarshalPacketBody(sshfx.NewBuffer(data[9:])); err != nil { |
| 50 | t.Fatal("unexpected error:", err) |
| 51 | } |
| 52 | |
| 53 | if p.ExtendedRequest != extensionHardlink { |
| 54 | t.Errorf("UnmarshalPacketBody(): ExtendedRequest was %q, but expected %q", p.ExtendedRequest, extensionHardlink) |
| 55 | } |
| 56 | |
| 57 | ep, ok := p.Data.(*HardlinkExtendedPacket) |
| 58 | if !ok { |
| 59 | t.Fatalf("UnmarshaledPacketBody(): Data was type %T, but expected *HardlinkExtendedPacket", p.Data) |
| 60 | } |
| 61 | |
| 62 | if ep.OldPath != oldpath { |
| 63 | t.Errorf("UnmarshalPacketBody(): OldPath was %q, but expected %q", ep.OldPath, oldpath) |
| 64 | } |
| 65 | |
| 66 | if ep.NewPath != newpath { |
| 67 | t.Errorf("UnmarshalPacketBody(): NewPath was %q, but expected %q", ep.NewPath, newpath) |
| 68 | } |
| 69 | } |