2022-06-01 14:50:30 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-06-01 14:50:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2022-10-04 08:31:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
2022-06-01 14:50:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConditionalHead(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
bktName, objName := "bucket-for-conditional", "object"
|
|
|
|
_, objInfo := createBucketAndObject(tc, bktName, objName)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
w, r := prepareTestRequest(tc, bktName, objName, nil)
|
2022-06-01 14:50:30 +00:00
|
|
|
tc.Handler().HeadObjectHandler(w, r)
|
|
|
|
assertStatus(t, w, http.StatusOK)
|
|
|
|
etag := w.Result().Header.Get(api.ETag)
|
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers := map[string]string{api.IfMatch: etag}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusOK)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfMatch: "etag"}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusPreconditionFailed)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfUnmodifiedSince: objInfo.Created.Add(time.Minute).Format(http.TimeFormat)}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusOK)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
|
|
|
var zeroTime time.Time
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfUnmodifiedSince: zeroTime.UTC().Format(http.TimeFormat)}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusPreconditionFailed)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{
|
|
|
|
api.IfMatch: etag,
|
|
|
|
api.IfUnmodifiedSince: zeroTime.UTC().Format(http.TimeFormat),
|
|
|
|
}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusOK)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfNoneMatch: etag}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusNotModified)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfNoneMatch: "etag"}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusOK)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfModifiedSince: zeroTime.UTC().Format(http.TimeFormat)}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusOK)
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
headers = map[string]string{api.IfModifiedSince: time.Now().Add(time.Minute).UTC().Format(http.TimeFormat)}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusNotModified)
|
|
|
|
|
|
|
|
headers = map[string]string{
|
|
|
|
api.IfNoneMatch: etag,
|
|
|
|
api.IfModifiedSince: zeroTime.UTC().Format(http.TimeFormat),
|
|
|
|
}
|
|
|
|
headObject(t, tc, bktName, objName, headers, http.StatusNotModified)
|
|
|
|
}
|
|
|
|
|
|
|
|
func headObject(t *testing.T, tc *handlerContext, bktName, objName string, headers map[string]string, status int) {
|
|
|
|
w, r := prepareTestRequest(tc, bktName, objName, nil)
|
|
|
|
|
|
|
|
for key, val := range headers {
|
|
|
|
r.Header.Set(key, val)
|
|
|
|
}
|
2022-06-01 14:50:30 +00:00
|
|
|
|
|
|
|
tc.Handler().HeadObjectHandler(w, r)
|
2022-10-04 08:31:09 +00:00
|
|
|
assertStatus(t, w, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidAccessThroughCache(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
bktName, objName := "bucket-for-cache", "obj-for-cache"
|
|
|
|
createBucketAndObject(tc, bktName, objName)
|
|
|
|
|
|
|
|
headObject(t, tc, bktName, objName, nil, http.StatusOK)
|
|
|
|
|
|
|
|
w, r := prepareTestRequest(tc, bktName, objName, nil)
|
|
|
|
tc.Handler().HeadObjectHandler(w, r.WithContext(context.WithValue(r.Context(), api.BoxData, newTestAccessBox(t, nil))))
|
|
|
|
assertStatus(t, w, http.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestAccessBox(t *testing.T, key *keys.PrivateKey) *accessbox.Box {
|
|
|
|
var err error
|
|
|
|
if key == nil {
|
|
|
|
key, err = keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var btoken bearer.Token
|
|
|
|
btoken.SetEACLTable(*eacl.NewTable())
|
|
|
|
err = btoken.Sign(key.PrivateKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &accessbox.Box{
|
|
|
|
Gate: &accessbox.GateData{
|
|
|
|
BearerToken: &btoken,
|
|
|
|
},
|
|
|
|
}
|
2022-06-01 14:50:30 +00:00
|
|
|
}
|