|
1 | | -#if canImport(Testing) |
2 | | - import Testing |
3 | | - |
4 | | - @testable import WASI |
5 | | - |
6 | | - @Suite |
7 | | - struct WASITests { |
8 | | - #if !os(Windows) |
9 | | - @Test |
10 | | - func pathOpen() throws { |
11 | | - let t = try TestSupport.TemporaryDirectory() |
12 | | - |
13 | | - try t.createDir(at: "External") |
14 | | - try t.createDir(at: "External/secret-dir-b") |
15 | | - try t.createFile(at: "External/secret-a.txt", contents: "Secret A") |
16 | | - try t.createFile(at: "External/secret-dir-b/secret-c.txt", contents: "Secret C") |
17 | | - try t.createDir(at: "Sandbox") |
18 | | - try t.createFile(at: "Sandbox/hello.txt", contents: "Hello") |
19 | | - try t.createSymlink(at: "Sandbox/link-hello.txt", to: "hello.txt") |
20 | | - try t.createDir(at: "Sandbox/world.dir") |
21 | | - try t.createSymlink(at: "Sandbox/link-world.dir", to: "world.dir") |
22 | | - try t.createSymlink(at: "Sandbox/link-external-secret-a.txt", to: "../External/secret-a.txt") |
23 | | - try t.createSymlink(at: "Sandbox/link-secret-dir-b", to: "../External/secret-dir-b") |
24 | | - try t.createSymlink(at: "Sandbox/link-updown-hello.txt", to: "../Sandbox/link-updown-hello.txt") |
25 | | - try t.createSymlink(at: "Sandbox/link-external-non-existent.txt", to: "../External/non-existent.txt") |
26 | | - try t.createSymlink(at: "Sandbox/link-root", to: "/") |
27 | | - try t.createSymlink(at: "Sandbox/link-loop.txt", to: "link-loop.txt") |
28 | | - |
29 | | - let wasi = try WASIBridgeToHost( |
30 | | - preopens: ["/Sandbox": t.url.appendingPathComponent("Sandbox").path] |
| 1 | +import Testing |
| 2 | + |
| 3 | +@testable import WASI |
| 4 | + |
| 5 | +@Suite |
| 6 | +struct WASITests { |
| 7 | + #if !os(Windows) |
| 8 | + @Test |
| 9 | + func pathOpen() throws { |
| 10 | + let t = try TestSupport.TemporaryDirectory() |
| 11 | + |
| 12 | + try t.createDir(at: "External") |
| 13 | + try t.createDir(at: "External/secret-dir-b") |
| 14 | + try t.createFile(at: "External/secret-a.txt", contents: "Secret A") |
| 15 | + try t.createFile(at: "External/secret-dir-b/secret-c.txt", contents: "Secret C") |
| 16 | + try t.createDir(at: "Sandbox") |
| 17 | + try t.createFile(at: "Sandbox/hello.txt", contents: "Hello") |
| 18 | + try t.createSymlink(at: "Sandbox/link-hello.txt", to: "hello.txt") |
| 19 | + try t.createDir(at: "Sandbox/world.dir") |
| 20 | + try t.createSymlink(at: "Sandbox/link-world.dir", to: "world.dir") |
| 21 | + try t.createSymlink(at: "Sandbox/link-external-secret-a.txt", to: "../External/secret-a.txt") |
| 22 | + try t.createSymlink(at: "Sandbox/link-secret-dir-b", to: "../External/secret-dir-b") |
| 23 | + try t.createSymlink(at: "Sandbox/link-updown-hello.txt", to: "../Sandbox/link-updown-hello.txt") |
| 24 | + try t.createSymlink(at: "Sandbox/link-external-non-existent.txt", to: "../External/non-existent.txt") |
| 25 | + try t.createSymlink(at: "Sandbox/link-root", to: "/") |
| 26 | + try t.createSymlink(at: "Sandbox/link-loop.txt", to: "link-loop.txt") |
| 27 | + |
| 28 | + let wasi = try WASIBridgeToHost( |
| 29 | + preopens: ["/Sandbox": t.url.appendingPathComponent("Sandbox").path] |
| 30 | + ) |
| 31 | + let mntFd: WASIAbi.Fd = 3 |
| 32 | + |
| 33 | + func assertResolve(_ path: String, followSymlink: Bool, directory: Bool = false) throws { |
| 34 | + let fd = try wasi.path_open( |
| 35 | + dirFd: mntFd, |
| 36 | + dirFlags: followSymlink ? [.SYMLINK_FOLLOW] : [], |
| 37 | + path: path, |
| 38 | + oflags: directory ? [.DIRECTORY] : [], |
| 39 | + fsRightsBase: .DIRECTORY_BASE_RIGHTS, |
| 40 | + fsRightsInheriting: .DIRECTORY_INHERITING_RIGHTS, |
| 41 | + fdflags: [] |
31 | 42 | ) |
32 | | - let mntFd: WASIAbi.Fd = 3 |
| 43 | + try wasi.fd_close(fd: fd) |
| 44 | + } |
33 | 45 |
|
34 | | - func assertResolve(_ path: String, followSymlink: Bool, directory: Bool = false) throws { |
35 | | - let fd = try wasi.path_open( |
| 46 | + func assertNotResolve( |
| 47 | + _ path: String, |
| 48 | + followSymlink: Bool, |
| 49 | + directory: Bool = false, |
| 50 | + sourceLocation: SourceLocation = #_sourceLocation, |
| 51 | + _ checkError: ((WASIAbi.Errno) throws -> Void)? |
| 52 | + ) throws { |
| 53 | + do { |
| 54 | + _ = try wasi.path_open( |
36 | 55 | dirFd: mntFd, |
37 | 56 | dirFlags: followSymlink ? [.SYMLINK_FOLLOW] : [], |
38 | 57 | path: path, |
|
41 | 60 | fsRightsInheriting: .DIRECTORY_INHERITING_RIGHTS, |
42 | 61 | fdflags: [] |
43 | 62 | ) |
44 | | - try wasi.fd_close(fd: fd) |
45 | | - } |
46 | | - |
47 | | - func assertNotResolve( |
48 | | - _ path: String, |
49 | | - followSymlink: Bool, |
50 | | - directory: Bool = false, |
51 | | - sourceLocation: SourceLocation = #_sourceLocation, |
52 | | - _ checkError: ((WASIAbi.Errno) throws -> Void)? |
53 | | - ) throws { |
54 | | - do { |
55 | | - _ = try wasi.path_open( |
56 | | - dirFd: mntFd, |
57 | | - dirFlags: followSymlink ? [.SYMLINK_FOLLOW] : [], |
58 | | - path: path, |
59 | | - oflags: directory ? [.DIRECTORY] : [], |
60 | | - fsRightsBase: .DIRECTORY_BASE_RIGHTS, |
61 | | - fsRightsInheriting: .DIRECTORY_INHERITING_RIGHTS, |
62 | | - fdflags: [] |
63 | | - ) |
64 | | - #expect((false), "Expected not to be able to open \(path)", sourceLocation: sourceLocation) |
65 | | - } catch { |
66 | | - guard let error = error as? WASIAbi.Errno else { |
67 | | - #expect((false), "Expected WASIAbi.Errno error but got \(error)", sourceLocation: sourceLocation) |
68 | | - return |
69 | | - } |
70 | | - try checkError?(error) |
| 63 | + #expect((false), "Expected not to be able to open \(path)", sourceLocation: sourceLocation) |
| 64 | + } catch { |
| 65 | + guard let error = error as? WASIAbi.Errno else { |
| 66 | + #expect((false), "Expected WASIAbi.Errno error but got \(error)", sourceLocation: sourceLocation) |
| 67 | + return |
71 | 68 | } |
| 69 | + try checkError?(error) |
72 | 70 | } |
| 71 | + } |
73 | 72 |
|
74 | | - try assertNotResolve("non-existent.txt", followSymlink: false) { error in |
75 | | - #expect(error == .ENOENT) |
76 | | - } |
| 73 | + try assertNotResolve("non-existent.txt", followSymlink: false) { error in |
| 74 | + #expect(error == .ENOENT) |
| 75 | + } |
77 | 76 |
|
78 | | - try assertResolve("link-hello.txt", followSymlink: true) |
79 | | - try assertNotResolve("link-hello.txt", followSymlink: false) { error in |
80 | | - #expect(error == .ELOOP) |
81 | | - } |
82 | | - try assertNotResolve("link-hello.txt", followSymlink: true, directory: true) { error in |
83 | | - #expect(error == .ENOTDIR) |
84 | | - } |
| 77 | + try assertResolve("link-hello.txt", followSymlink: true) |
| 78 | + try assertNotResolve("link-hello.txt", followSymlink: false) { error in |
| 79 | + #expect(error == .ELOOP) |
| 80 | + } |
| 81 | + try assertNotResolve("link-hello.txt", followSymlink: true, directory: true) { error in |
| 82 | + #expect(error == .ENOTDIR) |
| 83 | + } |
85 | 84 |
|
86 | | - try assertNotResolve("link-hello.txt/", followSymlink: true) { error in |
87 | | - #expect(error == .ENOTDIR) |
88 | | - } |
| 85 | + try assertNotResolve("link-hello.txt/", followSymlink: true) { error in |
| 86 | + #expect(error == .ENOTDIR) |
| 87 | + } |
89 | 88 |
|
90 | | - try assertResolve("link-world.dir", followSymlink: true) |
91 | | - try assertNotResolve("link-world.dir", followSymlink: false) { error in |
92 | | - #expect(error == .ELOOP) |
93 | | - } |
| 89 | + try assertResolve("link-world.dir", followSymlink: true) |
| 90 | + try assertNotResolve("link-world.dir", followSymlink: false) { error in |
| 91 | + #expect(error == .ELOOP) |
| 92 | + } |
94 | 93 |
|
95 | | - try assertNotResolve("link-external-secret-a.txt", followSymlink: true) { error in |
96 | | - #expect(error == .EPERM) |
97 | | - } |
98 | | - try assertNotResolve("link-external-secret-a.txt", followSymlink: false) { error in |
99 | | - #expect(error == .ELOOP) |
100 | | - } |
| 94 | + try assertNotResolve("link-external-secret-a.txt", followSymlink: true) { error in |
| 95 | + #expect(error == .EPERM) |
| 96 | + } |
| 97 | + try assertNotResolve("link-external-secret-a.txt", followSymlink: false) { error in |
| 98 | + #expect(error == .ELOOP) |
| 99 | + } |
101 | 100 |
|
102 | | - try assertNotResolve("link-external-non-existent.txt", followSymlink: true) { error in |
103 | | - #expect(error == .EPERM) |
104 | | - } |
105 | | - try assertNotResolve("link-external-non-existent.txt", followSymlink: false) { error in |
106 | | - #expect(error == .ELOOP) |
107 | | - } |
| 101 | + try assertNotResolve("link-external-non-existent.txt", followSymlink: true) { error in |
| 102 | + #expect(error == .EPERM) |
| 103 | + } |
| 104 | + try assertNotResolve("link-external-non-existent.txt", followSymlink: false) { error in |
| 105 | + #expect(error == .ELOOP) |
| 106 | + } |
108 | 107 |
|
109 | | - try assertNotResolve("link-updown-hello.txt", followSymlink: true) { error in |
110 | | - #expect(error == .EPERM) |
111 | | - } |
112 | | - try assertNotResolve("link-updown-hello.txt", followSymlink: false) { error in |
113 | | - #expect(error == .ELOOP) |
114 | | - } |
| 108 | + try assertNotResolve("link-updown-hello.txt", followSymlink: true) { error in |
| 109 | + #expect(error == .EPERM) |
| 110 | + } |
| 111 | + try assertNotResolve("link-updown-hello.txt", followSymlink: false) { error in |
| 112 | + #expect(error == .ELOOP) |
| 113 | + } |
115 | 114 |
|
116 | | - try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: true) { error in |
117 | | - #expect(error == .EPERM) |
118 | | - } |
119 | | - try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: false) { error in |
120 | | - #expect(error == .ENOTDIR) |
121 | | - } |
| 115 | + try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: true) { error in |
| 116 | + #expect(error == .EPERM) |
| 117 | + } |
| 118 | + try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: false) { error in |
| 119 | + #expect(error == .ENOTDIR) |
| 120 | + } |
122 | 121 |
|
123 | | - try assertNotResolve("link-root", followSymlink: true) { error in |
124 | | - #expect(error == .EPERM) |
125 | | - } |
126 | | - try assertNotResolve("link-root", followSymlink: false) { error in |
127 | | - #expect(error == .ELOOP) |
128 | | - } |
| 122 | + try assertNotResolve("link-root", followSymlink: true) { error in |
| 123 | + #expect(error == .EPERM) |
| 124 | + } |
| 125 | + try assertNotResolve("link-root", followSymlink: false) { error in |
| 126 | + #expect(error == .ELOOP) |
| 127 | + } |
129 | 128 |
|
130 | | - try assertNotResolve("link-loop.txt", followSymlink: false) { error in |
131 | | - #expect(error == .ELOOP) |
132 | | - } |
133 | | - try assertNotResolve("link-loop.txt", followSymlink: true) { error in |
134 | | - #expect(error == .ELOOP) |
135 | | - } |
| 129 | + try assertNotResolve("link-loop.txt", followSymlink: false) { error in |
| 130 | + #expect(error == .ELOOP) |
| 131 | + } |
| 132 | + try assertNotResolve("link-loop.txt", followSymlink: true) { error in |
| 133 | + #expect(error == .ELOOP) |
136 | 134 | } |
137 | | - #endif |
138 | | - } |
139 | | -#endif |
| 135 | + } |
| 136 | + #endif |
| 137 | +} |
0 commit comments