mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 06:42:16 +00:00
Clean up test cases -> testify
This commit is contained in:
parent
1bc1947e3f
commit
6c2bf8c24b
@ -1,25 +1,20 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSimpleArmor(t *testing.T) {
|
||||
blockType := "MINT TEST"
|
||||
data := []byte("somedata")
|
||||
armorStr := EncodeArmor(blockType, nil, data)
|
||||
t.Log("Got armor: ", armorStr)
|
||||
|
||||
// Decode armorStr and test for equivalence.
|
||||
blockType2, _, data2, err := DecodeArmor(armorStr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if blockType != blockType2 {
|
||||
t.Errorf("Expected block type %v but got %v", blockType, blockType2)
|
||||
}
|
||||
if !bytes.Equal(data, data2) {
|
||||
t.Errorf("Expected data %X but got %X", data2, data)
|
||||
}
|
||||
require.Nil(t, err, "%+v", err)
|
||||
assert.Equal(t, blockType, blockType2)
|
||||
assert.Equal(t, data, data2)
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -18,45 +17,33 @@ func TestSignAndValidateEd25519(t *testing.T) {
|
||||
|
||||
msg := CRandBytes(128)
|
||||
sig := privKey.Sign(msg)
|
||||
t.Logf("msg: %X, sig: %X", msg, sig)
|
||||
|
||||
// Test the signature
|
||||
if !pubKey.VerifyBytes(msg, sig) {
|
||||
t.Errorf("Account message signature verification failed")
|
||||
}
|
||||
assert.True(t, pubKey.VerifyBytes(msg, sig))
|
||||
|
||||
// Mutate the signature, just one bit.
|
||||
sigEd := sig.(SignatureEd25519)
|
||||
sigEd[0] ^= byte(0x01)
|
||||
sig = Signature(sigEd)
|
||||
|
||||
if pubKey.VerifyBytes(msg, sig) {
|
||||
t.Errorf("Account message signature verification should have failed but passed instead")
|
||||
}
|
||||
assert.False(t, pubKey.VerifyBytes(msg, sig))
|
||||
}
|
||||
|
||||
func TestSignAndValidateSecp256k1(t *testing.T) {
|
||||
|
||||
privKey := GenPrivKeySecp256k1()
|
||||
pubKey := privKey.PubKey()
|
||||
|
||||
msg := CRandBytes(128)
|
||||
sig := privKey.Sign(msg)
|
||||
t.Logf("msg: %X, sig: %X", msg, sig)
|
||||
|
||||
// Test the signature
|
||||
if !pubKey.VerifyBytes(msg, sig) {
|
||||
t.Errorf("Account message signature verification failed")
|
||||
}
|
||||
assert.True(t, pubKey.VerifyBytes(msg, sig))
|
||||
|
||||
// Mutate the signature, just one bit.
|
||||
sigEd := sig.(SignatureSecp256k1)
|
||||
sigEd[0] ^= byte(0x01)
|
||||
sig = Signature(sigEd)
|
||||
|
||||
if pubKey.VerifyBytes(msg, sig) {
|
||||
t.Errorf("Account message signature verification should have failed but passed instead")
|
||||
}
|
||||
assert.False(t, pubKey.VerifyBytes(msg, sig))
|
||||
}
|
||||
|
||||
func TestSignatureEncodings(t *testing.T) {
|
||||
@ -106,7 +93,6 @@ func TestSignatureEncodings(t *testing.T) {
|
||||
js, err := data.ToJSON(sig)
|
||||
require.Nil(t, err, "%+v", err)
|
||||
assert.True(t, strings.Contains(string(js), tc.sigName))
|
||||
fmt.Println(string(js))
|
||||
|
||||
// and back
|
||||
sig3 := SignatureS{}
|
||||
@ -114,5 +100,10 @@ func TestSignatureEncodings(t *testing.T) {
|
||||
require.Nil(t, err, "%+v", err)
|
||||
assert.EqualValues(t, sig, sig3)
|
||||
assert.True(t, pubKey.VerifyBytes(msg, sig3))
|
||||
|
||||
// and make sure we can textify it
|
||||
text, err := data.ToText(sig)
|
||||
require.Nil(t, err, "%+v", err)
|
||||
assert.True(t, strings.HasPrefix(text, tc.sigName))
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
@ -14,16 +16,10 @@ func TestSimple(t *testing.T) {
|
||||
plaintext := []byte("sometext")
|
||||
secret := []byte("somesecretoflengththirtytwo===32")
|
||||
ciphertext := EncryptSymmetric(plaintext, secret)
|
||||
|
||||
plaintext2, err := DecryptSymmetric(ciphertext, secret)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(plaintext, plaintext2) {
|
||||
t.Errorf("Decrypted plaintext was %X, expected %X", plaintext2, plaintext)
|
||||
}
|
||||
|
||||
require.Nil(t, err, "%+v", err)
|
||||
assert.Equal(t, plaintext, plaintext2)
|
||||
}
|
||||
|
||||
func TestSimpleWithKDF(t *testing.T) {
|
||||
@ -39,14 +35,8 @@ func TestSimpleWithKDF(t *testing.T) {
|
||||
secret = Sha256(secret)
|
||||
|
||||
ciphertext := EncryptSymmetric(plaintext, secret)
|
||||
|
||||
plaintext2, err := DecryptSymmetric(ciphertext, secret)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(plaintext, plaintext2) {
|
||||
t.Errorf("Decrypted plaintext was %X, expected %X", plaintext2, plaintext)
|
||||
}
|
||||
|
||||
require.Nil(t, err, "%+v", err)
|
||||
assert.Equal(t, plaintext, plaintext2)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user