MCPcopy
hub / github.com/syncthing/syncthing / TestXattr

Function TestXattr

lib/fs/basicfs_test.go:573–651  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

571}
572
573func 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 }

Callers

nothing calls this directly

Calls 10

IntnFunction · 0.92
ReadFunction · 0.92
setupFunction · 0.85
FatalMethod · 0.80
MkdirMethod · 0.65
SetXattrMethod · 0.65
GetXattrMethod · 0.65
IsMethod · 0.45
EqualMethod · 0.45
CompareMethod · 0.45

Tested by

no test coverage detected