github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/manager/diff_test.go (about)

     1  // Copyright 2025 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 manager
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/google/syzkaller/pkg/mgrconfig"
    11  	"github.com/google/syzkaller/pkg/osutil"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPatchFocusAreas(t *testing.T) {
    17  	cfg := &mgrconfig.Config{
    18  		KernelSrc: t.TempDir(),
    19  	}
    20  	require.NoError(t, osutil.FillDirectory(cfg.KernelSrc, map[string]string{
    21  		"header.h": `Test`,
    22  		"a.c": `#include <header.h>
    23  int main(void) { }`,
    24  		"b.c": `int main(void) { }`,
    25  		"c.c": `int main(void) { }`,
    26  	}))
    27  
    28  	baseHashes, patchedHashes := dummySymbolHashes(), dummySymbolHashes()
    29  	baseHashes["function"] = "hash1"
    30  	patchedHashes["function"] = "hash2"
    31  
    32  	PatchFocusAreas(cfg, [][]byte{
    33  		[]byte(`diff --git a/b.c b/b.c
    34  index 103167d..fbf7a68 100644
    35  --- a/b.c
    36  +++ b/b.c
    37  @@ -1 +1 @@
    38  -int main(void) { }
    39  \ No newline at end of file
    40  +int main(void) { return 1; }
    41  \ No newline at end of file`),
    42  		// Also, emulate an update to header.h.
    43  		[]byte(`diff --git a/header.h b/header.h
    44  index 103167d..fbf7a68 100644
    45  --- a/header.h
    46  +++ b/header.h
    47  @@ -1 +1 @@
    48  -Test
    49  \ No newline at end of file
    50  +Test2
    51  \ No newline at end of file`),
    52  	}, baseHashes, patchedHashes)
    53  
    54  	assert.Equal(t, []mgrconfig.FocusArea{
    55  		{
    56  			Name: symbolsArea,
    57  			Filter: mgrconfig.CovFilterCfg{
    58  				Functions: []string{"function"},
    59  			},
    60  			Weight: 6.0,
    61  		},
    62  		{
    63  			Name: filesArea,
    64  			Filter: mgrconfig.CovFilterCfg{
    65  				Files: []string{"b.c", "header.h"},
    66  			},
    67  			Weight: 3.0,
    68  		},
    69  		{
    70  			Name: includesArea,
    71  			Filter: mgrconfig.CovFilterCfg{
    72  				Files: []string{"a.c"},
    73  			},
    74  			Weight: 2.0,
    75  		},
    76  		{
    77  			Weight: 1.0,
    78  		},
    79  	}, cfg.Experimental.FocusAreas)
    80  }
    81  
    82  func dummySymbolHashes() map[string]string {
    83  	ret := map[string]string{}
    84  	for i := 0; i < 100; i++ {
    85  		ret[fmt.Sprint(i)] = fmt.Sprint(i)
    86  	}
    87  	return ret
    88  }
    89  
    90  func TestModifiedSymbols(t *testing.T) {
    91  	t.Run("too many changed", func(t *testing.T) {
    92  		ret := modifiedSymbols(map[string]string{
    93  			"functionA": "hash1",
    94  			"functionB": "hash2",
    95  		}, map[string]string{
    96  			"functionA": "hash1",
    97  			"functionB": "hash is not hash2",
    98  		})
    99  		assert.Empty(t, ret)
   100  	})
   101  	t.Run("less than threshold", func(t *testing.T) {
   102  		base, patched := dummySymbolHashes(), dummySymbolHashes()
   103  		base["function"] = "hash1"
   104  		patched["function"] = "hash2"
   105  		base["function2"] = "hash1"
   106  		patched["function2"] = "hash2"
   107  		assert.Equal(t, []string{"function", "function2"}, modifiedSymbols(base, patched))
   108  	})
   109  }
   110  
   111  func TestNeedReproForTitle(t *testing.T) {
   112  	for title, skip := range map[string]bool{
   113  		"no output from test machine":                          false,
   114  		"SYZFAIL: read failed":                                 false,
   115  		"lost connection to test machine":                      false,
   116  		"INFO: rcu detected stall in clone":                    false,
   117  		"WARNING in arch_install_hw_breakpoint":                true,
   118  		"KASAN: slab-out-of-bounds Write in __bpf_get_stackid": true,
   119  	} {
   120  		assert.Equal(t, skip, needReproForTitle(title), "title=%q", title)
   121  	}
   122  }