git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/fmt_test.go (about)

     1  package object
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestVerificationFields(t *testing.T) {
    12  	obj := New()
    13  
    14  	payload := make([]byte, 10)
    15  	_, _ = rand.Read(payload)
    16  
    17  	obj.SetPayload(payload)
    18  	obj.SetPayloadSize(uint64(len(payload)))
    19  
    20  	p, err := keys.NewPrivateKey()
    21  	require.NoError(t, err)
    22  	require.NoError(t, SetVerificationFields(p.PrivateKey, obj))
    23  
    24  	require.NoError(t, CheckVerificationFields(obj))
    25  
    26  	items := []struct {
    27  		corrupt func()
    28  		restore func()
    29  	}{
    30  		{
    31  			corrupt: func() {
    32  				payload[0]++
    33  			},
    34  			restore: func() {
    35  				payload[0]--
    36  			},
    37  		},
    38  		{
    39  			corrupt: func() {
    40  				obj.SetPayloadSize(obj.PayloadSize() + 1)
    41  			},
    42  			restore: func() {
    43  				obj.SetPayloadSize(obj.PayloadSize() - 1)
    44  			},
    45  		},
    46  		{
    47  			corrupt: func() {
    48  				obj.ToV2().GetObjectID().GetValue()[0]++
    49  			},
    50  			restore: func() {
    51  				obj.ToV2().GetObjectID().GetValue()[0]--
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, item := range items {
    57  		item.corrupt()
    58  
    59  		require.Error(t, CheckVerificationFields(obj))
    60  
    61  		item.restore()
    62  
    63  		require.NoError(t, CheckVerificationFields(obj))
    64  	}
    65  }