package server import ( "bytes" "errors" "io/fs" "os" "os/exec" "path/filepath" "slices" "testing" ) // The published repository names none of the music-finding tools that used to // live in Minstrel or run beside it: not in code, comments, docs, config or a // file's path. Minstrel plays the library it is given; whatever fills that // library is the operator's choice, and the public tree points at none of them. // // The names are held as FNV-1a 64-bit hashes of their lowercase spelling, // keyed by length, so this file does not carry them either. A name matches // case-insensitively and inside a longer word, as `git grep -i` would. var unpublishedNames = map[int][]uint64{ 6: {0xf9a02fd5dc574c88, 0xf44b33950e1b02a5}, 8: {0x8cb31206e3250360}, 10: {0x49db22150da0e6a4}, } // The tracked files are what a clone of the repository carries, so they are // what is checked: a build output or an untracked scratch file is not // published. func TestPublishedTreeNamesNoMusicFindingTool(t *testing.T) { root := repoRoot(t) cmd := exec.Command("git", "ls-files", "-z") cmd.Dir = root out, err := cmd.Output() if err != nil { t.Fatalf("git ls-files: %v (the check reads the checkout's git metadata)", err) } paths := bytes.Split(bytes.TrimRight(out, "\x00"), []byte{0}) // An empty or truncated listing would pass by checking nothing. if len(paths) < 500 { t.Fatalf("git ls-files listed %d files; expected the whole repository", len(paths)) } for _, p := range paths { path := string(p) if len(findNames(p, unpublishedNames)) > 0 { t.Errorf("%s: the path names a music-finding tool", path) } body, err := os.ReadFile(filepath.Join(root, path)) if errors.Is(err, fs.ErrNotExist) { continue // deleted in the working tree and not yet committed } if err != nil { t.Fatalf("read %s: %v", path, err) } // Binary files are skipped, as git grep -I skips them. if bytes.IndexByte(body[:min(len(body), 8000)], 0) >= 0 { continue } for _, line := range findNames(body, unpublishedNames) { t.Errorf("%s:%d names a music-finding tool", path, line) } } } // The matcher finds a name in any case, inside a longer word and on the right // line, and ignores a different word of the same length. It is shown working // on a stand-in name, since the real ones are only hashes here. func TestFindNames_MatchesTheWayGrepIDoes(t *testing.T) { names := map[int][]uint64{9: {fnv1a([]byte("zebrafish"))}} text := []byte("first line\nthe ZebraFishTank is here\nzebrafist\nZEBRAFISH, twice: zebrafish\n") if got, want := findNames(text, names), []int{2, 4}; !slices.Equal(got, want) { t.Errorf("lines = %v, want %v", got, want) } if got := findNames([]byte("nothing to see"), names); len(got) != 0 { t.Errorf("lines = %v, want none", got) } } // findNames returns the lines of text that hold one of names (FNV-1a hashes // by length), matched in any case and inside longer words. A line is reported // once however many names it holds. func findNames(text []byte, names map[int][]uint64) []int { var lines []int line, i := 1, 0 for i < len(text) { switch c := text[i]; { case c == '\n': line++ i++ continue case !isASCIILetter(c): i++ continue } start := i for i < len(text) && isASCIILetter(text[i]) { i++ } if wordHoldsName(bytes.ToLower(text[start:i]), names) && (len(lines) == 0 || lines[len(lines)-1] != line) { lines = append(lines, line) } } return lines } func wordHoldsName(word []byte, names map[int][]uint64) bool { for n, hashes := range names { for j := 0; j+n <= len(word); j++ { if slices.Contains(hashes, fnv1a(word[j:j+n])) { return true } } } return false } func isASCIILetter(c byte) bool { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') } // fnv1a is FNV-1a, 64-bit. func fnv1a(b []byte) uint64 { h := uint64(0xcbf29ce484222325) for _, c := range b { h ^= uint64(c) h *= 0x100000001b3 } return h }