package certpaths import ( "os" "path/filepath" "runtime" "strings" "testing" ) func TestPaths_HonorORCAHOME(t *testing.T) { dir := t.TempDir() t.Setenv("ORCA_HOME", dir) // Ensure ORCA_DB doesn't leak from the environment / prior tests. t.Setenv("ORCA_DB", "") cases := []struct { name string got string file string }{ {"CACertPath", CACertPath(), "ca.crt"}, {"CAKeyPath", CAKeyPath(), "ca.key"}, {"ServerCertPath", ServerCertPath(), "server.crt"}, {"ServerKeyPath", ServerKeyPath(), "server.key"}, {"SSHKeyPath", SSHKeyPath(), "orca_ssh_key"}, {"SSHPubPath", SSHPubPath(), "orca_ssh_key.pub"}, {"KnownHostsPath", KnownHostsPath(), "known_hosts"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { want := filepath.Join(dir, tc.file) if tc.got != want { t.Errorf("%s = %q, want %q", tc.name, tc.got, want) } }) } // DBPath defaults to $ORCA_HOME/orca.db. if got, want := DBPath(), filepath.Join(dir, "orca.db"); got != want { t.Errorf("DBPath = %q, want %q", got, want) } // Dir() returns ORCA_HOME verbatim. if got, want := Dir(), dir; got != want { t.Errorf("Dir = %q, want %q", got, want) } } func TestDBPath_OrcaDBOverride(t *testing.T) { home := t.TempDir() t.Setenv("ORCA_HOME", home) custom := filepath.Join(t.TempDir(), "custom.db") t.Setenv("ORCA_DB", custom) if got := DBPath(); got != custom { t.Errorf("DBPath = %q, want %q (ORCA_DB override)", got, custom) } } func TestDBPath_OrcaDBEmptyStringFallsBackToHome(t *testing.T) { home := t.TempDir() t.Setenv("ORCA_HOME", home) t.Setenv("ORCA_DB", "") want := filepath.Join(home, "orca.db") if got := DBPath(); got != want { t.Errorf("DBPath = %q, want %q", got, want) } } func TestDir_DefaultHomeFallback(t *testing.T) { // Unset ORCA_HOME so Dir() falls back to ~/.orca. // We can't reliably mutate the real HOME in a portable way, so just // assert that the returned path ends with the default subdir on the // current OS and is absolute. os.Unsetenv("ORCA_HOME") // Also clear ORCA_DB so DBPath's fallback to Dir() is exercised. os.Unsetenv("ORCA_DB") home, err := os.UserHomeDir() if err != nil { t.Skipf("os.UserHomeDir: %v (cannot verify default fallback)", err) } want := filepath.Join(home, defaultCADir) if got := Dir(); got != want { t.Errorf("Dir() default = %q, want %q", got, want) } if got := CACertPath(); got != filepath.Join(want, "ca.crt") { t.Errorf("CACertPath default = %q, want %q", got, filepath.Join(want, "ca.crt")) } } func TestDir_ORCAHOMEEmptyFallsBack(t *testing.T) { // Empty string ORCA_HOME is treated as unset → ~/.orca fallback. t.Setenv("ORCA_HOME", "") home, err := os.UserHomeDir() if err != nil { t.Skipf("os.UserHomeDir: %v", err) } want := filepath.Join(home, defaultCADir) if got := Dir(); got != want { t.Errorf("Dir() with empty ORCA_HOME = %q, want %q", got, want) } } func TestDir_ORCAHOMERelativePath(t *testing.T) { // A relative ORCA_HOME is honored verbatim (no cleaning/absolutizing). t.Setenv("ORCA_HOME", "relative/orca/home") if got, want := Dir(), "relative/orca/home"; got != want { t.Errorf("Dir() relative = %q, want %q", got, want) } // CACertPath joins the relative dir with ca.crt using filepath.Join. if got, want := CACertPath(), filepath.Join("relative/orca/home", "ca.crt"); got != want { t.Errorf("CACertPath relative = %q, want %q", got, want) } } func TestAllPaths_AreConsistentWithDir(t *testing.T) { dir := t.TempDir() t.Setenv("ORCA_HOME", dir) t.Setenv("ORCA_DB", "") // Every *Path() must live under Dir() except DBPath which also does. base := Dir() for _, p := range []string{ CACertPath(), CAKeyPath(), ServerCertPath(), ServerKeyPath(), SSHKeyPath(), SSHPubPath(), KnownHostsPath(), DBPath(), } { if !strings.HasPrefix(p, base+string(filepath.Separator)) && p != filepath.Join(base, filepath.Base(p)) { t.Errorf("path %q is not under Dir() %q", p, base) } } } func TestSSHPaths_Filenames(t *testing.T) { dir := t.TempDir() t.Setenv("ORCA_HOME", dir) if got, want := filepath.Base(SSHKeyPath()), "orca_ssh_key"; got != want { t.Errorf("SSHKeyPath base = %q, want %q", got, want) } if got, want := filepath.Base(SSHPubPath()), "orca_ssh_key.pub"; got != want { t.Errorf("SSHPubPath base = %q, want %q", got, want) } if got, want := filepath.Base(KnownHostsPath()), "known_hosts"; got != want { t.Errorf("KnownHostsPath base = %q, want %q", got, want) } } func init() { // On Windows the default home subdir is still ".orca"; the test for // default fallback uses os.UserHomeDir which is platform-aware. This // guard keeps the suite from running a meaningless check on plan9. _ = runtime.GOOS }