2021-09-08 06:15:42 +00:00
|
|
|
package uploader
|
|
|
|
|
|
|
|
import (
|
2022-09-01 09:21:27 +00:00
|
|
|
"math"
|
2021-11-26 12:44:07 +00:00
|
|
|
"strconv"
|
2021-09-08 06:15:42 +00:00
|
|
|
"testing"
|
2021-11-26 12:44:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-11-30 07:22:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/utils"
|
2021-09-08 06:15:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/valyala/fasthttp"
|
2022-03-25 13:06:33 +00:00
|
|
|
"go.uber.org/zap"
|
2021-09-08 06:15:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFilter(t *testing.T) {
|
2022-03-25 13:06:33 +00:00
|
|
|
log := zap.NewNop()
|
2021-09-08 06:15:42 +00:00
|
|
|
|
|
|
|
req := &fasthttp.RequestHeader{}
|
|
|
|
req.DisableNormalizing()
|
|
|
|
req.Set("X-Attribute-Neofs-Expiration-Epoch1", "101")
|
|
|
|
req.Set("X-Attribute-NEOFS-Expiration-Epoch2", "102")
|
|
|
|
req.Set("X-Attribute-neofs-Expiration-Epoch3", "103")
|
|
|
|
req.Set("X-Attribute-MyAttribute", "value")
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"__NEOFS__EXPIRATION_EPOCH1": "101",
|
|
|
|
"MyAttribute": "value",
|
|
|
|
"__NEOFS__EXPIRATION_EPOCH3": "103",
|
|
|
|
"__NEOFS__EXPIRATION_EPOCH2": "102",
|
|
|
|
}
|
|
|
|
|
|
|
|
result := filterHeaders(log, req)
|
|
|
|
|
|
|
|
require.Equal(t, expected, result)
|
|
|
|
}
|
2021-11-26 12:44:07 +00:00
|
|
|
|
|
|
|
func TestPrepareExpirationHeader(t *testing.T) {
|
|
|
|
tomorrow := time.Now().Add(24 * time.Hour)
|
|
|
|
tomorrowUnix := tomorrow.Unix()
|
|
|
|
tomorrowUnixNano := tomorrow.UnixNano()
|
|
|
|
tomorrowUnixMilli := tomorrowUnixNano / 1e6
|
|
|
|
|
|
|
|
epoch := "100"
|
|
|
|
duration := "24h"
|
|
|
|
timestampSec := strconv.FormatInt(tomorrowUnix, 10)
|
|
|
|
timestampMilli := strconv.FormatInt(tomorrowUnixMilli, 10)
|
|
|
|
timestampNano := strconv.FormatInt(tomorrowUnixNano, 10)
|
|
|
|
|
|
|
|
defaultDurations := &epochDurations{
|
|
|
|
currentEpoch: 10,
|
|
|
|
msPerBlock: 1000,
|
|
|
|
blockPerEpoch: 101,
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:21:27 +00:00
|
|
|
msPerBlock := defaultDurations.blockPerEpoch * uint64(defaultDurations.msPerBlock)
|
|
|
|
epochPerDay := uint64((24 * time.Hour).Milliseconds()) / msPerBlock
|
|
|
|
if uint64((24*time.Hour).Milliseconds())%msPerBlock != 0 {
|
|
|
|
epochPerDay++
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultExpEpoch := strconv.FormatUint(defaultDurations.currentEpoch+epochPerDay, 10)
|
2021-11-26 12:44:07 +00:00
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
headers map[string]string
|
|
|
|
durations *epochDurations
|
|
|
|
err bool
|
|
|
|
expected map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid epoch",
|
|
|
|
headers: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid epoch, valid duration",
|
|
|
|
headers: map[string]string{
|
2021-11-30 07:22:05 +00:00
|
|
|
object.SysAttributeExpEpoch: epoch,
|
|
|
|
utils.ExpirationDurationAttr: duration,
|
2021-11-26 12:44:07 +00:00
|
|
|
},
|
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid epoch, valid rfc3339",
|
|
|
|
headers: map[string]string{
|
|
|
|
object.SysAttributeExpEpoch: epoch,
|
2021-11-30 07:22:05 +00:00
|
|
|
utils.ExpirationRFC3339Attr: tomorrow.Format(time.RFC3339),
|
2021-11-26 12:44:07 +00:00
|
|
|
},
|
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid epoch, valid timestamp sec",
|
|
|
|
headers: map[string]string{
|
2021-11-30 07:22:05 +00:00
|
|
|
object.SysAttributeExpEpoch: epoch,
|
|
|
|
utils.ExpirationTimestampAttr: timestampSec,
|
2021-11-26 12:44:07 +00:00
|
|
|
},
|
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid epoch, valid timestamp milli",
|
|
|
|
headers: map[string]string{
|
2021-11-30 07:22:05 +00:00
|
|
|
object.SysAttributeExpEpoch: epoch,
|
|
|
|
utils.ExpirationTimestampAttr: timestampMilli,
|
2021-11-26 12:44:07 +00:00
|
|
|
},
|
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid epoch, valid timestamp nano",
|
|
|
|
headers: map[string]string{
|
2021-11-30 07:22:05 +00:00
|
|
|
object.SysAttributeExpEpoch: epoch,
|
|
|
|
utils.ExpirationTimestampAttr: timestampNano,
|
2021-11-26 12:44:07 +00:00
|
|
|
},
|
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: epoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid timestamp sec",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationTimestampAttr: timestampSec},
|
2021-11-26 12:44:07 +00:00
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: defaultExpEpoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid duration",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationDurationAttr: duration},
|
2021-11-26 12:44:07 +00:00
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: defaultExpEpoch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid rfc3339",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationRFC3339Attr: tomorrow.Format(time.RFC3339)},
|
2021-11-26 12:44:07 +00:00
|
|
|
durations: defaultDurations,
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: defaultExpEpoch},
|
|
|
|
},
|
2022-09-01 09:21:27 +00:00
|
|
|
{
|
|
|
|
name: "valid max uint 64",
|
|
|
|
headers: map[string]string{utils.ExpirationRFC3339Attr: tomorrow.Format(time.RFC3339)},
|
|
|
|
durations: &epochDurations{
|
|
|
|
currentEpoch: math.MaxUint64 - 1,
|
|
|
|
msPerBlock: defaultDurations.msPerBlock,
|
|
|
|
blockPerEpoch: defaultDurations.blockPerEpoch,
|
|
|
|
},
|
|
|
|
expected: map[string]string{object.SysAttributeExpEpoch: strconv.FormatUint(uint64(math.MaxUint64), 10)},
|
|
|
|
},
|
2021-11-26 12:44:07 +00:00
|
|
|
{
|
|
|
|
name: "invalid timestamp sec",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationTimestampAttr: "abc"},
|
2021-11-26 12:44:07 +00:00
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid timestamp sec zero",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationTimestampAttr: "0"},
|
2021-11-26 12:44:07 +00:00
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid duration",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationDurationAttr: "1d"},
|
2021-11-26 12:44:07 +00:00
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid duration negative",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationDurationAttr: "-5h"},
|
2021-11-26 12:44:07 +00:00
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid rfc3339",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationRFC3339Attr: "abc"},
|
2021-11-26 12:44:07 +00:00
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid rfc3339 zero",
|
2021-11-30 07:22:05 +00:00
|
|
|
headers: map[string]string{utils.ExpirationRFC3339Attr: time.RFC3339},
|
2021-11-26 12:44:07 +00:00
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-11-08 06:55:13 +00:00
|
|
|
err := prepareExpirationHeader(tc.headers, tc.durations, time.Now())
|
2021-11-26 12:44:07 +00:00
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, tc.headers)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|