-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for crypto package (issue #59, improve code coverage)
- Loading branch information
1 parent
0ec9dce
commit d523994
Showing
1 changed file
with
54 additions
and
0 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,54 @@ | ||
package crypto | ||
|
||
import "testing" | ||
|
||
func TestRsaKeys(t *testing.T) { | ||
sk, _, err := GenerateKeyPair(RSA, 512) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
testKeySignature(t, sk) | ||
testKeyEncoding(t, sk) | ||
} | ||
|
||
func testKeySignature(t *testing.T, sk PrivKey) { | ||
pk := sk.GetPublic() | ||
|
||
text := sk.GenSecret() | ||
sig, err := sk.Sign(text) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
valid, err := pk.Verify(text, sig) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !valid { | ||
t.Fatal("Invalid signature.") | ||
} | ||
} | ||
|
||
func testKeyEncoding(t *testing.T, sk PrivKey) { | ||
skb, err := sk.Bytes() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = UnmarshalPrivateKey(skb) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
pk := sk.GetPublic() | ||
pkb, err := pk.Bytes() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = UnmarshalPublicKey(pkb) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |