github.com/opencontainers/runtime-tools@v0.9.0/filepath/ancestor_test.go (about) 1 package filepath 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestIsAncestor(t *testing.T) { 9 for _, test := range []struct { 10 os string 11 pathA string 12 pathB string 13 cwd string 14 expected bool 15 }{ 16 { 17 os: "linux", 18 pathA: "/", 19 pathB: "/a", 20 cwd: "/cwd", 21 expected: true, 22 }, 23 { 24 os: "linux", 25 pathA: "/a", 26 pathB: "/a", 27 cwd: "/cwd", 28 expected: false, 29 }, 30 { 31 os: "linux", 32 pathA: "/a", 33 pathB: "/", 34 cwd: "/cwd", 35 expected: false, 36 }, 37 { 38 os: "linux", 39 pathA: "/a", 40 pathB: "/ab", 41 cwd: "/cwd", 42 expected: false, 43 }, 44 { 45 os: "linux", 46 pathA: "/a/", 47 pathB: "/a", 48 cwd: "/cwd", 49 expected: false, 50 }, 51 { 52 os: "linux", 53 pathA: "//a", 54 pathB: "/a", 55 cwd: "/cwd", 56 expected: false, 57 }, 58 { 59 os: "linux", 60 pathA: "//a", 61 pathB: "/a/b", 62 cwd: "/cwd", 63 expected: true, 64 }, 65 { 66 os: "linux", 67 pathA: "/a", 68 pathB: "/a/", 69 cwd: "/cwd", 70 expected: false, 71 }, 72 { 73 os: "linux", 74 pathA: "/a", 75 pathB: ".", 76 cwd: "/cwd", 77 expected: false, 78 }, 79 { 80 os: "linux", 81 pathA: "/a", 82 pathB: "b", 83 cwd: "/a", 84 expected: true, 85 }, 86 { 87 os: "linux", 88 pathA: "/a", 89 pathB: "../a", 90 cwd: "/cwd", 91 expected: false, 92 }, 93 { 94 os: "linux", 95 pathA: "/a", 96 pathB: "../a/b", 97 cwd: "/cwd", 98 expected: true, 99 }, 100 { 101 os: "windows", 102 pathA: "c:\\", 103 pathB: "c:\\a", 104 cwd: "c:\\cwd", 105 expected: true, 106 }, 107 { 108 os: "windows", 109 pathA: "c:\\", 110 pathB: "d:\\a", 111 cwd: "c:\\cwd", 112 expected: false, 113 }, 114 { 115 os: "windows", 116 pathA: "c:\\", 117 pathB: ".", 118 cwd: "d:\\cwd", 119 expected: false, 120 }, 121 { 122 os: "windows", 123 pathA: "c:\\a", 124 pathB: "c:\\a", 125 cwd: "c:\\cwd", 126 expected: false, 127 }, 128 { 129 os: "windows", 130 pathA: "c:\\a", 131 pathB: "c:\\", 132 cwd: "c:\\cwd", 133 expected: false, 134 }, 135 { 136 os: "windows", 137 pathA: "c:\\a", 138 pathB: "c:\\ab", 139 cwd: "c:\\cwd", 140 expected: false, 141 }, 142 { 143 os: "windows", 144 pathA: "c:\\a\\", 145 pathB: "c:\\a", 146 cwd: "c:\\cwd", 147 expected: false, 148 }, 149 { 150 os: "windows", 151 pathA: "c:\\\\a", 152 pathB: "c:\\a", 153 cwd: "c:\\cwd", 154 expected: false, 155 }, 156 { 157 os: "windows", 158 pathA: "c:\\\\a", 159 pathB: "c:\\a\\b", 160 cwd: "c:\\cwd", 161 expected: true, 162 }, 163 { 164 os: "windows", 165 pathA: "c:\\a", 166 pathB: "c:\\a\\", 167 cwd: "c:\\cwd", 168 expected: false, 169 }, 170 { 171 os: "windows", 172 pathA: "c:\\a", 173 pathB: ".", 174 cwd: "c:\\cwd", 175 expected: false, 176 }, 177 { 178 os: "windows", 179 pathA: "c:\\a", 180 pathB: "b", 181 cwd: "c:\\a", 182 expected: true, 183 }, 184 { 185 os: "windows", 186 pathA: "c:\\a", 187 pathB: "..\\a", 188 cwd: "c:\\cwd", 189 expected: false, 190 }, 191 { 192 os: "windows", 193 pathA: "c:\\a", 194 pathB: "..\\a\\b", 195 cwd: "c:\\cwd", 196 expected: true, 197 }, 198 } { 199 t.Run( 200 fmt.Sprintf("IsAncestor(%q,%q,%q,%q)", test.os, test.pathA, test.pathB, test.cwd), 201 func(t *testing.T) { 202 ancestor, err := IsAncestor(test.os, test.pathA, test.pathB, test.cwd) 203 if err != nil { 204 t.Error(err) 205 } else if ancestor != test.expected { 206 t.Errorf("unexpected result: %t", ancestor) 207 } 208 }, 209 ) 210 } 211 }