aboutsummaryrefslogtreecommitdiff
path: root/private_key_test.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-03-25 20:36:21 +0100
committerGibheer <gibheer@gmail.com>2015-03-25 20:36:21 +0100
commit301d931ad76c0754e58cf024b46bebe685faafc5 (patch)
treede5c62a0d4270bc48839e977e6763646967d10aa /private_key_test.go
parentb3f621a31251c225944125c17f9f97333ba33209 (diff)
reformat everything with gofmt
Yes, I know that this will more or less destroy the history, but it had to be done. I also adjusted my editor to use gofmt rules by default now.
Diffstat (limited to 'private_key_test.go')
-rw-r--r--private_key_test.go141
1 files changed, 83 insertions, 58 deletions
diff --git a/private_key_test.go b/private_key_test.go
index a22d2ed..7479f11 100644
--- a/private_key_test.go
+++ b/private_key_test.go
@@ -1,84 +1,109 @@
package pki
import (
- "crypto"
- "crypto/elliptic"
- "encoding/pem"
- "testing"
+ "crypto"
+ "crypto/elliptic"
+ "encoding/pem"
+ "testing"
)
var (
- SignatureMessage = []byte("foobar")
- SignatureHash = crypto.SHA512
+ SignatureMessage = []byte("foobar")
+ SignatureHash = crypto.SHA512
)
type (
- Loader func(raw []byte) (PublicKey, error)
+ Loader func(raw []byte) (PublicKey, error)
)
// run the marshal test
func RunMarshalTest(pkType string, pe Pemmer, label string, t *testing.T) ([]byte, error) {
- marshPem, err := pe.MarshalPem()
- if err != nil {
- t.Errorf("%s: marshal pem not working: %s", pkType, err)
- return nil, err
- }
-
- block, _ := pem.Decode(marshPem)
- if block.Type != label {
- t.Errorf("%s: marshalled pem wrong: %s", pkType, err)
- return nil, err
- }
- return block.Bytes, nil
+ marshPem, err := pe.MarshalPem()
+ if err != nil {
+ t.Errorf("%s: marshal pem not working: %s", pkType, err)
+ return nil, err
+ }
+
+ block, _ := pem.Decode(marshPem)
+ if block.Type != label {
+ t.Errorf("%s: marshalled pem wrong: %s", pkType, err)
+ return nil, err
+ }
+ return block.Bytes, nil
}
// test other private key functions
func RunPrivateKeyTests(pkType string, pk PrivateKey, pu PublicKey, t *testing.T) {
- signature, err := pk.Sign(SignatureMessage, SignatureHash)
- if err != nil { t.Errorf("%s: error creating a signature: %s", pkType, err) }
-
- valid, err := pu.Verify(SignatureMessage, signature, SignatureHash)
- if err != nil { t.Errorf("%s: could not verify message: %s", pkType, err) }
- if !valid { t.Errorf("%s: signature invalid, but should be valid!", pkType) }
+ signature, err := pk.Sign(SignatureMessage, SignatureHash)
+ if err != nil {
+ t.Errorf("%s: error creating a signature: %s", pkType, err)
+ }
+
+ valid, err := pu.Verify(SignatureMessage, signature, SignatureHash)
+ if err != nil {
+ t.Errorf("%s: could not verify message: %s", pkType, err)
+ }
+ if !valid {
+ t.Errorf("%s: signature invalid, but should be valid!", pkType)
+ }
}
// test ecdsa private key functions
func TestEcdsaFunctions(t *testing.T) {
- pk, err := NewPrivateKeyEcdsa(elliptic.P521())
- if err != nil { t.Errorf("ecdsa: creating private key failed: %s", err) }
-
- blockBytes, err := RunMarshalTest("ecdsa", pk, PemLabelEcdsa, t)
- if err != nil { return }
-
- pk, err = LoadPrivateKeyEcdsa(blockBytes)
- if err != nil { t.Errorf("ecdsa: pem content wrong: %s", err) }
-
- blockBytes, err = RunMarshalTest("ecdsa-public", pk.Public(), PemLabelPublic, t)
- if err != nil { return }
-
- pu, err := LoadPublicKeyEcdsa(blockBytes)
- if err != nil { t.Errorf("ecdsa-public: pem content wrong: %s", err) }
-
- RunPrivateKeyTests("ecdsa", pk, pu, t)
+ pk, err := NewPrivateKeyEcdsa(elliptic.P521())
+ if err != nil {
+ t.Errorf("ecdsa: creating private key failed: %s", err)
+ }
+
+ blockBytes, err := RunMarshalTest("ecdsa", pk, PemLabelEcdsa, t)
+ if err != nil {
+ return
+ }
+
+ pk, err = LoadPrivateKeyEcdsa(blockBytes)
+ if err != nil {
+ t.Errorf("ecdsa: pem content wrong: %s", err)
+ }
+
+ blockBytes, err = RunMarshalTest("ecdsa-public", pk.Public(), PemLabelPublic, t)
+ if err != nil {
+ return
+ }
+
+ pu, err := LoadPublicKeyEcdsa(blockBytes)
+ if err != nil {
+ t.Errorf("ecdsa-public: pem content wrong: %s", err)
+ }
+
+ RunPrivateKeyTests("ecdsa", pk, pu, t)
}
// test rsa private key functions
func TestRsaFunctions(t *testing.T) {
- pk, err := NewPrivateKeyRsa(2048)
- if err != nil { t.Errorf("rsa: creating private key failed: %s", err) }
-
- blockBytes, err := RunMarshalTest("rsa", pk, PemLabelRsa, t)
- if err != nil { return }
-
- pk, err = LoadPrivateKeyRsa(blockBytes)
- if err != nil { t.Errorf("rsa: pem content wrong: %s", err) }
-
-
- blockBytes, err = RunMarshalTest("rsa-public", pk.Public(), PemLabelPublic, t)
- if err != nil { return }
-
- pu, err := LoadPublicKeyRsa(blockBytes)
- if err != nil { t.Errorf("rsa-public: pem content wrong: %s", err) }
-
- RunPrivateKeyTests("rsa", pk, pu, t)
+ pk, err := NewPrivateKeyRsa(2048)
+ if err != nil {
+ t.Errorf("rsa: creating private key failed: %s", err)
+ }
+
+ blockBytes, err := RunMarshalTest("rsa", pk, PemLabelRsa, t)
+ if err != nil {
+ return
+ }
+
+ pk, err = LoadPrivateKeyRsa(blockBytes)
+ if err != nil {
+ t.Errorf("rsa: pem content wrong: %s", err)
+ }
+
+ blockBytes, err = RunMarshalTest("rsa-public", pk.Public(), PemLabelPublic, t)
+ if err != nil {
+ return
+ }
+
+ pu, err := LoadPublicKeyRsa(blockBytes)
+ if err != nil {
+ t.Errorf("rsa-public: pem content wrong: %s", err)
+ }
+
+ RunPrivateKeyTests("rsa", pk, pu, t)
}