github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/vcs/vcs_test.go (about)

     1  // Copyright 2018 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  
     4  package vcs
     5  
     6  import (
     7  	"net/mail"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestCanonicalizeCommit(t *testing.T) {
    14  	tests := map[string]string{
    15  		"foo bar":                     "foo bar",
    16  		" foo ":                       "foo",
    17  		"UPSTREAM: foo bar":           "foo bar",
    18  		"BACKPORT: UPSTREAM: foo bar": "UPSTREAM: foo bar",
    19  	}
    20  	for in, want := range tests {
    21  		got := CanonicalizeCommit(in)
    22  		if got != want {
    23  			t.Errorf("input %q: got %q, want %q", in, got, want)
    24  		}
    25  	}
    26  }
    27  
    28  func TestCheckRepoAddress(t *testing.T) {
    29  	testPredicate(t, CheckRepoAddress, map[string]bool{
    30  		"git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git":      true,
    31  		"https://github.com/torvalds/linux.git":                                 true,
    32  		"git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git": true,
    33  		"git://git.cmpxchg.org/linux-mmots.git":                                 true,
    34  		"https://anonscm.debian.org/git/kernel/linux.git":                       true,
    35  		"git://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git":                       true,
    36  		"git://git.armlinux.org.uk/~rmk/linux-arm.git":                          true,
    37  		"http://host.xz:123/path/to/repo.git/":                                  true,
    38  		"https://chromium.googlesource.com/chromiumos/third_party/kernel":       true,
    39  		"https://fuchsia.googlesource.com":                                      true,
    40  		"git@my-github.com:my/fd.git":                                           true,
    41  		"file:///repo/linux.git":                                                true,
    42  		"git://kernel/ubuntu.git":                                               true,
    43  		"git@my-github/fd.git":                                                  true,
    44  		"sso://server/repo":                                                     true,
    45  		"git@my-github.com:/fd.git":                                             false,
    46  		"gitgit@my-github:/fd.git":                                              false,
    47  		"":                                                                      false,
    48  		"foobar":                                                                false,
    49  		"linux-next":                                                            false,
    50  		"foo://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git":                       false,
    51  		"gitgit://kernel.ubuntu.com/ubuntu/ubuntu-zesty.git":                    false,
    52  	})
    53  }
    54  
    55  func TestCheckBranch(t *testing.T) {
    56  	testPredicate(t, CheckBranch, map[string]bool{
    57  		"master":                  true,
    58  		"core/core":               true,
    59  		"irq-irqdomain-for-linus": true,
    60  		"timers/2038":             true,
    61  		"ubuntu-zesty/v4.9.4":     true,
    62  		"WIP.locking/atomics":     true,
    63  		"linux-4.9.y":             true,
    64  		"abi_spec":                true,
    65  		"@":                       false,
    66  		"":                        false,
    67  	})
    68  }
    69  
    70  func TestCheckCommitHash(t *testing.T) {
    71  	testPredicate(t, CheckCommitHash, map[string]bool{
    72  		"ff12bea91c22bba93d3ffc3034d813d686bc7eeb": true, // 40
    73  		"eae05cb0aaeae05cb0aa":                     true, // 20
    74  		"449dd6984d0eaabbc":                        true, // 17
    75  		"449dd6984d0eaabb":                         true, // 16
    76  		"a4983672f9ca4c":                           true, // 14
    77  		"449dd6984d0e":                             true, // 12
    78  		"eae05cb0aab":                              true, // 11
    79  		"eae05cb0aa":                               true, // 10
    80  		"eae05cb0":                                 true, // 8
    81  		"":                                         false,
    82  		"aa":                                       false,
    83  		"eae05cb":                                  false,
    84  		"ff12bea91c22bba93d3ffc3034d813d686bc7eebb": false,
    85  		"xxxxxxxx": false,
    86  	})
    87  }
    88  
    89  func testPredicate(t *testing.T, fn func(string) bool, tests map[string]bool) {
    90  	for input, want := range tests {
    91  		res := fn(input)
    92  		if res != want {
    93  			t.Errorf("%v: got %v, want %v", input, res, want)
    94  		}
    95  	}
    96  }
    97  
    98  func TestCommitLink(t *testing.T) {
    99  	type Test struct {
   100  		URL        string
   101  		Hash       string
   102  		CommitLink string
   103  	}
   104  	tests := []Test{
   105  		{
   106  			"https://github.com/google/syzkaller",
   107  			"76dd003f1b102b791d8b342a1f92a6486ff56a1e",
   108  			"https://github.com/google/syzkaller/commit/76dd003f1b102b791d8b342a1f92a6486ff56a1e",
   109  		},
   110  		{
   111  			"https://github.com/google/syzkaller",
   112  			"master",
   113  			"https://github.com/google/syzkaller/commit/master",
   114  		},
   115  		{
   116  			"https://github.com/google/syzkaller.git",
   117  			"76dd003f1b",
   118  			"https://github.com/google/syzkaller/commit/76dd003f1b",
   119  		},
   120  		{
   121  			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
   122  			"8fe28cb58bcb",
   123  			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8fe28cb58bcb",
   124  		},
   125  		{
   126  			"git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
   127  			"8fe28cb58b",
   128  			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8fe28cb58b",
   129  		},
   130  
   131  		{
   132  			"https://android.googlesource.com/kernel/common",
   133  			"d0c3914ffbe4c00f0a131bae83f811d5606699bc",
   134  			"https://android.googlesource.com/kernel/common/+/d0c3914ffbe4c00f0a131bae83f811d5606699bc^!",
   135  		},
   136  		{
   137  			"https://gvisor.googlesource.com/gvisor",
   138  			"5301cbf8430e5436211bc142c0886d8c11cc71ab",
   139  			"https://gvisor.googlesource.com/gvisor/+/5301cbf8430e5436211bc142c0886d8c11cc71ab^!",
   140  		},
   141  		{
   142  			"https://fuchsia.googlesource.com",
   143  			"13ee3dc5e4c46bf127977ad28645c47442ec517d",
   144  			"https://fuchsia.googlesource.com/fuchsia/+/13ee3dc5e4c46bf127977ad28645c47442ec517d^!",
   145  		},
   146  		{
   147  			"git://git.cmpxchg.org/linux-mmots.git",
   148  			"8fe28cb58b",
   149  			"",
   150  		},
   151  		{
   152  			"",
   153  			"8fe28cb58b",
   154  			"",
   155  		},
   156  		{
   157  			"https://android.googlesource.com/kernel/common",
   158  			"",
   159  			"",
   160  		},
   161  		{
   162  			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux",
   163  			"a40f7c63275979a11b7c146a83aa08d91c7d3ae8",
   164  			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux/+/a40f7c63275979a11b7c146a83aa08d91c7d3ae8^!",
   165  		},
   166  		{
   167  			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux",
   168  			"refs/changes/36/8736/3",
   169  			"https://linux.googlesource.com/linux/kernel/git/torvalds/linux/+/refs/changes/36/8736/3^!",
   170  		},
   171  		{
   172  			"git://git.kernel.dk/linux-block",
   173  			"c9387501192c24c14e5a97f97bc1f60cdd071a29",
   174  			"https://git.kernel.dk/cgit/linux-block/commit/?id=c9387501192c24c14e5a97f97bc1f60cdd071a29",
   175  		},
   176  		{
   177  			"git://git.kernel.dk/linux-block",
   178  			"syzbot-test",
   179  			"https://git.kernel.dk/cgit/linux-block/commit/?id=syzbot-test",
   180  		},
   181  		{
   182  			"git://git.breakpoint.cc/fw/net-next.git",
   183  			"06690d5c6466b604f674477b522a809673c17eff",
   184  			"https://git.breakpoint.cc/cgit/fw/net-next.git/commit/?id=06690d5c6466b604f674477b522a809673c17eff",
   185  		},
   186  	}
   187  	for _, test := range tests {
   188  		link := CommitLink(test.URL, test.Hash)
   189  		if link != test.CommitLink {
   190  			t.Errorf("URL: %v\nhash: %v\nwant: %v\ngot:  %v", test.URL, test.Hash, test.CommitLink, link)
   191  		}
   192  	}
   193  }
   194  
   195  func TestFileLink(t *testing.T) {
   196  	type Test struct {
   197  		URL      string
   198  		Hash     string
   199  		File     string
   200  		Line     int
   201  		FileLink string
   202  	}
   203  	tests := []Test{
   204  		{
   205  			"https://github.com/google/syzkaller",
   206  			"76dd003f1b102b791d8b342a1f92a6486ff56a1e",
   207  			"Makefile",
   208  			42,
   209  			"https://github.com/google/syzkaller/blob/76dd003f1b102b791d8b342a1f92a6486ff56a1e/Makefile#L42",
   210  		},
   211  		{
   212  			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
   213  			"8fe28cb58bcb",
   214  			"Makefile",
   215  			42,
   216  			"https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Makefile?id=8fe28cb58bcb#n42",
   217  		},
   218  		{
   219  			"https://android.googlesource.com/kernel/common",
   220  			"d0c3914ffbe4c00f0a131bae83f811d5606699bc",
   221  			"Makefile",
   222  			42,
   223  			"https://android.googlesource.com/kernel/common/+/d0c3914ffbe4c00f0a131bae83f811d5606699bc/Makefile#42",
   224  		},
   225  	}
   226  	for _, test := range tests {
   227  		link := FileLink(test.URL, test.Hash, test.File, test.Line)
   228  		if link != test.FileLink {
   229  			t.Errorf("test: %+v\ngot:  %v", test, link)
   230  		}
   231  	}
   232  }
   233  
   234  func TestParse(t *testing.T) {
   235  	// nolint: lll
   236  	test1 := []byte(`Foo Bar <a@email.com> (maintainer:KERNEL)
   237  	Foo Bar<b@email.com> (reviewer:KERNEL)
   238  	<somelist@list.com> (open list:FOO)
   239  	"Supporter Foo" <c@email.com> (supporter:KERNEL)
   240  	linux-kernel@vger.kernel.org (open list)`)
   241  	// nolint: lll
   242  	test2 := []byte(`Foo Bar <a@email.com> (maintainer:KERNEL)
   243  	Foo Bar<b@email.com> (reviewer:KERNEL)
   244  	"Supporter Foo" <c@email.com> (supporter:KERNEL)
   245  	linux-kernel@vger.kernel.org (open list)`)
   246  
   247  	maintainers1 := Recipients{{mail.Address{Name: "Foo Bar", Address: "a@email.com"}, To},
   248  		{mail.Address{Name: "Foo Bar", Address: "b@email.com"}, Cc},
   249  		{mail.Address{Name: "Supporter Foo", Address: "c@email.com"}, To},
   250  		{mail.Address{Name: "", Address: "linux-kernel@vger.kernel.org"}, Cc},
   251  		{mail.Address{Name: "", Address: "somelist@list.com"}, To}}
   252  	maintainers2 := Recipients{{mail.Address{Name: "Foo Bar", Address: "a@email.com"}, To},
   253  		{mail.Address{Name: "Foo Bar", Address: "b@email.com"}, Cc},
   254  		{mail.Address{Name: "Supporter Foo", Address: "c@email.com"}, To},
   255  		{mail.Address{Name: "", Address: "linux-kernel@vger.kernel.org"}, To}}
   256  
   257  	if diff := cmp.Diff(ParseMaintainersLinux(test1), maintainers1); diff != "" {
   258  		t.Fatal(diff)
   259  	}
   260  	if diff := cmp.Diff(ParseMaintainersLinux(test2), maintainers2); diff != "" {
   261  		t.Fatal(diff)
   262  	}
   263  	if diff := cmp.Diff(ParseMaintainersLinux([]byte("")), Recipients(nil)); diff != "" {
   264  		t.Fatal(diff)
   265  	}
   266  }