46 lines
806 B
Go
46 lines
806 B
Go
package sharedkey
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"math/rand"
|
|
"mpbl3p/shared"
|
|
"testing"
|
|
)
|
|
|
|
func TestBlake2s_GenerateVerify(t *testing.T) {
|
|
t.Run("GeneratedVerifies", func(t *testing.T) {
|
|
// ASSIGN
|
|
key := make([]byte, 16)
|
|
rand.Read(key)
|
|
buf := make([]byte, 500)
|
|
rand.Read(buf)
|
|
|
|
// ACT
|
|
b := Blake2s{key}
|
|
code := b.Generate(buf)
|
|
|
|
// ASSERT
|
|
err := b.Verify(buf, code)
|
|
assert.Nil(t, err)
|
|
})
|
|
|
|
t.Run("FlippedBitFailsVerify", func(t *testing.T) {
|
|
// ASSIGN
|
|
key := make([]byte, 16)
|
|
rand.Read(key)
|
|
buf := make([]byte, 500)
|
|
rand.Read(buf)
|
|
|
|
// ACT
|
|
b := Blake2s{key}
|
|
code := b.Generate(buf)
|
|
|
|
offset := rand.Intn(len(buf) * 8)
|
|
buf[offset/8] ^= 1 << (offset % 8)
|
|
|
|
// ASSERT
|
|
err := b.Verify(buf, code)
|
|
assert.Equal(t, shared.ErrBadChecksum, err)
|
|
})
|
|
}
|