storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/net/port_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 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 net 18 19 import ( 20 "testing" 21 ) 22 23 func TestPortString(t *testing.T) { 24 testCases := []struct { 25 port Port 26 expectedStr string 27 }{ 28 {Port(0), "0"}, 29 {Port(9000), "9000"}, 30 {Port(65535), "65535"}, 31 {Port(1024), "1024"}, 32 } 33 34 for i, testCase := range testCases { 35 str := testCase.port.String() 36 37 if str != testCase.expectedStr { 38 t.Fatalf("test %v: error: port: %v, got: %v", i+1, testCase.expectedStr, str) 39 } 40 } 41 } 42 43 func TestParsePort(t *testing.T) { 44 testCases := []struct { 45 s string 46 expectedPort Port 47 expectErr bool 48 }{ 49 {"0", Port(0), false}, 50 {"9000", Port(9000), false}, 51 {"65535", Port(65535), false}, 52 {"http", Port(80), false}, 53 {"https", Port(443), false}, 54 {"90000", Port(0), true}, 55 {"-10", Port(0), true}, 56 {"", Port(0), true}, 57 {" 1024", Port(0), true}, 58 } 59 60 for i, testCase := range testCases { 61 port, err := ParsePort(testCase.s) 62 expectErr := (err != nil) 63 64 if expectErr != testCase.expectErr { 65 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 66 } 67 68 if !testCase.expectErr { 69 if port != testCase.expectedPort { 70 t.Fatalf("test %v: error: port: %v, got: %v", i+1, testCase.expectedPort, port) 71 } 72 } 73 } 74 }