2021-08-06 13:05:57 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-06-01 13:45:28 +00:00
|
|
|
"bytes"
|
2021-08-30 19:44:53 +00:00
|
|
|
"encoding/json"
|
2022-01-17 12:36:10 +00:00
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2023-06-01 13:45:28 +00:00
|
|
|
"strconv"
|
2022-08-17 11:18:36 +00:00
|
|
|
"strings"
|
2021-08-06 13:05:57 +00:00
|
|
|
"testing"
|
2021-08-30 19:44:53 +00:00
|
|
|
"time"
|
2021-08-06 13:05:57 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2021-08-06 13:05:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckBucketName(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{name: "bucket"},
|
|
|
|
{name: "2bucket"},
|
|
|
|
{name: "buc.ket"},
|
|
|
|
{name: "buc-ket"},
|
|
|
|
{name: "abc"},
|
|
|
|
{name: "63aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
|
|
|
|
{name: "buc.-ket", err: true},
|
|
|
|
{name: "bucket.", err: true},
|
|
|
|
{name: ".bucket", err: true},
|
|
|
|
{name: "bucket.", err: true},
|
|
|
|
{name: "bucket-", err: true},
|
|
|
|
{name: "-bucket", err: true},
|
|
|
|
{name: "Bucket", err: true},
|
|
|
|
{name: "buc.-ket", err: true},
|
|
|
|
{name: "buc-.ket", err: true},
|
|
|
|
{name: "Bucket", err: true},
|
|
|
|
{name: "buc!ket", err: true},
|
|
|
|
{name: "buc_ket", err: true},
|
|
|
|
{name: "xn--bucket", err: true},
|
|
|
|
{name: "bucket-s3alias", err: true},
|
|
|
|
{name: "192.168.0.1", err: true},
|
|
|
|
{name: "as", err: true},
|
|
|
|
{name: "64aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", err: true},
|
|
|
|
} {
|
|
|
|
err := checkBucketName(tc.name)
|
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err, "bucket name: %s", tc.name)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, "bucket name: %s", tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
|
|
|
|
func TestCustomJSONMarshal(t *testing.T) {
|
|
|
|
data := []byte(`
|
|
|
|
{ "expiration": "2015-12-30T12:00:00.000Z",
|
|
|
|
"conditions": [
|
|
|
|
["content-length-range", 1048576, 10485760],
|
|
|
|
{"bucket": "bucketName"},
|
|
|
|
["starts-with", "$key", "user/user1/"]
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
|
|
|
|
parsedTime, err := time.Parse(time.RFC3339, "2015-12-30T12:00:00.000Z")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedPolicy := &postPolicy{
|
|
|
|
Expiration: parsedTime,
|
|
|
|
Conditions: []*policyCondition{
|
|
|
|
{
|
|
|
|
Matching: "content-length-range",
|
|
|
|
Key: "1048576",
|
|
|
|
Value: "10485760",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Matching: "eq",
|
|
|
|
Key: "bucket",
|
|
|
|
Value: "bucketName",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Matching: "starts-with",
|
|
|
|
Key: "key",
|
|
|
|
Value: "user/user1/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := &postPolicy{}
|
|
|
|
err = json.Unmarshal(data, policy)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, expectedPolicy, policy)
|
|
|
|
}
|
2022-01-17 12:36:10 +00:00
|
|
|
|
|
|
|
func TestEmptyPostPolicy(t *testing.T) {
|
|
|
|
r := &http.Request{
|
|
|
|
MultipartForm: &multipart.Form{
|
|
|
|
Value: map[string][]string{
|
|
|
|
"key": {"some-key"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
reqInfo := &api.ReqInfo{}
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
|
|
|
|
_, err := checkPostPolicy(r, reqInfo, metadata)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2022-08-17 11:18:36 +00:00
|
|
|
|
|
|
|
func TestPutObjectOverrideCopiesNumber(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
|
|
|
bktName, objName := "bucket-for-copies-number", "object-for-copies-number"
|
2022-10-04 08:31:09 +00:00
|
|
|
bktInfo := createTestBucket(tc, bktName)
|
2022-08-17 11:18:36 +00:00
|
|
|
|
2022-10-04 08:31:09 +00:00
|
|
|
w, r := prepareTestRequest(tc, bktName, objName, nil)
|
2022-12-20 08:38:58 +00:00
|
|
|
r.Header.Set(api.MetadataPrefix+strings.ToUpper(layer.AttributeFrostfsCopiesNumber), "1")
|
2022-08-17 11:18:36 +00:00
|
|
|
tc.Handler().PutObjectHandler(w, r)
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: objName,
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := tc.Layer().GetObjectInfo(tc.Context(), p)
|
|
|
|
require.NoError(t, err)
|
2022-12-20 08:38:58 +00:00
|
|
|
require.Equal(t, "1", objInfo.Headers[layer.AttributeFrostfsCopiesNumber])
|
2022-08-17 11:18:36 +00:00
|
|
|
}
|
2023-06-01 13:45:28 +00:00
|
|
|
|
|
|
|
func TestPutObjectWithNegativeContentLength(t *testing.T) {
|
|
|
|
tc := prepareHandlerContext(t)
|
|
|
|
|
|
|
|
bktName, objName := "bucket-for-put", "object-for-put"
|
|
|
|
createTestBucket(tc, bktName)
|
|
|
|
|
|
|
|
content := []byte("content")
|
|
|
|
w, r := prepareTestPayloadRequest(tc, bktName, objName, bytes.NewReader(content))
|
|
|
|
r.ContentLength = -1
|
|
|
|
tc.Handler().PutObjectHandler(w, r)
|
|
|
|
assertStatus(t, w, http.StatusOK)
|
|
|
|
|
|
|
|
w, r = prepareTestRequest(tc, bktName, objName, nil)
|
|
|
|
tc.Handler().HeadObjectHandler(w, r)
|
|
|
|
assertStatus(t, w, http.StatusOK)
|
|
|
|
require.Equal(t, strconv.Itoa(len(content)), w.Header().Get(api.ContentLength))
|
|
|
|
}
|