Skip to content

Commit

Permalink
make tests ignore root more robustly (#42533)
Browse files Browse the repository at this point in the history
(cherry picked from commit 536d35e)
  • Loading branch information
vtjnash authored and staticfloat committed Dec 22, 2022
1 parent 5e38e5e commit ba65b5b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ Interface to the C `srand(seed)` function.
"""
srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed)

getuid() = ccall(:jl_getuid, Culong, ())
geteuid() = ccall(:jl_geteuid, Culong, ())

# Include dlopen()/dlpath() code
include("libdl.jl")
using .Libdl
Expand Down
18 changes: 18 additions & 0 deletions src/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ JL_DLLEXPORT double jl_stat_ctime(char *statbuf)
return (double)s->st_ctim.tv_sec + (double)s->st_ctim.tv_nsec * 1e-9;
}

JL_DLLEXPORT unsigned long jl_getuid(void)
{
#ifdef _OS_WINDOWS_
return -1;
#else
return getuid();
#endif
}

JL_DLLEXPORT unsigned long jl_geteuid(void)
{
#ifdef _OS_WINDOWS_
return -1;
#else
return geteuid();
#endif
}

// --- buffer manipulation ---

JL_DLLEXPORT jl_array_t *jl_take_buffer(ios_t *s)
Expand Down
5 changes: 4 additions & 1 deletion test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,10 @@ end

if !Sys.iswindows()
# chown will give an error if the user does not have permissions to change files
if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root"
uid = Libc.geteuid()
@test stat(file).uid == uid
@test uid == Libc.getuid()
if uid == 0 # root user
chown(file, -2, -1) # Change the file owner to nobody
@test stat(file).uid != 0
chown(file, 0, -2) # Change the file group to nogroup (and owner back to root)
Expand Down
4 changes: 2 additions & 2 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ rm(f)
io = Base.Filesystem.open(f, Base.Filesystem.JL_O_WRONLY | Base.Filesystem.JL_O_CREAT | Base.Filesystem.JL_O_EXCL, 0o000)
@test write(io, "abc") == 3
close(io)
if !Sys.iswindows() && get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
if !Sys.iswindows() && Libc.geteuid() != 0 # root user
# msvcrt _wchmod documentation states that all files are readable,
# so we don't test that it correctly set the umask on windows
@test_throws SystemError open(f)
Expand Down Expand Up @@ -515,7 +515,7 @@ close(f1)
close(f2)
@test eof(f1)
@test_throws Base.IOError eof(f2)
if get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
if Libc.geteuid() != 0 # root user
@test_throws SystemError open(f, "r+")
@test_throws Base.IOError Base.Filesystem.open(f, Base.Filesystem.JL_O_RDWR)
else
Expand Down

0 comments on commit ba65b5b

Please sign in to comment.