Download
55 lines 1.6 KiBGoModified 2026-10-18 raw
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
package config

import (
	"os/exec"
	"strings"
	"testing"
)

// Run one with `make fuzz`; plain `go test` runs only the seeds.

// A branch name ValidBranchName admits must be one git accepts, survive the
// trip through its URL slug, and keep its view key a single path component.
func FuzzValidBranchName(f *testing.F) {
	for _, s := range []string{
		"main", "dev", "my-name/feature", "a..b", "a.lock", "a.", ".a", "-a", "a//b",
		"a/", "@", "a@{1}", "HEAD", "a:b", "a b", "_x/y.z",
	} {
		f.Add(s)
	}
	git, gitErr := exec.LookPath("git")
	f.Fuzz(func(t *testing.T, b string) {
		if !ValidBranchName(b) {
			return
		}
		slug := BranchSlug(b)
		if strings.ReplaceAll(slug, ":", "/") != b {
			t.Fatalf("slug %q of %q does not reverse", slug, b)
		}
		if k := ViewKey("r", b); strings.ContainsAny(k, "/\\") || !strings.HasPrefix(k, "r@") {
			t.Fatalf("view key of %q is %q", b, k)
		}
		if gitErr == nil {
			if out, err := exec.Command(git, "check-ref-format", "--branch", b).CombinedOutput(); err != nil {
				t.Fatalf("ValidBranchName(%q) but git rejects it: %s", b, out)
			}
		}
	})
}

// A repository name names a directory, a page and the prefix of a view key.
func FuzzValidRepoName(f *testing.F) {
	for _, s := range []string{"bp", "tomo-json", ".", "..", ".git", "a.git", "a/b", "a@b", "-a", "a:b", "_a"} {
		f.Add(s)
	}
	f.Fuzz(func(t *testing.T, r string) {
		if !ValidRepoName(r) {
			return
		}
		// "@" separates a view key's repo from its branch; "/" and "\" would
		// split the paths a name is used in.
		if strings.ContainsAny(r, "@/\\:\x00") || strings.HasPrefix(r, ".") || strings.HasPrefix(r, "-") {
			t.Fatalf("ValidRepoName(%q)", r)
		}
	})
}