summaryrefslogtreecommitdiff
path: root/internal/security/credentials_test.go
blob: 07fa59e3082db36a88d3e26362d28cb15e79ac6d (plain)
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
package security

import (
	"encoding/base64"
	"strings"
	"testing"
)

func TestCredentialCipherRoundTrip(t *testing.T) {
	key := base64.StdEncoding.EncodeToString([]byte(strings.Repeat("k", 32)))
	cipher, err := NewCredentialCipher(key)
	if err != nil {
		t.Fatal(err)
	}
	ciphertext, err := cipher.Encrypt("upstream-secret")
	if err != nil {
		t.Fatal(err)
	}
	plaintext, err := cipher.Decrypt(ciphertext)
	if err != nil {
		t.Fatal(err)
	}
	if plaintext != "upstream-secret" {
		t.Fatalf("unexpected plaintext: %q", plaintext)
	}
	ciphertext[len(ciphertext)-1] ^= 1
	if _, err := cipher.Decrypt(ciphertext); err == nil {
		t.Fatal("expected authentication failure for modified ciphertext")
	}
}