ysm starts its own agent on $XDG_RUNTIME_DIR/ysm-agent.sock, reconnects to it on later runs, and shows agent status in the header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.0 KiB
Go
65 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseFingerprint(t *testing.T) {
|
|
bits, fp, comment, typ, ok := parseFingerprint("256 SHA256:abc me@box two (ED25519-SK)")
|
|
if !ok || bits != "256" || fp != "SHA256:abc" || comment != "me@box two" || typ != "ED25519-SK" {
|
|
t.Fatalf("got %q %q %q %q %v", bits, fp, comment, typ, ok)
|
|
}
|
|
if _, _, _, _, ok := parseFingerprint(""); ok {
|
|
t.Fatal("empty line parsed")
|
|
}
|
|
}
|
|
|
|
func TestLoadKeysAndValidate(t *testing.T) {
|
|
dir := t.TempDir()
|
|
out, err := exec.Command("ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "test@ysm", "-f", filepath.Join(dir, "id_test")).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%v: %s", err, out)
|
|
}
|
|
os.WriteFile(filepath.Join(dir, "junk.pub"), []byte("not a key"), 0o600)
|
|
|
|
keys, err := loadKeys(dir)
|
|
if err != nil || len(keys) != 1 {
|
|
t.Fatalf("keys=%v err=%v", keys, err)
|
|
}
|
|
k := keys[0]
|
|
if k.Name != "id_test" || k.Type != "ED25519" || k.Comment != "test@ysm" || !k.HasPrivate || k.Hardware() {
|
|
t.Fatalf("bad key %+v", k)
|
|
}
|
|
|
|
for name, wantErr := range map[string]bool{"id_test": true, "": true, "../x": true, "a.pub": true, ".hidden": true, "id_new": false} {
|
|
if err := validateName(dir, name); (err != nil) != wantErr {
|
|
t.Errorf("validateName(%q) = %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEnsureAgent(t *testing.T) {
|
|
t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
|
|
t.Setenv("SSH_AUTH_SOCK", "/nonexistent/agent.sock")
|
|
t.Setenv("SSH_AGENT_PID", "")
|
|
|
|
started, err := ensureAgent()
|
|
if err != nil || !started || !agentRunning() {
|
|
t.Fatalf("started=%v err=%v running=%v", started, err, agentRunning())
|
|
}
|
|
pid := os.Getenv("SSH_AGENT_PID")
|
|
defer exec.Command("kill", pid).Run()
|
|
|
|
if started, err := ensureAgent(); err != nil || started {
|
|
t.Fatalf("second call started=%v err=%v", started, err)
|
|
}
|
|
// A new ysm run without SSH_AUTH_SOCK finds the agent it started before.
|
|
os.Unsetenv("SSH_AUTH_SOCK")
|
|
if !connectAgent() || os.Getenv("SSH_AUTH_SOCK") != agentSocket() {
|
|
t.Fatal("did not reconnect to ysm's agent")
|
|
}
|
|
}
|