Skip to content

Commit 286e49d

Browse files
committed
refactor: add test dir for helpers
1 parent 19c1894 commit 286e49d

File tree

3 files changed

+101
-36
lines changed

3 files changed

+101
-36
lines changed

internal/shared/services/checksum.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import (
55
"crypto/sha256"
66
"encoding/hex"
77
"errors"
8+
"fmt"
89
"hash"
910
)
1011

11-
var ErrVerify = errors.New("unverified data")
12+
var (
13+
ErrVerify = errors.New("unverified data")
14+
ErrInvalidData = errors.New("invalid data")
15+
)
1216

1317
type Checksum struct {
1418
hmac hash.Hash
@@ -23,7 +27,7 @@ func NewChecksum(secretkey string) *Checksum {
2327
func (c *Checksum) Verify(dataHash, data string) error {
2428
sum, err := c.Sum(data)
2529
if err != nil {
26-
return err
30+
return fmt.Errorf("%w: %w", ErrVerify, err)
2731
}
2832

2933
if sum != dataHash {
@@ -36,9 +40,8 @@ func (c *Checksum) Verify(dataHash, data string) error {
3640
func (c *Checksum) Sum(data string) (string, error) {
3741
defer c.hmac.Reset()
3842

39-
_, err := c.hmac.Write([]byte(data))
40-
if err != nil {
41-
return "", err
43+
if _, err := c.hmac.Write([]byte(data)); err != nil {
44+
return "", fmt.Errorf("%w: %w", ErrInvalidData, err)
4245
}
4346

4447
return hex.EncodeToString(c.hmac.Sum(nil)), nil
Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,89 @@
11
package services
22

33
import (
4-
"crypto/hmac"
5-
"crypto/rand"
6-
"crypto/sha256"
7-
"encoding/base64"
8-
"encoding/hex"
4+
"fmt"
95
"testing"
106

7+
"github.com/srg-bnd/observator/tests/helpers"
118
"github.com/stretchr/testify/assert"
129
)
1310

1411
func TestChecksum(t *testing.T) {
15-
randomString, err := secureRandomString(16)
12+
randomString, err := helpers.SecureRandomString(16)
1613
assert.Nil(t, err)
1714

18-
data := "test data"
19-
hmac := hmac.New(sha256.New, []byte(randomString))
20-
hmac.Write([]byte(data))
15+
testCases := []struct {
16+
data string
17+
dataHash string
18+
err error
19+
}{
20+
{
21+
data: "correctData",
22+
dataHash: helpers.Sha256Hash("correctData", randomString),
23+
},
24+
{
25+
data: "",
26+
dataHash: helpers.Sha256Hash("", randomString),
27+
},
28+
{
29+
data: "correctData",
30+
dataHash: helpers.Sha256Hash("incorrectData", randomString),
31+
err: ErrVerify,
32+
},
33+
}
2134

22-
err = NewChecksum(randomString).Verify(hex.EncodeToString(hmac.Sum(nil)), data)
23-
assert.Nil(t, err)
35+
service := NewChecksum(randomString)
36+
37+
for i, tc := range testCases {
38+
t.Run(fmt.Sprint("Test ", i+1, ": ", tc.data), func(t *testing.T) {
39+
err := service.Verify(tc.dataHash, tc.data)
40+
41+
if tc.err != nil {
42+
assert.NotNil(t, err)
43+
} else {
44+
assert.ErrorIs(t, err, tc.err)
45+
}
46+
})
47+
}
2448
}
2549

2650
func TestSum(t *testing.T) {
27-
randomString, err := secureRandomString(16)
51+
randomString, err := helpers.SecureRandomString(16)
2852
assert.Nil(t, err)
2953

30-
data := "test data"
31-
hmac := hmac.New(sha256.New, []byte(randomString))
32-
hmac.Write([]byte(data))
33-
service := NewChecksum(randomString)
34-
sum, err := service.Sum(data)
35-
assert.Nil(t, err)
36-
assert.Equal(t, hex.EncodeToString(hmac.Sum(nil)), sum)
54+
testCases := []struct {
55+
input string
56+
expected string
57+
fail bool
58+
}{
59+
{
60+
input: "correctData",
61+
expected: helpers.Sha256Hash("correctData", randomString),
62+
},
63+
// For testing resets
64+
{
65+
input: "newCorrectData",
66+
expected: helpers.Sha256Hash("newCorrectData", randomString),
67+
},
68+
{
69+
input: "oldData",
70+
expected: helpers.Sha256Hash("newData", randomString),
71+
fail: true,
72+
},
73+
}
3774

38-
sum, err = service.Sum(data)
39-
assert.Nil(t, err)
40-
assert.Equal(t, hex.EncodeToString(hmac.Sum(nil)), sum)
41-
}
75+
service := NewChecksum(randomString)
4276

43-
// Helpers
77+
for i, tc := range testCases {
78+
t.Run(fmt.Sprint("Test ", i+1, ": ", tc.input), func(t *testing.T) {
79+
result, err := service.Sum(tc.input)
80+
assert.Nil(t, err)
4481

45-
func secureRandomString(length int) (string, error) {
46-
b := make([]byte, length)
47-
_, err := rand.Read(b)
48-
if err != nil {
49-
return "", err
82+
if tc.fail {
83+
assert.NotEqual(t, tc.expected, result)
84+
} else {
85+
assert.Equal(t, tc.expected, result)
86+
}
87+
})
5088
}
51-
52-
return base64.StdEncoding.EncodeToString(b), nil
5389
}

tests/helpers/helpers.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package helpers
2+
3+
import (
4+
"crypto/hmac"
5+
"crypto/rand"
6+
"crypto/sha256"
7+
"encoding/base64"
8+
"encoding/hex"
9+
)
10+
11+
func Sha256Hash(data string, key string) string {
12+
hmac := hmac.New(sha256.New, []byte(key))
13+
hmac.Write([]byte(data))
14+
15+
return hex.EncodeToString(hmac.Sum(nil))
16+
}
17+
18+
func SecureRandomString(length int) (string, error) {
19+
b := make([]byte, length)
20+
_, err := rand.Read(b)
21+
if err != nil {
22+
return "", err
23+
}
24+
25+
return base64.StdEncoding.EncodeToString(b), nil
26+
}

0 commit comments

Comments
 (0)