github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/auth/credentials_test.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package auth 19 20 import ( 21 "encoding/json" 22 "testing" 23 "time" 24 ) 25 26 func TestExpToInt64(t *testing.T) { 27 testCases := []struct { 28 exp interface{} 29 expectedFailure bool 30 }{ 31 {"", true}, 32 {"-1", true}, 33 {"1574812326", false}, 34 {1574812326, false}, 35 {int64(1574812326), false}, 36 {int(1574812326), false}, 37 {uint(1574812326), false}, 38 {uint64(1574812326), false}, 39 {json.Number("1574812326"), false}, 40 {1574812326.000, false}, 41 {time.Duration(3) * time.Minute, false}, 42 } 43 44 for _, testCase := range testCases { 45 testCase := testCase 46 t.Run("", func(t *testing.T) { 47 _, err := ExpToInt64(testCase.exp) 48 if err != nil && !testCase.expectedFailure { 49 t.Errorf("Expected success but got failure %s", err) 50 } 51 if err == nil && testCase.expectedFailure { 52 t.Error("Expected failure but got success") 53 } 54 }) 55 } 56 } 57 58 func TestIsAccessKeyValid(t *testing.T) { 59 testCases := []struct { 60 accessKey string 61 expectedResult bool 62 }{ 63 {alphaNumericTable[:accessKeyMinLen], true}, 64 {alphaNumericTable[:accessKeyMinLen+1], true}, 65 {alphaNumericTable[:accessKeyMinLen-1], false}, 66 } 67 68 for i, testCase := range testCases { 69 result := IsAccessKeyValid(testCase.accessKey) 70 if result != testCase.expectedResult { 71 t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 72 } 73 } 74 } 75 76 func TestIsSecretKeyValid(t *testing.T) { 77 testCases := []struct { 78 secretKey string 79 expectedResult bool 80 }{ 81 {alphaNumericTable[:secretKeyMinLen], true}, 82 {alphaNumericTable[:secretKeyMinLen+1], true}, 83 {alphaNumericTable[:secretKeyMinLen-1], false}, 84 } 85 86 for i, testCase := range testCases { 87 result := IsSecretKeyValid(testCase.secretKey) 88 if result != testCase.expectedResult { 89 t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 90 } 91 } 92 } 93 94 func TestGetNewCredentials(t *testing.T) { 95 cred, err := GetNewCredentials() 96 if err != nil { 97 t.Fatalf("Failed to get a new credential") 98 } 99 if !cred.IsValid() { 100 t.Fatalf("Failed to get new valid credential") 101 } 102 if len(cred.AccessKey) != accessKeyMaxLen { 103 t.Fatalf("access key length: expected: %v, got: %v", secretKeyMaxLen, len(cred.AccessKey)) 104 } 105 if len(cred.SecretKey) != secretKeyMaxLen { 106 t.Fatalf("secret key length: expected: %v, got: %v", secretKeyMaxLen, len(cred.SecretKey)) 107 } 108 } 109 110 func TestCreateCredentials(t *testing.T) { 111 testCases := []struct { 112 accessKey string 113 secretKey string 114 valid bool 115 expectedErr error 116 }{ 117 // Valid access and secret keys with minimum length. 118 {alphaNumericTable[:accessKeyMinLen], alphaNumericTable[:secretKeyMinLen], true, nil}, 119 // Valid access and/or secret keys are longer than minimum length. 120 {alphaNumericTable[:accessKeyMinLen+1], alphaNumericTable[:secretKeyMinLen+1], true, nil}, 121 // Smaller access key. 122 {alphaNumericTable[:accessKeyMinLen-1], alphaNumericTable[:secretKeyMinLen], false, ErrInvalidAccessKeyLength}, 123 // Smaller secret key. 124 {alphaNumericTable[:accessKeyMinLen], alphaNumericTable[:secretKeyMinLen-1], false, ErrInvalidSecretKeyLength}, 125 } 126 127 for i, testCase := range testCases { 128 cred, err := CreateCredentials(testCase.accessKey, testCase.secretKey) 129 130 if err != nil { 131 if testCase.expectedErr == nil { 132 t.Fatalf("test %v: error: expected = <nil>, got = %v", i+1, err) 133 } 134 if testCase.expectedErr.Error() != err.Error() { 135 t.Fatalf("test %v: error: expected = %v, got = %v", i+1, testCase.expectedErr, err) 136 } 137 } else { 138 if testCase.expectedErr != nil { 139 t.Fatalf("test %v: error: expected = %v, got = <nil>", i+1, testCase.expectedErr) 140 } 141 if !cred.IsValid() { 142 t.Fatalf("test %v: got invalid credentials", i+1) 143 } 144 } 145 } 146 } 147 148 func TestCredentialsEqual(t *testing.T) { 149 cred, err := GetNewCredentials() 150 if err != nil { 151 t.Fatalf("Failed to get a new credential: %v", err) 152 } 153 cred2, err := GetNewCredentials() 154 if err != nil { 155 t.Fatalf("Failed to get a new credential: %v", err) 156 } 157 testCases := []struct { 158 cred Credentials 159 ccred Credentials 160 expectedResult bool 161 }{ 162 // Same Credentialss. 163 {cred, cred, true}, 164 // Empty credentials to compare. 165 {cred, Credentials{}, false}, 166 // Empty credentials. 167 {Credentials{}, cred, false}, 168 // Two different credentialss 169 {cred, cred2, false}, 170 // Access key is different in credentials to compare. 171 {cred, Credentials{AccessKey: "myuser", SecretKey: cred.SecretKey}, false}, 172 // Secret key is different in credentials to compare. 173 {cred, Credentials{AccessKey: cred.AccessKey, SecretKey: "mypassword"}, false}, 174 } 175 176 for i, testCase := range testCases { 177 result := testCase.cred.Equal(testCase.ccred) 178 if result != testCase.expectedResult { 179 t.Fatalf("test %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) 180 } 181 } 182 }