From cd97c4d16650f6ca52988035e863c582874b1b41 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Thu, 22 Jul 2021 20:21:27 -0400 Subject: [PATCH] Update the `file.jl` tests to allow for both `EPERM` and `EINVAL` in the non-root CHOWN tests (#41682) Co-authored-by: Elliot Saba Co-authored-by: Elliot Saba (cherry picked from commit 114ee174cff04b7ecf3514d58e310384fa1bbf17) --- test/file.jl | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/test/file.jl b/test/file.jl index 0f39bc7c140f6..4f93417deb112 100644 --- a/test/file.jl +++ b/test/file.jl @@ -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" @@ -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