|
1 | 1 | package services |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "crypto/hmac" |
5 | | - "crypto/rand" |
6 | | - "crypto/sha256" |
7 | | - "encoding/base64" |
8 | | - "encoding/hex" |
| 4 | + "fmt" |
9 | 5 | "testing" |
10 | 6 |
|
| 7 | + "github.com/srg-bnd/observator/tests/helpers" |
11 | 8 | "github.com/stretchr/testify/assert" |
12 | 9 | ) |
13 | 10 |
|
14 | 11 | func TestChecksum(t *testing.T) { |
15 | | - randomString, err := secureRandomString(16) |
| 12 | + randomString, err := helpers.SecureRandomString(16) |
16 | 13 | assert.Nil(t, err) |
17 | 14 |
|
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 | + } |
21 | 34 |
|
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 | + } |
24 | 48 | } |
25 | 49 |
|
26 | 50 | func TestSum(t *testing.T) { |
27 | | - randomString, err := secureRandomString(16) |
| 51 | + randomString, err := helpers.SecureRandomString(16) |
28 | 52 | assert.Nil(t, err) |
29 | 53 |
|
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 | + } |
37 | 74 |
|
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) |
42 | 76 |
|
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) |
44 | 81 |
|
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 | + }) |
50 | 88 | } |
51 | | - |
52 | | - return base64.StdEncoding.EncodeToString(b), nil |
53 | 89 | } |
0 commit comments