Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

4.1 KB
Raw Blame
package gitwork

import (
	"crypto/sha256"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestBindSSHRemotesRewritesSortedURLsAfterExistingConfig(t *testing.T) {
	keyed := "ssh://[email protected]:2222/group/b.git"
	plain := "[email protected]:group/a.git"
	env := map[string]string{"GIT_CONFIG_COUNT": "1"}
	bindings, remotes, err := bindSSHRemotes(env, map[string]string{keyed: "KEY", plain: ""})
	if err != nil {
		t.Fatal(err)
	}
	if len(bindings) != 1 || len(remotes) != 1 {
		t.Fatalf("bindings=%d remotes=%d, want 1/1", len(bindings), len(remotes))
	}
	remote, err := ParseSSHRemote(keyed)
	if err != nil {
		t.Fatal(err)
	}
	sum := sha256.Sum256([]byte(remote.URL))
	alias := fmt.Sprintf("lf-%x", sum[:16])
	if got := bindings[0]; got.alias != alias || got.key != "KEY" || got.remote != remote || remotes[0] != remote {
		t.Fatalf("binding = %+v, want alias %s for %+v", got, alias, remote)
	}
	want := map[string]string{
		"GIT_CONFIG_COUNT":   "3",
		"GIT_CONFIG_KEY_1":   "url." + plain + ".insteadOf",
		"GIT_CONFIG_VALUE_1": plain,
		"GIT_CONFIG_KEY_2":   "url." + remote.aliasURL(alias) + ".insteadOf",
		"GIT_CONFIG_VALUE_2": keyed,
	}
	if len(env) != len(want) {
		t.Fatalf("env = %v, want %v", env, want)
	}
	for k, v := range want {
		if env[k] != v {
			t.Fatalf("env[%s] = %q, want %q", k, env[k], v)
		}
	}
}

func TestBindSSHRemotesRejectsInvalidRemote(t *testing.T) {
	env := map[string]string{}
	if _, _, err := bindSSHRemotes(env, map[string]string{"https://example.com/a.git": "KEY"}); err == nil {
		t.Fatal("expected invalid SSH remote error")
	}
}

func TestStartSSHWithoutAssignedKeyOnlyRewritesURLs(t *testing.T) {
	raw := "[email protected]:group/a.git"
	env := map[string]string{}
	stop, err := StartSSH(env, map[string]string{raw: ""}, t.TempDir(), "")
	if err != nil {
		t.Fatal(err)
	}
	stop()
	if env["GIT_CONFIG_COUNT"] != "1" || env["GIT_CONFIG_VALUE_0"] != raw || env["GIT_SSH_VARIANT"] != "ssh" {
		t.Fatalf("env = %v", env)
	}
	if env["GIT_SSH_COMMAND"] == "" {
		t.Fatal("expected failing GIT_SSH_COMMAND without assigned key")
	}
}

// A workspace CLI clones whatever URL it likes, so personal keys are bound
// by host:port instead of by registered remote.
func TestStartSSHHostsBindsByEndpoint(t *testing.T) {
	env := map[string]string{}
	stop, err := StartSSHHosts(env, map[string]string{"git.example.org:22": "", "other.example.org:2222": " "}, t.TempDir(), "")
	if err != nil || len(env) != 0 {
		t.Fatalf("keys without content must bind nothing: env=%v err=%v", env, err)
	}
	stop()
	if _, err := StartSSHHosts(map[string]string{}, map[string]string{"git.example.org": "KEY"}, t.TempDir(), ""); err == nil {
		t.Fatal("an endpoint without a port was accepted")
	}
	if got := (sshBinding{alias: "lf-x", match: "git.example.org"}).matchHost(); got != "git.example.org" {
		t.Fatalf("host-bound match = %q", got)
	}
	if got := (sshBinding{alias: "lf-x"}).matchHost(); got != "lf-x" {
		t.Fatalf("remote-bound match = %q", got)
	}
}

func TestBuiltinKnownHostsTrustGitHubOnly(t *testing.T) {
	t.Setenv(SSHKnownHostsEnv, "")
	pinned, err := sshHostKeys([]SSHRemote{{Host: "github.com", Port: "22"}, {Host: "ssh.github.com", Port: "443"}})
	if err != nil {
		t.Fatalf("github.com should be trusted without configuration: %v", err)
	}
	if got := string(pinned); !strings.Contains(got, "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl") || !strings.Contains(got, "[ssh.github.com]:443 ssh-rsa") {
		t.Fatalf("pinned keys = %s", got)
	}
	if _, err := sshHostKeys([]SSHRemote{{Host: "git.example.org", Port: "22"}}); err == nil || !strings.Contains(err.Error(), SSHKnownHostsEnv) {
		t.Fatalf("other hosts still need configured keys: %v", err)
	}
	file := filepath.Join(t.TempDir(), "known_hosts")
	if err := os.WriteFile(file, []byte("git.example.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n"), 0o600); err != nil {
		t.Fatal(err)
	}
	t.Setenv(SSHKnownHostsEnv, file)
	if _, err := sshHostKeys([]SSHRemote{{Host: "git.example.org", Port: "22"}, {Host: "github.com", Port: "22"}}); err != nil {
		t.Fatalf("configured and built-in keys combine: %v", err)
	}
}