authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-31 00:31:11+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-07-31 16:33:02+02:00
loga694f575adbe4ca6852cc7c9022101ebcfc3a8d7
tree1381e5721073c39d28e2de2bb36064bf790b6396
parent390194431e7fa439d67686e5a0e9efceed2d898a

Add some os.open and os.openat smoke tests


1 files changed, 90 insertions(+), 0 deletions(-)

lib/std/os/test.zig+90
......@@ -3,6 +3,7 @@ const os = std.os;
33const testing = std.testing;
44const expect = testing.expect;
55const expectEqual = testing.expectEqual;
6const expectError = testing.expectError;
67const io = std.io;
78const fs = std.fs;
89const mem = std.mem;
......@@ -19,6 +20,95 @@ const tmpDir = std.testing.tmpDir;
1920const Dir = std.fs.Dir;
2021const ArenaAllocator = std.heap.ArenaAllocator;
2122
23test "open smoke test" {
24 if (builtin.os.tag == .windows) return error.SkipZigTest;
25 if (builtin.os.tag == .wasi) return error.SkipZigTest;
26
27 // TODO verify file attributes using `fstat`
28
29 var tmp = tmpDir(.{});
30 defer tmp.cleanup();
31
32 // Get base abs path
33 var arena = ArenaAllocator.init(testing.allocator);
34 defer arena.deinit();
35
36 const base_path = blk: {
37 const relative_path = try fs.path.join(&arena.allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
38 break :blk try fs.realpathAlloc(&arena.allocator, relative_path);
39 };
40
41 var file_path: []u8 = undefined;
42 var fd: os.fd_t = undefined;
43
44 // Create some file using `open`.
45 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
46 fd = try os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666);
47 os.close(fd);
48
49 // Try this again with the same flags. This op should fail with error.PathAlreadyExists.
50 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
51 expectError(error.PathAlreadyExists, os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666));
52
53 // Try opening without `O_EXCL` flag.
54 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
55 fd = try os.open(file_path, os.O_RDWR | os.O_CREAT, 0o666);
56 os.close(fd);
57
58 // Try opening as a directory which should fail.
59 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_file" });
60 expectError(error.NotDir, os.open(file_path, os.O_RDWR | os.O_DIRECTORY, 0o666));
61
62 // Create some directory
63 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" });
64 try os.mkdir(file_path, 0o666);
65
66 // Open dir using `open`
67 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" });
68 fd = try os.open(file_path, os.O_RDONLY | os.O_DIRECTORY, 0o666);
69 os.close(fd);
70
71 // Try opening as file which should fail.
72 file_path = try fs.path.join(&arena.allocator, &[_][]const u8{ base_path, "some_dir" });
73 expectError(error.IsDir, os.open(file_path, os.O_RDWR, 0o666));
74}
75
76test "openat smoke test" {
77 if (builtin.os.tag == .windows) return error.SkipZigTest;
78 if (builtin.os.tag == .wasi) return error.SkipZigTest;
79
80 // TODO verify file attributes using `fstatat`
81
82 var tmp = tmpDir(.{});
83 defer tmp.cleanup();
84
85 var fd: os.fd_t = undefined;
86
87 // Create some file using `openat`.
88 fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666);
89 os.close(fd);
90
91 // Try this again with the same flags. This op should fail with error.PathAlreadyExists.
92 expectError(error.PathAlreadyExists, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o666));
93
94 // Try opening without `O_EXCL` flag.
95 fd = try os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_CREAT, 0o666);
96 os.close(fd);
97
98 // Try opening as a directory which should fail.
99 expectError(error.NotDir, os.openat(tmp.dir.fd, "some_file", os.O_RDWR | os.O_DIRECTORY, 0o666));
100
101 // Create some directory
102 try os.mkdirat(tmp.dir.fd, "some_dir", 0o666);
103
104 // Open dir using `open`
105 fd = try os.openat(tmp.dir.fd, "some_dir", os.O_RDONLY | os.O_DIRECTORY, 0o666);
106 os.close(fd);
107
108 // Try opening as file which should fail.
109 expectError(error.IsDir, os.openat(tmp.dir.fd, "some_dir", os.O_RDWR, 0o666));
110}
111
22112test "symlink with relative paths" {
23113 if (builtin.os.tag == .wasi) return error.SkipZigTest;
24114