k8s.io/apiserver@v0.31.1/pkg/storage/cacher/util_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     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 cacher
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestHasPathPrefix(t *testing.T) {
    24  	validTestcases := []struct {
    25  		s      string
    26  		prefix string
    27  	}{
    28  		// Exact matches
    29  		{"", ""},
    30  		{"a", "a"},
    31  		{"a/", "a/"},
    32  		{"a/../", "a/../"},
    33  
    34  		// Path prefix matches
    35  		{"a/b", "a"},
    36  		{"a/b", "a/"},
    37  		{"中文/", "中文"},
    38  	}
    39  	for i, tc := range validTestcases {
    40  		if !hasPathPrefix(tc.s, tc.prefix) {
    41  			t.Errorf(`%d: Expected hasPathPrefix("%s","%s") to be true`, i, tc.s, tc.prefix)
    42  		}
    43  	}
    44  
    45  	invalidTestcases := []struct {
    46  		s      string
    47  		prefix string
    48  	}{
    49  		// Mismatch
    50  		{"a", "b"},
    51  
    52  		// Dir requirement
    53  		{"a", "a/"},
    54  
    55  		// Prefix mismatch
    56  		{"ns2", "ns"},
    57  		{"ns2", "ns/"},
    58  		{"中文文", "中文"},
    59  
    60  		// Ensure no normalization is applied
    61  		{"a/c/../b/", "a/b/"},
    62  		{"a/", "a/b/.."},
    63  	}
    64  	for i, tc := range invalidTestcases {
    65  		if hasPathPrefix(tc.s, tc.prefix) {
    66  			t.Errorf(`%d: Expected hasPathPrefix("%s","%s") to be false`, i, tc.s, tc.prefix)
    67  		}
    68  	}
    69  }