...

Source file src/k8s.io/kubernetes/pkg/util/filesystem/util_test.go

Documentation: k8s.io/kubernetes/pkg/util/filesystem

     1  /*
     2  Copyright 2023 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 filesystem
    18  
    19  import (
    20  	"net"
    21  	"os"
    22  	"runtime"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestIsUnixDomainSocket(t *testing.T) {
    30  	tests := []struct {
    31  		label          string
    32  		listenOnSocket bool
    33  		expectSocket   bool
    34  		expectError    bool
    35  		invalidFile    bool
    36  	}{
    37  		{
    38  			label:          "Domain Socket file",
    39  			listenOnSocket: true,
    40  			expectSocket:   true,
    41  			expectError:    false,
    42  		},
    43  		{
    44  			label:       "Non Existent file",
    45  			invalidFile: true,
    46  			expectError: true,
    47  		},
    48  		{
    49  			label:          "Regular file",
    50  			listenOnSocket: false,
    51  			expectSocket:   false,
    52  			expectError:    false,
    53  		},
    54  	}
    55  	for _, test := range tests {
    56  		f, err := os.CreateTemp("", "test-domain-socket")
    57  		require.NoErrorf(t, err, "Failed to create file for test purposes: %v while setting up: %s", err, test.label)
    58  		addr := f.Name()
    59  		f.Close()
    60  		var ln *net.UnixListener
    61  		if test.listenOnSocket {
    62  			os.Remove(addr)
    63  			ta, err := net.ResolveUnixAddr("unix", addr)
    64  			require.NoErrorf(t, err, "Failed to ResolveUnixAddr: %v while setting up: %s", err, test.label)
    65  			ln, err = net.ListenUnix("unix", ta)
    66  			require.NoErrorf(t, err, "Failed to ListenUnix: %v while setting up: %s", err, test.label)
    67  		}
    68  		fileToTest := addr
    69  		if test.invalidFile {
    70  			fileToTest = fileToTest + ".invalid"
    71  		}
    72  		result, err := IsUnixDomainSocket(fileToTest)
    73  		if test.listenOnSocket {
    74  			// this takes care of removing the file associated with the domain socket
    75  			ln.Close()
    76  		} else {
    77  			// explicitly remove regular file
    78  			os.Remove(addr)
    79  		}
    80  		if test.expectError {
    81  			assert.Errorf(t, err, "Unexpected nil error from IsUnixDomainSocket for %s", test.label)
    82  		} else {
    83  			assert.NoErrorf(t, err, "Unexpected error invoking IsUnixDomainSocket for %s", test.label)
    84  		}
    85  		assert.Equal(t, result, test.expectSocket, "Unexpected result from IsUnixDomainSocket: %v for %s", result, test.label)
    86  	}
    87  }
    88  
    89  func TestIsCleanPath(t *testing.T) {
    90  	type Case struct {
    91  		path     string
    92  		expected bool
    93  	}
    94  
    95  	// Credits https://github.com/kubernetes/kubernetes/pull/124084/files#r1557566941
    96  	cases := []Case{
    97  		{path: "/logs", expected: true},
    98  		{path: "/var/lib/something", expected: true},
    99  		{path: "var/lib/something", expected: true},
   100  		{path: "var\\lib\\something", expected: true},
   101  		{path: "/", expected: true},
   102  		{path: ".", expected: true},
   103  		{path: "/var/../something", expected: false},
   104  		{path: "/var//lib/something", expected: false},
   105  		{path: "/var/./lib/something", expected: false},
   106  	}
   107  
   108  	// Additional cases applicable on Windows
   109  	if runtime.GOOS == "windows" {
   110  		cases = append(cases, []Case{
   111  			{path: "\\", expected: true},
   112  			{path: "C:/var/lib/something", expected: true},
   113  			{path: "C:\\var\\lib\\something", expected: true},
   114  			{path: "C:/", expected: true},
   115  			{path: "C:\\", expected: true},
   116  			{path: "C:/var//lib/something", expected: false},
   117  			{path: "\\var\\\\lib\\something", expected: false},
   118  			{path: "C:\\var\\\\lib\\something", expected: false},
   119  			{path: "C:\\var\\..\\something", expected: false},
   120  			{path: "\\var\\.\\lib\\something", expected: false},
   121  			{path: "C:\\var\\.\\lib\\something", expected: false},
   122  		}...)
   123  	}
   124  
   125  	for _, tc := range cases {
   126  		actual := IsPathClean(tc.path)
   127  		if actual != tc.expected {
   128  			t.Errorf("actual: %t, expected: %t, for path: %s\n", actual, tc.expected, tc.path)
   129  		}
   130  	}
   131  
   132  }
   133  

View as plain text