|  | 
|  | 1 | +package hasher_test | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"encoding/hex" | 
|  | 5 | +	"testing" | 
|  | 6 | + | 
|  | 7 | +	"github.com/stretchr/testify/assert" | 
|  | 8 | +	"github.com/tarantool/go-storage/hasher" | 
|  | 9 | +) | 
|  | 10 | + | 
|  | 11 | +func TestSHA1Hasher(t *testing.T) { | 
|  | 12 | +	t.Parallel() | 
|  | 13 | + | 
|  | 14 | +	tests := []struct { | 
|  | 15 | +		name string | 
|  | 16 | +		in   []byte | 
|  | 17 | +		out  string | 
|  | 18 | +	}{ | 
|  | 19 | +		{"empty", []byte(""), "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, | 
|  | 20 | +		{"abc", []byte("abc"), "a9993e364706816aba3e25717850c26c9cd0d89d"}, | 
|  | 21 | +	} | 
|  | 22 | + | 
|  | 23 | +	for _, test := range tests { | 
|  | 24 | +		t.Run(test.name, func(t *testing.T) { | 
|  | 25 | +			t.Parallel() | 
|  | 26 | + | 
|  | 27 | +			h := hasher.NewSHA1Hasher() | 
|  | 28 | + | 
|  | 29 | +			result, _ := h.Hash(test.in) | 
|  | 30 | + | 
|  | 31 | +			assert.Equal(t, test.out, hex.EncodeToString(result)) | 
|  | 32 | +		}) | 
|  | 33 | +	} | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +func TestSHA1Hasher_negative(t *testing.T) { | 
|  | 37 | +	t.Parallel() | 
|  | 38 | + | 
|  | 39 | +	tests := []struct { | 
|  | 40 | +		name              string | 
|  | 41 | +		in                []byte | 
|  | 42 | +		expectedErrorText string | 
|  | 43 | +	}{ | 
|  | 44 | +		{"nil", nil, "data is nil"}, | 
|  | 45 | +	} | 
|  | 46 | + | 
|  | 47 | +	for _, test := range tests { | 
|  | 48 | +		t.Run(test.name, func(t *testing.T) { | 
|  | 49 | +			t.Parallel() | 
|  | 50 | + | 
|  | 51 | +			h := hasher.NewSHA1Hasher() | 
|  | 52 | + | 
|  | 53 | +			_, err := h.Hash(test.in) | 
|  | 54 | + | 
|  | 55 | +			assert.Contains(t, err.Error(), test.expectedErrorText) | 
|  | 56 | +		}) | 
|  | 57 | +	} | 
|  | 58 | +} | 
|  | 59 | + | 
|  | 60 | +func TestSHA256Hasher(t *testing.T) { | 
|  | 61 | +	t.Parallel() | 
|  | 62 | + | 
|  | 63 | +	tests := []struct { | 
|  | 64 | +		name string | 
|  | 65 | +		in   []byte | 
|  | 66 | +		out  string | 
|  | 67 | +	}{ | 
|  | 68 | +		{"empty", []byte(""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, | 
|  | 69 | +		{"abc", []byte("abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}, | 
|  | 70 | +	} | 
|  | 71 | + | 
|  | 72 | +	for _, test := range tests { | 
|  | 73 | +		t.Run(test.name, func(t *testing.T) { | 
|  | 74 | +			t.Parallel() | 
|  | 75 | + | 
|  | 76 | +			h := hasher.NewSHA256Hasher() | 
|  | 77 | + | 
|  | 78 | +			result, _ := h.Hash(test.in) | 
|  | 79 | + | 
|  | 80 | +			assert.Equal(t, test.out, hex.EncodeToString(result)) | 
|  | 81 | +		}) | 
|  | 82 | +	} | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | +func TestSHA256Hasher_negative(t *testing.T) { | 
|  | 86 | +	t.Parallel() | 
|  | 87 | + | 
|  | 88 | +	tests := []struct { | 
|  | 89 | +		name              string | 
|  | 90 | +		in                []byte | 
|  | 91 | +		expectedErrorText string | 
|  | 92 | +	}{ | 
|  | 93 | +		{"nil", nil, "data is nil"}, | 
|  | 94 | +	} | 
|  | 95 | + | 
|  | 96 | +	for _, test := range tests { | 
|  | 97 | +		t.Run(test.name, func(t *testing.T) { | 
|  | 98 | +			t.Parallel() | 
|  | 99 | + | 
|  | 100 | +			h := hasher.NewSHA256Hasher() | 
|  | 101 | + | 
|  | 102 | +			_, err := h.Hash(test.in) | 
|  | 103 | + | 
|  | 104 | +			assert.Contains(t, err.Error(), test.expectedErrorText) | 
|  | 105 | +		}) | 
|  | 106 | +	} | 
|  | 107 | +} | 
0 commit comments