aboutsummaryrefslogtreecommitdiff
path: root/private_key_test.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-18 21:36:42 +0100
committerGibheer <gibheer@gmail.com>2015-02-18 21:36:42 +0100
commitdfa8c8a384b6af9feba4e2f3fb746a7303a7f275 (patch)
tree958a695eec29e7ebb1b0afb31c4c0a6a22045ec2 /private_key_test.go
parent67f850c9fbd4186b41113240a5a545676dbbf0b7 (diff)
add first batch of tests
Diffstat (limited to 'private_key_test.go')
-rw-r--r--private_key_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/private_key_test.go b/private_key_test.go
new file mode 100644
index 0000000..ad96928
--- /dev/null
+++ b/private_key_test.go
@@ -0,0 +1,66 @@
+package pki
+
+import (
+ "crypto/elliptic"
+ "encoding/pem"
+ "testing"
+)
+
+var (
+ SignatureMessage = []byte("foobar")
+)
+
+// run the marshal test
+func RunMarshalTest(pk_type string, pk Pemmer, label string, t *testing.T) ([]byte, error) {
+ marsh_pem, err := pk.MarshalPem()
+ if err != nil {
+ t.Errorf("%s: marshal pem not working: %s", pk_type, err)
+ return nil, err
+ }
+
+ block, _ := pem.Decode(marsh_pem)
+ if block.Type != label {
+ t.Errorf("%s: marshalled pem wrong: %s", pk_type, err)
+ return nil, err
+ }
+ return block.Bytes, nil
+}
+
+// test other private key functions
+func RunPrivateKeyTests(pk_type string, pk PrivateKey, t *testing.T) {
+ pu := pk.Public()
+
+ signature, err := pk.Sign(SignatureMessage)
+ if err != nil { t.Errorf("%s: error creating a signature: %s", pk_type, err) }
+
+ valid, err := pu.Verify(SignatureMessage, signature)
+ if err != nil { t.Errorf("%s: could not verify message: %s", pk_type, err) }
+ if !valid { t.Errorf("%s: signature invalid, but should be valid!", pk_type) }
+}
+
+// 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) }
+
+ block_bytes, err := RunMarshalTest("ecdsa", pk, PemLabelEcdsa, t)
+ if err != nil { return }
+
+ pk, err = LoadPrivateKeyEcdsa(block_bytes)
+ if err != nil { t.Errorf("ecdsa: pem content wrong: %s", err) }
+
+ RunPrivateKeyTests("ecdsa", pk, t)
+}
+
+func TestRsaFunctions(t *testing.T) {
+ pk, err := NewPrivateKeyRsa(2048)
+ if err != nil { t.Errorf("rsa: creating private key failed: %s", err) }
+
+ block_bytes, err := RunMarshalTest("rsa", pk, PemLabelRsa, t)
+ if err != nil { return }
+
+ pk, err = LoadPrivateKeyRsa(block_bytes)
+ if err != nil { t.Errorf("rsa: pem content wrong: %s", err) }
+
+ RunPrivateKeyTests("rsa", pk, t)
+}