Skip to content

Commit 536d35e

Browse files
authored
make tests ignore root more robustly (#42533)
1 parent dce7ebb commit 536d35e

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

base/libc.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,36 +404,36 @@ srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed)
404404

405405
struct Cpasswd
406406
username::Cstring
407-
uid::Clong
408-
gid::Clong
407+
uid::Culong
408+
gid::Culong
409409
shell::Cstring
410410
homedir::Cstring
411411
gecos::Cstring
412-
Cpasswd() = new(C_NULL, -1, -1, C_NULL, C_NULL, C_NULL)
412+
Cpasswd() = new(C_NULL, typemax(Culong), typemax(Culong), C_NULL, C_NULL, C_NULL)
413413
end
414414
mutable struct Cgroup
415-
groupname::Cstring # group name
416-
gid::Clong # group ID
417-
mem::Ptr{Cstring} # group members
418-
Cgroup() = new(C_NULL, -1, C_NULL)
415+
groupname::Cstring # group name
416+
gid::Culong # group ID
417+
mem::Ptr{Cstring} # group members
418+
Cgroup() = new(C_NULL, typemax(Culong), C_NULL)
419419
end
420420
struct Passwd
421421
username::String
422-
uid::Int
423-
gid::Int
422+
uid::UInt
423+
gid::UInt
424424
shell::String
425425
homedir::String
426426
gecos::String
427427
end
428428
struct Group
429429
groupname::String
430-
gid::Int
430+
gid::UInt
431431
mem::Vector{String}
432432
end
433433

434434
function getpwuid(uid::Unsigned, throw_error::Bool=true)
435435
ref_pd = Ref(Cpasswd())
436-
ret = ccall(:jl_os_get_passwd, Cint, (Ref{Cpasswd}, UInt), ref_pd, uid)
436+
ret = ccall(:jl_os_get_passwd, Cint, (Ref{Cpasswd}, Culong), ref_pd, uid)
437437
if ret != 0
438438
throw_error && Base.uv_error("getpwuid", ret)
439439
return
@@ -452,7 +452,7 @@ function getpwuid(uid::Unsigned, throw_error::Bool=true)
452452
end
453453
function getgrgid(gid::Unsigned, throw_error::Bool=true)
454454
ref_gp = Ref(Cgroup())
455-
ret = ccall(:jl_os_get_group, Cint, (Ref{Cgroup}, UInt), ref_gp, gid)
455+
ret = ccall(:jl_os_get_group, Cint, (Ref{Cgroup}, Culong), ref_gp, gid)
456456
if ret != 0
457457
throw_error && Base.uv_error("getgrgid", ret)
458458
return
@@ -475,6 +475,9 @@ function getgrgid(gid::Unsigned, throw_error::Bool=true)
475475
return gp
476476
end
477477

478+
getuid() = ccall(:jl_getuid, Culong, ())
479+
geteuid() = ccall(:jl_geteuid, Culong, ())
480+
478481
# Include dlopen()/dlpath() code
479482
include("libdl.jl")
480483
using .Libdl

src/sys.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,25 @@ JL_DLLEXPORT double jl_stat_ctime(char *statbuf)
229229
return (double)s->st_ctim.tv_sec + (double)s->st_ctim.tv_nsec * 1e-9;
230230
}
231231

232-
JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, size_t uid)
232+
JL_DLLEXPORT unsigned long jl_getuid(void)
233+
{
234+
#ifdef _OS_WINDOWS_
235+
return -1;
236+
#else
237+
return getuid();
238+
#endif
239+
}
240+
241+
JL_DLLEXPORT unsigned long jl_geteuid(void)
242+
{
243+
#ifdef _OS_WINDOWS_
244+
return -1;
245+
#else
246+
return geteuid();
247+
#endif
248+
}
249+
250+
JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, unsigned long uid)
233251
{
234252
#ifdef _OS_WINDOWS_
235253
return UV_ENOTSUP;
@@ -342,11 +360,11 @@ JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, size_t uid)
342360

343361
typedef struct jl_group_s {
344362
char* groupname;
345-
long gid;
363+
unsigned long gid;
346364
char** members;
347365
} jl_group_t;
348366

349-
JL_DLLEXPORT int jl_os_get_group(jl_group_t *grp, size_t gid)
367+
JL_DLLEXPORT int jl_os_get_group(jl_group_t *grp, unsigned long gid)
350368
{
351369
#ifdef _OS_WINDOWS_
352370
return UV_ENOTSUP;

test/file.jl

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

532532
if !Sys.iswindows()
533533
# chown will give an error if the user does not have permissions to change files
534-
if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root"
534+
uid = Libc.geteuid()
535+
@test stat(file).uid == uid
536+
@test uid == Libc.getuid()
537+
if uid == 0 # root user
535538
chown(file, -2, -1) # Change the file owner to nobody
536539
@test stat(file).uid != 0
537540
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
@@ -461,7 +461,7 @@ rm(f)
461461
io = Base.Filesystem.open(f, Base.Filesystem.JL_O_WRONLY | Base.Filesystem.JL_O_CREAT | Base.Filesystem.JL_O_EXCL, 0o000)
462462
@test write(io, "abc") == 3
463463
close(io)
464-
if !Sys.iswindows() && get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
464+
if !Sys.iswindows() && Libc.geteuid() != 0 # root user
465465
# msvcrt _wchmod documentation states that all files are readable,
466466
# so we don't test that it correctly set the umask on windows
467467
@test_throws SystemError open(f)
@@ -511,7 +511,7 @@ close(f1)
511511
close(f2)
512512
@test eof(f1)
513513
@test_throws Base.IOError eof(f2)
514-
if get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
514+
if Libc.geteuid() != 0 # root user
515515
@test_throws SystemError open(f, "r+")
516516
@test_throws Base.IOError Base.Filesystem.open(f, Base.Filesystem.JL_O_RDWR)
517517
else

0 commit comments

Comments
 (0)