From fe9921457c7c6c4359ebca769c51fc2bcd99e27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Tue, 29 Sep 2020 13:29:40 +0200 Subject: [PATCH] Fix failing static analyzer tests (#7363) * add "want" expectations * add build step with analyzer tests * add colon to yml * Merge refs/heads/master into fix-analyzer-test-expectations * Merge refs/heads/master into fix-analyzer-test-expectations * Merge refs/heads/master into fix-analyzer-test-expectations * Merge refs/heads/master into fix-analyzer-test-expectations * remove tests from CI * readme file * change header size --- tools/analyzers/README.md | 7 +++++++ tools/analyzers/cryptorand/testdata/custom_import.go | 10 ++++++---- tools/analyzers/cryptorand/testdata/rand_new.go | 8 +++++--- tools/analyzers/nop/testdata/no_op.go | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 tools/analyzers/README.md diff --git a/tools/analyzers/README.md b/tools/analyzers/README.md new file mode 100644 index 0000000000..739c54d13e --- /dev/null +++ b/tools/analyzers/README.md @@ -0,0 +1,7 @@ +# Running analyzer unit tests + +Analyzers' unit tests are ignored in bazel's build files, and therefore are not being triggered as part of the CI +pipeline. Because of this they should be invoked manually when writing a new analyzer or making changes to an existing +one. Otherwise, any issues will go unnoticed during the CI build. + +The easiest way to run all unit tests for all analyzers is `go test ./tools/analyzers/...` \ No newline at end of file diff --git a/tools/analyzers/cryptorand/testdata/custom_import.go b/tools/analyzers/cryptorand/testdata/custom_import.go index 5991244ef6..6c21801c59 100644 --- a/tools/analyzers/cryptorand/testdata/custom_import.go +++ b/tools/analyzers/cryptorand/testdata/custom_import.go @@ -7,17 +7,19 @@ import ( ) func UseRandNewCustomImport() { - randGenerator := mathRand.New(mathRand.NewSource(time.Now().UnixNano())) + source := mathRand.NewSource(time.Now().UnixNano()) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" + randGenerator := mathRand.New(source) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" start := uint64(randGenerator.Intn(32)) _ = start - randGenerator = mathRand.New(mathRand.NewSource(time.Now().UnixNano())) + source = mathRand.NewSource(time.Now().UnixNano()) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" + randGenerator = mathRand.New(source) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" } func UseWithoutSeeCustomImportd() { - assignedIndex := mathRand.Intn(128) + assignedIndex := mathRand.Intn(128) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" _ = assignedIndex - foobar.Shuffle(10, func(i, j int) { + foobar.Shuffle(10, func(i, j int) { // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" }) } diff --git a/tools/analyzers/cryptorand/testdata/rand_new.go b/tools/analyzers/cryptorand/testdata/rand_new.go index 5978041461..d916a14b7b 100644 --- a/tools/analyzers/cryptorand/testdata/rand_new.go +++ b/tools/analyzers/cryptorand/testdata/rand_new.go @@ -7,14 +7,16 @@ import ( ) func UseRandNew() { - randGenerator := mathRand.New(rand.NewSource(time.Now().UnixNano())) + source := rand.NewSource(time.Now().UnixNano()) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" + randGenerator := mathRand.New(source) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" start := uint64(randGenerator.Intn(32)) _ = start - randGenerator = rand.New(rand.NewSource(time.Now().UnixNano())) + source = rand.NewSource(time.Now().UnixNano()) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" + randGenerator = rand.New(source) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" } func UseWithoutSeed() { - assignedIndex := rand.Intn(128) + assignedIndex := rand.Intn(128) // want "crypto-secure RNGs are required, use CSPRNG or PRNG defined in github.com/prysmaticlabs/prysm/shared/rand" _ = assignedIndex } diff --git a/tools/analyzers/nop/testdata/no_op.go b/tools/analyzers/nop/testdata/no_op.go index b10d1fdeca..8f962665eb 100644 --- a/tools/analyzers/nop/testdata/no_op.go +++ b/tools/analyzers/nop/testdata/no_op.go @@ -5,10 +5,10 @@ type foo struct { func AddressOfDereferencedValue() { x := &foo{} - _ = &*x // want "Found a no-op instruction that can be safely removed. It might be a result of writing code that does not work as expected." + _ = &*x // want "Found a no-op instruction that can be safely removed. It might be a result of writing code that does not do what was intended." } func DereferencedAddressOfValue() { x := foo{} - _ = *&x // want "Found a no-op instruction that can be safely removed. It might be a result of writing code that does not work as expected." + _ = *&x // want "Found a no-op instruction that can be safely removed. It might be a result of writing code that does not do what was intended." }