github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/workflow/fuzz-step/main_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 main
     5  
     6  import (
     7  	"encoding/json"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/google/syzkaller/pkg/build"
    12  	"github.com/google/syzkaller/pkg/osutil"
    13  	"github.com/google/syzkaller/syz-cluster/pkg/api"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestReadSectionHashes(t *testing.T) {
    19  	hashes := build.SectionHashes{
    20  		Text: map[string]string{"A": "1"},
    21  		Data: map[string]string{"B": "2"},
    22  	}
    23  
    24  	jsonData, err := json.Marshal(hashes)
    25  	require.NoError(t, err)
    26  
    27  	file, err := osutil.WriteTempFile(jsonData)
    28  	require.NoError(t, err)
    29  	defer os.Remove(file)
    30  
    31  	fromFile, err := readSectionHashes(file)
    32  	require.NoError(t, err)
    33  	assert.Equal(t, hashes, fromFile)
    34  }
    35  
    36  // nolint: dupl
    37  func TestShouldSkipFuzzing(t *testing.T) {
    38  	t.Run("one empty", func(t *testing.T) {
    39  		assert.False(t, shouldSkipFuzzing(
    40  			build.SectionHashes{},
    41  			build.SectionHashes{
    42  				Text: map[string]string{"A": "1"},
    43  			},
    44  		))
    45  	})
    46  	t.Run("equal symbols", func(t *testing.T) {
    47  		assert.True(t, shouldSkipFuzzing(
    48  			build.SectionHashes{
    49  				Text: map[string]string{"A": "1", "B": "2"},
    50  				Data: map[string]string{"C": "1", "D": "2"},
    51  			},
    52  			build.SectionHashes{
    53  				Text: map[string]string{"A": "1", "B": "2"},
    54  				Data: map[string]string{"C": "1", "D": "2"},
    55  			},
    56  		))
    57  	})
    58  	t.Run("ignore known variables", func(t *testing.T) {
    59  		assert.True(t, shouldSkipFuzzing(
    60  			build.SectionHashes{
    61  				Text: map[string]string{"A": "1", "B": "2"},
    62  				Data: map[string]string{"C": "1", "raw_data": "A", "vermagic": "A"},
    63  			},
    64  			build.SectionHashes{
    65  				Text: map[string]string{"A": "1", "B": "2"},
    66  				Data: map[string]string{"C": "1", "raw_data": "B", "vermagic": "B"},
    67  			},
    68  		))
    69  	})
    70  	t.Run("same len, different hashes", func(t *testing.T) {
    71  		assert.False(t, shouldSkipFuzzing(
    72  			build.SectionHashes{
    73  				Text: map[string]string{"A": "1", "B": "2"},
    74  			},
    75  			build.SectionHashes{
    76  				Text: map[string]string{"A": "1", "B": "different"},
    77  			},
    78  		))
    79  		assert.False(t, shouldSkipFuzzing(
    80  			build.SectionHashes{
    81  				Text: map[string]string{"A": "1", "B": "2"},
    82  				Data: map[string]string{"C": "1", "D": "2"},
    83  			},
    84  			build.SectionHashes{
    85  				Text: map[string]string{"A": "1", "B": "2"},
    86  				Data: map[string]string{"C": "1", "D": "different"},
    87  			},
    88  		))
    89  	})
    90  	t.Run("different len, same hashes", func(t *testing.T) {
    91  		assert.False(t, shouldSkipFuzzing(
    92  			build.SectionHashes{
    93  				Text: map[string]string{"A": "1", "B": "2"},
    94  			},
    95  			build.SectionHashes{
    96  				Text: map[string]string{"A": "1", "B": "2", "C": "new"},
    97  			},
    98  		))
    99  	})
   100  }
   101  
   102  func TestBugTitleRe(t *testing.T) {
   103  	assert.True(t, titleMatchesFilter(&api.FuzzConfig{}, "any title must match"))
   104  	assert.True(t, titleMatchesFilter(&api.FuzzConfig{
   105  		BugTitleRe: `^Prefix:`,
   106  	}, "Prefix: must pass"))
   107  	assert.False(t, titleMatchesFilter(&api.FuzzConfig{
   108  		BugTitleRe: `^Prefix:`,
   109  	}, "Without prefix"))
   110  }