| 571 | } |
| 572 | |
| 573 | func TestXattr(t *testing.T) { |
| 574 | tfs, _ := setup(t) |
| 575 | if err := tfs.Mkdir("/test", 0o755); err != nil { |
| 576 | t.Fatal(err) |
| 577 | } |
| 578 | |
| 579 | xattrSize := func() int { return 20 + rand.Intn(20) } |
| 580 | |
| 581 | // Create a set of random attributes that we will set and read back |
| 582 | var attrs []protocol.Xattr |
| 583 | for i := 0; i < 10; i++ { |
| 584 | key := fmt.Sprintf("user.test-%d", i) |
| 585 | value := make([]byte, xattrSize()) |
| 586 | rand.Read(value) |
| 587 | attrs = append(attrs, protocol.Xattr{ |
| 588 | Name: key, |
| 589 | Value: value, |
| 590 | }) |
| 591 | } |
| 592 | |
| 593 | // Set the xattrs, read them back and compare |
| 594 | if err := tfs.SetXattr("/test", attrs, testXattrFilter{}); errors.Is(err, ErrXattrsNotSupported) || errors.Is(err, syscall.EOPNOTSUPP) { |
| 595 | t.Skip("xattrs not supported") |
| 596 | } else if err != nil { |
| 597 | t.Fatal(err) |
| 598 | } |
| 599 | res, err := tfs.GetXattr("/test", testXattrFilter{}) |
| 600 | if err != nil { |
| 601 | t.Fatal(err) |
| 602 | } |
| 603 | if len(res) != len(attrs) { |
| 604 | t.Fatalf("length of returned xattrs does not match (%d != %d)", len(res), len(attrs)) |
| 605 | } |
| 606 | for i, xa := range res { |
| 607 | if xa.Name != attrs[i].Name { |
| 608 | t.Errorf("xattr name %q != %q", xa.Name, attrs[i].Name) |
| 609 | } |
| 610 | if !bytes.Equal(xa.Value, attrs[i].Value) { |
| 611 | t.Errorf("xattr value %q != %q", xa.Value, attrs[i].Value) |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Remove a couple, change a couple, and add another couple of |
| 616 | // attributes. Replacing the xattrs again should work. |
| 617 | attrs = attrs[2:] |
| 618 | attrs[1].Value = make([]byte, xattrSize()) |
| 619 | rand.Read(attrs[1].Value) |
| 620 | attrs[3].Value = make([]byte, xattrSize()) |
| 621 | rand.Read(attrs[3].Value) |
| 622 | for i := 10; i < 12; i++ { |
| 623 | key := fmt.Sprintf("user.test-%d", i) |
| 624 | value := make([]byte, xattrSize()) |
| 625 | rand.Read(value) |
| 626 | attrs = append(attrs, protocol.Xattr{ |
| 627 | Name: key, |
| 628 | Value: value, |
| 629 | }) |
| 630 | } |