github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/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, pc uint64) ([]Frame, error) { 17 key := cacheKey{bin, pc} 18 assert.False(t, called[key]) 19 called[key] = true 20 if bin == "error" { 21 return nil, fmt.Errorf("error %v", pc) 22 } 23 return []Frame{{PC: pc, Func: bin + "_func"}}, nil 24 } 25 var cache Cache 26 check := func(bin string, pc uint64, frames []Frame, err error) { 27 gotFrames, gotErr := cache.Symbolize(inner, bin, pc) 28 assert.Equal(t, gotFrames, frames) 29 assert.Equal(t, gotErr, err) 30 } 31 check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) 32 check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) 33 check("foo", 2, []Frame{{PC: 2, Func: "foo_func"}}, nil) 34 check("foo", 1, []Frame{{PC: 1, Func: "foo_func"}}, nil) 35 check("error", 10, nil, errors.New("error 10")) 36 check("error", 10, nil, errors.New("error 10")) 37 check("error", 11, nil, errors.New("error 11")) 38 }