authorgravatar for dev@sgregoratto.meStephen Gregoratto <dev@sgregoratto.me> 2023-11-05 05:30:26+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-13 23:52:01-07:00
log01cc904fc7c10bd58a7eaf17e63126f9cc63922c
tree9d77ade9b29bd62e5fecc3d70ee8391d6a3188d2
parentbc69d62669fac591bda4c91d695d32cf2b78f34c

Rework fchmodat tests

Based on the Linux kernel fchmodat2 test

1 files changed, 35 insertions(+), 5 deletions(-)

lib/std/os/test.zig+35-5
......@@ -1217,16 +1217,46 @@ test "pwrite with empty buffer" {
12171217 _ = try os.pwrite(file.handle, bytes, 0);
12181218}
12191219
1220fn expectMode(dir: os.fd_t, file: []const u8, mode: os.mode_t) !void {
1221 const st = try os.fstatat(dir, file, os.AT.SYMLINK_NOFOLLOW);
1222 try expectEqual(mode, st.mode & 0b111_111_111);
1223}
1224
12201225test "fchmodat smoke test" {
12211226 if (!std.fs.has_executable_bit) return error.SkipZigTest;
12221227
12231228 var tmp = tmpDir(.{});
12241229 defer tmp.cleanup();
12251230
1226 try expectError(error.FileNotFound, os.fchmodat(tmp.dir.fd, "foo.txt", 0o666, 0));
1227 const fd = try os.openat(tmp.dir.fd, "foo.txt", os.O.RDWR | os.O.CREAT | os.O.EXCL, 0o666);
1231 try expectError(error.FileNotFound, os.fchmodat(tmp.dir.fd, "regfile", 0o666, 0));
1232 const fd = try os.openat(
1233 tmp.dir.fd,
1234 "regfile",
1235 os.O.WRONLY | os.O.CREAT | os.O.EXCL | os.O.TRUNC,
1236 0o644,
1237 );
12281238 os.close(fd);
1229 try os.fchmodat(tmp.dir.fd, "foo.txt", 0o755, 0);
1230 const st = try os.fstatat(tmp.dir.fd, "foo.txt", 0);
1231 try expectEqual(@as(os.mode_t, 0o755), st.mode & 0b111_111_111);
1239 try os.symlinkat("regfile", tmp.dir.fd, "symlink");
1240 const sym_mode = blk: {
1241 const st = try os.fstatat(tmp.dir.fd, "symlink", os.AT.SYMLINK_NOFOLLOW);
1242 break :blk st.mode & 0b111_111_111;
1243 };
1244
1245 try os.fchmodat(tmp.dir.fd, "regfile", 0o640, 0);
1246 try expectMode(tmp.dir.fd, "regfile", 0o640);
1247 try os.fchmodat(tmp.dir.fd, "regfile", 0o600, os.AT.SYMLINK_NOFOLLOW);
1248 try expectMode(tmp.dir.fd, "regfile", 0o600);
1249
1250 try os.fchmodat(tmp.dir.fd, "symlink", 0o640, 0);
1251 try expectMode(tmp.dir.fd, "regfile", 0o640);
1252 try expectMode(tmp.dir.fd, "symlink", sym_mode);
1253
1254 var test_link = true;
1255 os.fchmodat(tmp.dir.fd, "symlink", 0o600, os.AT.SYMLINK_NOFOLLOW) catch |err| switch (err) {
1256 error.OperationNotSupported => test_link = false,
1257 else => |e| return e,
1258 };
1259 if (test_link)
1260 try expectMode(tmp.dir.fd, "symlink", 0o600);
1261 try expectMode(tmp.dir.fd, "regfile", 0o640);
12321262}