Skip to content

Commit a42e9e0

Browse files
committed
Add umask unit test
1 parent b7d046d commit a42e9e0

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

test/stat/test_umask.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2013 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include <assert.h>
9+
#include <errno.h>
10+
#include <fcntl.h>
11+
#include <signal.h>
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
#include <string.h>
15+
#include <unistd.h>
16+
#include <sys/stat.h>
17+
#include <sys/types.h>
18+
19+
mode_t get_umask() {
20+
mode_t current = umask(0); // Set umask to 0 and get the old value
21+
umask(current); // Immediately set it back
22+
return current;
23+
}
24+
25+
void create_file(const char *path, const char *buffer) {
26+
mode_t mode = 0777 - get_umask();
27+
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
28+
assert(fd >= 0);
29+
30+
int err = write(fd, buffer, sizeof(char) * strlen(buffer));
31+
assert(err == (sizeof(char) * strlen(buffer)));
32+
33+
close(fd);
34+
}
35+
36+
void cleanup() {
37+
unlink("umask_test_file");
38+
}
39+
40+
void test() {
41+
// Get the default umask
42+
mode_t default_umask = get_umask();
43+
printf("default umask: %o\n", default_umask);
44+
assert(default_umask == 027);
45+
46+
// Create a new file with default umask
47+
create_file("umask_test_file", "abcdef");
48+
struct stat st;
49+
stat("umask_test_file", &st);
50+
printf("default_umask - stat: %o\n", st.st_mode);
51+
assert((st.st_mode & 0666) == 0640);
52+
unlink("umask_test_file");
53+
54+
// Set new umask
55+
mode_t new_umask = 022;
56+
mode_t old_umask = umask(new_umask);
57+
58+
// Create a new file with new umask
59+
create_file("umask_test_file", "abcdef");
60+
stat("umask_test_file", &st);
61+
printf("new_umask - stat: %o\n", st.st_mode);
62+
assert((st.st_mode & 0666) == 0644);
63+
64+
// Restore the old umask
65+
umask(old_umask);
66+
67+
puts("success");
68+
}
69+
70+
int main() {
71+
atexit(cleanup);
72+
signal(SIGABRT, cleanup);
73+
test();
74+
return EXIT_SUCCESS;
75+
}

test/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,6 +5609,10 @@ def test_statx(self):
56095609
self.set_setting("FORCE_FILESYSTEM")
56105610
self.do_runf('stat/test_statx.c', 'success')
56115611

5612+
def test_umask(self):
5613+
self.set_setting("FORCE_FILESYSTEM")
5614+
self.do_runf('stat/test_umask.c', 'success')
5615+
56125616
def test_fstatat(self):
56135617
self.do_runf('stat/test_fstatat.c', 'success')
56145618

0 commit comments

Comments
 (0)