aboutsummaryrefslogtreecommitdiff
path: root/config/config_test.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-01 10:57:26 +0800
committerGravatar Runxi Yu2026-03-01 11:53:21 +0800
commitc0d1cc442031c8200df4a3a499c07081fab40553 (patch)
tree6489cb5ab8469e85e20da862795a10122109e4d5 /config/config_test.go
parentobject: Rename ident to signature (diff)
signatureNo signature
config: Add fuzz, regression tests, and updates v0.1.39
Diffstat (limited to 'config/config_test.go')
-rw-r--r--config/config_test.go281
1 files changed, 269 insertions, 12 deletions
diff --git a/config/config_test.go b/config/config_test.go
index 9a630415..416222e7 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -1,7 +1,9 @@
package config_test
import (
+ "bytes"
"os"
+ "os/exec"
"path/filepath"
"strings"
"testing"
@@ -25,6 +27,35 @@ func gitConfigGet(t *testing.T, testRepo *testgit.TestRepo, key string) string {
return testRepo.Run(t, "config", "--get", key)
}
+func gitConfigGetE(testRepo *testgit.TestRepo, key string) (string, error) {
+ //nolint:noctx
+ cmd := exec.Command("git", "config", "--get", key) //#nosec G204
+ cmd.Dir = testRepo.Dir()
+ cmd.Env = append(os.Environ(),
+ "GIT_CONFIG_GLOBAL=/dev/null",
+ "GIT_CONFIG_SYSTEM=/dev/null",
+ )
+ out, err := cmd.CombinedOutput()
+ return strings.TrimSpace(string(out)), err
+}
+
+func lookupValue(cfg *config.Config, section, subsection, key string) string {
+ result := cfg.Lookup(section, subsection, key)
+ if result.Kind == config.ValueMissing {
+ return ""
+ }
+ return result.Value
+}
+
+func lookupAllValues(cfg *config.Config, section, subsection, key string) []string {
+ results := cfg.LookupAll(section, subsection, key)
+ values := make([]string, 0, len(results))
+ for _, result := range results {
+ values = append(values, result.Value)
+ }
+ return values
+}
+
func TestConfigAgainstGit(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
@@ -42,16 +73,16 @@ func TestConfigAgainstGit(t *testing.T) {
t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("core", "", "bare"); got != "true" {
+ if got := lookupValue(cfg, "core", "", "bare"); got != "true" {
t.Errorf("core.bare: got %q, want %q", got, "true")
}
- if got := cfg.Get("core", "", "filemode"); got != "false" {
+ if got := lookupValue(cfg, "core", "", "filemode"); got != "false" {
t.Errorf("core.filemode: got %q, want %q", got, "false")
}
- if got := cfg.Get("user", "", "name"); got != "Jane Doe" {
+ if got := lookupValue(cfg, "user", "", "name"); got != "Jane Doe" {
t.Errorf("user.name: got %q, want %q", got, "Jane Doe")
}
- if got := cfg.Get("user", "", "email"); got != "jane@example.org" {
+ if got := lookupValue(cfg, "user", "", "email"); got != "jane@example.org" {
t.Errorf("user.email: got %q, want %q", got, "jane@example.org")
}
})
@@ -72,10 +103,10 @@ func TestConfigSubsectionAgainstGit(t *testing.T) {
t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("remote", "origin", "url"); got != "https://example.org/repo.git" {
+ if got := lookupValue(cfg, "remote", "origin", "url"); got != "https://example.org/repo.git" {
t.Errorf("remote.origin.url: got %q, want %q", got, "https://example.org/repo.git")
}
- if got := cfg.Get("remote", "origin", "fetch"); got != "+refs/heads/*:refs/remotes/origin/*" {
+ if got := lookupValue(cfg, "remote", "origin", "fetch"); got != "+refs/heads/*:refs/remotes/origin/*" {
t.Errorf("remote.origin.fetch: got %q, want %q", got, "+refs/heads/*:refs/remotes/origin/*")
}
})
@@ -97,7 +128,7 @@ func TestConfigMultiValueAgainstGit(t *testing.T) {
t.Fatalf("ParseConfig failed: %v", err)
}
- fetches := cfg.GetAll("remote", "origin", "fetch")
+ fetches := lookupAllValues(cfg, "remote", "origin", "fetch")
if len(fetches) != 3 {
t.Fatalf("expected 3 fetch values, got %d", len(fetches))
}
@@ -133,13 +164,13 @@ func TestConfigCaseInsensitiveAgainstGit(t *testing.T) {
t.Fatalf("ParseConfig failed: %v", err)
}
- if got := cfg.Get("core", "", "bare"); got != gitVerifyBare {
+ if got := lookupValue(cfg, "core", "", "bare"); got != gitVerifyBare {
t.Errorf("core.bare: got %q, want %q (from git)", got, gitVerifyBare)
}
- if got := cfg.Get("CORE", "", "BARE"); got != gitVerifyBare {
+ if got := lookupValue(cfg, "CORE", "", "BARE"); got != gitVerifyBare {
t.Errorf("CORE.BARE: got %q, want %q (from git)", got, gitVerifyBare)
}
- if got := cfg.Get("core", "", "filemode"); got != gitVerifyFilemode {
+ if got := lookupValue(cfg, "core", "", "filemode"); got != gitVerifyFilemode {
t.Errorf("core.filemode: got %q, want %q (from git)", got, gitVerifyFilemode)
}
})
@@ -173,13 +204,112 @@ func TestConfigBooleanAgainstGit(t *testing.T) {
}
for _, tt := range tests {
- if got := cfg.Get("test", "", tt.key); got != tt.want {
+ if got := lookupValue(cfg, "test", "", tt.key); got != tt.want {
t.Errorf("test.%s: got %q, want %q (from git)", tt.key, got, tt.want)
}
}
})
}
+func TestConfigLookupKindsAndBool(t *testing.T) {
+ t.Parallel()
+ cfgText := "[test]\nnovalue\nempty =\ntruthy = yes\nnumeric = -2\nleadspace = \" 1\"\nleadtab = \"\t-2\"\nksuffix = 1k\nhex = 0x10\nmaxi32 = 2147483647\ntoobig = 2147483648\ntoosmall = -2147483649\nbadnum = \" 2x\"\n"
+ cfg, err := config.ParseConfig(strings.NewReader(cfgText))
+ if err != nil {
+ t.Fatalf("ParseConfig failed: %v", err)
+ }
+
+ novalue := cfg.Lookup("test", "", "novalue")
+ if novalue.Kind != config.ValueValueless {
+ t.Fatalf("novalue kind: got %v, want %v", novalue.Kind, config.ValueValueless)
+ }
+ novalueBool, err := novalue.Bool()
+ if err != nil || !novalueBool {
+ t.Fatalf("novalue bool: got (%v, %v), want (true, nil)", novalueBool, err)
+ }
+
+ empty := cfg.Lookup("test", "", "empty")
+ if empty.Kind != config.ValueString || empty.Value != "" {
+ t.Fatalf("empty: got (%v, %q), want (%v, %q)", empty.Kind, empty.Value, config.ValueString, "")
+ }
+ emptyBool, err := empty.Bool()
+ if err != nil || emptyBool {
+ t.Fatalf("empty bool: got (%v, %v), want (false, nil)", emptyBool, err)
+ }
+
+ truthyBool, err := cfg.Lookup("test", "", "truthy").Bool()
+ if err != nil || !truthyBool {
+ t.Fatalf("truthy bool: got (%v, %v), want (true, nil)", truthyBool, err)
+ }
+ numericBool, err := cfg.Lookup("test", "", "numeric").Bool()
+ if err != nil || !numericBool {
+ t.Fatalf("numeric bool: got (%v, %v), want (true, nil)", numericBool, err)
+ }
+ leadspaceBool, err := cfg.Lookup("test", "", "leadspace").Bool()
+ if err != nil || !leadspaceBool {
+ t.Fatalf("leadspace bool: got (%v, %v), want (true, nil)", leadspaceBool, err)
+ }
+ leadtabBool, err := cfg.Lookup("test", "", "leadtab").Bool()
+ if err != nil || !leadtabBool {
+ t.Fatalf("leadtab bool: got (%v, %v), want (true, nil)", leadtabBool, err)
+ }
+ ksuffixBool, err := cfg.Lookup("test", "", "ksuffix").Bool()
+ if err != nil || !ksuffixBool {
+ t.Fatalf("ksuffix bool: got (%v, %v), want (true, nil)", ksuffixBool, err)
+ }
+ maxi32Bool, err := cfg.Lookup("test", "", "maxi32").Bool()
+ if err != nil || !maxi32Bool {
+ t.Fatalf("maxi32 bool: got (%v, %v), want (true, nil)", maxi32Bool, err)
+ }
+ if _, err := cfg.Lookup("test", "", "toobig").Bool(); err == nil {
+ t.Fatal("toobig bool: expected error")
+ }
+ if _, err := cfg.Lookup("test", "", "toosmall").Bool(); err == nil {
+ t.Fatal("toosmall bool: expected error")
+ }
+ if _, err := cfg.Lookup("test", "", "badnum").Bool(); err == nil {
+ t.Fatal("badnum bool: expected error")
+ }
+
+ if _, err := novalue.String(); err == nil {
+ t.Fatal("novalue string: expected error")
+ }
+ emptyString, err := empty.String()
+ if err != nil || emptyString != "" {
+ t.Fatalf("empty string: got (%q, %v), want (%q, nil)", emptyString, err, "")
+ }
+
+ numericInt, err := cfg.Lookup("test", "", "numeric").Int()
+ if err != nil || numericInt != -2 {
+ t.Fatalf("numeric int: got (%v, %v), want (-2, nil)", numericInt, err)
+ }
+ ksuffixInt, err := cfg.Lookup("test", "", "ksuffix").Int()
+ if err != nil || ksuffixInt != 1024 {
+ t.Fatalf("ksuffix int: got (%v, %v), want (1024, nil)", ksuffixInt, err)
+ }
+ hexInt64, err := cfg.Lookup("test", "", "hex").Int64()
+ if err != nil || hexInt64 != 16 {
+ t.Fatalf("hex int64: got (%v, %v), want (16, nil)", hexInt64, err)
+ }
+ if _, err := cfg.Lookup("test", "", "badnum").Int(); err == nil {
+ t.Fatal("badnum int: expected error")
+ }
+
+ missing := cfg.Lookup("test", "", "missing")
+ if missing.Kind != config.ValueMissing {
+ t.Fatalf("missing kind: got %v, want %v", missing.Kind, config.ValueMissing)
+ }
+ if _, err := missing.Bool(); err == nil {
+ t.Fatal("missing bool: expected error")
+ }
+ if _, err := missing.Int(); err == nil {
+ t.Fatal("missing int: expected error")
+ }
+ if _, err := missing.String(); err == nil {
+ t.Fatal("missing string: expected error")
+ }
+}
+
func TestConfigComplexValuesAgainstGit(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
@@ -200,7 +330,7 @@ func TestConfigComplexValuesAgainstGit(t *testing.T) {
tests := []string{"spaced", "special", "path", "number"}
for _, key := range tests {
want := gitConfigGet(t, testRepo, "test."+key)
- if got := cfg.Get("test", "", key); got != want {
+ if got := lookupValue(cfg, "test", "", key); got != want {
t.Errorf("test.%s: got %q, want %q (from git)", key, got, want)
}
}
@@ -283,3 +413,130 @@ func TestConfigErrorCases(t *testing.T) {
})
}
}
+
+func TestConfigEOFAfterKeyAgainstGit(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectid.AlgorithmSHA1, Bare: true})
+ cfgPath := filepath.Join(testRepo.Dir(), "config")
+
+ cfgData := []byte("[Core]BAre")
+ if err := os.WriteFile(cfgPath, cfgData, 0o600); err != nil {
+ t.Fatalf("failed to write config: %v", err)
+ }
+
+ gitValue, gitErr := gitConfigGetE(testRepo, "Core.BAre")
+ furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
+
+ if (gitErr == nil) != (furErr == nil) {
+ t.Fatalf("git: %v\nfur: %v", gitErr, furErr)
+ }
+ if furErr != nil {
+ return
+ }
+
+ if got := lookupValue(furConfig, "Core", "", "BAre"); got != gitValue {
+ t.Fatalf("git: %q\nfur: %q", gitValue, got)
+ }
+}
+
+func TestConfigNULValueAgainstGit(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectid.AlgorithmSHA1, Bare: true})
+ cfgPath := filepath.Join(testRepo.Dir(), "config")
+
+ cfgData := []byte("[Core]BAre=\x00")
+ if err := os.WriteFile(cfgPath, cfgData, 0o600); err != nil {
+ t.Fatalf("failed to write config: %v", err)
+ }
+
+ gitValue, gitErr := gitConfigGetE(testRepo, "Core.BAre")
+ furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
+
+ if (gitErr == nil) != (furErr == nil) {
+ t.Fatalf("git: %v\nfur: %v", gitErr, furErr)
+ }
+ if furErr != nil {
+ return
+ }
+
+ if got := lookupValue(furConfig, "Core", "", "BAre"); got != gitValue {
+ t.Fatalf("git: %q\nfur: %q", gitValue, got)
+ }
+}
+
+func TestConfigCarriageReturnSeparatorAgainstGit(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectid.AlgorithmSHA1, Bare: true})
+ cfgPath := filepath.Join(testRepo.Dir(), "config")
+
+ cfgData := []byte("[Core \"sub\"]\rBAre")
+ if err := os.WriteFile(cfgPath, cfgData, 0o600); err != nil {
+ t.Fatalf("failed to write config: %v", err)
+ }
+
+ gitValue, gitErr := gitConfigGetE(testRepo, "Core.sub.BAre")
+ furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
+
+ if (gitErr == nil) != (furErr == nil) {
+ t.Fatalf("git: %v\nfur: %v", gitErr, furErr)
+ }
+ if furErr != nil {
+ return
+ }
+
+ if got := lookupValue(furConfig, "Core", "sub", "BAre"); got != gitValue {
+ t.Fatalf("git: %q\nfur: %q", gitValue, got)
+ }
+}
+
+func FuzzConfig(f *testing.F) {
+ f.Add([]byte("[core]\nbare = true"), "core.bare")
+ f.Add([]byte("[core]\nbare = true\n[core/invalid]"), "core.bare")
+ f.Add([]byte("[core \"sub\"]\nbare = true"), "core.sub.bare")
+
+ testRepo := testgit.NewRepo(f, testgit.RepoOptions{ObjectFormat: objectid.AlgorithmSHA1, Bare: true})
+ cfgPath := filepath.Join(testRepo.Dir(), "config")
+
+ f.Fuzz(func(t *testing.T, cfgData []byte, gitKey string) {
+ if err := os.WriteFile(cfgPath, cfgData, 0o600); err != nil {
+ t.Fatalf("failed to write config: %v", err)
+ }
+
+ gitValue, gitErr := gitConfigGetE(testRepo, gitKey)
+ furConfig, furErr := config.ParseConfig(bytes.NewReader(cfgData))
+ if furErr == nil && furConfig == nil {
+ t.Fatalf("ParseConfig returned nil config with nil error")
+ }
+
+ sameErr := (gitErr == nil) == (furErr == nil)
+ if !sameErr {
+ if furErr == nil {
+ return
+ }
+ t.Fatalf("git: %v\nfur: %v", gitErr, furErr)
+ }
+ if furErr == nil {
+ parts := strings.SplitN(gitKey, ".", 3)
+ furSection := parts[0]
+ var furSubsection, furKey string
+ switch len(parts) {
+ case 1:
+ case 2:
+ furKey = parts[1]
+ case 3:
+ furSubsection = parts[1]
+ furKey = parts[2]
+ default:
+ t.Fatalf("unexpected split(%q): %v", gitKey, parts)
+ }
+
+ furValue := lookupValue(furConfig, furSection, furSubsection, furKey)
+ if gitValue != furValue {
+ t.Fatalf(
+ "key: %v (%v.%v.%v)\ngit: %q\nfur: %q",
+ gitKey, furSection, furSubsection, furKey, gitValue, furValue,
+ )
+ }
+ }
+ })
+}