From 43da5e3a1baae2b2fa4f00e2218632e882654517 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 27 Jun 2018 13:07:03 +0200 Subject: pkg/vcs: move from pkg/git Rename pkg/git to pkg/vcs because we need to support not only git. --- pkg/bisect/bisect.go | 60 +++--- pkg/git/git.go | 496 ----------------------------------------------- pkg/git/git_test.go | 239 ----------------------- pkg/instance/instance.go | 4 +- pkg/vcs/git.go | 496 +++++++++++++++++++++++++++++++++++++++++++++++ pkg/vcs/git_test.go | 239 +++++++++++++++++++++++ 6 files changed, 767 insertions(+), 767 deletions(-) delete mode 100644 pkg/git/git.go delete mode 100644 pkg/git/git_test.go create mode 100644 pkg/vcs/git.go create mode 100644 pkg/vcs/git_test.go (limited to 'pkg') diff --git a/pkg/bisect/bisect.go b/pkg/bisect/bisect.go index 78ac998a4..3dab05290 100644 --- a/pkg/bisect/bisect.go +++ b/pkg/bisect/bisect.go @@ -10,9 +10,9 @@ import ( "time" "github.com/google/syzkaller/pkg/build" - "github.com/google/syzkaller/pkg/git" "github.com/google/syzkaller/pkg/instance" "github.com/google/syzkaller/pkg/osutil" + "github.com/google/syzkaller/pkg/vcs" "github.com/google/syzkaller/syz-manager/mgrconfig" ) @@ -51,7 +51,7 @@ type ReproConfig struct { type env struct { cfg *Config - head *git.Commit + head *vcs.Commit inst *instance.Env numTests int buildTime time.Duration @@ -62,7 +62,7 @@ type buildEnv struct { compiler string } -func Run(cfg *Config) (*git.Commit, error) { +func Run(cfg *Config) (*vcs.Commit, error) { env := &env{ cfg: cfg, } @@ -92,13 +92,13 @@ func Run(cfg *Config) (*git.Commit, error) { return res, nil } -func (env *env) bisect() (*git.Commit, error) { +func (env *env) bisect() (*vcs.Commit, error) { cfg := env.cfg var err error if env.inst, err = instance.NewEnv(&cfg.Manager); err != nil { return nil, err } - if env.head, err = git.Poll(cfg.Manager.KernelSrc, cfg.Kernel.Repo, cfg.Kernel.Branch); err != nil { + if env.head, err = vcs.Poll(cfg.Manager.KernelSrc, cfg.Kernel.Repo, cfg.Kernel.Branch); err != nil { return nil, err } if err := build.Clean(cfg.Manager.TargetOS, cfg.Manager.TargetArch, @@ -109,12 +109,12 @@ func (env *env) bisect() (*git.Commit, error) { if err := env.inst.BuildSyzkaller(cfg.Syzkaller.Repo, cfg.Syzkaller.Commit); err != nil { return nil, err } - if _, err := git.SwitchCommit(cfg.Manager.KernelSrc, cfg.Kernel.Commit); err != nil { + if _, err := vcs.SwitchCommit(cfg.Manager.KernelSrc, cfg.Kernel.Commit); err != nil { return nil, err } if res, err := env.test(); err != nil { return nil, err - } else if res != git.BisectBad { + } else if res != vcs.BisectBad { return nil, fmt.Errorf("the crash wasn't reproduced on the original commit") } res, bad, good, err := env.commitRange() @@ -127,44 +127,44 @@ func (env *env) bisect() (*git.Commit, error) { if good == "" { return nil, nil // still not fixed } - return git.Bisect(cfg.Manager.KernelSrc, bad, good, cfg.Trace, func() (git.BisectResult, error) { + return vcs.Bisect(cfg.Manager.KernelSrc, bad, good, cfg.Trace, func() (vcs.BisectResult, error) { res, err := env.test() if cfg.Fix { - if res == git.BisectBad { - res = git.BisectGood - } else if res == git.BisectGood { - res = git.BisectBad + if res == vcs.BisectBad { + res = vcs.BisectGood + } else if res == vcs.BisectGood { + res = vcs.BisectBad } } return res, err }) } -func (env *env) commitRange() (*git.Commit, string, string, error) { +func (env *env) commitRange() (*vcs.Commit, string, string, error) { if env.cfg.Fix { return env.commitRangeForFix() } return env.commitRangeForBug() } -func (env *env) commitRangeForFix() (*git.Commit, string, string, error) { +func (env *env) commitRangeForFix() (*vcs.Commit, string, string, error) { env.log("testing current HEAD %v", env.head.Hash) - if _, err := git.SwitchCommit(env.cfg.Manager.KernelSrc, env.head.Hash); err != nil { + if _, err := vcs.SwitchCommit(env.cfg.Manager.KernelSrc, env.head.Hash); err != nil { return nil, "", "", err } res, err := env.test() if err != nil { return nil, "", "", err } - if res != git.BisectGood { + if res != vcs.BisectGood { return nil, "", "", nil } return nil, env.head.Hash, env.cfg.Kernel.Commit, nil } -func (env *env) commitRangeForBug() (*git.Commit, string, string, error) { +func (env *env) commitRangeForBug() (*vcs.Commit, string, string, error) { cfg := env.cfg - tags, err := git.PreviousReleaseTags(cfg.Manager.KernelSrc, cfg.Kernel.Commit) + tags, err := vcs.PreviousReleaseTags(cfg.Manager.KernelSrc, cfg.Kernel.Commit) if err != nil { return nil, "", "", err } @@ -183,7 +183,7 @@ func (env *env) commitRangeForBug() (*git.Commit, string, string, error) { lastBad := cfg.Kernel.Commit for i, tag := range tags { env.log("testing release %v", tag) - commit, err := git.SwitchCommit(cfg.Manager.KernelSrc, tag) + commit, err := vcs.SwitchCommit(cfg.Manager.KernelSrc, tag) if err != nil { return nil, "", "", err } @@ -191,10 +191,10 @@ func (env *env) commitRangeForBug() (*git.Commit, string, string, error) { if err != nil { return nil, "", "", err } - if res == git.BisectGood { + if res == vcs.BisectGood { return nil, lastBad, tag, nil } - if res == git.BisectBad { + if res == vcs.BisectBad { lastBad = tag } if i == len(tags)-1 { @@ -204,10 +204,10 @@ func (env *env) commitRangeForBug() (*git.Commit, string, string, error) { panic("unreachable") } -func (env *env) test() (git.BisectResult, error) { +func (env *env) test() (vcs.BisectResult, error) { cfg := env.cfg env.numTests++ - current, err := git.HeadCommit(cfg.Manager.KernelSrc) + current, err := vcs.HeadCommit(cfg.Manager.KernelSrc) if err != nil { return 0, err } @@ -235,26 +235,26 @@ func (env *env) test() (git.BisectResult, error) { } else { env.log("%v", err) } - return git.BisectSkip, nil + return vcs.BisectSkip, nil } testStart := time.Now() results, err := env.inst.Test(8, cfg.Repro.Syz, cfg.Repro.Opts, cfg.Repro.C) env.testTime += time.Since(testStart) if err != nil { env.log("failed: %v", err) - return git.BisectSkip, nil + return vcs.BisectSkip, nil } bad, good := env.processResults(current, results) - res := git.BisectSkip + res := vcs.BisectSkip if bad != 0 { - res = git.BisectBad + res = vcs.BisectBad } else if good != 0 { - res = git.BisectGood + res = vcs.BisectGood } return res, nil } -func (env *env) processResults(current *git.Commit, results []error) (bad, good int) { +func (env *env) processResults(current *vcs.Commit, results []error) (bad, good int) { var verdicts []string for i, res := range results { if res == nil { @@ -303,7 +303,7 @@ func (env *env) processResults(current *git.Commit, results []error) (bad, good // Note: linux-specific. func (env *env) buildEnvForCommit(commit string) (*buildEnv, error) { cfg := env.cfg - tags, err := git.PreviousReleaseTags(cfg.Manager.KernelSrc, commit) + tags, err := vcs.PreviousReleaseTags(cfg.Manager.KernelSrc, commit) if err != nil { return nil, err } diff --git a/pkg/git/git.go b/pkg/git/git.go deleted file mode 100644 index 899930bff..000000000 --- a/pkg/git/git.go +++ /dev/null @@ -1,496 +0,0 @@ -// Copyright 2017 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -// Package git provides helper functions for working with git repositories. -package git - -import ( - "bufio" - "bytes" - "fmt" - "io" - "net/mail" - "os" - "os/exec" - "regexp" - "sort" - "strconv" - "strings" - "time" - - "github.com/google/syzkaller/pkg/osutil" -) - -const ( - DateFormat = "Mon Jan 2 15:04:05 2006 -0700" - timeout = time.Hour // timeout for all git invocations -) - -// Poll checkouts the specified repository/branch in dir. -// This involves fetching/resetting/cloning as necessary to recover from all possible problems. -// Returns hash of the HEAD commit in the specified branch. -func Poll(dir, repo, branch string) (*Commit, error) { - runSandboxed(dir, "git", "bisect", "reset") - runSandboxed(dir, "git", "reset", "--hard") - origin, err := runSandboxed(dir, "git", "remote", "get-url", "origin") - if err != nil || strings.TrimSpace(string(origin)) != repo { - // The repo is here, but it has wrong origin (e.g. repo in config has changed), re-clone. - if err := clone(dir, repo, branch); err != nil { - return nil, err - } - } - // Use origin/branch for the case the branch was force-pushed, - // in such case branch is not the same is origin/branch and we will - // stuck with the local version forever (git checkout won't fail). - if _, err := runSandboxed(dir, "git", "checkout", "origin/"+branch); err != nil { - // No such branch (e.g. branch in config has changed), re-clone. - if err := clone(dir, repo, branch); err != nil { - return nil, err - } - } - if _, err := runSandboxed(dir, "git", "fetch", "--no-tags"); err != nil { - // Something else is wrong, re-clone. - if err := clone(dir, repo, branch); err != nil { - return nil, err - } - } - if _, err := runSandboxed(dir, "git", "checkout", "origin/"+branch); err != nil { - return nil, err - } - return HeadCommit(dir) -} - -// CheckoutBranch checkouts the specified repository/branch in dir. -func CheckoutBranch(dir, repo, branch string) (*Commit, error) { - runSandboxed(dir, "git", "bisect", "reset") - if _, err := runSandboxed(dir, "git", "reset", "--hard"); err != nil { - if err := initRepo(dir); err != nil { - return nil, err - } - } - _, err := runSandboxed(dir, "git", "fetch", repo, branch) - if err != nil { - return nil, err - } - if _, err := runSandboxed(dir, "git", "checkout", "FETCH_HEAD"); err != nil { - return nil, err - } - return HeadCommit(dir) -} - -// CheckoutCommit checkouts the specified repository on the specified commit in dir. -func CheckoutCommit(dir, repo, commit string) (*Commit, error) { - runSandboxed(dir, "git", "bisect", "reset") - if _, err := runSandboxed(dir, "git", "reset", "--hard"); err != nil { - if err := initRepo(dir); err != nil { - return nil, err - } - } - _, err := runSandboxed(dir, "git", "fetch", repo) - if err != nil { - return nil, err - } - return SwitchCommit(dir, commit) -} - -// SwitchCommit checkouts the specified commit without fetching. -func SwitchCommit(dir, commit string) (*Commit, error) { - if _, err := runSandboxed(dir, "git", "checkout", commit); err != nil { - return nil, err - } - return HeadCommit(dir) -} - -func clone(dir, repo, branch string) error { - if err := initRepo(dir); err != nil { - return err - } - if _, err := runSandboxed(dir, "git", "remote", "add", "origin", repo); err != nil { - return err - } - if _, err := runSandboxed(dir, "git", "fetch", "origin", branch); err != nil { - return err - } - return nil -} - -func initRepo(dir string) error { - if err := os.RemoveAll(dir); err != nil { - return fmt.Errorf("failed to remove repo dir: %v", err) - } - if err := osutil.MkdirAll(dir); err != nil { - return fmt.Errorf("failed to create repo dir: %v", err) - } - if err := osutil.SandboxChown(dir); err != nil { - return err - } - if _, err := runSandboxed(dir, "git", "init"); err != nil { - return err - } - return nil -} - -type Commit struct { - Hash string - Title string - Author string - CC []string - Date time.Time -} - -// HeadCommit returns info about the HEAD commit of the current branch of git repository in dir. -func HeadCommit(dir string) (*Commit, error) { - return GetCommit(dir, "HEAD") -} - -func GetCommit(dir, commit string) (*Commit, error) { - output, err := runSandboxed(dir, "git", "log", "--format=%H%n%s%n%ae%n%ad%n%b", "-n", "1", commit) - if err != nil { - return nil, err - } - return parseCommit(output) -} - -func parseCommit(output []byte) (*Commit, error) { - lines := bytes.Split(output, []byte{'\n'}) - if len(lines) < 4 || len(lines[0]) != 40 { - return nil, fmt.Errorf("unexpected git log output: %q", output) - } - date, err := time.Parse(DateFormat, string(lines[3])) - if err != nil { - return nil, fmt.Errorf("failed to parse date in git log output: %v\n%q", err, output) - } - cc := make(map[string]bool) - cc[strings.ToLower(string(lines[2]))] = true - for _, line := range lines[4:] { - for _, re := range ccRes { - matches := re.FindSubmatchIndex(line) - if matches == nil { - continue - } - addr, err := mail.ParseAddress(string(line[matches[2]:matches[3]])) - if err != nil { - break - } - cc[strings.ToLower(addr.Address)] = true - break - } - } - sortedCC := make([]string, 0, len(cc)) - for addr := range cc { - sortedCC = append(sortedCC, addr) - } - sort.Strings(sortedCC) - com := &Commit{ - Hash: string(lines[0]), - Title: string(lines[1]), - Author: string(lines[2]), - CC: sortedCC, - Date: date, - } - return com, nil -} - -// ListRecentCommits returns list of recent commit titles starting from baseCommit. -func ListRecentCommits(dir, baseCommit string) ([]string, error) { - // On upstream kernel this produces ~11MB of output. - // Somewhat inefficient to collect whole output in a slice - // and then convert to string, but should be bearable. - output, err := runSandboxed(dir, "git", "log", - "--pretty=format:%s", "--no-merges", "-n", "200000", baseCommit) - if err != nil { - return nil, err - } - return strings.Split(string(output), "\n"), nil -} - -type FixCommit struct { - Tag string - Title string -} - -// ExtractFixTagsFromCommits extracts fixing tags for bugs from git log. -// Given email = "user@domain.com", it searches for tags of the form "user+tag@domain.com" -// and return pairs {tag, commit title}. -func ExtractFixTagsFromCommits(dir, baseCommit, email string) ([]FixCommit, error) { - since := time.Now().Add(-time.Hour * 24 * 365).Format("01-02-2006") - cmd := exec.Command("git", "log", "--no-merges", "--since", since, baseCommit) - cmd.Dir = dir - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - if err := cmd.Start(); err != nil { - return nil, err - } - defer cmd.Wait() - defer cmd.Process.Kill() - return extractFixTags(stdout, email) -} - -func extractFixTags(r io.Reader, email string) ([]FixCommit, error) { - user, domain, err := splitEmail(email) - if err != nil { - return nil, fmt.Errorf("failed to parse email %q: %v", email, err) - } - var ( - s = bufio.NewScanner(r) - commits []FixCommit - commitTitle = "" - commitStart = []byte("commit ") - bodyPrefix = []byte(" ") - userBytes = []byte(user + "+") - domainBytes = []byte(domain) - ) - for s.Scan() { - ln := s.Bytes() - if bytes.HasPrefix(ln, commitStart) { - commitTitle = "" - continue - } - if !bytes.HasPrefix(ln, bodyPrefix) { - continue - } - ln = ln[len(bodyPrefix):] - if len(ln) == 0 { - continue - } - if commitTitle == "" { - commitTitle = string(ln) - continue - } - userPos := bytes.Index(ln, userBytes) - if userPos == -1 { - continue - } - domainPos := bytes.Index(ln[userPos+len(userBytes)+1:], domainBytes) - if domainPos == -1 { - continue - } - startPos := userPos + len(userBytes) - endPos := userPos + len(userBytes) + domainPos + 1 - tag := string(ln[startPos:endPos]) - commits = append(commits, FixCommit{tag, commitTitle}) - } - return commits, s.Err() -} - -func splitEmail(email string) (user, domain string, err error) { - addr, err := mail.ParseAddress(email) - if err != nil { - return "", "", err - } - at := strings.IndexByte(addr.Address, '@') - if at == -1 { - return "", "", fmt.Errorf("no @ in email address") - } - user = addr.Address[:at] - domain = addr.Address[at:] - if plus := strings.IndexByte(user, '+'); plus != -1 { - user = user[:plus] - } - return -} - -// CanonicalizeCommit returns commit title that can be used when checking -// if a particular commit is present in a git tree. -// Some trees add prefixes to commit titles during backporting, -// so we want e.g. commit "foo bar" match "BACKPORT: foo bar". -func CanonicalizeCommit(title string) string { - for _, prefix := range commitPrefixes { - if strings.HasPrefix(title, prefix) { - title = title[len(prefix):] - break - } - } - return strings.TrimSpace(title) -} - -var commitPrefixes = []string{ - "UPSTREAM:", - "CHROMIUM:", - "FROMLIST:", - "BACKPORT:", - "FROMGIT:", - "net-backports:", -} - -func Patch(dir string, patch []byte) error { - // Do --dry-run first to not mess with partially consistent state. - cmd := osutil.Command("patch", "-p1", "--force", "--ignore-whitespace", "--dry-run") - if err := osutil.Sandbox(cmd, true, true); err != nil { - return err - } - cmd.Stdin = bytes.NewReader(patch) - cmd.Dir = dir - if output, err := cmd.CombinedOutput(); err != nil { - // If it reverses clean, then it's already applied - // (seems to be the easiest way to detect it). - cmd = osutil.Command("patch", "-p1", "--force", "--ignore-whitespace", "--reverse", "--dry-run") - if err := osutil.Sandbox(cmd, true, true); err != nil { - return err - } - cmd.Stdin = bytes.NewReader(patch) - cmd.Dir = dir - if _, err := cmd.CombinedOutput(); err == nil { - return fmt.Errorf("patch is already applied") - } - return fmt.Errorf("failed to apply patch:\n%s", output) - } - // Now apply for real. - cmd = osutil.Command("patch", "-p1", "--force", "--ignore-whitespace") - if err := osutil.Sandbox(cmd, true, true); err != nil { - return err - } - cmd.Stdin = bytes.NewReader(patch) - cmd.Dir = dir - if output, err := cmd.CombinedOutput(); err != nil { - return fmt.Errorf("failed to apply patch after dry run:\n%s", output) - } - return nil -} - -type BisectResult int - -const ( - BisectBad BisectResult = iota - BisectGood - BisectSkip -) - -// Bisect bisects good..bad commit range against the provided predicate (wrapper around git bisect). -// The predicate should return an error only if there is no way to proceed -// (it will abort the process), if possible it should prefer to return BisectSkip. -// Progress of the process is streamed to the provided trace. -// Returns the first commit on which the predicate returns BisectBad. -func Bisect(dir, bad, good string, trace io.Writer, pred func() (BisectResult, error)) (*Commit, error) { - runSandboxed(dir, "git", "bisect", "reset") - runSandboxed(dir, "git", "reset", "--hard") - firstBad, err := GetCommit(dir, bad) - if err != nil { - return nil, err - } - output, err := runSandboxed(dir, "git", "bisect", "start", bad, good) - if err != nil { - return nil, err - } - defer runSandboxed(dir, "git", "bisect", "reset") - fmt.Fprintf(trace, "# git bisect start %v %v\n%s", bad, good, output) - current, err := HeadCommit(dir) - if err != nil { - return nil, err - } - var bisectTerms = [...]string{ - BisectBad: "bad", - BisectGood: "good", - BisectSkip: "skip", - } - for { - res, err := pred() - if err != nil { - return nil, err - } - if res == BisectBad { - firstBad = current - } - output, err = runSandboxed(dir, "git", "bisect", bisectTerms[res]) - if err != nil { - return nil, err - } - fmt.Fprintf(trace, "# git bisect %v %v\n%s", bisectTerms[res], current.Hash, output) - next, err := HeadCommit(dir) - if err != nil { - return nil, err - } - if current.Hash == next.Hash { - return firstBad, nil - } - current = next - } -} - -// PreviousReleaseTags returns list of preceding release tags that are reachable from the given commit. -// Note: linux-specific. -func PreviousReleaseTags(dir, commit string) ([]string, error) { - output, err := runSandboxed(dir, "git", "tag", "--no-contains", commit, "--merged", commit, "v*.*") - if err != nil { - return nil, err - } - return parseReleaseTags(output) -} - -func parseReleaseTags(output []byte) ([]string, error) { - var tags []string - for _, tag := range bytes.Split(output, []byte{'\n'}) { - if releaseTagRe.Match(tag) && releaseTagToInt(string(tag)) != 0 { - tags = append(tags, string(tag)) - } - } - sort.Slice(tags, func(i, j int) bool { - return releaseTagToInt(tags[i]) > releaseTagToInt(tags[j]) - }) - return tags, nil -} - -func releaseTagToInt(tag string) uint64 { - matches := releaseTagRe.FindStringSubmatchIndex(tag) - v1, err := strconv.ParseUint(tag[matches[2]:matches[3]], 10, 64) - if err != nil { - return 0 - } - v2, err := strconv.ParseUint(tag[matches[4]:matches[5]], 10, 64) - if err != nil { - return 0 - } - var v3 uint64 - if matches[6] != -1 { - v3, err = strconv.ParseUint(tag[matches[6]:matches[7]], 10, 64) - if err != nil { - return 0 - } - } - return v1*1e6 + v2*1e3 + v3 -} - -func runSandboxed(dir, command string, args ...string) ([]byte, error) { - cmd := osutil.Command(command, args...) - cmd.Dir = dir - if err := osutil.Sandbox(cmd, true, false); err != nil { - return nil, err - } - return osutil.Run(timeout, cmd) -} - -// CheckRepoAddress does a best-effort approximate check of a git repo address. -func CheckRepoAddress(repo string) bool { - return gitRepoRe.MatchString(repo) -} - -// CheckBranch does a best-effort approximate check of a git branch name. -func CheckBranch(branch string) bool { - return gitBranchRe.MatchString(branch) -} - -func CheckCommitHash(hash string) bool { - if !gitHashRe.MatchString(hash) { - return false - } - ln := len(hash) - return ln == 8 || ln == 10 || ln == 12 || ln == 16 || ln == 20 || ln == 40 -} - -var ( - // nolint: lll - gitRepoRe = regexp.MustCompile(`^(git|ssh|http|https|ftp|ftps)://[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)+(:[0-9]+)?/[a-zA-Z0-9-_./]+\.git(/)?$`) - gitBranchRe = regexp.MustCompile("^[a-zA-Z0-9-_/.]{2,200}$") - gitHashRe = regexp.MustCompile("^[a-f0-9]+$") - releaseTagRe = regexp.MustCompile(`^v([0-9]+).([0-9]+)(?:\.([0-9]+))?$`) - ccRes = []*regexp.Regexp{ - regexp.MustCompile(`^Reviewed\-.*: (.*)$`), - regexp.MustCompile(`^[A-Za-z-]+\-and\-[Rr]eviewed\-.*: (.*)$`), - regexp.MustCompile(`^Acked\-.*: (.*)$`), - regexp.MustCompile(`^[A-Za-z-]+\-and\-[Aa]cked\-.*: (.*)$`), - regexp.MustCompile(`^Tested\-.*: (.*)$`), - regexp.MustCompile(`^[A-Za-z-]+\-and\-[Tt]ested\-.*: (.*)$`), - } -) diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go deleted file mode 100644 index 86f033acf..000000000 --- a/pkg/git/git_test.go +++ /dev/null @@ -1,239 +0,0 @@ -// Copyright 2017 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -package git - -import ( - "reflect" - "strings" - "testing" - "time" -) - -func TestParseCommit(t *testing.T) { - tests := map[string]*Commit{ - `2075b16e32c26e4031b9fd3cbe26c54676a8fcb5 -rbtree: include rcu.h -foobar@foobar.de -Fri May 11 16:02:14 2018 -0700 -Since commit c1adf20052d8 ("Introduce rb_replace_node_rcu()") -rbtree_augmented.h uses RCU related data structures but does not include -the header file. It works as long as it gets somehow included before -that and fails otherwise. - -Link: http://lkml.kernel.org/r/20180504103159.19938-1-bigeasy@linutronix.de -Signed-off-by: Foo Bad Baz -Reviewed-by: -Cc: Unrelated Guy -Acked-by: Subsystem reviewer -Reported-and-tested-by: and@me.com -Reported-and-Tested-by: Name-name -Tested-by: Must be correct -Signed-off-by: Linux Master -`: &Commit{ - Hash: "2075b16e32c26e4031b9fd3cbe26c54676a8fcb5", - Title: "rbtree: include rcu.h", - Author: "foobar@foobar.de", - CC: []string{ - "and@me.com", - "foobar@foobar.de", - "mustbe@correct.com", - "name@name.com", - "subsystem@reviewer.com", - "yetanother@email.org", - }, - Date: time.Date(2018, 5, 11, 16, 02, 14, 0, time.FixedZone("", -7*60*60)), - }, - } - for input, com := range tests { - res, err := parseCommit([]byte(input)) - if err != nil && com != nil { - t.Fatalf("want %+v, got error: %v", com, err) - } - if err == nil && com == nil { - t.Fatalf("want error, got commit %+v", res) - } - if com == nil { - continue - } - if com.Hash != res.Hash { - t.Fatalf("want hash %q, got %q", com.Hash, res.Hash) - } - if com.Title != res.Title { - t.Fatalf("want title %q, got %q", com.Title, res.Title) - } - if com.Author != res.Author { - t.Fatalf("want author %q, got %q", com.Author, res.Author) - } - if !reflect.DeepEqual(com.CC, res.CC) { - t.Fatalf("want CC %q, got %q", com.CC, res.CC) - } - if !com.Date.Equal(res.Date) { - t.Fatalf("want date %v, got %v", com.Date, res.Date) - } - } -} - -func TestCanonicalizeCommit(t *testing.T) { - tests := map[string]string{ - "foo bar": "foo bar", - " foo ": "foo", - "UPSTREAM: foo bar": "foo bar", - "BACKPORT: UPSTREAM: foo bar": "UPSTREAM: foo bar", - } - for in, want := range tests { - got := CanonicalizeCommit(in) - if got != want { - t.Errorf("input %q: got %q, want %q", in, got, want) - } - } -} - -func TestCheckRepoAddress(t *testing.T) { - testPredicate(t, CheckRepoAddress, map[string]bool{ - "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git": true, - "https://github.com/torvalds/linux.git": true, - "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git": true, - "git://git.cmpxchg.org/linux-mmots.git": true, - "https://anonscm.debian.org/git/kernel/linux.git": true, - "git://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": true, - "http://host.xz:123/path/to/repo.git/": true, - "": false, - "foobar": false, - "linux-next": false, - "foo://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": false, - "git://kernel/ubuntu.git": false, - "git://kernel.com/ubuntu": false, - "gitgit://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": false, - }) -} - -func TestCheckBranch(t *testing.T) { - testPredicate(t, CheckBranch, map[string]bool{ - "master": true, - "core/core": true, - "irq-irqdomain-for-linus": true, - "timers/2038": true, - "ubuntu-zesty/v4.9.4": true, - "WIP.locking/atomics": true, - "linux-4.9.y": true, - "abi_spec": true, - "@": false, - "": false, - }) -} - -func TestCheckCommitHash(t *testing.T) { - testPredicate(t, CheckCommitHash, map[string]bool{ - "ff12bea91c22bba93d3ffc3034d813d686bc7eeb": true, // 40 - "eae05cb0aaeae05cb0aa": true, // 20 - "449dd6984d0eaabb": true, // 16 - "449dd6984d0e": true, // 12 - "eae05cb0aa": true, // 10 - "eae05cb0": true, // 8 - "": false, - "aa": false, - "eae05cb0aab": false, - "xxxxxxxx": false, - }) -} - -func testPredicate(t *testing.T, fn func(string) bool, tests map[string]bool) { - for input, want := range tests { - res := fn(input) - if res != want { - t.Errorf("%v: got %v, want %v", input, res, want) - } - } -} - -func TestParseReleaseTags(t *testing.T) { - input := ` -v3.1 -v2.6.12 -v2.6.39 -v3.0 -v3.10 -v2.6.13 -v3.11 -v3.19 -v3.9 -v3.2 -v4.9 -v2.6.32 -v4.0 -voo -v1.foo -v10.2.foo -v1.2. -v1. -` - want := []string{ - "v4.9", - "v4.0", - "v3.19", - "v3.11", - "v3.10", - "v3.9", - "v3.2", - "v3.1", - "v3.0", - "v2.6.39", - "v2.6.32", - "v2.6.13", - "v2.6.12", - } - got, err := parseReleaseTags([]byte(input)) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(got, want) { - t.Fatalf("got bad tags\ngot: %+v\nwant: %+v", got, want) - } -} - -func TestExtractFixTags(t *testing.T) { - commits, err := extractFixTags(strings.NewReader(extractFixTagsInput), extractFixTagsEmail) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(commits, extractFixTagsOutput) { - t.Fatalf("got : %+v\twant: %+v", commits, extractFixTagsOutput) - } -} - -const extractFixTagsEmail = "\"syzbot\" " - -var extractFixTagsOutput = []FixCommit{ - {"8e4090902540da8c6e8f", "dashboard/app: bump max repros per bug to 10"}, - {"8e4090902540da8c6e8f", "executor: remove dead code"}, - {"a640a0fc325c29c3efcb", "executor: remove dead code"}, - {"8e4090902540da8c6e8fa640a0fc325c29c3efcb", "pkg/csource: fix string escaping bug"}, -} - -var extractFixTagsInput = ` -commit 73aba437a774237b1130837b856f3b40b3ec3bf0 (HEAD -> master, origin/master) -Author: me -Date: Fri Dec 22 19:59:56 2017 +0100 - - dashboard/app: bump max repros per bug to 10 - - Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com - -commit 26cd53f078db858a6ccca338e13e7f4d1d291c22 -Author: me -Date: Fri Dec 22 13:42:27 2017 +0100 - - executor: remove dead code - - Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com - Reported-by: syzbot - -commit 7b62abdb0abadbaf7b3f3a23ab4d78485fbf9059 -Author: Dmitry Vyukov -Date: Fri Dec 22 11:59:09 2017 +0100 - - pkg/csource: fix string escaping bug - - Reported-and-tested-by: syzbot+8e4090902540da8c6e8fa640a0fc325c29c3efcb@my.mail.com -` diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 4e28e0b92..a41a4cbc2 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -16,10 +16,10 @@ import ( "github.com/google/syzkaller/pkg/build" "github.com/google/syzkaller/pkg/csource" - "github.com/google/syzkaller/pkg/git" "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/pkg/osutil" "github.com/google/syzkaller/pkg/report" + "github.com/google/syzkaller/pkg/vcs" "github.com/google/syzkaller/prog" "github.com/google/syzkaller/syz-manager/mgrconfig" "github.com/google/syzkaller/vm" @@ -59,7 +59,7 @@ func (env *Env) BuildSyzkaller(repo, commit string) error { if srcIndex == -1 { return fmt.Errorf("syzkaller path %q is not in GOPATH", cfg.Syzkaller) } - if _, err := git.CheckoutCommit(cfg.Syzkaller, repo, commit); err != nil { + if _, err := vcs.CheckoutCommit(cfg.Syzkaller, repo, commit); err != nil { return fmt.Errorf("failed to checkout syzkaller repo: %v", err) } cmd := osutil.Command("make", "target") diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go new file mode 100644 index 000000000..e942b3c39 --- /dev/null +++ b/pkg/vcs/git.go @@ -0,0 +1,496 @@ +// Copyright 2017 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +// Package vcs provides helper functions for working with git repositories. +package vcs + +import ( + "bufio" + "bytes" + "fmt" + "io" + "net/mail" + "os" + "os/exec" + "regexp" + "sort" + "strconv" + "strings" + "time" + + "github.com/google/syzkaller/pkg/osutil" +) + +const ( + DateFormat = "Mon Jan 2 15:04:05 2006 -0700" + timeout = time.Hour // timeout for all git invocations +) + +// Poll checkouts the specified repository/branch in dir. +// This involves fetching/resetting/cloning as necessary to recover from all possible problems. +// Returns hash of the HEAD commit in the specified branch. +func Poll(dir, repo, branch string) (*Commit, error) { + runSandboxed(dir, "git", "bisect", "reset") + runSandboxed(dir, "git", "reset", "--hard") + origin, err := runSandboxed(dir, "git", "remote", "get-url", "origin") + if err != nil || strings.TrimSpace(string(origin)) != repo { + // The repo is here, but it has wrong origin (e.g. repo in config has changed), re-clone. + if err := clone(dir, repo, branch); err != nil { + return nil, err + } + } + // Use origin/branch for the case the branch was force-pushed, + // in such case branch is not the same is origin/branch and we will + // stuck with the local version forever (git checkout won't fail). + if _, err := runSandboxed(dir, "git", "checkout", "origin/"+branch); err != nil { + // No such branch (e.g. branch in config has changed), re-clone. + if err := clone(dir, repo, branch); err != nil { + return nil, err + } + } + if _, err := runSandboxed(dir, "git", "fetch", "--no-tags"); err != nil { + // Something else is wrong, re-clone. + if err := clone(dir, repo, branch); err != nil { + return nil, err + } + } + if _, err := runSandboxed(dir, "git", "checkout", "origin/"+branch); err != nil { + return nil, err + } + return HeadCommit(dir) +} + +// CheckoutBranch checkouts the specified repository/branch in dir. +func CheckoutBranch(dir, repo, branch string) (*Commit, error) { + runSandboxed(dir, "git", "bisect", "reset") + if _, err := runSandboxed(dir, "git", "reset", "--hard"); err != nil { + if err := initRepo(dir); err != nil { + return nil, err + } + } + _, err := runSandboxed(dir, "git", "fetch", repo, branch) + if err != nil { + return nil, err + } + if _, err := runSandboxed(dir, "git", "checkout", "FETCH_HEAD"); err != nil { + return nil, err + } + return HeadCommit(dir) +} + +// CheckoutCommit checkouts the specified repository on the specified commit in dir. +func CheckoutCommit(dir, repo, commit string) (*Commit, error) { + runSandboxed(dir, "git", "bisect", "reset") + if _, err := runSandboxed(dir, "git", "reset", "--hard"); err != nil { + if err := initRepo(dir); err != nil { + return nil, err + } + } + _, err := runSandboxed(dir, "git", "fetch", repo) + if err != nil { + return nil, err + } + return SwitchCommit(dir, commit) +} + +// SwitchCommit checkouts the specified commit without fetching. +func SwitchCommit(dir, commit string) (*Commit, error) { + if _, err := runSandboxed(dir, "git", "checkout", commit); err != nil { + return nil, err + } + return HeadCommit(dir) +} + +func clone(dir, repo, branch string) error { + if err := initRepo(dir); err != nil { + return err + } + if _, err := runSandboxed(dir, "git", "remote", "add", "origin", repo); err != nil { + return err + } + if _, err := runSandboxed(dir, "git", "fetch", "origin", branch); err != nil { + return err + } + return nil +} + +func initRepo(dir string) error { + if err := os.RemoveAll(dir); err != nil { + return fmt.Errorf("failed to remove repo dir: %v", err) + } + if err := osutil.MkdirAll(dir); err != nil { + return fmt.Errorf("failed to create repo dir: %v", err) + } + if err := osutil.SandboxChown(dir); err != nil { + return err + } + if _, err := runSandboxed(dir, "git", "init"); err != nil { + return err + } + return nil +} + +type Commit struct { + Hash string + Title string + Author string + CC []string + Date time.Time +} + +// HeadCommit returns info about the HEAD commit of the current branch of git repository in dir. +func HeadCommit(dir string) (*Commit, error) { + return GetCommit(dir, "HEAD") +} + +func GetCommit(dir, commit string) (*Commit, error) { + output, err := runSandboxed(dir, "git", "log", "--format=%H%n%s%n%ae%n%ad%n%b", "-n", "1", commit) + if err != nil { + return nil, err + } + return parseCommit(output) +} + +func parseCommit(output []byte) (*Commit, error) { + lines := bytes.Split(output, []byte{'\n'}) + if len(lines) < 4 || len(lines[0]) != 40 { + return nil, fmt.Errorf("unexpected git log output: %q", output) + } + date, err := time.Parse(DateFormat, string(lines[3])) + if err != nil { + return nil, fmt.Errorf("failed to parse date in git log output: %v\n%q", err, output) + } + cc := make(map[string]bool) + cc[strings.ToLower(string(lines[2]))] = true + for _, line := range lines[4:] { + for _, re := range ccRes { + matches := re.FindSubmatchIndex(line) + if matches == nil { + continue + } + addr, err := mail.ParseAddress(string(line[matches[2]:matches[3]])) + if err != nil { + break + } + cc[strings.ToLower(addr.Address)] = true + break + } + } + sortedCC := make([]string, 0, len(cc)) + for addr := range cc { + sortedCC = append(sortedCC, addr) + } + sort.Strings(sortedCC) + com := &Commit{ + Hash: string(lines[0]), + Title: string(lines[1]), + Author: string(lines[2]), + CC: sortedCC, + Date: date, + } + return com, nil +} + +// ListRecentCommits returns list of recent commit titles starting from baseCommit. +func ListRecentCommits(dir, baseCommit string) ([]string, error) { + // On upstream kernel this produces ~11MB of output. + // Somewhat inefficient to collect whole output in a slice + // and then convert to string, but should be bearable. + output, err := runSandboxed(dir, "git", "log", + "--pretty=format:%s", "--no-merges", "-n", "200000", baseCommit) + if err != nil { + return nil, err + } + return strings.Split(string(output), "\n"), nil +} + +type FixCommit struct { + Tag string + Title string +} + +// ExtractFixTagsFromCommits extracts fixing tags for bugs from git log. +// Given email = "user@domain.com", it searches for tags of the form "user+tag@domain.com" +// and return pairs {tag, commit title}. +func ExtractFixTagsFromCommits(dir, baseCommit, email string) ([]FixCommit, error) { + since := time.Now().Add(-time.Hour * 24 * 365).Format("01-02-2006") + cmd := exec.Command("git", "log", "--no-merges", "--since", since, baseCommit) + cmd.Dir = dir + stdout, err := cmd.StdoutPipe() + if err != nil { + return nil, err + } + if err := cmd.Start(); err != nil { + return nil, err + } + defer cmd.Wait() + defer cmd.Process.Kill() + return extractFixTags(stdout, email) +} + +func extractFixTags(r io.Reader, email string) ([]FixCommit, error) { + user, domain, err := splitEmail(email) + if err != nil { + return nil, fmt.Errorf("failed to parse email %q: %v", email, err) + } + var ( + s = bufio.NewScanner(r) + commits []FixCommit + commitTitle = "" + commitStart = []byte("commit ") + bodyPrefix = []byte(" ") + userBytes = []byte(user + "+") + domainBytes = []byte(domain) + ) + for s.Scan() { + ln := s.Bytes() + if bytes.HasPrefix(ln, commitStart) { + commitTitle = "" + continue + } + if !bytes.HasPrefix(ln, bodyPrefix) { + continue + } + ln = ln[len(bodyPrefix):] + if len(ln) == 0 { + continue + } + if commitTitle == "" { + commitTitle = string(ln) + continue + } + userPos := bytes.Index(ln, userBytes) + if userPos == -1 { + continue + } + domainPos := bytes.Index(ln[userPos+len(userBytes)+1:], domainBytes) + if domainPos == -1 { + continue + } + startPos := userPos + len(userBytes) + endPos := userPos + len(userBytes) + domainPos + 1 + tag := string(ln[startPos:endPos]) + commits = append(commits, FixCommit{tag, commitTitle}) + } + return commits, s.Err() +} + +func splitEmail(email string) (user, domain string, err error) { + addr, err := mail.ParseAddress(email) + if err != nil { + return "", "", err + } + at := strings.IndexByte(addr.Address, '@') + if at == -1 { + return "", "", fmt.Errorf("no @ in email address") + } + user = addr.Address[:at] + domain = addr.Address[at:] + if plus := strings.IndexByte(user, '+'); plus != -1 { + user = user[:plus] + } + return +} + +// CanonicalizeCommit returns commit title that can be used when checking +// if a particular commit is present in a git tree. +// Some trees add prefixes to commit titles during backporting, +// so we want e.g. commit "foo bar" match "BACKPORT: foo bar". +func CanonicalizeCommit(title string) string { + for _, prefix := range commitPrefixes { + if strings.HasPrefix(title, prefix) { + title = title[len(prefix):] + break + } + } + return strings.TrimSpace(title) +} + +var commitPrefixes = []string{ + "UPSTREAM:", + "CHROMIUM:", + "FROMLIST:", + "BACKPORT:", + "FROMGIT:", + "net-backports:", +} + +func Patch(dir string, patch []byte) error { + // Do --dry-run first to not mess with partially consistent state. + cmd := osutil.Command("patch", "-p1", "--force", "--ignore-whitespace", "--dry-run") + if err := osutil.Sandbox(cmd, true, true); err != nil { + return err + } + cmd.Stdin = bytes.NewReader(patch) + cmd.Dir = dir + if output, err := cmd.CombinedOutput(); err != nil { + // If it reverses clean, then it's already applied + // (seems to be the easiest way to detect it). + cmd = osutil.Command("patch", "-p1", "--force", "--ignore-whitespace", "--reverse", "--dry-run") + if err := osutil.Sandbox(cmd, true, true); err != nil { + return err + } + cmd.Stdin = bytes.NewReader(patch) + cmd.Dir = dir + if _, err := cmd.CombinedOutput(); err == nil { + return fmt.Errorf("patch is already applied") + } + return fmt.Errorf("failed to apply patch:\n%s", output) + } + // Now apply for real. + cmd = osutil.Command("patch", "-p1", "--force", "--ignore-whitespace") + if err := osutil.Sandbox(cmd, true, true); err != nil { + return err + } + cmd.Stdin = bytes.NewReader(patch) + cmd.Dir = dir + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("failed to apply patch after dry run:\n%s", output) + } + return nil +} + +type BisectResult int + +const ( + BisectBad BisectResult = iota + BisectGood + BisectSkip +) + +// Bisect bisects good..bad commit range against the provided predicate (wrapper around git bisect). +// The predicate should return an error only if there is no way to proceed +// (it will abort the process), if possible it should prefer to return BisectSkip. +// Progress of the process is streamed to the provided trace. +// Returns the first commit on which the predicate returns BisectBad. +func Bisect(dir, bad, good string, trace io.Writer, pred func() (BisectResult, error)) (*Commit, error) { + runSandboxed(dir, "git", "bisect", "reset") + runSandboxed(dir, "git", "reset", "--hard") + firstBad, err := GetCommit(dir, bad) + if err != nil { + return nil, err + } + output, err := runSandboxed(dir, "git", "bisect", "start", bad, good) + if err != nil { + return nil, err + } + defer runSandboxed(dir, "git", "bisect", "reset") + fmt.Fprintf(trace, "# git bisect start %v %v\n%s", bad, good, output) + current, err := HeadCommit(dir) + if err != nil { + return nil, err + } + var bisectTerms = [...]string{ + BisectBad: "bad", + BisectGood: "good", + BisectSkip: "skip", + } + for { + res, err := pred() + if err != nil { + return nil, err + } + if res == BisectBad { + firstBad = current + } + output, err = runSandboxed(dir, "git", "bisect", bisectTerms[res]) + if err != nil { + return nil, err + } + fmt.Fprintf(trace, "# git bisect %v %v\n%s", bisectTerms[res], current.Hash, output) + next, err := HeadCommit(dir) + if err != nil { + return nil, err + } + if current.Hash == next.Hash { + return firstBad, nil + } + current = next + } +} + +// PreviousReleaseTags returns list of preceding release tags that are reachable from the given commit. +// Note: linux-specific. +func PreviousReleaseTags(dir, commit string) ([]string, error) { + output, err := runSandboxed(dir, "git", "tag", "--no-contains", commit, "--merged", commit, "v*.*") + if err != nil { + return nil, err + } + return parseReleaseTags(output) +} + +func parseReleaseTags(output []byte) ([]string, error) { + var tags []string + for _, tag := range bytes.Split(output, []byte{'\n'}) { + if releaseTagRe.Match(tag) && releaseTagToInt(string(tag)) != 0 { + tags = append(tags, string(tag)) + } + } + sort.Slice(tags, func(i, j int) bool { + return releaseTagToInt(tags[i]) > releaseTagToInt(tags[j]) + }) + return tags, nil +} + +func releaseTagToInt(tag string) uint64 { + matches := releaseTagRe.FindStringSubmatchIndex(tag) + v1, err := strconv.ParseUint(tag[matches[2]:matches[3]], 10, 64) + if err != nil { + return 0 + } + v2, err := strconv.ParseUint(tag[matches[4]:matches[5]], 10, 64) + if err != nil { + return 0 + } + var v3 uint64 + if matches[6] != -1 { + v3, err = strconv.ParseUint(tag[matches[6]:matches[7]], 10, 64) + if err != nil { + return 0 + } + } + return v1*1e6 + v2*1e3 + v3 +} + +func runSandboxed(dir, command string, args ...string) ([]byte, error) { + cmd := osutil.Command(command, args...) + cmd.Dir = dir + if err := osutil.Sandbox(cmd, true, false); err != nil { + return nil, err + } + return osutil.Run(timeout, cmd) +} + +// CheckRepoAddress does a best-effort approximate check of a git repo address. +func CheckRepoAddress(repo string) bool { + return gitRepoRe.MatchString(repo) +} + +// CheckBranch does a best-effort approximate check of a git branch name. +func CheckBranch(branch string) bool { + return gitBranchRe.MatchString(branch) +} + +func CheckCommitHash(hash string) bool { + if !gitHashRe.MatchString(hash) { + return false + } + ln := len(hash) + return ln == 8 || ln == 10 || ln == 12 || ln == 16 || ln == 20 || ln == 40 +} + +var ( + // nolint: lll + gitRepoRe = regexp.MustCompile(`^(git|ssh|http|https|ftp|ftps)://[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)+(:[0-9]+)?/[a-zA-Z0-9-_./]+\.git(/)?$`) + gitBranchRe = regexp.MustCompile("^[a-zA-Z0-9-_/.]{2,200}$") + gitHashRe = regexp.MustCompile("^[a-f0-9]+$") + releaseTagRe = regexp.MustCompile(`^v([0-9]+).([0-9]+)(?:\.([0-9]+))?$`) + ccRes = []*regexp.Regexp{ + regexp.MustCompile(`^Reviewed\-.*: (.*)$`), + regexp.MustCompile(`^[A-Za-z-]+\-and\-[Rr]eviewed\-.*: (.*)$`), + regexp.MustCompile(`^Acked\-.*: (.*)$`), + regexp.MustCompile(`^[A-Za-z-]+\-and\-[Aa]cked\-.*: (.*)$`), + regexp.MustCompile(`^Tested\-.*: (.*)$`), + regexp.MustCompile(`^[A-Za-z-]+\-and\-[Tt]ested\-.*: (.*)$`), + } +) diff --git a/pkg/vcs/git_test.go b/pkg/vcs/git_test.go new file mode 100644 index 000000000..6b7cf183e --- /dev/null +++ b/pkg/vcs/git_test.go @@ -0,0 +1,239 @@ +// Copyright 2017 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package vcs + +import ( + "reflect" + "strings" + "testing" + "time" +) + +func TestParseCommit(t *testing.T) { + tests := map[string]*Commit{ + `2075b16e32c26e4031b9fd3cbe26c54676a8fcb5 +rbtree: include rcu.h +foobar@foobar.de +Fri May 11 16:02:14 2018 -0700 +Since commit c1adf20052d8 ("Introduce rb_replace_node_rcu()") +rbtree_augmented.h uses RCU related data structures but does not include +the header file. It works as long as it gets somehow included before +that and fails otherwise. + +Link: http://lkml.kernel.org/r/20180504103159.19938-1-bigeasy@linutronix.de +Signed-off-by: Foo Bad Baz +Reviewed-by: +Cc: Unrelated Guy +Acked-by: Subsystem reviewer +Reported-and-tested-by: and@me.com +Reported-and-Tested-by: Name-name +Tested-by: Must be correct +Signed-off-by: Linux Master +`: &Commit{ + Hash: "2075b16e32c26e4031b9fd3cbe26c54676a8fcb5", + Title: "rbtree: include rcu.h", + Author: "foobar@foobar.de", + CC: []string{ + "and@me.com", + "foobar@foobar.de", + "mustbe@correct.com", + "name@name.com", + "subsystem@reviewer.com", + "yetanother@email.org", + }, + Date: time.Date(2018, 5, 11, 16, 02, 14, 0, time.FixedZone("", -7*60*60)), + }, + } + for input, com := range tests { + res, err := parseCommit([]byte(input)) + if err != nil && com != nil { + t.Fatalf("want %+v, got error: %v", com, err) + } + if err == nil && com == nil { + t.Fatalf("want error, got commit %+v", res) + } + if com == nil { + continue + } + if com.Hash != res.Hash { + t.Fatalf("want hash %q, got %q", com.Hash, res.Hash) + } + if com.Title != res.Title { + t.Fatalf("want title %q, got %q", com.Title, res.Title) + } + if com.Author != res.Author { + t.Fatalf("want author %q, got %q", com.Author, res.Author) + } + if !reflect.DeepEqual(com.CC, res.CC) { + t.Fatalf("want CC %q, got %q", com.CC, res.CC) + } + if !com.Date.Equal(res.Date) { + t.Fatalf("want date %v, got %v", com.Date, res.Date) + } + } +} + +func TestCanonicalizeCommit(t *testing.T) { + tests := map[string]string{ + "foo bar": "foo bar", + " foo ": "foo", + "UPSTREAM: foo bar": "foo bar", + "BACKPORT: UPSTREAM: foo bar": "UPSTREAM: foo bar", + } + for in, want := range tests { + got := CanonicalizeCommit(in) + if got != want { + t.Errorf("input %q: got %q, want %q", in, got, want) + } + } +} + +func TestCheckRepoAddress(t *testing.T) { + testPredicate(t, CheckRepoAddress, map[string]bool{ + "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git": true, + "https://github.com/torvalds/linux.git": true, + "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git": true, + "git://git.cmpxchg.org/linux-mmots.git": true, + "https://anonscm.debian.org/git/kernel/linux.git": true, + "git://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": true, + "http://host.xz:123/path/to/repo.git/": true, + "": false, + "foobar": false, + "linux-next": false, + "foo://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": false, + "git://kernel/ubuntu.git": false, + "git://kernel.com/ubuntu": false, + "gitgit://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git": false, + }) +} + +func TestCheckBranch(t *testing.T) { + testPredicate(t, CheckBranch, map[string]bool{ + "master": true, + "core/core": true, + "irq-irqdomain-for-linus": true, + "timers/2038": true, + "ubuntu-zesty/v4.9.4": true, + "WIP.locking/atomics": true, + "linux-4.9.y": true, + "abi_spec": true, + "@": false, + "": false, + }) +} + +func TestCheckCommitHash(t *testing.T) { + testPredicate(t, CheckCommitHash, map[string]bool{ + "ff12bea91c22bba93d3ffc3034d813d686bc7eeb": true, // 40 + "eae05cb0aaeae05cb0aa": true, // 20 + "449dd6984d0eaabb": true, // 16 + "449dd6984d0e": true, // 12 + "eae05cb0aa": true, // 10 + "eae05cb0": true, // 8 + "": false, + "aa": false, + "eae05cb0aab": false, + "xxxxxxxx": false, + }) +} + +func testPredicate(t *testing.T, fn func(string) bool, tests map[string]bool) { + for input, want := range tests { + res := fn(input) + if res != want { + t.Errorf("%v: got %v, want %v", input, res, want) + } + } +} + +func TestParseReleaseTags(t *testing.T) { + input := ` +v3.1 +v2.6.12 +v2.6.39 +v3.0 +v3.10 +v2.6.13 +v3.11 +v3.19 +v3.9 +v3.2 +v4.9 +v2.6.32 +v4.0 +voo +v1.foo +v10.2.foo +v1.2. +v1. +` + want := []string{ + "v4.9", + "v4.0", + "v3.19", + "v3.11", + "v3.10", + "v3.9", + "v3.2", + "v3.1", + "v3.0", + "v2.6.39", + "v2.6.32", + "v2.6.13", + "v2.6.12", + } + got, err := parseReleaseTags([]byte(input)) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("got bad tags\ngot: %+v\nwant: %+v", got, want) + } +} + +func TestExtractFixTags(t *testing.T) { + commits, err := extractFixTags(strings.NewReader(extractFixTagsInput), extractFixTagsEmail) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(commits, extractFixTagsOutput) { + t.Fatalf("got : %+v\twant: %+v", commits, extractFixTagsOutput) + } +} + +const extractFixTagsEmail = "\"syzbot\" " + +var extractFixTagsOutput = []FixCommit{ + {"8e4090902540da8c6e8f", "dashboard/app: bump max repros per bug to 10"}, + {"8e4090902540da8c6e8f", "executor: remove dead code"}, + {"a640a0fc325c29c3efcb", "executor: remove dead code"}, + {"8e4090902540da8c6e8fa640a0fc325c29c3efcb", "pkg/csource: fix string escaping bug"}, +} + +var extractFixTagsInput = ` +commit 73aba437a774237b1130837b856f3b40b3ec3bf0 (HEAD -> master, origin/master) +Author: me +Date: Fri Dec 22 19:59:56 2017 +0100 + + dashboard/app: bump max repros per bug to 10 + + Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com + +commit 26cd53f078db858a6ccca338e13e7f4d1d291c22 +Author: me +Date: Fri Dec 22 13:42:27 2017 +0100 + + executor: remove dead code + + Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com + Reported-by: syzbot + +commit 7b62abdb0abadbaf7b3f3a23ab4d78485fbf9059 +Author: Dmitry Vyukov +Date: Fri Dec 22 11:59:09 2017 +0100 + + pkg/csource: fix string escaping bug + + Reported-and-tested-by: syzbot+8e4090902540da8c6e8fa640a0fc325c29c3efcb@my.mail.com +` -- cgit mrf-deployment