aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/git/git_test.go
blob: 18ba03d6eac0ba7cbf751b3eb939d36190274b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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"
)

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 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\" <syzbot@my.mail.com>"

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 <foo@bar.com>
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 <foo@bar.com>
Date:   Fri Dec 22 13:42:27 2017 +0100

    executor: remove dead code
    
    Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com
    Reported-by: syzbot <syzbot+a640a0fc325c29c3efcb@my.mail.com>

commit 7b62abdb0abadbaf7b3f3a23ab4d78485fbf9059
Author: Dmitry Vyukov <dvyukov@google.com>
Date:   Fri Dec 22 11:59:09 2017 +0100

    pkg/csource: fix string escaping bug
    
    Reported-and-tested-by: syzbot+8e4090902540da8c6e8fa640a0fc325c29c3efcb@my.mail.com
`