package gitutil import ( "context" "fmt" "net/http" "net/http/httptest" "os/exec" "slices" "strings" "testing" ) // os/exec refuses to start a command that carries a Cancel it did not get from // CommandContext, and the sandbox-wrapped commands in knotserver are exactly // that shape, so containment has to degrade instead of breaking them func TestContainContextlessCommand(t *testing.T) { cmd := Contain(exec.Command("git", "version")) out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("%v: %s", err, out) } if !cmd.SysProcAttr.Setpgid { t.Fatal("a contained command has to lead its own process group") } if cmd.WaitDelay == 0 { t.Fatal("a contained command has to bound its wait") } } func TestCommandSpeaksEnglishWhateverTheHostLocale(t *testing.T) { t.Setenv("LC_ALL", "de_DE.UTF-8") t.Setenv("LANG", "de_DE.UTF-8") t.Setenv("LANGUAGE", "de") out, err := Command(context.Background(), "-C", t.TempDir(), "rev-parse", "HEAD").CombinedOutput() if err == nil { t.Fatalf("rev-parse outside a repo succeeded: %s", out) } if !strings.Contains(string(out), "not a git repository") { t.Fatalf("git said %q, want its English message so the callers' matches hold", out) } } func TestCommandSendsTheHeaderOnlyToItsURL(t *testing.T) { if env := Command(context.Background(), "version").Env; slices.ContainsFunc(env, func(v string) bool { return strings.HasPrefix(v, "GIT_CONFIG_") }) { t.Fatalf("a plain command set git config: %v", env) } minted := 0 ctx := WithHTTPHeader(context.Background(), "https://knot.test/did:plc:repo", func() string { minted++ return fmt.Sprintf("Authorization: Bearer %d", minted) }) Command(ctx, "version") env := Command(ctx, "version").Env for _, want := range []string{ "GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=http.https://knot.test/did:plc:repo.extraHeader", "GIT_CONFIG_VALUE_0=Authorization: Bearer 2", } { if !slices.Contains(env, want) { t.Errorf("env lacks %q, want a fresh header scoped to the url", want) } } if env[len(env)-1] != "LC_ALL=C" { t.Errorf("env ends with %q, want LC_ALL=C last", env[len(env)-1]) } } func TestGitSendsTheHeaderToTheRemote(t *testing.T) { got := make(chan string, 4) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { got <- r.Header.Get("Authorization") http.NotFound(w, r) })) t.Cleanup(srv.Close) remote := srv.URL + "/did:plc:repo" ctx := WithHTTPHeader(context.Background(), remote, func() string { return "Authorization: Bearer token" }) _ = Run(ctx, "ls-remote", remote) if header := <-got; header != "Bearer token" { t.Errorf("the remote got %q, want the header", header) } _ = Run(ctx, "ls-remote", srv.URL+"/did:plc:other") if header := <-got; header != "" { t.Errorf("another repo got %q, want nothing", header) } } func TestCommandPinsTheLocaleInItsEnvironment(t *testing.T) { t.Setenv("LC_ALL", "de_DE.UTF-8") env := Command(context.Background(), "version").Env if len(env) == 0 || env[len(env)-1] != "LC_ALL=C" { t.Fatalf("env ends with %q, want LC_ALL=C last so it beats the host's", env[max(0, len(env)-1):]) } }