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

     1  // Copyright 2024 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 covermerger
     5  
     6  import (
     7  	"encoding/json"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  // nolint: lll
    17  func TestAggregateStreamData(t *testing.T) {
    18  	testsPath := "testdata/integration"
    19  	type Test struct {
    20  		name              string
    21  		workdir           string
    22  		bqTable           string
    23  		simpleAggregation string
    24  		baseRepo          string
    25  		baseBranch        string
    26  		baseCommit        string
    27  	}
    28  	tests := []Test{
    29  		{
    30  			name:              "aesni-intel_glue",
    31  			workdir:           testsPath + "/aesni-intel_glue/test-workdir-covermerger",
    32  			bqTable:           readFileOrFail(t, testsPath+"/aesni-intel_glue/bqTable.txt"),
    33  			simpleAggregation: readFileOrFail(t, testsPath+"/aesni-intel_glue/merge_result.txt"),
    34  			baseRepo:          "git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
    35  			baseBranch:        "master",
    36  			baseCommit:        "fe46a7dd189e25604716c03576d05ac8a5209743",
    37  		},
    38  		{
    39  			name:    "code deleted",
    40  			workdir: testsPath + "/all/test-workdir-covermerger",
    41  			bqTable: `timestamp,version,fuzzing_minutes,arch,build_id,manager,kernel_repo,kernel_branch,kernel_commit,file_path,func_name,sl,sc,el,ec,hit_count,inline,pc
    42  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit1,delete_code.c,func1,2,0,2,-1,1,true,1`,
    43  			simpleAggregation: `{
    44    "delete_code.c":
    45    {
    46      "HitCounts":{},
    47  		"FileExists": true
    48    }
    49  }`,
    50  			baseRepo:   "git://repo",
    51  			baseBranch: "master",
    52  			baseCommit: "commit2",
    53  		},
    54  		{
    55  			name:    "file deleted",
    56  			workdir: testsPath + "/all/test-workdir-covermerger",
    57  			bqTable: `timestamp,version,fuzzing_minutes,arch,build_id,manager,kernel_repo,kernel_branch,kernel_commit,file_path,func_name,sl,sc,el,ec,hit_count,inline,pc
    58  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit1,delete_file.c,func1,2,0,2,-1,1,true,1`,
    59  			simpleAggregation: `{
    60    "delete_file.c":
    61    {
    62  		"FileExists": false
    63    }
    64  }`,
    65  			baseRepo:   "git://repo",
    66  			baseBranch: "master",
    67  			baseCommit: "commit2",
    68  		},
    69  		{
    70  			name:    "covered line changed",
    71  			workdir: testsPath + "/all/test-workdir-covermerger",
    72  			bqTable: `timestamp,version,fuzzing_minutes,arch,build_id,manager,kernel_repo,kernel_branch,kernel_commit,file_path,func_name,sl,sc,el,ec,hit_count,inline,pc
    73  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit1,change_line.c,func1,2,0,2,-1,1,true,1
    74  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit1,change_line.c,func1,3,0,3,-1,1,true,1`,
    75  			simpleAggregation: `{
    76    "change_line.c":
    77    {
    78  		"HitCounts":{"3": 1},
    79  		"FileExists": true
    80    }
    81  }`,
    82  			baseRepo:   "git://repo",
    83  			baseBranch: "master",
    84  			baseCommit: "commit2",
    85  		},
    86  		{
    87  			name:    "add line",
    88  			workdir: testsPath + "/all/test-workdir-covermerger",
    89  			bqTable: `timestamp,version,fuzzing_minutes,arch,build_id,manager,kernel_repo,kernel_branch,kernel_commit,file_path,func_name,sl,sc,el,ec,hit_count,inline,pc
    90  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit1,add_line.c,func1,2,0,2,-1,1,true,1`,
    91  			simpleAggregation: `{
    92    "add_line.c":
    93    {
    94  		"HitCounts":{"2": 1},
    95  		"FileExists": true
    96    }
    97  }`,
    98  			baseRepo:   "git://repo",
    99  			baseBranch: "master",
   100  			baseCommit: "commit2",
   101  		},
   102  		{
   103  			name:    "instrumented lines w/o coverage are reported",
   104  			workdir: testsPath + "/all/test-workdir-covermerger",
   105  			bqTable: `timestamp,version,fuzzing_minutes,arch,build_id,manager,kernel_repo,kernel_branch,kernel_commit,file_path,func_name,sl,sc,el,ec,hit_count,inline,pc
   106  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit1,not_changed.c,func1,3,0,3,-1,0,true,1
   107  samp_time,1,360,arch,b1,ci-mock,git://repo,master,commit2,not_changed.c,func1,4,0,4,-1,0,true,1`,
   108  			simpleAggregation: `{
   109    "not_changed.c":
   110    {
   111  		"HitCounts":{"3": 0, "4": 0},
   112  		"FileExists": true
   113    }
   114  }`,
   115  			baseRepo:   "git://repo",
   116  			baseBranch: "master",
   117  			baseCommit: "commit2",
   118  		},
   119  	}
   120  	for _, test := range tests {
   121  		t.Run(test.name, func(t *testing.T) {
   122  			aggregation, err := AggregateStreamData(
   123  				&Config{
   124  					Workdir:       test.workdir,
   125  					skipRepoClone: true,
   126  				},
   127  				strings.NewReader(test.bqTable),
   128  				RepoBranchCommit{
   129  					Repo:   test.baseRepo,
   130  					Branch: test.baseBranch,
   131  					Commit: test.baseCommit,
   132  				})
   133  			assert.Nil(t, err)
   134  			var simpleAggregationJSON map[string]*MergeResult
   135  			assert.Nil(t, json.Unmarshal([]byte(test.simpleAggregation), &simpleAggregationJSON))
   136  			assert.Equal(t, simpleAggregationJSON, aggregation)
   137  		})
   138  	}
   139  }
   140  
   141  func readFileOrFail(t *testing.T, path string) string {
   142  	absPath, err := filepath.Abs(path)
   143  	assert.Nil(t, err)
   144  	content, err := os.ReadFile(absPath)
   145  	assert.Nil(t, err)
   146  	return string(content)
   147  }