2019-11-25 16:58:12 +02:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncrypt(t *testing.T) {
|
2020-03-09 19:10:08 +02:00
|
|
|
key := []byte("tokenpassphrase")
|
2020-03-11 19:55:39 +02:00
|
|
|
gcm, err := newCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:58:12 +02:00
|
|
|
data := []byte("supersecret")
|
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
cipherText, err := Encrypt(gcm, data)
|
2019-11-25 16:58:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to encrypt data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the cipherText is not the same as data
|
|
|
|
if bytes.Equal(data, cipherText) {
|
|
|
|
t.Error("encrypted data are the same as plaintext")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrypt(t *testing.T) {
|
2020-03-09 19:10:08 +02:00
|
|
|
key := []byte("tokenpassphrase")
|
2020-03-11 19:55:39 +02:00
|
|
|
gcm, err := newCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:58:12 +02:00
|
|
|
data := []byte("supersecret")
|
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
cipherText, err := Encrypt(gcm, data)
|
2019-11-25 16:58:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to encrypt data: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
plainText, err := Decrypt(gcm, cipherText)
|
2019-11-25 16:58:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to decrypt data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the plainText is the same as data
|
|
|
|
if !bytes.Equal(data, plainText) {
|
|
|
|
t.Error("decrypted data not the same as plaintext")
|
|
|
|
}
|
|
|
|
}
|