github.com/databricks/cli@v0.203.0/libs/filer/local_root_path_test.go (about) 1 package filer 2 3 import ( 4 "path/filepath" 5 "runtime" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func testUnixLocalRootPath(t *testing.T, uncleanRoot string) { 12 cleanRoot := filepath.Clean(uncleanRoot) 13 rp := NewLocalRootPath(uncleanRoot) 14 15 remotePath, err := rp.Join("a/b/c") 16 assert.NoError(t, err) 17 assert.Equal(t, cleanRoot+"/a/b/c", remotePath) 18 19 remotePath, err = rp.Join("a/b/../d") 20 assert.NoError(t, err) 21 assert.Equal(t, cleanRoot+"/a/d", remotePath) 22 23 remotePath, err = rp.Join("a/../c") 24 assert.NoError(t, err) 25 assert.Equal(t, cleanRoot+"/c", remotePath) 26 27 remotePath, err = rp.Join("a/b/c/.") 28 assert.NoError(t, err) 29 assert.Equal(t, cleanRoot+"/a/b/c", remotePath) 30 31 remotePath, err = rp.Join("a/b/c/d/./../../f/g") 32 assert.NoError(t, err) 33 assert.Equal(t, cleanRoot+"/a/b/f/g", remotePath) 34 35 remotePath, err = rp.Join(".//a/..//./b/..") 36 assert.NoError(t, err) 37 assert.Equal(t, cleanRoot, remotePath) 38 39 remotePath, err = rp.Join("a/b/../..") 40 assert.NoError(t, err) 41 assert.Equal(t, cleanRoot, remotePath) 42 43 remotePath, err = rp.Join("") 44 assert.NoError(t, err) 45 assert.Equal(t, cleanRoot, remotePath) 46 47 remotePath, err = rp.Join(".") 48 assert.NoError(t, err) 49 assert.Equal(t, cleanRoot, remotePath) 50 51 remotePath, err = rp.Join("/") 52 assert.NoError(t, err) 53 assert.Equal(t, cleanRoot, remotePath) 54 55 _, err = rp.Join("..") 56 assert.ErrorContains(t, err, `relative path escapes root: ..`) 57 58 _, err = rp.Join("a/../..") 59 assert.ErrorContains(t, err, `relative path escapes root: a/../..`) 60 61 _, err = rp.Join("./../.") 62 assert.ErrorContains(t, err, `relative path escapes root: ./../.`) 63 64 _, err = rp.Join("/./.././..") 65 assert.ErrorContains(t, err, `relative path escapes root: /./.././..`) 66 67 _, err = rp.Join("./../.") 68 assert.ErrorContains(t, err, `relative path escapes root: ./../.`) 69 70 _, err = rp.Join("./..") 71 assert.ErrorContains(t, err, `relative path escapes root: ./..`) 72 73 _, err = rp.Join("./../../..") 74 assert.ErrorContains(t, err, `relative path escapes root: ./../../..`) 75 76 _, err = rp.Join("./../a/./b../../..") 77 assert.ErrorContains(t, err, `relative path escapes root: ./../a/./b../../..`) 78 79 _, err = rp.Join("../..") 80 assert.ErrorContains(t, err, `relative path escapes root: ../..`) 81 } 82 83 func TestUnixLocalRootPath(t *testing.T) { 84 if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { 85 t.SkipNow() 86 } 87 88 testUnixLocalRootPath(t, "/some/root/path") 89 testUnixLocalRootPath(t, "/some/root/path/") 90 testUnixLocalRootPath(t, "/some/root/path/.") 91 testUnixLocalRootPath(t, "/some/root/../path/") 92 } 93 94 func testWindowsLocalRootPath(t *testing.T, uncleanRoot string) { 95 cleanRoot := filepath.Clean(uncleanRoot) 96 rp := NewLocalRootPath(uncleanRoot) 97 98 remotePath, err := rp.Join(`a\b\c`) 99 assert.NoError(t, err) 100 assert.Equal(t, cleanRoot+`\a\b\c`, remotePath) 101 102 remotePath, err = rp.Join(`a\b\..\d`) 103 assert.NoError(t, err) 104 assert.Equal(t, cleanRoot+`\a\d`, remotePath) 105 106 remotePath, err = rp.Join(`a\..\c`) 107 assert.NoError(t, err) 108 assert.Equal(t, cleanRoot+`\c`, remotePath) 109 110 remotePath, err = rp.Join(`a\b\c\.`) 111 assert.NoError(t, err) 112 assert.Equal(t, cleanRoot+`\a\b\c`, remotePath) 113 114 remotePath, err = rp.Join(`a\b\..\..`) 115 assert.NoError(t, err) 116 assert.Equal(t, cleanRoot, remotePath) 117 118 remotePath, err = rp.Join("") 119 assert.NoError(t, err) 120 assert.Equal(t, cleanRoot, remotePath) 121 122 remotePath, err = rp.Join(".") 123 assert.NoError(t, err) 124 assert.Equal(t, cleanRoot, remotePath) 125 126 _, err = rp.Join("..") 127 assert.ErrorContains(t, err, `relative path escapes root`) 128 129 _, err = rp.Join(`a\..\..`) 130 assert.ErrorContains(t, err, `relative path escapes root`) 131 } 132 133 func TestWindowsLocalRootPath(t *testing.T) { 134 if runtime.GOOS != "windows" { 135 t.SkipNow() 136 } 137 138 testWindowsLocalRootPath(t, `c:\some\root\path`) 139 testWindowsLocalRootPath(t, `c:\some\root\path\`) 140 testWindowsLocalRootPath(t, `c:\some\root\path\.`) 141 testWindowsLocalRootPath(t, `C:\some\root\..\path\`) 142 }