2016-12-27 11:28:10 -05:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-12-27 11:28:10 -05: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2017-08-12 22:25:43 -04:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
2016-12-27 11:28:10 -05:00
|
|
|
|
|
|
|
func testAuthenticate(authType string, t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
testPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-12-27 11:28:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(testPath)
|
2017-08-03 23:03:37 -04:00
|
|
|
// Create access and secret keys in length, 300 and 600
|
|
|
|
cred, err := getNewCredential(300, 600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get new credential, %v", err)
|
|
|
|
}
|
|
|
|
serverConfig.SetCredential(cred)
|
2016-12-27 11:28:10 -05:00
|
|
|
|
|
|
|
// Define test cases.
|
|
|
|
testCases := []struct {
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
2017-08-03 23:03:37 -04:00
|
|
|
// Access key (less than 5 chrs) too small.
|
|
|
|
{"user", cred.SecretKey, errInvalidAccessKeyLength},
|
|
|
|
// Secret key (less than 8 chrs) too small.
|
|
|
|
{cred.AccessKey, "pass", errInvalidSecretKeyLength},
|
2016-12-27 11:28:10 -05:00
|
|
|
// Authentication error.
|
|
|
|
{"myuser", "mypassword", errInvalidAccessKeyID},
|
|
|
|
// Authentication error.
|
2017-08-03 23:03:37 -04:00
|
|
|
{cred.AccessKey, "mypassword", errAuthentication},
|
2016-12-27 11:28:10 -05:00
|
|
|
// Success.
|
2017-08-03 23:03:37 -04:00
|
|
|
{cred.AccessKey, cred.SecretKey, nil},
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run tests.
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
var err error
|
|
|
|
if authType == "node" {
|
|
|
|
_, err = authenticateNode(testCase.accessKey, testCase.secretKey)
|
|
|
|
} else if authType == "web" {
|
|
|
|
_, err = authenticateWeb(testCase.accessKey, testCase.secretKey)
|
2017-07-24 15:46:37 -04:00
|
|
|
} else if authType == "url" {
|
|
|
|
_, err = authenticateURL(testCase.accessKey, testCase.secretKey)
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if testCase.expectedErr != nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("%+v: expected: %s, got: <nil>", testCase, testCase.expectedErr)
|
|
|
|
}
|
|
|
|
if testCase.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("%+v: expected: %s, got: %s", testCase, testCase.expectedErr, err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("%+v: expected: <nil>, got: %s", testCase, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:51:51 -05:00
|
|
|
func TestAuthenticateNode(t *testing.T) {
|
2016-12-27 11:28:10 -05:00
|
|
|
testAuthenticate("node", t)
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:51:51 -05:00
|
|
|
func TestAuthenticateWeb(t *testing.T) {
|
2016-12-27 11:28:10 -05:00
|
|
|
testAuthenticate("web", t)
|
|
|
|
}
|
2017-01-26 19:51:51 -05:00
|
|
|
|
2017-07-24 15:46:37 -04:00
|
|
|
func TestAuthenticateURL(t *testing.T) {
|
|
|
|
testAuthenticate("url", t)
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:51:51 -05:00
|
|
|
func BenchmarkAuthenticateNode(b *testing.B) {
|
|
|
|
testPath, err := newTestConfig(globalMinioDefaultRegion)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(testPath)
|
2017-01-26 19:51:51 -05:00
|
|
|
|
|
|
|
creds := serverConfig.GetCredential()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
authenticateNode(creds.AccessKey, creds.SecretKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAuthenticateWeb(b *testing.B) {
|
|
|
|
testPath, err := newTestConfig(globalMinioDefaultRegion)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(testPath)
|
2017-01-26 19:51:51 -05:00
|
|
|
|
|
|
|
creds := serverConfig.GetCredential()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
authenticateWeb(creds.AccessKey, creds.SecretKey)
|
|
|
|
}
|
|
|
|
}
|