-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
37 lines (32 loc) · 872 Bytes
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package envcrypt
import (
"context"
"os"
"strings"
"testing"
)
func TestRoundtrip(t *testing.T) {
keyspec := os.Getenv("KMS_KEYSPEC")
if keyspec == "" {
t.Errorf("Must specify KMS_KEYSPEC environment variable: project/{{ project_name }}/locations/{{ location }}/keyRings/{{ keyRing }}/cryptoKeys/{{ keyname }}")
return
}
plaintext := "This is a test message."
ctx := context.Background()
message, err := EncryptMessage(ctx, keyspec, strings.NewReader(plaintext))
if err != nil {
t.Errorf("Error encrypting plaintext: %q", err)
return
}
var b strings.Builder
err = DecryptMessage(ctx, keyspec, message, &b)
if err != nil {
t.Errorf("Error decrypting plaintext: %q", err)
return
}
rttext := b.String()
cmp := strings.Compare(plaintext, rttext)
if cmp != 0 {
t.Errorf("Round trip does not match, sent %q got %q", plaintext, rttext)
}
}