forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cosmos#4 from tendermint/develop
go-data support
- Loading branch information
Showing
8 changed files
with
370 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.PHONY: docs | ||
REPO:=github.com/tendermint/go-crypto | ||
|
||
docs: | ||
@go get github.com/davecheney/godoc2md | ||
godoc2md $(REPO) > README.md | ||
|
||
test: | ||
go test ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package crypto | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
data "github.com/tendermint/go-data" | ||
) | ||
|
||
type byter interface { | ||
Bytes() []byte | ||
} | ||
|
||
// go to wire encoding and back | ||
func checkWire(t *testing.T, in byter, reader interface{}, typ byte) { | ||
// test to and from binary | ||
bin, err := data.ToWire(in) | ||
require.Nil(t, err, "%+v", err) | ||
assert.Equal(t, typ, bin[0]) | ||
// make sure this is compatible with current (Bytes()) encoding | ||
assert.Equal(t, in.Bytes(), bin) | ||
|
||
err = data.FromWire(bin, reader) | ||
require.Nil(t, err, "%+v", err) | ||
} | ||
|
||
// go to json encoding and back | ||
func checkJSON(t *testing.T, in interface{}, reader interface{}, typ string) { | ||
// test to and from binary | ||
js, err := data.ToJSON(in) | ||
require.Nil(t, err, "%+v", err) | ||
styp := `"` + typ + `"` | ||
assert.True(t, strings.Contains(string(js), styp)) | ||
|
||
err = data.FromJSON(js, reader) | ||
require.Nil(t, err, "%+v", err) | ||
|
||
// also check text format | ||
text, err := data.ToText(in) | ||
require.Nil(t, err, "%+v", err) | ||
parts := strings.Split(text, ":") | ||
require.Equal(t, 2, len(parts)) | ||
// make sure the first part is the typ string | ||
assert.Equal(t, typ, parts[0]) | ||
// and the data is also present in the json | ||
assert.True(t, strings.Contains(string(js), parts[1])) | ||
} | ||
|
||
func TestKeyEncodings(t *testing.T) { | ||
cases := []struct { | ||
privKey PrivKeyS | ||
keyType byte | ||
keyName string | ||
}{ | ||
{ | ||
privKey: PrivKeyS{GenPrivKeyEd25519()}, | ||
keyType: TypeEd25519, | ||
keyName: NameEd25519, | ||
}, | ||
{ | ||
privKey: PrivKeyS{GenPrivKeySecp256k1()}, | ||
keyType: TypeSecp256k1, | ||
keyName: NameSecp256k1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
// check (de/en)codings of private key | ||
priv2 := PrivKeyS{} | ||
checkWire(t, tc.privKey, &priv2, tc.keyType) | ||
assert.EqualValues(t, tc.privKey, priv2) | ||
priv3 := PrivKeyS{} | ||
checkJSON(t, tc.privKey, &priv3, tc.keyName) | ||
assert.EqualValues(t, tc.privKey, priv3) | ||
|
||
// check (de/en)codings of public key | ||
pubKey := PubKeyS{tc.privKey.PubKey()} | ||
pub2 := PubKeyS{} | ||
checkWire(t, pubKey, &pub2, tc.keyType) | ||
assert.EqualValues(t, pubKey, pub2) | ||
pub3 := PubKeyS{} | ||
checkJSON(t, pubKey, &pub3, tc.keyName) | ||
assert.EqualValues(t, pubKey, pub3) | ||
} | ||
} | ||
|
||
func toFromJSON(t *testing.T, in interface{}, recvr interface{}) { | ||
js, err := data.ToJSON(in) | ||
require.Nil(t, err, "%+v", err) | ||
err = data.FromJSON(js, recvr) | ||
require.Nil(t, err, "%+v", err) | ||
} | ||
|
||
func TestNilEncodings(t *testing.T) { | ||
// make sure sigs are okay with nil | ||
a, b := SignatureS{}, SignatureS{} | ||
toFromJSON(t, a, &b) | ||
assert.EqualValues(t, a, b) | ||
|
||
// make sure sigs are okay with nil | ||
c, d := PubKeyS{}, PubKeyS{} | ||
toFromJSON(t, c, &d) | ||
assert.EqualValues(t, c, d) | ||
|
||
// make sure sigs are okay with nil | ||
e, f := PrivKeyS{}, PrivKeyS{} | ||
toFromJSON(t, e, &f) | ||
assert.EqualValues(t, e, f) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.