Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the file.jl tests to allow for both EPERM and EINVAL in the non-root CHOWN tests #41682

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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