From 8a9c63809b66b4796392dbd38e3b663ac873aebb Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Thu, 11 Jul 2019 17:11:04 -0400 Subject: [PATCH] tests for PiecePublicKey/PiecePrivateKey (#2526) --- pkg/storj/piecekey_test.go | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 pkg/storj/piecekey_test.go diff --git a/pkg/storj/piecekey_test.go b/pkg/storj/piecekey_test.go new file mode 100644 index 000000000..acbd7b88b --- /dev/null +++ b/pkg/storj/piecekey_test.go @@ -0,0 +1,68 @@ +// Copyright (C) 2019 Storj Labs, Inc. +// See LICENSE for copying information. + +package storj_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "storj.io/storj/internal/memory" + "storj.io/storj/internal/testrand" + "storj.io/storj/pkg/storj" +) + +func TestPublicPrivatePieceKey(t *testing.T) { + expectedPublicKey, expectedPrivateKey, err := storj.NewPieceKey() + require.NoError(t, err) + + publicKey, err := storj.PiecePublicKeyFromBytes(expectedPublicKey.Bytes()) + require.NoError(t, err) + require.Equal(t, expectedPublicKey, publicKey) + + privateKey, err := storj.PiecePrivateKeyFromBytes(expectedPrivateKey.Bytes()) + require.NoError(t, err) + require.Equal(t, expectedPrivateKey, privateKey) + + { + data := testrand.Bytes(10 * memory.KiB) + signature, err := privateKey.Sign(data) + require.NoError(t, err) + + err = publicKey.Verify(data, signature) + require.NoError(t, err) + + err = publicKey.Verify(data, testrand.BytesInt(32)) + require.Error(t, err) + + err = publicKey.Verify(testrand.Bytes(10*memory.KiB), signature) + require.Error(t, err) + } + + { + // to small + _, err = storj.PiecePublicKeyFromBytes([]byte{1}) + require.Error(t, err) + + // to small + _, err = storj.PiecePrivateKeyFromBytes([]byte{1}) + require.Error(t, err) + + // to large + _, err = storj.PiecePublicKeyFromBytes(testrand.Bytes(33)) + require.Error(t, err) + + // to large + _, err = storj.PiecePrivateKeyFromBytes(testrand.Bytes(65)) + require.Error(t, err) + + // public key from private + _, err = storj.PiecePublicKeyFromBytes(expectedPrivateKey.Bytes()) + require.Error(t, err) + + // private key from public + _, err = storj.PiecePrivateKeyFromBytes(expectedPublicKey.Bytes()) + require.Error(t, err) + } +}