| 1 | package sshfx |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | var _ Packet = &ClosePacket{} |
| 9 | |
| 10 | func TestClosePacket(t *testing.T) { |
| 11 | const ( |
| 12 | id = 42 |
| 13 | handle = "somehandle" |
| 14 | ) |
| 15 | |
| 16 | p := &ClosePacket{ |
| 17 | Handle: "somehandle", |
| 18 | } |
| 19 | |
| 20 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 21 | if err != nil { |
| 22 | t.Fatal("unexpected error:", err) |
| 23 | } |
| 24 | |
| 25 | want := []byte{ |
| 26 | 0x00, 0x00, 0x00, 19, |
| 27 | 4, |
| 28 | 0x00, 0x00, 0x00, 42, |
| 29 | 0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e', |
| 30 | } |
| 31 | |
| 32 | if !bytes.Equal(buf, want) { |
| 33 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 34 | } |
| 35 | |
| 36 | *p = ClosePacket{} |
| 37 | |
| 38 | |
| 39 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 40 | t.Fatal("unexpected error:", err) |
| 41 | } |
| 42 | |
| 43 | if p.Handle != handle { |
| 44 | t.Errorf("UnmarshalPacketBody(): Handle was %q, but expected %q", p.Handle, handle) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | var _ Packet = &ReadPacket{} |
| 49 | |
| 50 | func TestReadPacket(t *testing.T) { |
| 51 | const ( |
| 52 | id = 42 |
| 53 | handle = "somehandle" |
| 54 | offset uint64 = 0x123456789ABCDEF0 |
| 55 | length uint32 = 0xFEDCBA98 |
| 56 | ) |
| 57 | |
| 58 | p := &ReadPacket{ |
| 59 | Handle: "somehandle", |
| 60 | Offset: offset, |
| 61 | Length: length, |
| 62 | } |
| 63 | |
| 64 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 65 | if err != nil { |
| 66 | t.Fatal("unexpected error:", err) |
| 67 | } |
| 68 | |
| 69 | want := []byte{ |
| 70 | 0x00, 0x00, 0x00, 31, |
| 71 | 5, |
| 72 | 0x00, 0x00, 0x00, 42, |
| 73 | 0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e', |
| 74 | 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, |
| 75 | 0xFE, 0xDC, 0xBA, 0x98, |
| 76 | } |
| 77 | |
| 78 | if !bytes.Equal(buf, want) { |
| 79 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 80 | } |
| 81 | |
| 82 | *p = ReadPacket{} |
| 83 | |
| 84 | |
| 85 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 86 | t.Fatal("unexpected error:", err) |
| 87 | } |
| 88 | |
| 89 | if p.Handle != handle { |
| 90 | t.Errorf("UnmarshalPacketBody(): Handle was %q, but expected %q", p.Handle, handle) |
| 91 | } |
| 92 | |
| 93 | if p.Offset != offset { |
| 94 | t.Errorf("UnmarshalPacketBody(): Offset was %x, but expected %x", p.Offset, offset) |
| 95 | } |
| 96 | |
| 97 | if p.Length != length { |
| 98 | t.Errorf("UnmarshalPacketBody(): Length was %x, but expected %x", p.Length, length) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | var _ Packet = &WritePacket{} |
| 103 | |
| 104 | func TestWritePacket(t *testing.T) { |
| 105 | const ( |
| 106 | id = 42 |
| 107 | handle = "somehandle" |
| 108 | offset uint64 = 0x123456789ABCDEF0 |
| 109 | ) |
| 110 | |
| 111 | var payload = []byte(`foobar`) |
| 112 | |
| 113 | p := &WritePacket{ |
| 114 | Handle: "somehandle", |
| 115 | Offset: offset, |
| 116 | Data: payload, |
| 117 | } |
| 118 | |
| 119 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 120 | if err != nil { |
| 121 | t.Fatal("unexpected error:", err) |
| 122 | } |
| 123 | |
| 124 | want := []byte{ |
| 125 | 0x00, 0x00, 0x00, 37, |
| 126 | 6, |
| 127 | 0x00, 0x00, 0x00, 42, |
| 128 | 0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e', |
| 129 | 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, |
| 130 | 0x00, 0x00, 0x00, 0x06, 'f', 'o', 'o', 'b', 'a', 'r', |
| 131 | } |
| 132 | |
| 133 | if !bytes.Equal(buf, want) { |
| 134 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 135 | } |
| 136 | |
| 137 | *p = WritePacket{} |
| 138 | |
| 139 | |
| 140 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 141 | t.Fatal("unexpected error:", err) |
| 142 | } |
| 143 | |
| 144 | if p.Handle != handle { |
| 145 | t.Errorf("UnmarshalPacketBody(): Handle was %q, but expected %q", p.Handle, handle) |
| 146 | } |
| 147 | |
| 148 | if p.Offset != offset { |
| 149 | t.Errorf("UnmarshalPacketBody(): Offset was %x, but expected %x", p.Offset, offset) |
| 150 | } |
| 151 | |
| 152 | if !bytes.Equal(p.Data, payload) { |
| 153 | t.Errorf("UnmarshalPacketBody(): Data was %X, but expected %X", p.Data, payload) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | var _ Packet = &FStatPacket{} |
| 158 | |
| 159 | func TestFStatPacket(t *testing.T) { |
| 160 | const ( |
| 161 | id = 42 |
| 162 | handle = "somehandle" |
| 163 | ) |
| 164 | |
| 165 | p := &FStatPacket{ |
| 166 | Handle: "somehandle", |
| 167 | } |
| 168 | |
| 169 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 170 | if err != nil { |
| 171 | t.Fatal("unexpected error:", err) |
| 172 | } |
| 173 | |
| 174 | want := []byte{ |
| 175 | 0x00, 0x00, 0x00, 19, |
| 176 | 8, |
| 177 | 0x00, 0x00, 0x00, 42, |
| 178 | 0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e', |
| 179 | } |
| 180 | |
| 181 | if !bytes.Equal(buf, want) { |
| 182 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 183 | } |
| 184 | |
| 185 | *p = FStatPacket{} |
| 186 | |
| 187 | |
| 188 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 189 | t.Fatal("unexpected error:", err) |
| 190 | } |
| 191 | |
| 192 | if p.Handle != handle { |
| 193 | t.Errorf("UnmarshalPacketBody(): Handle was %q, but expected %q", p.Handle, handle) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | var _ Packet = &FSetstatPacket{} |
| 198 | |
| 199 | func TestFSetstatPacket(t *testing.T) { |
| 200 | const ( |
| 201 | id = 42 |
| 202 | handle = "somehandle" |
| 203 | perms = 0x87654321 |
| 204 | ) |
| 205 | |
| 206 | p := &FSetstatPacket{ |
| 207 | Handle: "somehandle", |
| 208 | Attrs: Attributes{ |
| 209 | Flags: AttrPermissions, |
| 210 | Permissions: perms, |
| 211 | }, |
| 212 | } |
| 213 | |
| 214 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 215 | if err != nil { |
| 216 | t.Fatal("unexpected error:", err) |
| 217 | } |
| 218 | |
| 219 | want := []byte{ |
| 220 | 0x00, 0x00, 0x00, 27, |
| 221 | 10, |
| 222 | 0x00, 0x00, 0x00, 42, |
| 223 | 0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e', |
| 224 | 0x00, 0x00, 0x00, 0x04, |
| 225 | 0x87, 0x65, 0x43, 0x21, |
| 226 | } |
| 227 | |
| 228 | if !bytes.Equal(buf, want) { |
| 229 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 230 | } |
| 231 | |
| 232 | *p = FSetstatPacket{} |
| 233 | |
| 234 | |
| 235 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 236 | t.Fatal("unexpected error:", err) |
| 237 | } |
| 238 | |
| 239 | if p.Handle != handle { |
| 240 | t.Errorf("UnmarshalPacketBody(): Handle was %q, but expected %q", p.Handle, handle) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | var _ Packet = &ReadDirPacket{} |
| 245 | |
| 246 | func TestReadDirPacket(t *testing.T) { |
| 247 | const ( |
| 248 | id = 42 |
| 249 | handle = "somehandle" |
| 250 | ) |
| 251 | |
| 252 | p := &ReadDirPacket{ |
| 253 | Handle: "somehandle", |
| 254 | } |
| 255 | |
| 256 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 257 | if err != nil { |
| 258 | t.Fatal("unexpected error:", err) |
| 259 | } |
| 260 | |
| 261 | want := []byte{ |
| 262 | 0x00, 0x00, 0x00, 19, |
| 263 | 12, |
| 264 | 0x00, 0x00, 0x00, 42, |
| 265 | 0x00, 0x00, 0x00, 10, 's', 'o', 'm', 'e', 'h', 'a', 'n', 'd', 'l', 'e', |
| 266 | } |
| 267 | |
| 268 | if !bytes.Equal(buf, want) { |
| 269 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 270 | } |
| 271 | |
| 272 | *p = ReadDirPacket{} |
| 273 | |
| 274 | |
| 275 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 276 | t.Fatal("unexpected error:", err) |
| 277 | } |
| 278 | |
| 279 | if p.Handle != handle { |
| 280 | t.Errorf("UnmarshalPacketBody(): Handle was %q, but expected %q", p.Handle, handle) |
| 281 | } |
| 282 | } |