2022-03-01 15:07:15 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2022-06-01 14:50:30 +00:00
|
|
|
"io"
|
2022-03-01 15:07:15 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-06-15 12:17:29 +00:00
|
|
|
"net/url"
|
2022-06-01 14:50:30 +00:00
|
|
|
"strconv"
|
2022-03-01 15:07:15 +00:00
|
|
|
"testing"
|
2022-06-01 14:50:30 +00:00
|
|
|
"time"
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-03-04 13:07:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-03-01 15:07:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2022-03-04 13:07:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2022-03-01 15:07:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/resolver"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-06-01 14:50:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-06-24 12:39:30 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-06-29 14:43:52 +00:00
|
|
|
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
|
2022-03-01 15:07:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-04-07 14:36:44 +00:00
|
|
|
"go.uber.org/zap"
|
2022-03-01 15:07:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type handlerContext struct {
|
2022-06-24 12:39:30 +00:00
|
|
|
h *handler
|
|
|
|
tp *layer.TestNeoFS
|
|
|
|
context context.Context
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *handlerContext) Handler() *handler {
|
|
|
|
return hc.h
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:09:09 +00:00
|
|
|
func (hc *handlerContext) MockedPool() *layer.TestNeoFS {
|
2022-03-01 15:07:15 +00:00
|
|
|
return hc.tp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *handlerContext) Layer() layer.Client {
|
|
|
|
return hc.h.obj
|
|
|
|
}
|
|
|
|
|
2022-06-24 12:39:30 +00:00
|
|
|
func (hc *handlerContext) Context() context.Context {
|
|
|
|
return hc.context
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
func prepareHandlerContext(t *testing.T) *handlerContext {
|
|
|
|
key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-15 12:17:29 +00:00
|
|
|
l := zap.NewExample()
|
2022-06-06 11:09:09 +00:00
|
|
|
tp := layer.NewTestNeoFS()
|
2022-03-01 15:07:15 +00:00
|
|
|
|
2022-09-12 13:46:55 +00:00
|
|
|
testResolver := &resolver.Resolver{Name: "test_resolver"}
|
2022-06-27 09:08:26 +00:00
|
|
|
testResolver.SetResolveFunc(func(_ context.Context, name string) (cid.ID, error) {
|
2022-03-04 13:07:27 +00:00
|
|
|
return tp.ContainerID(name)
|
2022-03-01 15:07:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
layerCfg := &layer.Config{
|
2022-05-20 08:26:35 +00:00
|
|
|
Caches: layer.DefaultCachesConfigs(zap.NewExample()),
|
|
|
|
AnonKey: layer.AnonymousKey{Key: key},
|
|
|
|
Resolver: testResolver,
|
2022-05-31 08:12:53 +00:00
|
|
|
TreeService: layer.NewTreeService(),
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h := &handler{
|
|
|
|
log: l,
|
|
|
|
obj: layer.NewLayer(l, tp, layerCfg),
|
2022-08-10 18:54:24 +00:00
|
|
|
cfg: &Config{
|
|
|
|
TLSEnabled: true,
|
|
|
|
},
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &handlerContext{
|
2022-06-24 12:39:30 +00:00
|
|
|
h: h,
|
|
|
|
tp: tp,
|
|
|
|
context: context.Background(),
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
func createTestBucket(ctx context.Context, t *testing.T, h *handlerContext, bktName string) *data.BucketInfo {
|
2022-06-06 11:09:09 +00:00
|
|
|
_, err := h.MockedPool().CreateContainer(ctx, layer.PrmContainerCreate{
|
2022-06-29 14:43:52 +00:00
|
|
|
Creator: *usertest.ID(),
|
|
|
|
Name: bktName,
|
2022-03-04 13:07:27 +00:00
|
|
|
})
|
2022-03-01 15:07:15 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
bktInfo, err := h.Layer().GetBucketInfo(ctx, bktName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return bktInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
func createTestBucketWithLock(ctx context.Context, t *testing.T, h *handlerContext, bktName string, conf *data.ObjectLockConfiguration) *data.BucketInfo {
|
2022-06-06 11:09:09 +00:00
|
|
|
cnrID, err := h.MockedPool().CreateContainer(ctx, layer.PrmContainerCreate{
|
2022-06-29 14:43:52 +00:00
|
|
|
Creator: *usertest.ID(),
|
2022-03-04 13:07:27 +00:00
|
|
|
Name: bktName,
|
|
|
|
AdditionalAttributes: [][2]string{{layer.AttributeLockEnabled, "true"}},
|
|
|
|
})
|
2022-03-01 15:07:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var ownerID user.ID
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo := &data.BucketInfo{
|
2022-06-27 09:08:26 +00:00
|
|
|
CID: cnrID,
|
2022-03-18 13:04:09 +00:00
|
|
|
Name: bktName,
|
|
|
|
ObjectLockEnabled: true,
|
2022-05-25 17:25:43 +00:00
|
|
|
Owner: ownerID,
|
2022-03-18 13:04:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
sp := &layer.PutSettingsParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-03-01 15:07:15 +00:00
|
|
|
Settings: &data.BucketSettings{
|
2022-07-20 10:30:19 +00:00
|
|
|
Versioning: data.VersioningEnabled,
|
2022-03-01 15:07:15 +00:00
|
|
|
LockConfiguration: conf,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.Layer().PutBucketSettings(ctx, sp)
|
|
|
|
require.NoError(t, err)
|
2022-03-18 13:04:09 +00:00
|
|
|
|
|
|
|
return bktInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 12:17:29 +00:00
|
|
|
func createTestObject(ctx context.Context, t *testing.T, h *handlerContext, bktInfo *data.BucketInfo, objName string) *data.ObjectInfo {
|
2022-03-01 15:07:15 +00:00
|
|
|
content := make([]byte, 1024)
|
|
|
|
_, err := rand.Read(content)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-01 14:50:30 +00:00
|
|
|
header := map[string]string{
|
|
|
|
object.AttributeTimestamp: strconv.FormatInt(time.Now().UTC().Unix(), 10),
|
|
|
|
}
|
|
|
|
|
2022-06-15 12:17:29 +00:00
|
|
|
objInfo, err := h.Layer().PutObject(ctx, &layer.PutObjectParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: objName,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
Reader: bytes.NewReader(content),
|
2022-06-01 14:50:30 +00:00
|
|
|
Header: header,
|
2022-03-01 15:07:15 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-06-15 12:17:29 +00:00
|
|
|
|
|
|
|
return objInfo
|
2022-03-01 15:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareTestRequest(t *testing.T, bktName, objName string, body interface{}) (*httptest.ResponseRecorder, *http.Request) {
|
2022-06-15 12:17:29 +00:00
|
|
|
return prepareTestFullRequest(t, bktName, objName, make(url.Values), body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareTestFullRequest(t *testing.T, bktName, objName string, query url.Values, body interface{}) (*httptest.ResponseRecorder, *http.Request) {
|
2022-03-01 15:07:15 +00:00
|
|
|
rawBody, err := xml.Marshal(body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
return prepareTestRequestWithQuery(bktName, objName, query, rawBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareTestRequestWithQuery(bktName, objName string, query url.Values, body []byte) (*httptest.ResponseRecorder, *http.Request) {
|
2022-03-01 15:07:15 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-08-01 16:52:09 +00:00
|
|
|
r := httptest.NewRequest(http.MethodPut, defaultURL, bytes.NewReader(body))
|
2022-06-15 12:17:29 +00:00
|
|
|
r.URL.RawQuery = query.Encode()
|
2022-03-01 15:07:15 +00:00
|
|
|
|
|
|
|
reqInfo := api.NewReqInfo(w, r, api.ObjectRequest{Bucket: bktName, Object: objName})
|
|
|
|
r = r.WithContext(api.SetReqInfo(r.Context(), reqInfo))
|
|
|
|
|
|
|
|
return w, r
|
|
|
|
}
|
2022-06-01 14:50:30 +00:00
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
func prepareTestPayloadRequest(bktName, objName string, payload io.Reader) (*httptest.ResponseRecorder, *http.Request) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest(http.MethodPut, defaultURL, payload)
|
|
|
|
|
|
|
|
reqInfo := api.NewReqInfo(w, r, api.ObjectRequest{Bucket: bktName, Object: objName})
|
|
|
|
r = r.WithContext(api.SetReqInfo(r.Context(), reqInfo))
|
|
|
|
|
|
|
|
return w, r
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTestResponse(t *testing.T, response *httptest.ResponseRecorder, body interface{}) {
|
|
|
|
assertStatus(t, response, http.StatusOK)
|
|
|
|
err := xml.NewDecoder(response.Result().Body).Decode(body)
|
|
|
|
require.NoError(t, err)
|
2022-06-01 14:50:30 +00:00
|
|
|
}
|
2022-06-24 12:39:30 +00:00
|
|
|
|
|
|
|
func existInMockedNeoFS(tc *handlerContext, bktInfo *data.BucketInfo, objInfo *data.ObjectInfo) bool {
|
|
|
|
p := &layer.GetObjectParams{
|
|
|
|
BucketInfo: bktInfo,
|
|
|
|
ObjectInfo: objInfo,
|
|
|
|
Writer: io.Discard,
|
|
|
|
}
|
|
|
|
|
|
|
|
return tc.Layer().GetObject(tc.Context(), p) == nil
|
|
|
|
}
|
|
|
|
|
2022-08-22 08:02:58 +00:00
|
|
|
func listOIDsFromMockedNeoFS(t *testing.T, tc *handlerContext, bktName string) []oid.ID {
|
2022-06-24 12:39:30 +00:00
|
|
|
bktInfo, err := tc.Layer().GetBucketInfo(tc.Context(), bktName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-22 08:02:58 +00:00
|
|
|
return tc.MockedPool().AllObjects(bktInfo.CID)
|
2022-06-24 12:39:30 +00:00
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
func assertStatus(t *testing.T, w *httptest.ResponseRecorder, status int) {
|
|
|
|
if w.Code != status {
|
|
|
|
resp, err := io.ReadAll(w.Result().Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Failf(t, "unexpected status", "expected: %d, actual: %d, resp: '%s'", status, w.Code, string(resp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readResponse(t *testing.T, w *httptest.ResponseRecorder, status int, model interface{}) {
|
|
|
|
assertStatus(t, w, status)
|
|
|
|
err := xml.NewDecoder(w.Result().Body).Decode(model)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|