1
|
|
|
package crypto_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
|
6
|
|
|
"github.com/memnix/memnix-rest/pkg/crypto" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// MockCrypto is a mock implementation of the ICrypto interface for testing. |
10
|
|
|
type MockCrypto struct{} |
11
|
|
|
|
12
|
|
|
func (mc MockCrypto) Hash(_ string) ([]byte, error) { |
13
|
|
|
// Implement a mock hashing function for testing |
14
|
|
|
return []byte("mocked-hash"), nil |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
func (mc MockCrypto) Verify(_ string, hash []byte) (bool, error) { |
18
|
|
|
// Implement a mock verify function for testing |
19
|
|
|
return string(hash) == "mocked-hash", nil |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
func TestCrypto_Hash(t *testing.T) { |
23
|
|
|
mockCrypto := MockCrypto{} |
24
|
|
|
cryptoHelper := crypto.Crypto{Crypto: mockCrypto} |
25
|
|
|
|
26
|
|
|
password := "my_password" |
27
|
|
|
hashedPassword, err := cryptoHelper.Hash(password) |
28
|
|
|
if err != nil { |
29
|
|
|
t.Errorf("Hash() returned an error: %v", err) |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if string(hashedPassword) != "mocked-hash" { |
33
|
|
|
t.Errorf("Hash() did not return the expected hashed value") |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
func TestCrypto_Verify(t *testing.T) { |
38
|
|
|
mockCrypto := MockCrypto{} |
39
|
|
|
cryptoHelper := crypto.Crypto{Crypto: mockCrypto} |
40
|
|
|
|
41
|
|
|
password := "my_password" |
42
|
|
|
hashedPassword, _ := cryptoHelper.Hash(password) |
43
|
|
|
|
44
|
|
|
match, err := cryptoHelper.Verify(password, hashedPassword) |
45
|
|
|
if err != nil { |
46
|
|
|
t.Errorf("Verify() returned an error: %v", err) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if !match { |
50
|
|
|
t.Errorf("Verify() did not return a match for the hashed password") |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|