Skip to content

Commit 13df7b7

Browse files
vtjnashKristofferC
authored andcommitted
make tests ignore root more robustly (#42533)
(cherry picked from commit 536d35e)
1 parent 0844d06 commit 13df7b7

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

base/libc.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ Interface to the C `srand(seed)` function.
402402
"""
403403
srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed)
404404

405+
getuid() = ccall(:jl_getuid, Culong, ())
406+
geteuid() = ccall(:jl_geteuid, Culong, ())
407+
405408
# Include dlopen()/dlpath() code
406409
include("libdl.jl")
407410
using .Libdl

src/sys.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ JL_DLLEXPORT double jl_stat_ctime(char *statbuf)
230230
return (double)s->st_ctim.tv_sec + (double)s->st_ctim.tv_nsec * 1e-9;
231231
}
232232

233+
JL_DLLEXPORT unsigned long jl_getuid(void)
234+
{
235+
#ifdef _OS_WINDOWS_
236+
return -1;
237+
#else
238+
return getuid();
239+
#endif
240+
}
241+
242+
JL_DLLEXPORT unsigned long jl_geteuid(void)
243+
{
244+
#ifdef _OS_WINDOWS_
245+
return -1;
246+
#else
247+
return geteuid();
248+
#endif
249+
}
250+
233251
// --- buffer manipulation ---
234252

235253
JL_DLLEXPORT jl_array_t *jl_take_buffer(ios_t *s)

test/file.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,10 @@ end
516516

517517
if !Sys.iswindows()
518518
# chown will give an error if the user does not have permissions to change files
519-
if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root"
519+
uid = Libc.geteuid()
520+
@test stat(file).uid == uid
521+
@test uid == Libc.getuid()
522+
if uid == 0 # root user
520523
chown(file, -2, -1) # Change the file owner to nobody
521524
@test stat(file).uid != 0
522525
chown(file, 0, -2) # Change the file group to nogroup (and owner back to root)

test/read.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ rm(f)
465465
io = Base.Filesystem.open(f, Base.Filesystem.JL_O_WRONLY | Base.Filesystem.JL_O_CREAT | Base.Filesystem.JL_O_EXCL, 0o000)
466466
@test write(io, "abc") == 3
467467
close(io)
468-
if !Sys.iswindows() && get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
468+
if !Sys.iswindows() && Libc.geteuid() != 0 # root user
469469
# msvcrt _wchmod documentation states that all files are readable,
470470
# so we don't test that it correctly set the umask on windows
471471
@test_throws SystemError open(f)
@@ -515,7 +515,7 @@ close(f1)
515515
close(f2)
516516
@test eof(f1)
517517
@test_throws Base.IOError eof(f2)
518-
if get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
518+
if Libc.geteuid() != 0 # root user
519519
@test_throws SystemError open(f, "r+")
520520
@test_throws Base.IOError Base.Filesystem.open(f, Base.Filesystem.JL_O_RDWR)
521521
else

0 commit comments

Comments
 (0)