1// Copyright 2017 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4package vcs
5
6import (
7	"reflect"
8	"strings"
9	"testing"
10	"time"
11)
12
13func TestGitParseCommit(t *testing.T) {
14	tests := map[string]*Commit{
15		`2075b16e32c26e4031b9fd3cbe26c54676a8fcb5
16rbtree: include rcu.h
17foobar@foobar.de
18Fri May 11 16:02:14 2018 -0700
19Since commit c1adf20052d8 ("Introduce rb_replace_node_rcu()")
20rbtree_augmented.h uses RCU related data structures but does not include
21the header file.  It works as long as it gets somehow included before
22that and fails otherwise.
23
24Link: http://lkml.kernel.org/r/20180504103159.19938-1-bigeasy@linutronix.de
25Signed-off-by: Foo Bad Baz <another@email.de>
26Reviewed-by: <yetanother@email.org>
27Cc: Unrelated Guy <somewhere@email.com>
28Acked-by: Subsystem reviewer <Subsystem@reviewer.com>
29Reported-and-tested-by: and@me.com
30Reported-and-Tested-by: Name-name <name@name.com>
31Tested-by: Must be correct <mustbe@correct.com>
32Signed-off-by: Linux Master <linux@linux-foundation.org>
33`: {
34			Hash:   "2075b16e32c26e4031b9fd3cbe26c54676a8fcb5",
35			Title:  "rbtree: include rcu.h",
36			Author: "foobar@foobar.de",
37			CC: []string{
38				"and@me.com",
39				"foobar@foobar.de",
40				"mustbe@correct.com",
41				"name@name.com",
42				"subsystem@reviewer.com",
43				"yetanother@email.org",
44			},
45			Date: time.Date(2018, 5, 11, 16, 02, 14, 0, time.FixedZone("", -7*60*60)),
46		},
47	}
48	for input, com := range tests {
49		res, err := gitParseCommit([]byte(input))
50		if err != nil && com != nil {
51			t.Fatalf("want %+v, got error: %v", com, err)
52		}
53		if err == nil && com == nil {
54			t.Fatalf("want error, got commit %+v", res)
55		}
56		if com == nil {
57			continue
58		}
59		if com.Hash != res.Hash {
60			t.Fatalf("want hash %q, got %q", com.Hash, res.Hash)
61		}
62		if com.Title != res.Title {
63			t.Fatalf("want title %q, got %q", com.Title, res.Title)
64		}
65		if com.Author != res.Author {
66			t.Fatalf("want author %q, got %q", com.Author, res.Author)
67		}
68		if !reflect.DeepEqual(com.CC, res.CC) {
69			t.Fatalf("want CC %q, got %q", com.CC, res.CC)
70		}
71		if !com.Date.Equal(res.Date) {
72			t.Fatalf("want date %v, got %v", com.Date, res.Date)
73		}
74	}
75}
76
77func TestGitParseReleaseTags(t *testing.T) {
78	input := `
79v3.1
80v2.6.12
81v2.6.39
82v3.0
83v3.10
84v2.6.13
85v3.11
86v3.19
87v3.9
88v3.2
89v4.9
90v2.6.32
91v4.0
92voo
93v1.foo
94v10.2.foo
95v1.2.
96v1.
97`
98	want := []string{
99		"v4.9",
100		"v4.0",
101		"v3.19",
102		"v3.11",
103		"v3.10",
104		"v3.9",
105		"v3.2",
106		"v3.1",
107		"v3.0",
108		"v2.6.39",
109		"v2.6.32",
110		"v2.6.13",
111		"v2.6.12",
112	}
113	got, err := gitParseReleaseTags([]byte(input))
114	if err != nil {
115		t.Fatal(err)
116	}
117	if !reflect.DeepEqual(got, want) {
118		t.Fatalf("got bad tags\ngot:  %+v\nwant: %+v", got, want)
119	}
120}
121
122func TestGitExtractFixTags(t *testing.T) {
123	commits, err := gitExtractFixTags(strings.NewReader(extractFixTagsInput), extractFixTagsEmail)
124	if err != nil {
125		t.Fatal(err)
126	}
127	if !reflect.DeepEqual(commits, extractFixTagsOutput) {
128		t.Fatalf("got : %+v\twant: %+v", commits, extractFixTagsOutput)
129	}
130}
131
132const extractFixTagsEmail = "\"syzbot\" <syzbot@my.mail.com>"
133
134var extractFixTagsOutput = []FixCommit{
135	{"8e4090902540da8c6e8f", "dashboard/app: bump max repros per bug to 10"},
136	{"8e4090902540da8c6e8f", "executor: remove dead code"},
137	{"a640a0fc325c29c3efcb", "executor: remove dead code"},
138	{"8e4090902540da8c6e8fa640a0fc325c29c3efcb", "pkg/csource: fix string escaping bug"},
139}
140
141var extractFixTagsInput = `
142commit 73aba437a774237b1130837b856f3b40b3ec3bf0 (HEAD -> master, origin/master)
143Author: me <foo@bar.com>
144Date:   Fri Dec 22 19:59:56 2017 +0100
145
146    dashboard/app: bump max repros per bug to 10
147
148    Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com
149
150commit 26cd53f078db858a6ccca338e13e7f4d1d291c22
151Author: me <foo@bar.com>
152Date:   Fri Dec 22 13:42:27 2017 +0100
153
154    executor: remove dead code
155
156    Reported-by: syzbot+8e4090902540da8c6e8f@my.mail.com
157    Reported-by: syzbot <syzbot+a640a0fc325c29c3efcb@my.mail.com>
158
159commit 7b62abdb0abadbaf7b3f3a23ab4d78485fbf9059
160Author: Dmitry Vyukov <dvyukov@google.com>
161Date:   Fri Dec 22 11:59:09 2017 +0100
162
163    pkg/csource: fix string escaping bug
164
165    Reported-and-tested-by: syzbot+8e4090902540da8c6e8fa640a0fc325c29c3efcb@my.mail.com
166`
167