From 8deddb82f4249f85c926e49f63309cec0388f468 Mon Sep 17 00:00:00 2001 From: karthic rao Date: Fri, 29 Apr 2016 08:31:11 +0530 Subject: [PATCH] Cleanup: Moving IsValidLocationContraint to handler utils --- handler-utils.go | 54 +++++++++++++++++++++++++++++++++++ handler-utils_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++ object-utils.go | 34 ---------------------- object-utils_test.go | 45 ----------------------------- 4 files changed, 120 insertions(+), 79 deletions(-) create mode 100644 handler-utils.go create mode 100644 handler-utils_test.go diff --git a/handler-utils.go b/handler-utils.go new file mode 100644 index 000000000..0a3102559 --- /dev/null +++ b/handler-utils.go @@ -0,0 +1,54 @@ +/* + * Minio Cloud Storage, (C) 2015, 2016 Minio, Inc. + * + * 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. + */ + +package main + +import ( + "io" +) + +// validates location constraint from the request body. +// the location value in the request body should match the Region in serverConfig. +// other values of location are not accepted. +// make bucket fails in such cases. +func isValidLocationContraint(reqBody io.Reader, serverRegion string) APIErrorCode { + var locationContraint createBucketLocationConfiguration + var errCode APIErrorCode + errCode = ErrNone + e := xmlDecoder(reqBody, &locationContraint) + if e != nil { + if e == io.EOF { + // Do nothing. + // failed due to empty body. The location will be set to default value from the serverConfig. + // this is valid. + errCode = ErrNone + } else { + // Failed due to malformed configuration. + errCode = ErrMalformedXML + //writeErrorResponse(w, r, ErrMalformedXML, r.URL.Path) + } + } else { + // Region obtained from the body. + // It should be equal to Region in serverConfig. + // Else ErrInvalidRegion returned. + // For empty value location will be to set to default value from the serverConfig. + if locationContraint.Location != "" && serverRegion != locationContraint.Location { + //writeErrorResponse(w, r, ErrInvalidRegion, r.URL.Path) + errCode = ErrInvalidRegion + } + } + return errCode +} diff --git a/handler-utils_test.go b/handler-utils_test.go new file mode 100644 index 000000000..0b5ac1cb7 --- /dev/null +++ b/handler-utils_test.go @@ -0,0 +1,66 @@ +/* + * Minio Cloud Storage, (C) 2015, 2016 Minio, Inc. + * + * 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. + */ + +package main + +import ( + "bytes" + "encoding/xml" + "io/ioutil" + "net/http" + "testing" +) + +// Tests validate bucket LocationConstraint. +func TestIsValidLocationContraint(t *testing.T) { + // 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) + return req, nil + } + + testCases := []struct { + locationForInputRequest string + serverConfigRegion string + expectedCode APIErrorCode + }{ + // Test case - 1. + {"us-east-1", "us-east-1", ErrNone}, + // Test case - 2. + // In case of empty request body ErrNone is returned. + {"", "us-east-1", ErrNone}, + // Test case - 3. + {"eu-central-1", "us-east-1", ErrInvalidRegion}, + } + 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) + } + actualCode := isValidLocationContraint(inputRequest.Body, testCase.serverConfigRegion) + if testCase.expectedCode != actualCode { + t.Errorf("Test %d: Expected the APIErrCode to be %d, but instead found %d", i+1, testCase.expectedCode, actualCode) + } + } +} diff --git a/object-utils.go b/object-utils.go index 88544047c..63d3e0627 100644 --- a/object-utils.go +++ b/object-utils.go @@ -17,7 +17,6 @@ package main import ( - "io" "regexp" "strings" "unicode/utf8" @@ -97,36 +96,3 @@ func retainSlash(s string) string { func pathJoin(s1 string, s2 string) string { return retainSlash(s1) + s2 } - -// validates location constraint from the request body. -// the location value in the request body should match the Region in serverConfig. -// other values of location are not accepted. -// make bucket fails in such cases. -func isValidLocationContraint(reqBody io.Reader, serverRegion string) APIErrorCode { - var locationContraint createBucketLocationConfiguration - var errCode APIErrorCode - errCode = ErrNone - e := xmlDecoder(reqBody, &locationContraint) - if e != nil { - if e == io.EOF { - // Do nothing. - // failed due to empty body. The location will be set to default value from the serverConfig. - // this is valid. - errCode = ErrNone - } else { - // Failed due to malformed configuration. - errCode = ErrMalformedXML - //writeErrorResponse(w, r, ErrMalformedXML, r.URL.Path) - } - } else { - // Region obtained from the body. - // It should be equal to Region in serverConfig. - // Else ErrInvalidRegion returned. - // For empty value location will be to set to default value from the serverConfig. - if locationContraint.Location != "" && serverRegion != locationContraint.Location { - //writeErrorResponse(w, r, ErrInvalidRegion, r.URL.Path) - errCode = ErrInvalidRegion - } - } - return errCode -} diff --git a/object-utils_test.go b/object-utils_test.go index 5561c5f6a..759f85777 100644 --- a/object-utils_test.go +++ b/object-utils_test.go @@ -17,10 +17,6 @@ package main import ( - "bytes" - "encoding/xml" - "io/ioutil" - "net/http" "testing" ) @@ -107,44 +103,3 @@ func TestIsValidObjectName(t *testing.T) { } } } - -// Tests validate bucket LocationConstraint. -func TestIsValidLocationContraint(t *testing.T) { - // 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) - return req, nil - } - - testCases := []struct { - locationForInputRequest string - serverConfigRegion string - expectedCode APIErrorCode - }{ - // Test case - 1. - {"us-east-1", "us-east-1", ErrNone}, - // Test case - 2. - // In case of empty request body ErrNone is returned. - {"", "us-east-1", ErrNone}, - // Test case - 3. - {"eu-central-1", "us-east-1", ErrInvalidRegion}, - } - 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) - } - actualCode := isValidLocationContraint(inputRequest.Body, testCase.serverConfigRegion) - if testCase.expectedCode != actualCode { - t.Errorf("Test %d: Expected the APIErrCode to be %d, but instead found %d", i+1, testCase.expectedCode, actualCode) - } - } -}