github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/filestore/filestore_unix_test.go (about)

     1  //go:build !windows
     2  
     3  package filestore
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"syscall"
    10  	"testing"
    11  )
    12  
    13  func TestFileStoreCreatesEveryParentOwnerOnlyUnderPermissiveUmask(t *testing.T) {
    14  	// Every directory the store creates — including intermediate parents,
    15  	// not just the leaf — is 0o700, even under a permissive umask.
    16  	old := syscall.Umask(0)
    17  	defer syscall.Umask(old)
    18  
    19  	tmp := t.TempDir()
    20  	root := filepath.Join(tmp, "nested", "store")
    21  	store := createdStore(t, root)
    22  	put(t, store, "projects/foo/notes.md", "hi")
    23  	for _, d := range []string{
    24  		filepath.Join(tmp, "nested"),
    25  		root,
    26  		filepath.Join(root, "projects"),
    27  		filepath.Join(root, "projects", "foo"),
    28  	} {
    29  		st, err := os.Stat(d)
    30  		if err != nil {
    31  			t.Fatalf("stat %s: %v", d, err)
    32  		}
    33  		if st.Mode().Perm() != 0o700 {
    34  			t.Fatalf("%s has mode %v, want 0o700", d, st.Mode().Perm())
    35  		}
    36  	}
    37  }
    38  
    39  func TestFileStoreGetRefusesANonRegularFile(t *testing.T) {
    40  	// Get must refuse — not hang on — a FIFO, and refuse a symlink leaf.
    41  	store, root := newStore(t)
    42  	put(t, store, "real.md", "data")
    43  	if err := syscall.Mkfifo(filepath.Join(root, "pipe"), 0o600); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if err := os.Symlink(filepath.Join(root, "real.md"), filepath.Join(root, "link.md")); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	_, err := store.Get("pipe")
    51  	requireRefused(t, err)
    52  	if !strings.Contains(err.Error(), "not a regular file") {
    53  		t.Fatalf("error %q does not say not a regular file", err)
    54  	}
    55  	_, err = store.Get("link.md")
    56  	requireRefused(t, err)
    57  	if !strings.Contains(err.Error(), "is a symlink") {
    58  		t.Fatalf("error %q does not say is a symlink", err)
    59  	}
    60  
    61  	// The open itself must refuse the fifo (O_NONBLOCK + fstat) — a swap
    62  	// after a caller's lstat would otherwise block the read forever.
    63  	_, err = openRegularFile("pipe", filepath.Join(root, "pipe"))
    64  	requireRefused(t, err)
    65  }