// Copyright 2018 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 (
	"net/mail"
	"testing"

	"github.com/google/go-cmp/cmp"
)

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,
		"git://git.armlinux.org.uk/~rmk/linux-arm.git":                          true,
		"http://host.xz:123/path/to/repo.git/":                                  true,
		"https://chromium.googlesource.com/chromiumos/third_party/kernel":       true,
		"https://fuchsia.googlesource.com":                                      true,
		"git@my-github.com:my/fd.git":                                           true,
		"file:///repo/linux.git":                                                true,
		"git://kernel/ubuntu.git":                                               true,
		"git@my-github/fd.git":                                                  true,
		"sso://server/repo":                                                     true,
		"git@my-github.com:/fd.git":                                             false,
		"gitgit@my-github:/fd.git":                                              false,
		"":                                                                      false,
		"foobar":                                                                false,
		"linux-next":                                                            false,
		"foo://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git":                       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
		"449dd6984d0eaabbc":                        true, // 17
		"449dd6984d0eaabb":                         true, // 16
		"a4983672f9ca4c":                           true, // 14
		"449dd6984d0e":                             true, // 12
		"eae05cb0aab":                              true, // 11
		"eae05cb0aa":                               true, // 10
		"eae05cb0":                                 true, // 8
		"":                                         false,
		"aa":                                       false,
		"eae05cb":                                  false,
		"ff12bea91c22bba93d3ffc3034d813d686bc7eebb": 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 TestCommitLink(t *testing.T) {
	type Test struct {
		URL        string
		Hash       string
		CommitLink string
	}
	tests := []Test{
		{
			"https://github.com/google/syzkaller",
			"76dd003f1b102b791d8b342a1f92a6486ff56a1e",
			"https://github.com/google/syzkaller/commit/76dd003f1b102b791d8b342a1f92a6486ff56a1e",
		},
		{
			"https://github.com/google/syzkaller",
			"master",
			"https://github.com/google/syzkaller/commit/master",
		},
		{
			"https://github.com/google/syzkaller.git",
			"76dd003f1b",
			"https://github.com/google/syzkaller/commit/76dd003f1b",
		},
		{
			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
			"8fe28cb58bcb",
			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8fe28cb58bcb",
		},
		{
			"git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
			"8fe28cb58b",
			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8fe28cb58b",
		},

		{
			"https://android.googlesource.com/kernel/common",
			"d0c3914ffbe4c00f0a131bae83f811d5606699bc",
			"https://android.googlesource.com/kernel/common/+/d0c3914ffbe4c00f0a131bae83f811d5606699bc^!",
		},
		{
			"https://gvisor.googlesource.com/gvisor",
			"5301cbf8430e5436211bc142c0886d8c11cc71ab",
			"https://gvisor.googlesource.com/gvisor/+/5301cbf8430e5436211bc142c0886d8c11cc71ab^!",
		},
		{
			"https://fuchsia.googlesource.com",
			"13ee3dc5e4c46bf127977ad28645c47442ec517d",
			"https://fuchsia.googlesource.com/fuchsia/+/13ee3dc5e4c46bf127977ad28645c47442ec517d^!",
		},
		{
			"git://git.cmpxchg.org/linux-mmots.git",
			"8fe28cb58b",
			"",
		},
		{
			"",
			"8fe28cb58b",
			"",
		},
		{
			"https://android.googlesource.com/kernel/common",
			"",
			"",
		},
		{
			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux",
			"a40f7c63275979a11b7c146a83aa08d91c7d3ae8",
			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux/+/a40f7c63275979a11b7c146a83aa08d91c7d3ae8^!",
		},
		{
			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux",
			"refs/changes/36/8736/3",
			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux/+/refs/changes/36/8736/3^!",
		},
		{
			"git://git.kernel.dk/linux-block",
			"c9387501192c24c14e5a97f97bc1f60cdd071a29",
			"https://git.kernel.dk/cgit/linux-block/commit/?id=c9387501192c24c14e5a97f97bc1f60cdd071a29",
		},
		{
			"git://git.kernel.dk/linux-block",
			"syzbot-test",
			"https://git.kernel.dk/cgit/linux-block/commit/?id=syzbot-test",
		},
		{
			"git://git.breakpoint.cc/fw/net-next.git",
			"06690d5c6466b604f674477b522a809673c17eff",
			"https://git.breakpoint.cc/cgit/fw/net-next.git/commit/?id=06690d5c6466b604f674477b522a809673c17eff",
		},
	}
	for _, test := range tests {
		link := CommitLink(test.URL, test.Hash)
		if link != test.CommitLink {
			t.Errorf("URL: %v\nhash: %v\nwant: %v\ngot:  %v", test.URL, test.Hash, test.CommitLink, link)
		}
	}
}

func TestFileLink(t *testing.T) {
	type Test struct {
		URL      string
		Hash     string
		File     string
		Line     int
		FileLink string
	}
	tests := []Test{
		{
			"https://github.com/google/syzkaller",
			"76dd003f1b102b791d8b342a1f92a6486ff56a1e",
			"Makefile",
			42,
			"https://github.com/google/syzkaller/blob/76dd003f1b102b791d8b342a1f92a6486ff56a1e/Makefile#L42",
		},
		{
			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
			"8fe28cb58bcb",
			"Makefile",
			42,
			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Makefile?id=8fe28cb58bcb#n42",
		},
		{
			"https://android.googlesource.com/kernel/common",
			"d0c3914ffbe4c00f0a131bae83f811d5606699bc",
			"Makefile",
			42,
			"https://android.googlesource.com/kernel/common/+/d0c3914ffbe4c00f0a131bae83f811d5606699bc/Makefile#42",
		},
	}
	for _, test := range tests {
		link := FileLink(test.URL, test.Hash, test.File, test.Line)
		if link != test.FileLink {
			t.Errorf("Test: %+v\ngot:  %v", test, link)
		}
	}
}

func TestParse(t *testing.T) {
	// nolint: lll
	test1 := []byte(`Foo (Maintainer) Bar <a@email.com> (maintainer:KERNEL)
	Foo Bar(Reviewer) <b@email.com> (reviewer:KERNEL)
	<somelist@list.com> (open list:FOO)
	"Supporter Foo" <c@email.com> (supporter:KERNEL)
	linux-kernel@vger.kernel.org (open list)`)
	// nolint: lll
	test2 := []byte(`Foo (Maintainer) Bar <a@email.com> (maintainer:KERNEL)
	Foo Bar(Reviewer) <b@email.com> (reviewer:KERNEL)
	"Supporter Foo" <c@email.com> (supporter:KERNEL)
	linux-kernel@vger.kernel.org (open list)`)

	maintainers1 := Recipients{{mail.Address{Name: "Foo (Maintainer) Bar", Address: "a@email.com"}, To},
		{mail.Address{Name: "Foo Bar(Reviewer)", Address: "b@email.com"}, Cc},
		{mail.Address{Name: "Supporter Foo", Address: "c@email.com"}, To},
		{mail.Address{Name: "", Address: "linux-kernel@vger.kernel.org"}, Cc},
		{mail.Address{Name: "", Address: "somelist@list.com"}, To}}
	maintainers2 := Recipients{{mail.Address{Name: "Foo (Maintainer) Bar", Address: "a@email.com"}, To},
		{mail.Address{Name: "Foo Bar(Reviewer)", Address: "b@email.com"}, Cc},
		{mail.Address{Name: "Supporter Foo", Address: "c@email.com"}, To},
		{mail.Address{Name: "", Address: "linux-kernel@vger.kernel.org"}, To}}

	if diff := cmp.Diff(ParseMaintainersLinux(test1), maintainers1); diff != "" {
		t.Fatal(diff)
	}
	if diff := cmp.Diff(ParseMaintainersLinux(test2), maintainers2); diff != "" {
		t.Fatal(diff)
	}
	if diff := cmp.Diff(ParseMaintainersLinux([]byte("")), Recipients(nil)); diff != "" {
		t.Fatal(diff)
	}
}
