github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/manager/crash_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 manager
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/syzkaller/pkg/report"
    10  	"github.com/google/syzkaller/pkg/repro"
    11  	"github.com/google/syzkaller/prog"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestCrashList(t *testing.T) {
    16  	crashStore := &CrashStore{
    17  		BaseDir:      t.TempDir(),
    18  		MaxCrashLogs: 10,
    19  	}
    20  
    21  	first, err := crashStore.SaveCrash(&Crash{Report: &report.Report{
    22  		Title:  "Title A",
    23  		Output: []byte("ABCD"),
    24  	}})
    25  	assert.NoError(t, err)
    26  	assert.True(t, first)
    27  	for i := 0; i < 2; i++ {
    28  		first, err := crashStore.SaveCrash(&Crash{Report: &report.Report{
    29  			Title:  "Title B",
    30  			Output: []byte("ABCD"),
    31  		}})
    32  		assert.NoError(t, err)
    33  		assert.Equal(t, i == 0, first)
    34  	}
    35  	for i := 0; i < 3; i++ {
    36  		first, err := crashStore.SaveCrash(&Crash{Report: &report.Report{
    37  			Title:  "Title C",
    38  			Output: []byte("ABCD"),
    39  		}})
    40  		assert.NoError(t, err)
    41  		assert.Equal(t, i == 0, first)
    42  	}
    43  
    44  	list, err := crashStore.BugList()
    45  	assert.NoError(t, err)
    46  	assert.Len(t, list, 3)
    47  
    48  	assert.Equal(t, "Title A", list[0].Title)
    49  	assert.Len(t, list[0].Crashes, 1)
    50  	assert.Equal(t, "Title B", list[1].Title)
    51  	assert.Len(t, list[1].Crashes, 2)
    52  	assert.Equal(t, "Title C", list[2].Title)
    53  	assert.Len(t, list[2].Crashes, 3)
    54  }
    55  
    56  func TestEmptyCrashList(t *testing.T) {
    57  	crashStore := &CrashStore{
    58  		BaseDir:      t.TempDir(),
    59  		MaxCrashLogs: 10,
    60  	}
    61  	_, err := crashStore.BugList()
    62  	assert.NoError(t, err)
    63  }
    64  
    65  func TestMaxCrashLogs(t *testing.T) {
    66  	crashStore := &CrashStore{
    67  		BaseDir:      t.TempDir(),
    68  		MaxCrashLogs: 5,
    69  	}
    70  
    71  	for i := 0; i < 20; i++ {
    72  		_, err := crashStore.SaveCrash(&Crash{Report: &report.Report{
    73  			Title:  "Title A",
    74  			Output: []byte("ABCD"),
    75  		}})
    76  		assert.NoError(t, err)
    77  	}
    78  
    79  	info, err := crashStore.BugInfo(crashHash("Title A"), false)
    80  	assert.NoError(t, err)
    81  	assert.Len(t, info.Crashes, 5)
    82  }
    83  
    84  func TestCrashRepro(t *testing.T) {
    85  	crashStore := &CrashStore{
    86  		Tag:          "abcd",
    87  		BaseDir:      t.TempDir(),
    88  		MaxCrashLogs: 5,
    89  	}
    90  
    91  	_, err := crashStore.SaveCrash(&Crash{Report: &report.Report{
    92  		Title:  "Some title",
    93  		Output: []byte("Some output"),
    94  	}})
    95  	assert.NoError(t, err)
    96  
    97  	err = crashStore.SaveRepro(&ReproResult{
    98  		Repro: &repro.Result{
    99  			Report: &report.Report{
   100  				Title:  "Some title",
   101  				Report: []byte("Some report"),
   102  			},
   103  			Prog: &prog.Prog{},
   104  		},
   105  	}, []byte("prog text"), []byte("c prog text"))
   106  	assert.NoError(t, err)
   107  
   108  	report, err := crashStore.Report(crashHash("Some title"))
   109  	assert.NoError(t, err)
   110  	assert.Equal(t, "Some title", report.Title)
   111  	assert.Equal(t, "abcd", report.Tag)
   112  	assert.Equal(t, []byte("prog text"), report.Prog)
   113  	assert.Equal(t, []byte("c prog text"), report.CProg)
   114  	assert.Equal(t, []byte("Some report"), report.Report)
   115  }