2016-04-28 23:01:11 -04:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
2016-04-28 23:01:11 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-04-28 23:01:11 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-07-22 23:31:45 -04:00
|
|
|
"reflect"
|
2017-03-13 17:41:13 -04:00
|
|
|
"strings"
|
2016-04-28 23:01:11 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tests validate bucket LocationConstraint.
|
|
|
|
func TestIsValidLocationContraint(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
path, err := newTestConfig(globalMinioDefaultRegion)
|
2016-07-19 00:20:17 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
2016-07-26 03:01:35 -04:00
|
|
|
defer removeAll(path)
|
2016-07-19 00:20:17 -04:00
|
|
|
|
2016-09-20 13:40:46 -04:00
|
|
|
// Test with corrupted XML
|
|
|
|
malformedReq := &http.Request{
|
|
|
|
Body: ioutil.NopCloser(bytes.NewBuffer([]byte("<>"))),
|
|
|
|
ContentLength: int64(len("<>")),
|
|
|
|
}
|
|
|
|
if err := isValidLocationConstraint(malformedReq); err != ErrMalformedXML {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:01:11 -04:00
|
|
|
// generates the input request with XML bucket configuration set to the request body.
|
|
|
|
createExpectedRequest := func(req *http.Request, location string) (*http.Request, error) {
|
|
|
|
createBucketConfig := createBucketLocationConfiguration{}
|
|
|
|
createBucketConfig.Location = location
|
|
|
|
var createBucketConfigBytes []byte
|
|
|
|
createBucketConfigBytes, e := xml.Marshal(createBucketConfig)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
createBucketConfigBuffer := bytes.NewBuffer(createBucketConfigBytes)
|
|
|
|
req.Body = ioutil.NopCloser(createBucketConfigBuffer)
|
2016-07-19 00:20:17 -04:00
|
|
|
req.ContentLength = int64(createBucketConfigBuffer.Len())
|
2016-04-28 23:01:11 -04:00
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
locationForInputRequest string
|
|
|
|
serverConfigRegion string
|
|
|
|
expectedCode APIErrorCode
|
|
|
|
}{
|
|
|
|
// Test case - 1.
|
2017-01-18 15:24:34 -05:00
|
|
|
{globalMinioDefaultRegion, globalMinioDefaultRegion, ErrNone},
|
2016-04-28 23:01:11 -04:00
|
|
|
// Test case - 2.
|
|
|
|
// In case of empty request body ErrNone is returned.
|
2017-01-18 15:24:34 -05:00
|
|
|
{"", globalMinioDefaultRegion, ErrNone},
|
2016-04-28 23:01:11 -04:00
|
|
|
// Test case - 3.
|
2017-01-18 15:24:34 -05:00
|
|
|
{"eu-central-1", globalMinioDefaultRegion, ErrInvalidRegion},
|
2016-04-28 23:01:11 -04:00
|
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
inputRequest, e := createExpectedRequest(&http.Request{}, testCase.locationForInputRequest)
|
|
|
|
if e != nil {
|
|
|
|
t.Fatalf("Test %d: Failed to Marshal bucket configuration", i+1)
|
|
|
|
}
|
2016-07-19 00:20:17 -04:00
|
|
|
serverConfig.SetRegion(testCase.serverConfigRegion)
|
|
|
|
actualCode := isValidLocationConstraint(inputRequest)
|
2016-04-28 23:01:11 -04:00
|
|
|
if testCase.expectedCode != actualCode {
|
|
|
|
t.Errorf("Test %d: Expected the APIErrCode to be %d, but instead found %d", i+1, testCase.expectedCode, actualCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 23:31:45 -04:00
|
|
|
|
2017-03-13 17:41:13 -04:00
|
|
|
// Test validate form field size.
|
|
|
|
func TestValidateFormFieldSize(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
header http.Header
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
// Empty header returns error as nil,
|
|
|
|
{
|
|
|
|
header: nil,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
// Valid header returns error as nil.
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"Content-Type": []string{"image/png"},
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
// Invalid header value > maxFormFieldSize+1
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"Garbage": []string{strings.Repeat("a", int(maxFormFieldSize)+1)},
|
|
|
|
},
|
|
|
|
err: errSizeUnexpected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run validate form field size check under all test cases.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
err := validateFormFieldSize(testCase.header)
|
|
|
|
if err != nil {
|
|
|
|
if errorCause(err).Error() != testCase.err.Error() {
|
|
|
|
t.Errorf("Test %d: Expected error %s, got %s", i+1, testCase.err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 23:31:45 -04:00
|
|
|
// Tests validate metadata extraction from http headers.
|
|
|
|
func TestExtractMetadataHeaders(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
header http.Header
|
|
|
|
metadata map[string]string
|
|
|
|
}{
|
|
|
|
// Validate if there a known 'content-type'.
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"Content-Type": []string{"image/png"},
|
|
|
|
},
|
|
|
|
metadata: map[string]string{
|
|
|
|
"content-type": "image/png",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Validate if there are no keys to extract.
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"test-1": []string{"123"},
|
|
|
|
},
|
|
|
|
metadata: map[string]string{},
|
|
|
|
},
|
2016-09-20 13:40:46 -04:00
|
|
|
// Validate if there are no keys to extract.
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"X-Amz-Meta-Appid": []string{"amz-meta"},
|
|
|
|
"X-Minio-Meta-Appid": []string{"minio-meta"},
|
|
|
|
},
|
|
|
|
metadata: map[string]string{
|
|
|
|
"X-Amz-Meta-Appid": "amz-meta",
|
|
|
|
"X-Minio-Meta-Appid": "minio-meta"},
|
|
|
|
},
|
2017-03-13 17:41:13 -04:00
|
|
|
// Empty header input returns empty metadata.
|
|
|
|
{
|
|
|
|
header: nil,
|
|
|
|
metadata: nil,
|
|
|
|
},
|
2016-07-22 23:31:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if the extracting headers.
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
metadata := extractMetadataFromHeader(testCase.header)
|
|
|
|
if !reflect.DeepEqual(metadata, testCase.metadata) {
|
|
|
|
t.Fatalf("Test %d failed: Expected \"%#v\", got \"%#v\"", i+1, testCase.metadata, metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|