Skip to content

Commit

Permalink
Update the file.jl tests to allow for both EPERM and EINVAL in …
Browse files Browse the repository at this point in the history
…the non-root CHOWN tests (JuliaLang#41682)

Co-authored-by: Elliot Saba <[email protected]>

Co-authored-by: Elliot Saba <[email protected]>
  • Loading branch information
DilumAluthge and staticfloat authored Jul 23, 2021
1 parent 9232de9 commit 114ee17
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,29 @@ rm(c_tmpdir, recursive=true)
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir, recursive=true)
@test rm(c_tmpdir, force=true, recursive=true) === nothing

# Some operations can return multiple different error codes depending on the system environment.
function throws_matching_exception(f::Function, acceptable_exceptions::AbstractVector)
try
f()
@error "No exception was thrown."
return false
catch ex
if ex in acceptable_exceptions
return true
else
@error "The thrown exception is not in the list of acceptable exceptions" acceptable_exceptions exception=(ex, catch_backtrace())
return false
end
end
end
function throws_matching_uv_error(f::Function, pfx::AbstractString, codes::AbstractVector{<:Integer})
acceptable_exceptions = multiple_uv_errors(pfx, codes)
return throws_matching_exception(f, acceptable_exceptions)
end
function multiple_uv_errors(pfx::AbstractString, codes::AbstractVector{<:Integer})
return [Base._UVError(pfx, code) for code in codes]
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"
Expand All @@ -518,8 +541,12 @@ if !Sys.iswindows()
@test stat(file).gid == 0
@test stat(file).uid == 0
else
@test_throws Base._UVError("chown($(repr(file)), -2, -1)", Base.UV_EPERM) chown(file, -2, -1) # Non-root user cannot change ownership to another user
@test_throws Base._UVError("chown($(repr(file)), -1, -2)", Base.UV_EPERM) chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
@test throws_matching_uv_error("chown($(repr(file)), -2, -1)", [Base.UV_EPERM, Base.UV_EINVAL]) do
chown(file, -2, -1) # Non-root user cannot change ownership to another user
end
@test throws_matching_uv_error("chown($(repr(file)), -1, -2)", [Base.UV_EPERM, Base.UV_EINVAL]) do
chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
end
end
else
# test that chown doesn't cause any errors for Windows
Expand Down

0 comments on commit 114ee17

Please sign in to comment.