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