| 1 | package ecdh |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/ecdsa" |
| 6 | "crypto/elliptic" |
| 7 | "crypto/rand" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | var testKey *ecdsa.PrivateKey |
| 12 | |
| 13 | func TestGenerateKey(t *testing.T) { |
| 14 | var err error |
| 15 | testKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 16 | if err != nil { |
| 17 | t.Fatalf("%v", err) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestCrypt(t *testing.T) { |
| 22 | message := []byte("One ping only, please.") |
| 23 | out, err := Encrypt(&testKey.PublicKey, message) |
| 24 | if err != nil { |
| 25 | t.Fatalf("%v", err) |
| 26 | } |
| 27 | |
| 28 | out, err = Decrypt(testKey, out) |
| 29 | if err != nil { |
| 30 | t.Fatalf("%v", err) |
| 31 | } |
| 32 | |
| 33 | if !bytes.Equal(out, message) { |
| 34 | t.Fatal("Decryption return different plaintext than original message.") |
| 35 | } |
| 36 | } |