istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/security/authz/model/util_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package model
    16  
    17  import (
    18  	"reflect"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  func TestConvertToPort(t *testing.T) {
    24  	testCases := []struct {
    25  		Name   string
    26  		V      string
    27  		Expect uint32
    28  		Err    string
    29  	}{
    30  		{
    31  			Name: "negative port",
    32  			V:    "-80",
    33  			Err:  "invalid port -80:",
    34  		},
    35  		{
    36  			Name: "invalid port",
    37  			V:    "xyz",
    38  			Err:  "invalid port xyz:",
    39  		},
    40  		{
    41  			Name: "port too large",
    42  			V:    "91234",
    43  			Err:  "invalid port 91234:",
    44  		},
    45  		{
    46  			Name:   "valid port",
    47  			V:      "443",
    48  			Expect: 443,
    49  		},
    50  	}
    51  
    52  	for _, tc := range testCases {
    53  		actual, err := convertToPort(tc.V)
    54  		if tc.Err != "" {
    55  			if err == nil {
    56  				t.Errorf("%s: expecting error %s but found no error", tc.Name, tc.Err)
    57  			} else if !strings.HasPrefix(err.Error(), tc.Err) {
    58  				t.Errorf("%s: expecting error %s, but got: %s", tc.Name, tc.Err, err.Error())
    59  			}
    60  		} else if tc.Expect != actual {
    61  			t.Errorf("%s: expecting %d, but got %d", tc.Name, tc.Expect, actual)
    62  		}
    63  	}
    64  }
    65  
    66  func TestExtractNameInBrackets(t *testing.T) {
    67  	cases := []struct {
    68  		s    string
    69  		want string
    70  		err  bool
    71  	}{
    72  		{s: "[good]", want: "good"},
    73  		{s: "[[good]]", want: "[good]"},
    74  		{s: "[]", want: ""},
    75  		{s: "[bad", err: true},
    76  		{s: "bad]", err: true},
    77  		{s: "bad", err: true},
    78  	}
    79  
    80  	for _, c := range cases {
    81  		t.Run(c.s, func(t *testing.T) {
    82  			s, err := extractNameInBrackets(c.s)
    83  			if s != c.want {
    84  				t.Errorf("want %s but found %s", c.want, s)
    85  			}
    86  			if c.err != (err != nil) {
    87  				t.Errorf("unexpected error: %v", err)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func TestExtractNameInNestedBrackets(t *testing.T) {
    94  	cases := []struct {
    95  		s    string
    96  		want []string
    97  		err  bool
    98  	}{
    99  		{s: "[good]", want: []string{"good"}},
   100  		{s: "[good][abc][xyz]", want: []string{"good", "abc", "xyz"}},
   101  		{s: "[]", want: []string{""}},
   102  		{s: "[[good]", want: []string{"[good"}},
   103  		{s: "[good]]", want: []string{"good]"}},
   104  		{s: "[[good]]", want: []string{"[good]"}},
   105  		{s: "x[bad]", err: true},
   106  		{s: "[bad", err: true},
   107  		{s: "bad]", err: true},
   108  		{s: "bad", err: true},
   109  	}
   110  
   111  	for _, c := range cases {
   112  		t.Run(c.s, func(t *testing.T) {
   113  			s, err := extractNameInNestedBrackets(c.s)
   114  			if !reflect.DeepEqual(s, c.want) {
   115  				t.Errorf("want %s but found %s", c.want, s)
   116  			}
   117  			if c.err != (err != nil) {
   118  				t.Errorf("unexpected error: %v", err)
   119  			}
   120  		})
   121  	}
   122  }