Commit a694f575ad
Changed files (1)
lib
std
os
lib/std/os/test.zig
@@ -3,6 +3,7 @@ const os = std.os;
const testing = std.testing;
const expect = testing.expect;
const expectEqual = testing.expectEqual;
+const expectError = testing.expectError;
const io = std.io;
const fs = std.fs;
const mem = std.mem;
@@ -19,6 +20,95 @@ const tmpDir = std.testing.tmpDir;
const Dir = std.fs.Dir;
const ArenaAllocator = std.heap.ArenaAllocator;
+test "open smoke test" {
+ if (builtin.os.tag == .windows) return error.SkipZigTest;
+ if (builtin.os.tag == .wasi) return error.SkipZigTest;
+
+ // TODO verify file attributes using `fstat`
+
+ var tmp = tmpDir(.{});
+ defer tmp.cleanup();
+
+ // Get base abs path
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ const base_path = blk: {
+ const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
+ break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
+ };
+
+ var file_path: []u8 = undefined;
+ var fd: os.fd_t = undefined;
+
+ // Create some file using `open`.
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
+ fd = try os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666);
+ os.close(fd);
+
+ // Try this again with the same flags. This op should fail with error.PathAlreadyExists.
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
+ expectError(error.PathAlreadyExists, os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666));
+
+ // Try opening without `O_EXCL` flag.
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
+ fd = try os.open(file_path, os.O_RDWR | os.O_CREAT, 0o666);
+ os.close(fd);
+
+ // Try opening as a directory which should fail.
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
+ expectError(error.NotDir, os.open(file_path, os.O_RDWR | os.O_DIRECTORY, 0o666));
+
+ // Create some directory
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" });
+ try os.mkdir(file_path, 0o666);
+
+ // Open dir using `open`
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" });
+ fd = try os.open(file_path, os.O_RDONLY | os.O_DIRECTORY, 0o666);
+ os.close(fd);
+
+ // Try opening as file which should fail.
+ file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" });
+ expectError(error.IsDir, os.open(file_path, os.O_RDWR, 0o666));
+}
+
+test "openat smoke test" {
+ if (builtin.os.tag == .windows) return error.SkipZigTest;
+ if (builtin.os.tag == .wasi) return error.SkipZigTest;
+
+ // TODO verify file attributes using `fstatat`
+
+ var tmp = tmpDir(.{});
+ defer tmp.cleanup();
+
+ var fd: os.fd_t = undefined;
+
+ // Create some file using `openat`.
+ fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666);
+ os.close(fd);
+
+ // Try this again with the same flags. This op should fail with error.PathAlreadyExists.
+ expectError(error.PathAlreadyExists, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666));
+
+ // Try opening without `O_EXCL` flag.
+ fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT, 0o666);
+ os.close(fd);
+
+ // Try opening as a directory which should fail.
+ expectError(error.NotDir, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_DIRECTORY, 0o666));
+
+ // Create some directory
+ try os.mkdirat(tmp.dir.fd, "some_dir", 0o666);
+
+ // Open dir using `open`
+ fd = try os.openat(tmp.dir.fd, "some_dir", os.O_RDONLY | os.O_DIRECTORY, 0o666);
+ os.close(fd);
+
+ // Try opening as file which should fail.
+ expectError(error.IsDir, os.openat(tmp.dir.fd, "some_dir", os.O_RDWR, 0o666));
+}
+
test "symlink with relative paths" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;