github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/internal/testutils/fixtures.go (about) 1 package testutils 2 3 import ( 4 "math/rand" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/network" 10 "github.com/onflow/flow-go/network/alsp" 11 "github.com/onflow/flow-go/utils/unittest" 12 ) 13 14 // MisbehaviorReportFixture generates a random misbehavior report. 15 // Args: 16 // - t: the test object. 17 // 18 // This is used in tests to generate random misbehavior reports. 19 // It fails the test if it cannot generate a valid report. 20 func MisbehaviorReportFixture(t *testing.T) network.MisbehaviorReport { 21 22 // pick a random misbehavior type 23 misbehaviorType := alsp.AllMisbehaviorTypes()[rand.Intn(len(alsp.AllMisbehaviorTypes()))] 24 25 amplification := 100 * rand.Float64() 26 report, err := alsp.NewMisbehaviorReport( 27 unittest.IdentifierFixture(), 28 misbehaviorType, 29 alsp.WithPenaltyAmplification(amplification)) 30 require.NoError(t, err) 31 return report 32 } 33 34 // MisbehaviorReportsFixture generates a slice of random misbehavior reports. 35 // Args: 36 // - t: the test object. 37 // 38 // It fails the test if it cannot generate a valid report. 39 // This is used in tests to generate random misbehavior reports. 40 func MisbehaviorReportsFixture(t *testing.T, count int) []network.MisbehaviorReport { 41 reports := make([]network.MisbehaviorReport, 0, count) 42 for i := 0; i < count; i++ { 43 reports = append(reports, MisbehaviorReportFixture(t)) 44 } 45 46 return reports 47 } 48 49 // MisbehaviorTypeFixture generates a random misbehavior type. 50 // Args: 51 // - t: the test object (used to emphasize that this is a test helper). 52 func MisbehaviorTypeFixture(_ *testing.T) network.Misbehavior { 53 return alsp.AllMisbehaviorTypes()[rand.Intn(len(alsp.AllMisbehaviorTypes()))] 54 }