返回 DeepSeek-Reasonix
transport_stdio_env_test.go
根目录 / internal / plugin / transport_stdio_env_test.go
1 package plugin
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "runtime"
8 "strings"
9 "testing"
10
11 "reasonix/internal/sandbox"
12 "reasonix/internal/secrets"
13 )
14
15 func TestStdioShellPATHProbeFiltersEnvWhenEnabled(t *testing.T) {
16 if runtime.GOOS == "windows" {
17 t.Skip("POSIX shell probe")
18 }
19 secrets.SetFilterSubprocessEnv(true)
20 t.Cleanup(func() { secrets.SetFilterSubprocessEnv(false) })
21 t.Setenv("REASONIX_TEST_SECRET_TOKEN", "ghp_abcdefghijklmnopqrstuvwxyz")
22
23 out := runShellPATHCommand(context.Background(), "/bin/sh", []string{"-c", `printf 'tok=%s' "${REASONIX_TEST_SECRET_TOKEN:-none}"`})
24 if !strings.Contains(string(out), "tok=none") {
25 t.Fatalf("stdio shell PATH probe leaked filtered env: %q", out)
26 }
27 }
28
29 func TestPrepareMCPPrivateStateWindowsPreservesHostTemp(t *testing.T) {
30 root := filepath.Join(t.TempDir(), "mcp-state", "0123456789abcdef", "matlab")
31 hostTemp := `C:\Users\user\AppData\Local\Temp`
32 env := []string{"TMP=" + hostTemp, "TEMP=" + hostTemp, "TMPDIR=" + hostTemp}
33
34 _, got, err := prepareMCPPrivateStateForOS(Spec{StateDir: root}, sandbox.Spec{}, env, "windows")
35 if err != nil {
36 t.Fatal(err)
37 }
38 for _, key := range []string{"TMP", "TEMP", "TMPDIR"} {
39 if value, ok := envValue(got, key); !ok || value != hostTemp {
40 t.Fatalf("%s = %q, %v; want inherited host temp %q", key, value, ok, hostTemp)
41 }
42 }
43 if _, err := os.Stat(filepath.Join(root, "tmp")); !os.IsNotExist(err) {
44 t.Fatalf("private Windows temp directory exists or stat failed: %v", err)
45 }
46 for key, want := range map[string]string{
47 "XDG_CACHE_HOME": filepath.Join(root, "cache"),
48 "XDG_STATE_HOME": filepath.Join(root, "state"),
49 } {
50 if value, ok := envValue(got, key); !ok || value != want {
51 t.Fatalf("%s = %q, %v; want %q", key, value, ok, want)
52 }
53 }
54 }
55
56 func TestPrepareMCPPrivateStateUnixIsolatesTemp(t *testing.T) {
57 root := filepath.Join(t.TempDir(), "mcp-state", "matlab")
58 hostTemp := "/tmp/host"
59 env := []string{"TMP=" + hostTemp, "TEMP=" + hostTemp, "TMPDIR=" + hostTemp}
60
61 _, got, err := prepareMCPPrivateStateForOS(Spec{StateDir: root}, sandbox.Spec{}, env, "linux")
62 if err != nil {
63 t.Fatal(err)
64 }
65 want := filepath.Join(root, "tmp")
66 for _, key := range []string{"TMP", "TEMP", "TMPDIR"} {
67 if value, ok := envValue(got, key); !ok || value != want {
68 t.Fatalf("%s = %q, %v; want private temp %q", key, value, ok, want)
69 }
70 }
71 if info, err := os.Stat(want); err != nil || !info.IsDir() {
72 t.Fatalf("private Unix temp directory = (%v, %v), want directory", info, err)
73 }
74 }
75
75 lines GO