2021-06-24 11:10:00 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-08-01 16:52:09 +00:00
|
|
|
"bytes"
|
2023-07-10 06:59:12 +00:00
|
|
|
stderrors "errors"
|
2022-08-01 16:52:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-06-24 11:10:00 +00:00
|
|
|
"net/http"
|
2023-07-06 13:37:53 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2021-06-24 11:10:00 +00:00
|
|
|
"testing"
|
2021-07-02 16:05:43 +00:00
|
|
|
"time"
|
2021-06-24 11:10:00 +00:00
|
|
|
|
2023-07-06 13:37:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-06 13:37:53 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2021-06-24 11:10:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFetchRangeHeader(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
header string
|
|
|
|
expected *layer.RangeParams
|
|
|
|
fullSize uint64
|
|
|
|
err bool
|
|
|
|
}{
|
2021-08-06 09:59:28 +00:00
|
|
|
{header: "bytes=0-256", expected: &layer.RangeParams{Start: 0, End: 256}, fullSize: 257, err: false},
|
|
|
|
{header: "bytes=0-0", expected: &layer.RangeParams{Start: 0, End: 0}, fullSize: 1, err: false},
|
|
|
|
{header: "bytes=0-256", expected: &layer.RangeParams{Start: 0, End: 255}, fullSize: 256, err: false},
|
2021-06-24 11:10:00 +00:00
|
|
|
{header: "bytes=0-", expected: &layer.RangeParams{Start: 0, End: 99}, fullSize: 100, err: false},
|
|
|
|
{header: "bytes=-10", expected: &layer.RangeParams{Start: 90, End: 99}, fullSize: 100, err: false},
|
|
|
|
{header: "", err: false},
|
|
|
|
{header: "bytes=-1-256", err: true},
|
|
|
|
{header: "bytes=256-0", err: true},
|
|
|
|
{header: "bytes=string-0", err: true},
|
|
|
|
{header: "bytes=0-string", err: true},
|
|
|
|
{header: "bytes:0-256", err: true},
|
|
|
|
{header: "bytes:-", err: true},
|
2021-08-06 09:59:28 +00:00
|
|
|
{header: "bytes=0-0", fullSize: 0, err: true},
|
|
|
|
{header: "bytes=10-20", fullSize: 5, err: true},
|
2021-06-24 11:10:00 +00:00
|
|
|
} {
|
|
|
|
h := make(http.Header)
|
|
|
|
h.Add("Range", tc.header)
|
|
|
|
params, err := fetchRangeHeader(h, tc.fullSize)
|
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expected, params)
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 16:05:43 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func newInfo(etag string, created time.Time) *data.ObjectInfo {
|
|
|
|
return &data.ObjectInfo{
|
2021-07-02 16:05:43 +00:00
|
|
|
HashSum: etag,
|
|
|
|
Created: created,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreconditions(t *testing.T) {
|
|
|
|
today := time.Now()
|
|
|
|
yesterday := today.Add(-24 * time.Hour)
|
|
|
|
etag := "etag"
|
|
|
|
etag2 := "etag2"
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
2021-09-10 06:56:56 +00:00
|
|
|
info *data.ObjectInfo
|
2021-07-02 16:21:53 +00:00
|
|
|
args *conditionalArgs
|
2021-07-14 12:51:45 +00:00
|
|
|
expected error
|
2021-07-02 16:05:43 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no conditions",
|
2021-09-10 06:56:56 +00:00
|
|
|
info: new(data.ObjectInfo),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: new(conditionalArgs),
|
2021-07-14 12:51:45 +00:00
|
|
|
expected: nil,
|
2021-07-02 16:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IfMatch true",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfMatch: etag},
|
2021-07-14 12:51:45 +00:00
|
|
|
expected: nil,
|
2021-07-02 16:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IfMatch false",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfMatch: etag2},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrPreconditionFailed)},
|
2021-07-02 16:05:43 +00:00
|
|
|
{
|
|
|
|
name: "IfNoneMatch true",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfNoneMatch: etag2},
|
2021-07-14 12:51:45 +00:00
|
|
|
expected: nil},
|
2021-07-02 16:05:43 +00:00
|
|
|
{
|
|
|
|
name: "IfNoneMatch false",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfNoneMatch: etag},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrNotModified)},
|
2021-07-02 16:05:43 +00:00
|
|
|
{
|
|
|
|
name: "IfModifiedSince true",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfModifiedSince: &yesterday},
|
2021-07-14 12:51:45 +00:00
|
|
|
expected: nil},
|
2021-07-02 16:05:43 +00:00
|
|
|
{
|
|
|
|
name: "IfModifiedSince false",
|
|
|
|
info: newInfo(etag, yesterday),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfModifiedSince: &today},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrNotModified)},
|
2021-07-02 16:05:43 +00:00
|
|
|
{
|
|
|
|
name: "IfUnmodifiedSince true",
|
|
|
|
info: newInfo(etag, yesterday),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfUnmodifiedSince: &today},
|
2021-07-14 12:51:45 +00:00
|
|
|
expected: nil},
|
2021-07-02 16:05:43 +00:00
|
|
|
{
|
|
|
|
name: "IfUnmodifiedSince false",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfUnmodifiedSince: &yesterday},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrPreconditionFailed)},
|
2021-07-02 16:05:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
name: "IfMatch true, IfUnmodifiedSince false",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfMatch: etag, IfUnmodifiedSince: &yesterday},
|
2021-07-14 12:51:45 +00:00
|
|
|
expected: nil,
|
2021-07-02 16:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IfMatch false, IfUnmodifiedSince true",
|
|
|
|
info: newInfo(etag, yesterday),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfMatch: etag2, IfUnmodifiedSince: &today},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrPreconditionFailed),
|
2021-07-02 16:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IfNoneMatch false, IfModifiedSince true",
|
|
|
|
info: newInfo(etag, today),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfNoneMatch: etag, IfModifiedSince: &yesterday},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrNotModified),
|
2021-07-02 16:05:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IfNoneMatch true, IfModifiedSince false",
|
|
|
|
info: newInfo(etag, yesterday),
|
2021-07-02 16:21:53 +00:00
|
|
|
args: &conditionalArgs{IfNoneMatch: etag2, IfModifiedSince: &today},
|
2021-08-09 08:53:58 +00:00
|
|
|
expected: errors.GetAPIError(errors.ErrNotModified),
|
2021-07-02 16:05:43 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-10-27 15:15:33 +00:00
|
|
|
actual := checkPreconditions(tc.info, tc.args, false)
|
2023-07-10 06:59:12 +00:00
|
|
|
if tc.expected == nil {
|
|
|
|
require.NoError(t, actual)
|
|
|
|
} else {
|
|
|
|
require.True(t, stderrors.Is(actual, tc.expected), tc.expected, actual)
|
|
|
|
}
|
2021-07-02 16:05:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
func TestGetRange(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
|
|
|
bktName, objName := "bucket-for-range", "object-to-range"
|
2022-10-04 08:31:09 +00:00
|
|
|
createTestBucket(tc, bktName)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
content := "123456789abcdef"
|
2022-10-25 10:16:08 +00:00
|
|
|
putObjectContent(tc, bktName, objName, content)
|
2022-08-01 16:52:09 +00:00
|
|
|
|
|
|
|
full := getObjectRange(t, tc, bktName, objName, 0, len(content)-1)
|
|
|
|
require.Equal(t, content, string(full))
|
|
|
|
|
|
|
|
beginning := getObjectRange(t, tc, bktName, objName, 0, 3)
|
|
|
|
require.Equal(t, content[:4], string(beginning))
|
|
|
|
|
|
|
|
middle := getObjectRange(t, tc, bktName, objName, 5, 10)
|
|
|
|
require.Equal(t, "6789ab", string(middle))
|
|
|
|
|
|
|
|
end := getObjectRange(t, tc, bktName, objName, 10, 15)
|
|
|
|
require.Equal(t, "bcdef", string(end))
|
|
|
|
}
|
|
|
|
|
2023-07-06 13:37:53 +00:00
|
|
|
func TestGetObject(t *testing.T) {
|
|
|
|
hc := prepareHandlerContext(t)
|
|
|
|
bktName, objName := "bucket", "obj"
|
|
|
|
bktInfo, objInfo := createVersionedBucketAndObject(hc.t, hc, bktName, objName)
|
|
|
|
|
2023-08-21 13:05:16 +00:00
|
|
|
putObject(hc, bktName, objName)
|
2023-07-06 13:37:53 +00:00
|
|
|
|
|
|
|
checkFound(hc.t, hc, bktName, objName, objInfo.VersionID())
|
|
|
|
checkFound(hc.t, hc, bktName, objName, emptyVersion)
|
|
|
|
|
|
|
|
addr := getAddressOfLastVersion(hc, bktInfo, objName)
|
2023-08-16 14:08:12 +00:00
|
|
|
hc.tp.SetObjectError(addr, &apistatus.ObjectNotFound{})
|
|
|
|
hc.tp.SetObjectError(objInfo.Address(), &apistatus.ObjectNotFound{})
|
2023-07-06 13:37:53 +00:00
|
|
|
|
2023-08-29 09:27:50 +00:00
|
|
|
getObjectAssertS3Error(hc, bktName, objName, objInfo.VersionID(), errors.ErrNoSuchVersion)
|
|
|
|
getObjectAssertS3Error(hc, bktName, objName, emptyVersion, errors.ErrNoSuchKey)
|
2023-07-06 13:37:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 08:52:07 +00:00
|
|
|
func TestGetObjectEnabledMD5(t *testing.T) {
|
|
|
|
hc := prepareHandlerContext(t)
|
|
|
|
bktName, objName := "bucket", "obj"
|
|
|
|
_, objInfo := createBucketAndObject(hc, bktName, objName)
|
|
|
|
|
|
|
|
_, headers := getObject(hc, bktName, objName)
|
2023-10-27 15:15:33 +00:00
|
|
|
require.Equal(t, data.Quote(objInfo.HashSum), headers.Get(api.ETag))
|
2023-10-02 08:52:07 +00:00
|
|
|
|
|
|
|
hc.config.md5Enabled = true
|
|
|
|
_, headers = getObject(hc, bktName, objName)
|
2023-10-27 15:15:33 +00:00
|
|
|
require.Equal(t, data.Quote(objInfo.MD5Sum), headers.Get(api.ETag))
|
2023-10-02 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 09:17:16 +00:00
|
|
|
func putObjectContent(hc *handlerContext, bktName, objName, content string) http.Header {
|
2022-08-01 16:52:09 +00:00
|
|
|
body := bytes.NewReader([]byte(content))
|
2022-10-25 10:16:08 +00:00
|
|
|
w, r := prepareTestPayloadRequest(hc, bktName, objName, body)
|
|
|
|
hc.Handler().PutObjectHandler(w, r)
|
|
|
|
assertStatus(hc.t, w, http.StatusOK)
|
2024-01-19 09:17:16 +00:00
|
|
|
return w.Result().Header
|
2022-08-01 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getObjectRange(t *testing.T, tc *handlerContext, bktName, objName string, start, end int) []byte {
|
2022-10-04 08:31:09 +00:00
|
|
|
w, r := prepareTestRequest(tc, bktName, objName, nil)
|
2022-08-01 16:52:09 +00:00
|
|
|
r.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
|
|
|
|
tc.Handler().GetObjectHandler(w, r)
|
|
|
|
assertStatus(t, w, http.StatusPartialContent)
|
|
|
|
content, err := io.ReadAll(w.Result().Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return content
|
|
|
|
}
|
2023-07-06 13:37:53 +00:00
|
|
|
|
2023-08-29 09:27:50 +00:00
|
|
|
func getObjectAssertS3Error(hc *handlerContext, bktName, objName, version string, code errors.ErrorCode) {
|
2023-07-12 09:08:56 +00:00
|
|
|
w := getObjectBaseResponse(hc, bktName, objName, version)
|
2023-08-29 09:27:50 +00:00
|
|
|
assertS3Error(hc.t, w, errors.GetAPIError(code))
|
2023-07-06 13:37:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 09:08:56 +00:00
|
|
|
func getObjectBaseResponse(hc *handlerContext, bktName, objName, version string) *httptest.ResponseRecorder {
|
2023-07-06 13:37:53 +00:00
|
|
|
query := make(url.Values)
|
|
|
|
query.Add(api.QueryVersionID, version)
|
|
|
|
|
|
|
|
w, r := prepareTestFullRequest(hc, bktName, objName, query, nil)
|
|
|
|
hc.Handler().GetObjectHandler(w, r)
|
|
|
|
return w
|
|
|
|
}
|