frostfs-api-go/service/verify_test.go

118 lines
2.2 KiB
Go
Raw Normal View History

2019-11-18 16:22:08 +00:00
package service
import (
"encoding/binary"
"io"
2019-11-18 16:22:08 +00:00
"math"
"testing"
2020-03-31 07:05:26 +00:00
"github.com/nspcc-dev/neofs-api-go/refs"
2019-11-18 16:22:08 +00:00
"github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require"
)
func (m TestRequest) SignedData() ([]byte, error) {
return SignedDataFromReader(m)
}
func (m TestRequest) SignedDataSize() (sz int) {
sz += 4
sz += len(m.StringField)
sz += len(m.BytesField)
sz += m.CustomField.Size()
return
}
func (m TestRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() {
return 0, io.ErrUnexpectedEOF
2019-11-18 16:22:08 +00:00
}
var off int
2019-11-18 16:22:08 +00:00
binary.BigEndian.PutUint32(p[off:], uint32(m.IntField))
off += 4
2019-11-18 16:22:08 +00:00
off += copy(p[off:], []byte(m.StringField))
2019-11-18 16:22:08 +00:00
off += copy(p[off:], m.BytesField)
2019-11-18 16:22:08 +00:00
n, err := m.CustomField.MarshalTo(p[off:])
off += n
2019-11-18 16:22:08 +00:00
return off, err
2019-11-18 16:22:08 +00:00
}
func BenchmarkSignDataWithSessionToken(b *testing.B) {
key := test.DecodeKey(0)
2019-11-18 16:22:08 +00:00
customField := testCustomField{1, 2, 3, 4, 5, 6, 7, 8}
2020-04-28 10:09:18 +00:00
token := new(Token)
2019-11-18 16:22:08 +00:00
req := &TestRequest{
IntField: math.MaxInt32,
StringField: "TestRequestStringField",
BytesField: make([]byte, 1<<22),
CustomField: &customField,
2019-11-26 10:42:47 +00:00
}
req.SetTTL(math.MaxInt32 - 8)
req.SetEpoch(math.MaxInt64 - 12)
req.SetToken(token)
2019-11-18 16:22:08 +00:00
b.ResetTimer()
b.ReportAllocs()
2019-11-18 16:22:08 +00:00
for i := 0; i < b.N; i++ {
require.NoError(b, SignDataWithSessionToken(key, req))
2019-11-18 16:22:08 +00:00
}
}
func BenchmarkVerifyAccumulatedSignaturesWithToken(b *testing.B) {
customField := testCustomField{1, 2, 3, 4, 5, 6, 7, 8}
token := new(Token)
req := &TestRequest{
IntField: math.MaxInt32,
StringField: "TestRequestStringField",
BytesField: make([]byte, 1<<22),
CustomField: &customField,
}
req.SetTTL(math.MaxInt32 - 8)
req.SetEpoch(math.MaxInt64 - 12)
req.SetToken(token)
for i := 0; i < 10; i++ {
key := test.DecodeKey(i)
require.NoError(b, SignDataWithSessionToken(key, req))
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
require.NoError(b, VerifyAccumulatedSignaturesWithToken(req))
}
}
func TestRequestVerificationHeader_SetToken(t *testing.T) {
id, err := refs.NewUUID()
require.NoError(t, err)
token := new(Token)
token.SetID(id)
h := new(RequestVerificationHeader)
h.SetToken(token)
require.Equal(t, token, h.GetToken())
}