File: hash_test.go

package info (click to toggle)
gittuf 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,692 kB
  • sloc: python: 85; makefile: 58; sh: 1
file content (63 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (2)
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
60
61
62
63
// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0

package gitinterface

import (
	"encoding/hex"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewHash(t *testing.T) {
	tests := map[string]struct {
		hash          string
		expectedError error
	}{
		"correctly encoded SHA-1 hash": {
			hash: "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
		},
		"correctly encoded SHA-256 hash": {
			hash: "61658570165bc04af68cef20d72da49b070dc9d8cd7c8a526c950b658f4d3ccf",
		},
		"correctly encoded SHA-1 zero hash": {
			hash: "0000000000000000000000000000000000000000",
		},
		"correctly encoded SHA-256 zero hash": {
			hash: "0000000000000000000000000000000000000000000000000000000000000000",
		},
		"incorrect length SHA-1 hash": {
			hash:          "e69de29bb2d1d6434b8",
			expectedError: ErrInvalidHashLength,
		},
		"incorrect length SHA-256 hash": {
			hash:          "61658570165bc04af68cef20d72da49b070dc9d8cd7c8a526c950b658f4d3ccfabcdef",
			expectedError: ErrInvalidHashLength,
		},
		"incorrectly encoded SHA-1 hash": {
			hash:          "e69de29bb2d1d6434b8b29ae775ad8c2e48c539g", // last char is 'g'
			expectedError: ErrInvalidHashEncoding,
		},
		"incorrectly encoded SHA-256 hash": {
			hash:          "61658570165bc04af68cef20d72da49b070dc9d8cd7c8a526c950b658f4d3ccg", // last char is 'g'
			expectedError: ErrInvalidHashEncoding,
		},
	}

	for name, test := range tests {
		hash, err := NewHash(test.hash)
		if test.expectedError == nil {
			expectedHash, secErr := hex.DecodeString(test.hash)
			require.Nil(t, secErr)

			assert.Equal(t, Hash(expectedHash), hash)
			assert.Equal(t, test.hash, hash.String())
			assert.Nil(t, err, fmt.Sprintf("unexpected error in test '%s'", name))
		} else {
			assert.ErrorIs(t, err, test.expectedError, fmt.Sprintf("unexpected error in test '%s'", name))
		}
	}
}