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