github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/subsystem/linux/maintainers_test.go (about)

     1  // Copyright 2023 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 linux
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/google/syzkaller/pkg/subsystem"
    12  )
    13  
    14  func TestRecordToPathRule(t *testing.T) {
    15  	tests := []struct {
    16  		name    string
    17  		record  maintainersRecord
    18  		match   []string
    19  		noMatch []string
    20  	}{
    21  		{
    22  			name: `general test`,
    23  			record: maintainersRecord{
    24  				includePatterns: []string{
    25  					`drivers/gpio/gpio-*wm*.c`,
    26  					`drivers/hwmon/wm83??-hwmon.c`,
    27  					`include/linux/mfd/arizona/`,
    28  					`include/linux/wm97xx.h`,
    29  				},
    30  			},
    31  			match: []string{
    32  				`drivers/gpio/gpio-wm831x.c`,
    33  				`drivers/gpio/gpio-abcdwm831x.c`,
    34  				`drivers/hwmon/wm8355-hwmon.c`,
    35  				`include/linux/mfd/arizona/file.c`,
    36  				`include/linux/mfd/arizona/subfolder/file.c`,
    37  				`include/linux/wm97xx.h`,
    38  			},
    39  			noMatch: []string{
    40  				`drivers/gpio/gpio-w831x.c`,
    41  				`drivers/hwmon/wm83556-hwmon.c`,
    42  				`drivers/hwmon/wm831-hwmon.c`,
    43  				`include/linux/mfd`,
    44  				`include`,
    45  				`random-file`,
    46  			},
    47  		},
    48  		{
    49  			name: `include patterns and regexp`,
    50  			record: maintainersRecord{
    51  				includePatterns: []string{`drivers/rtc/rtc-opal.c`},
    52  				regexps:         []string{`[^a-z0-9]ps3`},
    53  			},
    54  			match: []string{
    55  				`drivers/rtc/rtc-opal.c`,
    56  				`drivers/ps3/a.c`,
    57  				`drivers/sub/ps3/a.c`,
    58  				`drivers/sub/sub/ps3.c`,
    59  			},
    60  			noMatch: []string{
    61  				`drivers/aps3/a.c`,
    62  				`drivers/abc/aps3.c`,
    63  			},
    64  		},
    65  		{
    66  			name: `exclude patterns`,
    67  			record: maintainersRecord{
    68  				includePatterns: []string{`security/`},
    69  				excludePatterns: []string{`security/selinux/`},
    70  			},
    71  			match: []string{
    72  				`security/apparmor/abcd.c`,
    73  				`security/abcd.c`,
    74  			},
    75  			noMatch: []string{
    76  				`security/selinux/abcd.c`,
    77  			},
    78  		},
    79  		{
    80  			name: `handle / at the end`,
    81  			record: maintainersRecord{
    82  				includePatterns: []string{
    83  					`with-subfolders/`,
    84  					`dir/only-one`,
    85  					`also-with-subfolders/*`,
    86  				},
    87  			},
    88  			match: []string{
    89  				`with-subfolders/a`,
    90  				`with-subfolders/a/b`,
    91  				`dir/only-one`,
    92  				`also-with-subfolders/a.c`,
    93  				`also-with-subfolders/b/a.c`,
    94  			},
    95  			noMatch: []string{
    96  				`dir/only-one/a.c`,
    97  				`dir/only-one/a/b.c`,
    98  			},
    99  		},
   100  		{
   101  			name: `wildcards are well escaped`,
   102  			record: maintainersRecord{
   103  				includePatterns: []string{`drivers/net/ethernet/smsc/smc91x.*`},
   104  			},
   105  			match: []string{
   106  				`drivers/net/ethernet/smsc/smc91x.c`,
   107  				`drivers/net/ethernet/smsc/smc91x.h`,
   108  			},
   109  			noMatch: []string{
   110  				`drivers/net/ethernet/smsc/smc91xAh`,
   111  			},
   112  		},
   113  		{
   114  			name: `match everything`,
   115  			record: maintainersRecord{
   116  				includePatterns: []string{`*`, `*/`},
   117  			},
   118  			match: []string{
   119  				`a`,
   120  				`a/b`,
   121  				`a/b/c`,
   122  			},
   123  		},
   124  	}
   125  
   126  	for _, loopTest := range tests {
   127  		test := loopTest
   128  		t.Run(test.name, func(t *testing.T) {
   129  			pm := subsystem.MakePathMatcher([]*subsystem.Subsystem{
   130  				{PathRules: []subsystem.PathRule{test.record.ToPathRule()}},
   131  			})
   132  			for _, path := range test.match {
   133  				if len(pm.Match(path)) != 1 {
   134  					t.Fatalf("did not match %#v", path)
   135  				}
   136  			}
   137  			for _, path := range test.noMatch {
   138  				if len(pm.Match(path)) > 0 {
   139  					t.Fatalf("matched %#v", path)
   140  				}
   141  			}
   142  		})
   143  	}
   144  }
   145  
   146  func TestLinuxMaintainers(t *testing.T) {
   147  	result, err := parseLinuxMaintainers(
   148  		strings.NewReader(maintainersSample),
   149  	)
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  	targetResult := []*maintainersRecord{
   154  		{
   155  			name: "3C59X NETWORK DRIVER",
   156  			includePatterns: []string{
   157  				"Documentation/networking/device_drivers/ethernet/3com/vortex.rst",
   158  				"drivers/net/ethernet/3com/3c59x.c",
   159  			},
   160  			lists:       []string{"netdev@vger.kernel.org"},
   161  			maintainers: []string{"email1@kernel.org"},
   162  		},
   163  		{
   164  			name: "ABI/API",
   165  			includePatterns: []string{
   166  				"include/linux/syscalls.h",
   167  				"kernel/sys_ni.c",
   168  			},
   169  			excludePatterns: []string{
   170  				"include/uapi/",
   171  				"arch/*/include/uapi/",
   172  			},
   173  			lists: []string{"linux-api@vger.kernel.org"},
   174  		},
   175  		{
   176  			name:            "AD1889 ALSA SOUND DRIVER",
   177  			includePatterns: []string{"sound/pci/ad1889.*"},
   178  			lists:           []string{"linux-parisc@vger.kernel.org"},
   179  		},
   180  		{
   181  			name: "PVRUSB2 VIDEO4LINUX DRIVER",
   182  			includePatterns: []string{
   183  				"Documentation/driver-api/media/drivers/pvrusb2*",
   184  				"drivers/media/usb/pvrusb2/",
   185  			},
   186  			lists: []string{
   187  				"pvrusb2@isely.net",
   188  				"linux-media@vger.kernel.org",
   189  			},
   190  			maintainers: []string{"email2@kernel.org"},
   191  			trees:       []string{"git git://linuxtv.org/media_tree.git"},
   192  		},
   193  		{
   194  			name:            "RISC-V ARCHITECTURE",
   195  			includePatterns: []string{"arch/riscv/"},
   196  			regexps:         []string{"riscv"},
   197  			lists:           []string{"linux-riscv@lists.infradead.org"},
   198  			maintainers: []string{
   199  				"email3@kernel.org",
   200  				"email4@kernel.org",
   201  				"email5@kernel.org",
   202  			},
   203  			trees: []string{"git git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux.git"},
   204  		},
   205  		{
   206  			name:            "THE REST",
   207  			includePatterns: []string{"*", "*/"},
   208  			lists:           []string{"linux-kernel@vger.kernel.org"},
   209  			maintainers:     []string{"email6@kernel.org"},
   210  			trees:           []string{"git git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git"},
   211  		},
   212  	}
   213  	if diff := cmp.Diff(targetResult, result,
   214  		cmp.AllowUnexported(maintainersRecord{})); diff != "" {
   215  		t.Fatal(diff)
   216  	}
   217  }
   218  
   219  const maintainersSample = `
   220  List of maintainers and how to submit kernel changes
   221  ====================================================
   222  
   223  Please try to follow the guidelines below.  This will make things
   224  easier on the maintainers.  Not all of these guidelines matter for every
   225  trivial patch so apply some common sense.
   226  
   227  Tips for patch submitters
   228  -------------------------
   229  
   230  1.	Always *test* your changes, however small, on at least 4 or
   231  	5 people, preferably many more.
   232  
   233  < ... >
   234  
   235  8.	Happy hacking.
   236  
   237  Descriptions of section entries and preferred order
   238  ---------------------------------------------------
   239  
   240  	M: *Mail* patches to: FullName <address@domain>
   241  	R: Designated *Reviewer*: FullName <address@domain>
   242  	   These reviewers should be CCed on patches.
   243  < ... >
   244  	K: *Content regex* (perl extended) pattern match in a patch or file.
   245  	   For instance:
   246  	   K: of_get_profile
   247  	      matches patches or files that contain "of_get_profile"
   248  	   K: \b(printk|pr_(info|err))\b
   249  	      matches patches or files that contain one or more of the words
   250  	      printk, pr_info or pr_err
   251  	   One regex pattern per line.  Multiple K: lines acceptable.
   252  
   253  Maintainers List
   254  ----------------
   255  
   256  .. note:: When reading this list, please look for the most precise areas
   257            first. When adding to this list, please keep the entries in
   258            alphabetical order.
   259  
   260  3C59X NETWORK DRIVER
   261  M:	Name1 Surname <email1@kernel.org>
   262  L:	netdev@vger.kernel.org
   263  S:	Odd Fixes
   264  F:	Documentation/networking/device_drivers/ethernet/3com/vortex.rst
   265  F:	drivers/net/ethernet/3com/3c59x.c
   266  
   267  ABI/API
   268  L:	linux-api@vger.kernel.org
   269  F:	include/linux/syscalls.h
   270  F:	kernel/sys_ni.c
   271  X:	include/uapi/
   272  X:	arch/*/include/uapi/
   273  
   274  AD1889 ALSA SOUND DRIVER
   275  L:	linux-parisc@vger.kernel.org
   276  S:	Maintained
   277  W:	https://parisc.wiki.kernel.org/index.php/AD1889
   278  F:	sound/pci/ad1889.*
   279  
   280  PVRUSB2 VIDEO4LINUX DRIVER
   281  M:	Name2 <email2@kernel.org>
   282  L:	pvrusb2@isely.net	(subscribers-only)
   283  L:	linux-media@vger.kernel.org
   284  S:	Maintained
   285  W:	http://www.isely.net/pvrusb2/
   286  T:	git git://linuxtv.org/media_tree.git
   287  F:	Documentation/driver-api/media/drivers/pvrusb2*
   288  F:	drivers/media/usb/pvrusb2/
   289  
   290  RISC-V ARCHITECTURE
   291  M:	Name3 <email3@kernel.org>
   292  M:	Name4 <email4@kernel.org>
   293  M:	Name5 <email5@kernel.org>
   294  L:	linux-riscv@lists.infradead.org
   295  S:	Supported
   296  Q:	https://patchwork.kernel.org/project/linux-riscv/list/
   297  P:	Documentation/riscv/patch-acceptance.rst
   298  T:	git git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux.git
   299  F:	arch/riscv/
   300  N:	riscv
   301  K:	riscv
   302  
   303  THE REST
   304  M:	Name6 <email6@kernel.org>
   305  L:	linux-kernel@vger.kernel.org
   306  S:	Buried alive in reporters
   307  T:	git git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
   308  F:	*
   309  F:	*/
   310  `