Core

/app/util/encryption_test.go (1.3 KB)

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//go:build test_all || !func_test
package util_test

import (
"testing"

"{{{ .Package }}}/app/util"
)

var hashTests = []struct {
plaintext string
hash32 uint32
hashSHA string
}{
{plaintext: "Hello, world!", hash32: 3985698964, hashSHA: "SGVsbG8sIHdvcmxkIeOwxEKY_BwUmvv0yJlvuSQnrkHkZJuTTKSVmRt4UrhV"},
{plaintext: "Goodbye, cruel world!", hash32: 55824456, hashSHA: "R29vZGJ5ZSwgY3J1ZWwgd29ybGQh47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU="},
}

func TestEncryptDecrypt(t *testing.T) {
t.Parallel()

for _, tt := range hashTests {
ciphertext, err := util.EncryptMessage(nil, tt.plaintext, nil)
if err != nil {
t.Fatal(err)
}

plaintext, err := util.DecryptMessage(nil, ciphertext, nil)
if err != nil {
t.Fatal(err)
}

if plaintext != tt.plaintext {
t.Errorf("plaintexts don't match")
}
}
}

func TestHashFNV32(t *testing.T) {
t.Parallel()

for _, tt := range hashTests {
h := util.HashFNV32(tt.plaintext)
if h != tt.hash32 {
t.Errorf("FNC32 hash didn't match for [%s], expected [%d], observed [%d]", tt.plaintext, tt.hash32, h)
}
}
}

func TestHashSHA256(t *testing.T) {
t.Parallel()

for _, tt := range hashTests {
h := util.HashSHA256(tt.plaintext)
if h != tt.hashSHA {
t.Errorf("SHA-256 hash didn't match for [%s], expected [%s], observed [%s]", tt.plaintext, tt.hashSHA, h)
}
}
}