MCPcopy Index your code
hub / github.com/pocketbase/pocketbase / TestEncrypt

Function TestEncrypt

tools/security/encrypt_test.go:10–46  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8)
9
10func TestEncrypt(t *testing.T) {
11 scenarios := []struct {
12 data string
13 key string
14 expectError bool
15 }{
16 {"", "", true},
17 {"123", "test", true}, // key must be valid 32 char aes string
18 {"123", "abcdabcdabcdabcdabcdabcdabcdabcd", false},
19 }
20
21 for i, s := range scenarios {
22 t.Run(fmt.Sprintf("%d_%s", i, s.data), func(t *testing.T) {
23 result, err := security.Encrypt([]byte(s.data), s.key)
24
25 hasErr := err != nil
26
27 if hasErr != s.expectError {
28 t.Fatalf("Expected hasErr %v, got %v (%v)", s.expectError, hasErr, err)
29 }
30
31 if hasErr {
32 if result != "" {
33 t.Fatalf("Expected empty Encrypt result on error, got %q", result)
34 }
35
36 return
37 }
38
39 // try to decrypt
40 decrypted, err := security.Decrypt(result, s.key)
41 if err != nil || string(decrypted) != s.data {
42 t.Fatalf("Expected decrypted value to match with the data input, got %q (%v)", decrypted, err)
43 }
44 })
45 }
46}
47
48func TestDecrypt(t *testing.T) {
49 scenarios := []struct {

Callers

nothing calls this directly

Calls 3

EncryptFunction · 0.92
DecryptFunction · 0.92
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…