1414// limitations under the License.
1515//===----------------------------------------------------------------------===//
1616
17+ import ContainerizationOCI
1718import Foundation
1819import Testing
1920
@@ -40,4 +41,61 @@ struct MountTests {
4041 #expect( Bool ( false ) , " Expected virtiofs runtime options " )
4142 }
4243 }
44+
45+ @Test func sortMountsByDestinationDepthPreventsParentShadowing( ) {
46+ let mounts : [ ContainerizationOCI . Mount ] = [
47+ . init( destination: " /tmp/foo/bar " ) ,
48+ . init( destination: " /tmp " ) ,
49+ . init( destination: " /var/log/app " ) ,
50+ . init( destination: " /var " ) ,
51+ ]
52+
53+ let sorted = sortMountsByDestinationDepth ( mounts)
54+
55+ #expect(
56+ sorted. map ( \. destination) == [
57+ " /tmp " ,
58+ " /var " ,
59+ " /tmp/foo/bar " ,
60+ " /var/log/app " ,
61+ ] )
62+ }
63+
64+ @Test func sortMountsByDestinationDepthPreservesOrderForEqualDepth( ) {
65+ let mounts : [ ContainerizationOCI . Mount ] = [
66+ . init( destination: " /b " ) ,
67+ . init( destination: " /a " ) ,
68+ . init( destination: " /c " ) ,
69+ ]
70+
71+ let sorted = sortMountsByDestinationDepth ( mounts)
72+
73+ // All same depth, order should be preserved (stable sort).
74+ #expect( sorted. map ( \. destination) == [ " /b " , " /a " , " /c " ] )
75+ }
76+
77+ @Test func sortMountsByDestinationDepthHandlesTrailingAndDoubleSlashes( ) {
78+ let mounts : [ ContainerizationOCI . Mount ] = [
79+ . init( destination: " /a//b/c " ) ,
80+ . init( destination: " /a/ " ) ,
81+ ]
82+
83+ let sorted = cleanAndSortMounts ( mounts)
84+
85+ // Paths are cleaned: "/a/" -> "/a", "/a//b/c" -> "/a/b/c"
86+ #expect( sorted. map ( \. destination) == [ " /a " , " /a/b/c " ] )
87+ }
88+
89+ @Test func sortMountsByDestinationDepthCleansDotAndDotDot( ) {
90+ let mounts : [ ContainerizationOCI . Mount ] = [
91+ . init( destination: " /tmp/../foo " ) ,
92+ . init( destination: " /tmp/./bar/baz " ) ,
93+ . init( destination: " / " ) ,
94+ ]
95+
96+ let sorted = cleanAndSortMounts ( mounts)
97+
98+ // "/tmp/../foo" -> "/foo", "/tmp/./bar/baz" -> "/tmp/bar/baz"
99+ #expect( sorted. map ( \. destination) == [ " / " , " /foo " , " /tmp/bar/baz " ] )
100+ }
43101}
0 commit comments