2021-10-27 07:44:59 +00:00
|
|
|
package checksum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/tzhash/tz"
|
2021-10-27 07:44:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestChecksum(t *testing.T) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var c Checksum
|
2021-10-27 07:44:59 +00:00
|
|
|
|
|
|
|
cSHA256 := [sha256.Size]byte{}
|
|
|
|
_, _ = rand.Read(cSHA256[:])
|
|
|
|
|
|
|
|
c.SetSHA256(cSHA256)
|
|
|
|
|
|
|
|
require.Equal(t, SHA256, c.Type())
|
2021-03-22 10:56:18 +00:00
|
|
|
require.Equal(t, cSHA256[:], c.Value())
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
var cV2 refs.Checksum
|
|
|
|
c.WriteToV2(&cV2)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
|
|
|
require.Equal(t, refs.SHA256, cV2.GetType())
|
|
|
|
require.Equal(t, cSHA256[:], cV2.GetSum())
|
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
cTZ := [tz.Size]byte{}
|
2021-10-27 07:44:59 +00:00
|
|
|
_, _ = rand.Read(cSHA256[:])
|
|
|
|
|
|
|
|
c.SetTillichZemor(cTZ)
|
|
|
|
|
|
|
|
require.Equal(t, TZ, c.Type())
|
2021-03-22 10:56:18 +00:00
|
|
|
require.Equal(t, cTZ[:], c.Value())
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
c.WriteToV2(&cV2)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
|
|
|
require.Equal(t, refs.TillichZemor, cV2.GetType())
|
|
|
|
require.Equal(t, cTZ[:], cV2.GetSum())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewChecksum(t *testing.T) {
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var chs Checksum
|
2021-10-27 07:44:59 +00:00
|
|
|
|
|
|
|
// check initial values
|
|
|
|
require.Equal(t, Unknown, chs.Type())
|
2021-03-22 10:56:18 +00:00
|
|
|
require.Nil(t, chs.Value())
|
2021-10-27 07:44:59 +00:00
|
|
|
|
|
|
|
// convert to v2 message
|
2021-03-22 10:56:18 +00:00
|
|
|
var chsV2 refs.Checksum
|
|
|
|
chs.WriteToV2(&chsV2)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
|
|
|
require.Equal(t, refs.UnknownChecksum, chsV2.GetType())
|
|
|
|
require.Nil(t, chsV2.GetSum())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
func TestCalculation(t *testing.T) {
|
|
|
|
var c Checksum
|
|
|
|
payload := []byte{0, 1, 2, 3, 4, 5}
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
t.Run("SHA256", func(t *testing.T) {
|
|
|
|
orig := sha256.Sum256(payload)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
Calculate(&c, SHA256, payload)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
require.Equal(t, orig[:], c.Value())
|
|
|
|
})
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
t.Run("TZ", func(t *testing.T) {
|
|
|
|
orig := tz.Sum(payload)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
Calculate(&c, TZ, payload)
|
2021-10-27 07:44:59 +00:00
|
|
|
|
2021-03-22 10:56:18 +00:00
|
|
|
require.Equal(t, orig[:], c.Value())
|
2021-10-27 07:44:59 +00:00
|
|
|
})
|
|
|
|
}
|