github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/symbolizer/cache_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 symbolizer 5 6 import ( 7 "errors" 8 "fmt" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestCache(t *testing.T) { 15 called := make(map[cacheKey]bool) 16 inner := func(bin string, pcs ...uint64) ([]Frame, error) { 17 var res []Frame 18 for _, pc := range pcs { 19 key := cacheKey{bin, pc} 20 assert.False(t, called[key]) 21 called[key] = true 22 if bin == "error" { 23 return nil, fmt.Errorf("error %v", pc) 24 } 25 res = append(res, Frame{PC: pc, Func: bin + "_func"}) 26 } 27 return res, nil 28 } 29 var cache Cache 30 check := func(bin string, pc uint64, frames []Frame, err error) { 31 gotFrames, gotErr := cache.Symbolize(inner, bin, pc) 32 assert.Equal(t, gotFrames, frames) 33 assert.Equal(t, gotErr, err) 34 } 35 check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) 36 check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) 37 check("foo", 2, []Frame{{PC: 2, Func: "foo_func"}}, nil) 38 check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) 39 check("error", 10, nil, errors.New("error 10")) 40 check("error", 10, nil, errors.New("error 10")) 41 check("error", 11, nil, errors.New("error 11")) 42 }