From 1297d36580a090e2dc5f1da7125a81454eac9fcf Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 21 May 2015 18:57:03 -0700 Subject: [PATCH] Fix a windows URL object to String() conversion, and add relevant tests --- pkg/client/auth.go | 37 ----------------- pkg/client/client_test.go | 84 +++++++++++++-------------------------- pkg/client/url.go | 2 +- 3 files changed, 28 insertions(+), 95 deletions(-) delete mode 100644 pkg/client/auth.go diff --git a/pkg/client/auth.go b/pkg/client/auth.go deleted file mode 100644 index 86d4e8e6..00000000 --- a/pkg/client/auth.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Minio Client (C) 2015 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 client - -import "regexp" - -// IsValidSecretKey - validate secret key -func IsValidSecretKey(secretAccessKey string) bool { - if secretAccessKey == "" { - return true - } - regex := regexp.MustCompile("^.{40}$") - return regex.MatchString(secretAccessKey) -} - -// IsValidAccessKey - validate access key -func IsValidAccessKey(accessKeyID string) bool { - if accessKeyID == "" { - return true - } - regex := regexp.MustCompile("^[A-Z0-9\\-\\.\\_\\~]{20}$") - return regex.MatchString(accessKeyID) -} diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index fc4d689c..1163a4ab 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -28,66 +28,36 @@ type MySuite struct{} var _ = Suite(&MySuite{}) -func (s *MySuite) TestAuthAccessKeyLength(c *C) { - // short - result := IsValidSecretKey("123456789012345678901234567890123456789") - c.Assert(result, Equals, false) +func (s *MySuite) TestURLParse(c *C) { + u, err := Parse("http://s3.example.com") + c.Assert(err, IsNil) + c.Assert(u.Scheme, Equals, "http") + c.Assert(u.Host, Equals, "s3.example.com") + c.Assert(u.Path, Equals, "") - // long - result = IsValidSecretKey("12345678901234567890123456789012345678901") - c.Assert(result, Equals, false) + u, err = Parse("http://s3.example.com/path/new") + c.Assert(err, IsNil) + c.Assert(u.Scheme, Equals, "http") + c.Assert(u.Host, Equals, "s3.example.com") + c.Assert(u.Path, Equals, "/path/new") - // 40 characters long - result = IsValidSecretKey("1234567890123456789012345678901234567890") - c.Assert(result, Equals, true) + u, err = Parse(":::://s3.example.com/path/new") + c.Assert(err, IsNil) + c.Assert(u.Scheme, Equals, "") + c.Assert(u.Host, Equals, "") + c.Assert(u.Path, Equals, ":::://s3.example.com/path/new") } -func (s *MySuite) TestValidAccessKeyLength(c *C) { - // short - result := IsValidAccessKey("1234567890123456789") - c.Assert(result, Equals, false) +func (s *MySuite) TestPathParse(c *C) { + u, err := Parse("path/test") + c.Assert(err, IsNil) + c.Assert(u.Scheme, Equals, "") + c.Assert(u.Host, Equals, "") + c.Assert(u.Path, Equals, "path/test") - // long - result = IsValidAccessKey("123456789012345678901") - c.Assert(result, Equals, false) - - // 40 characters long - result = IsValidAccessKey("12345678901234567890") - c.Assert(result, Equals, true) - - // alphanumberic characters long - result = IsValidAccessKey("ABCDEFGHIJ1234567890") - c.Assert(result, Equals, true) - - // alphanumberic characters long - result = IsValidAccessKey("A1B2C3D4E5F6G7H8I9J0") - c.Assert(result, Equals, true) - - // alphanumberic lower case characters long - result = IsValidAccessKey("a1b2c3d4e5f6g7h8i9j0") - c.Assert(result, Equals, false) - - // alphanumberic with - - result = IsValidAccessKey("A1B2C3D4-5F6G7H8I9J0") - c.Assert(result, Equals, true) - - // alphanumberic with . - result = IsValidAccessKey("A1B2C3D4E5F6G7H8I.J0") - c.Assert(result, Equals, true) - - // alphanumberic with _ - result = IsValidAccessKey("A1B2C3D4E_F6G7H8I9J0") - c.Assert(result, Equals, true) - - // alphanumberic with ~ - result = IsValidAccessKey("A1B2C3D4E~F6G7H8I9J0") - c.Assert(result, Equals, true) - - // with all classes - result = IsValidAccessKey("A1B2C3D4E~F.G_H8~9J0") - c.Assert(result, Equals, true) - - // with all classes and an extra invalid - result = IsValidAccessKey("A1B2$3D4E~F.G_H8~9J0") - c.Assert(result, Equals, false) + u, err = Parse("/path/test") + c.Assert(err, IsNil) + c.Assert(u.Scheme, Equals, "") + c.Assert(u.Host, Equals, "") + c.Assert(u.Path, Equals, "/path/test") } diff --git a/pkg/client/url.go b/pkg/client/url.go index 8eeb07b1..0ea6d7b7 100644 --- a/pkg/client/url.go +++ b/pkg/client/url.go @@ -119,7 +119,7 @@ func (u *URL) String() string { } switch runtime.GOOS { case "windows": - if u.Path != "" && u.Path[0] != '\\' && u.Host != "" { + if u.Path != "" && u.Path[0] != '\\' && u.Host != "" && u.Path[0] != '/' { buf.WriteByte('/') } buf.WriteString(strings.Replace(u.Path, "\\", "/", -1))