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 (#41682)

Co-authored-by: Elliot Saba <staticfloat@gmail.com>

Co-authored-by: Elliot Saba <staticfloat@gmail.com>
(cherry picked from commit 114ee17)
  • Loading branch information
DilumAluthge authored and KristofferC committed Sep 3, 2021
1 parent d32c44d commit 3e8eb9e
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 @@ -491,6 +491,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 @@ -503,8 +526,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 3e8eb9e

Please sign in to comment.