github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/config_test.go (about)

     1  // Copyright (c) 2015-2022 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 cmd
    19  
    20  import "testing"
    21  
    22  // Tests valid host URL functionality.
    23  func TestParseEnvURLStr(t *testing.T) {
    24  	testCases := []struct {
    25  		hostURL      string
    26  		accessKey    string
    27  		secretKey    string
    28  		sessionToken string
    29  		hostname     string
    30  		port         string
    31  	}{
    32  		{
    33  			hostURL:   "https://minio:minio1#23@localhost:9000",
    34  			accessKey: "minio",
    35  			secretKey: "minio1#23",
    36  			hostname:  "localhost",
    37  			port:      "9000",
    38  		},
    39  		{
    40  			hostURL:   "https://minio:minio123@@localhost:9000",
    41  			accessKey: "minio",
    42  			secretKey: "minio123@",
    43  			hostname:  "localhost",
    44  			port:      "9000",
    45  		},
    46  		{
    47  			hostURL:   "https://minio:minio@123@@localhost:9000",
    48  			accessKey: "minio",
    49  			secretKey: "minio@123@",
    50  			hostname:  "localhost",
    51  			port:      "9000",
    52  		},
    53  		{
    54  			hostURL:   "https://localhost:9000",
    55  			accessKey: "",
    56  			secretKey: "",
    57  			hostname:  "localhost",
    58  			port:      "9000",
    59  		},
    60  		{
    61  			hostURL:      "https://minio:minio123:token@localhost:9000",
    62  			accessKey:    "minio",
    63  			secretKey:    "minio123",
    64  			sessionToken: "token",
    65  			hostname:     "localhost",
    66  			port:         "9000",
    67  		},
    68  		{
    69  			hostURL:      "https://minio:minio@123:token@@localhost:9000",
    70  			accessKey:    "minio",
    71  			secretKey:    "minio@123",
    72  			sessionToken: "token@",
    73  			hostname:     "localhost",
    74  			port:         "9000",
    75  		},
    76  		{
    77  			hostURL:   "https://minio@localhost:9000",
    78  			accessKey: "minio",
    79  			hostname:  "localhost",
    80  			port:      "9000",
    81  		},
    82  		{
    83  			hostURL:   "https://minio:@localhost:9000",
    84  			accessKey: "minio",
    85  			hostname:  "localhost",
    86  			port:      "9000",
    87  		},
    88  		{
    89  			hostURL:  "https://:@localhost:9000",
    90  			hostname: "localhost",
    91  			port:     "9000",
    92  		},
    93  		{
    94  			hostURL:   "https://:minio123@localhost:9000",
    95  			hostname:  "localhost",
    96  			secretKey: "minio123",
    97  			port:      "9000",
    98  		},
    99  		{
   100  			hostURL:      "https://:minio123:token@localhost:9000",
   101  			hostname:     "localhost",
   102  			secretKey:    "minio123",
   103  			sessionToken: "token",
   104  			port:         "9000",
   105  		},
   106  		{
   107  			hostURL:   "https://:minio123:@localhost:9000",
   108  			hostname:  "localhost",
   109  			secretKey: "minio123",
   110  			port:      "9000",
   111  		},
   112  	}
   113  
   114  	for _, testCase := range testCases {
   115  		t.Run("", func(t *testing.T) {
   116  			url, ak, sk, token, err := parseEnvURLStr(testCase.hostURL)
   117  			if testCase.accessKey != ak {
   118  				t.Fatalf("Expected %s, got %s", testCase.accessKey, ak)
   119  			}
   120  			if testCase.secretKey != sk {
   121  				t.Fatalf("Expected %s, got %s", testCase.secretKey, sk)
   122  			}
   123  			if testCase.sessionToken != token {
   124  				t.Fatalf("Expected %s, got %s", testCase.sessionToken, token)
   125  			}
   126  			if testCase.hostname != url.Hostname() {
   127  				t.Fatalf("Expected %s, got %s", testCase.hostname, url.Hostname())
   128  			}
   129  			if testCase.port != url.Port() {
   130  				t.Fatalf("Expected %s, got %s", testCase.port, url.Port())
   131  			}
   132  			if err != nil {
   133  				t.Fatalf("Expected test to pass. Failed with err %s", err)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestParseEnvURLStrInvalid(t *testing.T) {
   140  	_, _, _, _, err := parseEnvURLStr("")
   141  	if err == nil {
   142  		t.Fatalf("Expected failure")
   143  	}
   144  }