| 1 | package sshfx |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | var _ Packet = &LStatPacket{} |
| 9 | |
| 10 | func TestLStatPacket(t *testing.T) { |
| 11 | const ( |
| 12 | id = 42 |
| 13 | path = "/foo" |
| 14 | ) |
| 15 | |
| 16 | p := &LStatPacket{ |
| 17 | Path: path, |
| 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, 13, |
| 27 | 7, |
| 28 | 0x00, 0x00, 0x00, 42, |
| 29 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 30 | } |
| 31 | |
| 32 | if !bytes.Equal(buf, want) { |
| 33 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 34 | } |
| 35 | |
| 36 | *p = LStatPacket{} |
| 37 | |
| 38 | |
| 39 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 40 | t.Fatal("unexpected error:", err) |
| 41 | } |
| 42 | |
| 43 | if p.Path != path { |
| 44 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | var _ Packet = &SetstatPacket{} |
| 49 | |
| 50 | func TestSetstatPacket(t *testing.T) { |
| 51 | const ( |
| 52 | id = 42 |
| 53 | path = "/foo" |
| 54 | perms FileMode = 0x87654321 |
| 55 | ) |
| 56 | |
| 57 | p := &SetstatPacket{ |
| 58 | Path: "/foo", |
| 59 | Attrs: Attributes{ |
| 60 | Flags: AttrPermissions, |
| 61 | Permissions: perms, |
| 62 | }, |
| 63 | } |
| 64 | |
| 65 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 66 | if err != nil { |
| 67 | t.Fatal("unexpected error:", err) |
| 68 | } |
| 69 | |
| 70 | want := []byte{ |
| 71 | 0x00, 0x00, 0x00, 21, |
| 72 | 9, |
| 73 | 0x00, 0x00, 0x00, 42, |
| 74 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 75 | 0x00, 0x00, 0x00, 0x04, |
| 76 | 0x87, 0x65, 0x43, 0x21, |
| 77 | } |
| 78 | |
| 79 | if !bytes.Equal(buf, want) { |
| 80 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 81 | } |
| 82 | |
| 83 | *p = SetstatPacket{} |
| 84 | |
| 85 | |
| 86 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 87 | t.Fatal("unexpected error:", err) |
| 88 | } |
| 89 | |
| 90 | if p.Path != path { |
| 91 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 92 | } |
| 93 | |
| 94 | if p.Attrs.Flags != AttrPermissions { |
| 95 | t.Errorf("UnmarshalPacketBody(): Attrs.Flags was %#x, but expected %#x", p.Attrs.Flags, AttrPermissions) |
| 96 | } |
| 97 | |
| 98 | if p.Attrs.Permissions != perms { |
| 99 | t.Errorf("UnmarshalPacketBody(): Attrs.Permissions was %#v, but expected %#v", p.Attrs.Permissions, perms) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | var _ Packet = &RemovePacket{} |
| 104 | |
| 105 | func TestRemovePacket(t *testing.T) { |
| 106 | const ( |
| 107 | id = 42 |
| 108 | path = "/foo" |
| 109 | ) |
| 110 | |
| 111 | p := &RemovePacket{ |
| 112 | Path: path, |
| 113 | } |
| 114 | |
| 115 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 116 | if err != nil { |
| 117 | t.Fatal("unexpected error:", err) |
| 118 | } |
| 119 | |
| 120 | want := []byte{ |
| 121 | 0x00, 0x00, 0x00, 13, |
| 122 | 13, |
| 123 | 0x00, 0x00, 0x00, 42, |
| 124 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 125 | } |
| 126 | |
| 127 | if !bytes.Equal(buf, want) { |
| 128 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 129 | } |
| 130 | |
| 131 | *p = RemovePacket{} |
| 132 | |
| 133 | |
| 134 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 135 | t.Fatal("unexpected error:", err) |
| 136 | } |
| 137 | |
| 138 | if p.Path != path { |
| 139 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | var _ Packet = &MkdirPacket{} |
| 144 | |
| 145 | func TestMkdirPacket(t *testing.T) { |
| 146 | const ( |
| 147 | id = 42 |
| 148 | path = "/foo" |
| 149 | perms FileMode = 0x87654321 |
| 150 | ) |
| 151 | |
| 152 | p := &MkdirPacket{ |
| 153 | Path: "/foo", |
| 154 | Attrs: Attributes{ |
| 155 | Flags: AttrPermissions, |
| 156 | Permissions: perms, |
| 157 | }, |
| 158 | } |
| 159 | |
| 160 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 161 | if err != nil { |
| 162 | t.Fatal("unexpected error:", err) |
| 163 | } |
| 164 | |
| 165 | want := []byte{ |
| 166 | 0x00, 0x00, 0x00, 21, |
| 167 | 14, |
| 168 | 0x00, 0x00, 0x00, 42, |
| 169 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 170 | 0x00, 0x00, 0x00, 0x04, |
| 171 | 0x87, 0x65, 0x43, 0x21, |
| 172 | } |
| 173 | |
| 174 | if !bytes.Equal(buf, want) { |
| 175 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 176 | } |
| 177 | |
| 178 | *p = MkdirPacket{} |
| 179 | |
| 180 | |
| 181 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 182 | t.Fatal("unexpected error:", err) |
| 183 | } |
| 184 | |
| 185 | if p.Path != path { |
| 186 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 187 | } |
| 188 | |
| 189 | if p.Attrs.Flags != AttrPermissions { |
| 190 | t.Errorf("UnmarshalPacketBody(): Attrs.Flags was %#x, but expected %#x", p.Attrs.Flags, AttrPermissions) |
| 191 | } |
| 192 | |
| 193 | if p.Attrs.Permissions != perms { |
| 194 | t.Errorf("UnmarshalPacketBody(): Attrs.Permissions was %#v, but expected %#v", p.Attrs.Permissions, perms) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | var _ Packet = &RmdirPacket{} |
| 199 | |
| 200 | func TestRmdirPacket(t *testing.T) { |
| 201 | const ( |
| 202 | id = 42 |
| 203 | path = "/foo" |
| 204 | ) |
| 205 | |
| 206 | p := &RmdirPacket{ |
| 207 | Path: path, |
| 208 | } |
| 209 | |
| 210 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 211 | if err != nil { |
| 212 | t.Fatal("unexpected error:", err) |
| 213 | } |
| 214 | |
| 215 | want := []byte{ |
| 216 | 0x00, 0x00, 0x00, 13, |
| 217 | 15, |
| 218 | 0x00, 0x00, 0x00, 42, |
| 219 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 220 | } |
| 221 | |
| 222 | if !bytes.Equal(buf, want) { |
| 223 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 224 | } |
| 225 | |
| 226 | *p = RmdirPacket{} |
| 227 | |
| 228 | |
| 229 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 230 | t.Fatal("unexpected error:", err) |
| 231 | } |
| 232 | |
| 233 | if p.Path != path { |
| 234 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | var _ Packet = &RealPathPacket{} |
| 239 | |
| 240 | func TestRealPathPacket(t *testing.T) { |
| 241 | const ( |
| 242 | id = 42 |
| 243 | path = "/foo" |
| 244 | ) |
| 245 | |
| 246 | p := &RealPathPacket{ |
| 247 | Path: path, |
| 248 | } |
| 249 | |
| 250 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 251 | if err != nil { |
| 252 | t.Fatal("unexpected error:", err) |
| 253 | } |
| 254 | |
| 255 | want := []byte{ |
| 256 | 0x00, 0x00, 0x00, 13, |
| 257 | 16, |
| 258 | 0x00, 0x00, 0x00, 42, |
| 259 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 260 | } |
| 261 | |
| 262 | if !bytes.Equal(buf, want) { |
| 263 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 264 | } |
| 265 | |
| 266 | *p = RealPathPacket{} |
| 267 | |
| 268 | |
| 269 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 270 | t.Fatal("unexpected error:", err) |
| 271 | } |
| 272 | |
| 273 | if p.Path != path { |
| 274 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | var _ Packet = &StatPacket{} |
| 279 | |
| 280 | func TestStatPacket(t *testing.T) { |
| 281 | const ( |
| 282 | id = 42 |
| 283 | path = "/foo" |
| 284 | ) |
| 285 | |
| 286 | p := &StatPacket{ |
| 287 | Path: path, |
| 288 | } |
| 289 | |
| 290 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 291 | if err != nil { |
| 292 | t.Fatal("unexpected error:", err) |
| 293 | } |
| 294 | |
| 295 | want := []byte{ |
| 296 | 0x00, 0x00, 0x00, 13, |
| 297 | 17, |
| 298 | 0x00, 0x00, 0x00, 42, |
| 299 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 300 | } |
| 301 | |
| 302 | if !bytes.Equal(buf, want) { |
| 303 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 304 | } |
| 305 | |
| 306 | *p = StatPacket{} |
| 307 | |
| 308 | |
| 309 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 310 | t.Fatal("unexpected error:", err) |
| 311 | } |
| 312 | |
| 313 | if p.Path != path { |
| 314 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | var _ Packet = &RenamePacket{} |
| 319 | |
| 320 | func TestRenamePacket(t *testing.T) { |
| 321 | const ( |
| 322 | id = 42 |
| 323 | oldpath = "/foo" |
| 324 | newpath = "/bar" |
| 325 | ) |
| 326 | |
| 327 | p := &RenamePacket{ |
| 328 | OldPath: oldpath, |
| 329 | NewPath: newpath, |
| 330 | } |
| 331 | |
| 332 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 333 | if err != nil { |
| 334 | t.Fatal("unexpected error:", err) |
| 335 | } |
| 336 | |
| 337 | want := []byte{ |
| 338 | 0x00, 0x00, 0x00, 21, |
| 339 | 18, |
| 340 | 0x00, 0x00, 0x00, 42, |
| 341 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 342 | 0x00, 0x00, 0x00, 4, '/', 'b', 'a', 'r', |
| 343 | } |
| 344 | |
| 345 | if !bytes.Equal(buf, want) { |
| 346 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 347 | } |
| 348 | |
| 349 | *p = RenamePacket{} |
| 350 | |
| 351 | |
| 352 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 353 | t.Fatal("unexpected error:", err) |
| 354 | } |
| 355 | |
| 356 | if p.OldPath != oldpath { |
| 357 | t.Errorf("UnmarshalPacketBody(): OldPath was %q, but expected %q", p.OldPath, oldpath) |
| 358 | } |
| 359 | |
| 360 | if p.NewPath != newpath { |
| 361 | t.Errorf("UnmarshalPacketBody(): NewPath was %q, but expected %q", p.NewPath, newpath) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | var _ Packet = &ReadLinkPacket{} |
| 366 | |
| 367 | func TestReadLinkPacket(t *testing.T) { |
| 368 | const ( |
| 369 | id = 42 |
| 370 | path = "/foo" |
| 371 | ) |
| 372 | |
| 373 | p := &ReadLinkPacket{ |
| 374 | Path: path, |
| 375 | } |
| 376 | |
| 377 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 378 | if err != nil { |
| 379 | t.Fatal("unexpected error:", err) |
| 380 | } |
| 381 | |
| 382 | want := []byte{ |
| 383 | 0x00, 0x00, 0x00, 13, |
| 384 | 19, |
| 385 | 0x00, 0x00, 0x00, 42, |
| 386 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 387 | } |
| 388 | |
| 389 | if !bytes.Equal(buf, want) { |
| 390 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 391 | } |
| 392 | |
| 393 | *p = ReadLinkPacket{} |
| 394 | |
| 395 | |
| 396 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 397 | t.Fatal("unexpected error:", err) |
| 398 | } |
| 399 | |
| 400 | if p.Path != path { |
| 401 | t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", p.Path, path) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | var _ Packet = &SymlinkPacket{} |
| 406 | |
| 407 | func TestSymlinkPacket(t *testing.T) { |
| 408 | const ( |
| 409 | id = 42 |
| 410 | linkpath = "/foo" |
| 411 | targetpath = "/bar" |
| 412 | ) |
| 413 | |
| 414 | p := &SymlinkPacket{ |
| 415 | LinkPath: linkpath, |
| 416 | TargetPath: targetpath, |
| 417 | } |
| 418 | |
| 419 | buf, err := ComposePacket(p.MarshalPacket(id, nil)) |
| 420 | if err != nil { |
| 421 | t.Fatal("unexpected error:", err) |
| 422 | } |
| 423 | |
| 424 | want := []byte{ |
| 425 | 0x00, 0x00, 0x00, 21, |
| 426 | 20, |
| 427 | 0x00, 0x00, 0x00, 42, |
| 428 | 0x00, 0x00, 0x00, 4, '/', 'b', 'a', 'r', |
| 429 | 0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o', |
| 430 | } |
| 431 | |
| 432 | if !bytes.Equal(buf, want) { |
| 433 | t.Fatalf("MarshalPacket() = %X, but wanted %X", buf, want) |
| 434 | } |
| 435 | |
| 436 | *p = SymlinkPacket{} |
| 437 | |
| 438 | |
| 439 | if err := p.UnmarshalPacketBody(NewBuffer(buf[9:])); err != nil { |
| 440 | t.Fatal("unexpected error:", err) |
| 441 | } |
| 442 | |
| 443 | if p.LinkPath != linkpath { |
| 444 | t.Errorf("UnmarshalPacketBody(): LinkPath was %q, but expected %q", p.LinkPath, linkpath) |
| 445 | } |
| 446 | |
| 447 | if p.TargetPath != targetpath { |
| 448 | t.Errorf("UnmarshalPacketBody(): TargetPath was %q, but expected %q", p.TargetPath, targetpath) |
| 449 | } |
| 450 | } |