storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/auth/credentials_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package auth
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestExpToInt64(t *testing.T) {
    26  	testCases := []struct {
    27  		exp             interface{}
    28  		expectedFailure bool
    29  	}{
    30  		{"", true},
    31  		{"-1", true},
    32  		{"1574812326", false},
    33  		{1574812326, false},
    34  		{int64(1574812326), false},
    35  		{int(1574812326), false},
    36  		{uint(1574812326), false},
    37  		{uint64(1574812326), false},
    38  		{json.Number("1574812326"), false},
    39  		{1574812326.000, false},
    40  		{time.Duration(3) * time.Minute, false},
    41  	}
    42  
    43  	for _, testCase := range testCases {
    44  		testCase := testCase
    45  		t.Run("", func(t *testing.T) {
    46  			_, err := ExpToInt64(testCase.exp)
    47  			if err != nil && !testCase.expectedFailure {
    48  				t.Errorf("Expected success but got failure %s", err)
    49  			}
    50  			if err == nil && testCase.expectedFailure {
    51  				t.Error("Expected failure but got success")
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestIsAccessKeyValid(t *testing.T) {
    58  	testCases := []struct {
    59  		accessKey      string
    60  		expectedResult bool
    61  	}{
    62  		{alphaNumericTable[:accessKeyMinLen], true},
    63  		{alphaNumericTable[:accessKeyMinLen+1], true},
    64  		{alphaNumericTable[:accessKeyMinLen-1], false},
    65  	}
    66  
    67  	for i, testCase := range testCases {
    68  		result := IsAccessKeyValid(testCase.accessKey)
    69  		if result != testCase.expectedResult {
    70  			t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    71  		}
    72  	}
    73  }
    74  
    75  func TestIsSecretKeyValid(t *testing.T) {
    76  	testCases := []struct {
    77  		secretKey      string
    78  		expectedResult bool
    79  	}{
    80  		{alphaNumericTable[:secretKeyMinLen], true},
    81  		{alphaNumericTable[:secretKeyMinLen+1], true},
    82  		{alphaNumericTable[:secretKeyMinLen-1], false},
    83  	}
    84  
    85  	for i, testCase := range testCases {
    86  		result := IsSecretKeyValid(testCase.secretKey)
    87  		if result != testCase.expectedResult {
    88  			t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    89  		}
    90  	}
    91  }
    92  
    93  func TestGetNewCredentials(t *testing.T) {
    94  	cred, err := GetNewCredentials()
    95  	if err != nil {
    96  		t.Fatalf("Failed to get a new credential")
    97  	}
    98  	if !cred.IsValid() {
    99  		t.Fatalf("Failed to get new valid credential")
   100  	}
   101  	if len(cred.AccessKey) != accessKeyMaxLen {
   102  		t.Fatalf("access key length: expected: %v, got: %v", secretKeyMaxLen, len(cred.AccessKey))
   103  	}
   104  	if len(cred.SecretKey) != secretKeyMaxLen {
   105  		t.Fatalf("secret key length: expected: %v, got: %v", secretKeyMaxLen, len(cred.SecretKey))
   106  	}
   107  }
   108  
   109  func TestCreateCredentials(t *testing.T) {
   110  	testCases := []struct {
   111  		accessKey   string
   112  		secretKey   string
   113  		valid       bool
   114  		expectedErr error
   115  	}{
   116  		// Valid access and secret keys with minimum length.
   117  		{alphaNumericTable[:accessKeyMinLen], alphaNumericTable[:secretKeyMinLen], true, nil},
   118  		// Valid access and/or secret keys are longer than minimum length.
   119  		{alphaNumericTable[:accessKeyMinLen+1], alphaNumericTable[:secretKeyMinLen+1], true, nil},
   120  		// Smaller access key.
   121  		{alphaNumericTable[:accessKeyMinLen-1], alphaNumericTable[:secretKeyMinLen], false, ErrInvalidAccessKeyLength},
   122  		// Smaller secret key.
   123  		{alphaNumericTable[:accessKeyMinLen], alphaNumericTable[:secretKeyMinLen-1], false, ErrInvalidSecretKeyLength},
   124  	}
   125  
   126  	for i, testCase := range testCases {
   127  		cred, err := CreateCredentials(testCase.accessKey, testCase.secretKey)
   128  
   129  		if err != nil {
   130  			if testCase.expectedErr == nil {
   131  				t.Fatalf("test %v: error: expected = <nil>, got = %v", i+1, err)
   132  			}
   133  			if testCase.expectedErr.Error() != err.Error() {
   134  				t.Fatalf("test %v: error: expected = %v, got = %v", i+1, testCase.expectedErr, err)
   135  			}
   136  		} else {
   137  			if testCase.expectedErr != nil {
   138  				t.Fatalf("test %v: error: expected = %v, got = <nil>", i+1, testCase.expectedErr)
   139  			}
   140  			if !cred.IsValid() {
   141  				t.Fatalf("test %v: got invalid credentials", i+1)
   142  			}
   143  		}
   144  	}
   145  }
   146  
   147  func TestCredentialsEqual(t *testing.T) {
   148  	cred, err := GetNewCredentials()
   149  	if err != nil {
   150  		t.Fatalf("Failed to get a new credential")
   151  	}
   152  	cred2, err := GetNewCredentials()
   153  	if err != nil {
   154  		t.Fatalf("Failed to get a new credential")
   155  	}
   156  	testCases := []struct {
   157  		cred           Credentials
   158  		ccred          Credentials
   159  		expectedResult bool
   160  	}{
   161  		// Same Credentialss.
   162  		{cred, cred, true},
   163  		// Empty credentials to compare.
   164  		{cred, Credentials{}, false},
   165  		// Empty credentials.
   166  		{Credentials{}, cred, false},
   167  		// Two different credentialss
   168  		{cred, cred2, false},
   169  		// Access key is different in credentials to compare.
   170  		{cred, Credentials{AccessKey: "myuser", SecretKey: cred.SecretKey}, false},
   171  		// Secret key is different in credentials to compare.
   172  		{cred, Credentials{AccessKey: cred.AccessKey, SecretKey: "mypassword"}, false},
   173  	}
   174  
   175  	for i, testCase := range testCases {
   176  		result := testCase.cred.Equal(testCase.ccred)
   177  		if result != testCase.expectedResult {
   178  			t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedResult, result)
   179  		}
   180  	}
   181  }