From 2e2e643522d848d1786b20cdaf8c231485c7fa86 Mon Sep 17 00:00:00 2001 From: Kurt McMillan Date: Fri, 14 Nov 2025 17:41:04 +0000 Subject: [PATCH 1/2] rocfile/test: Introduce expect_file_registration(...) --- rocfile/test/driver.cpp | 14 +++----- rocfile/test/handle.cpp | 61 +++++++++++++++++----------------- rocfile/test/io.cpp | 66 +++++++++++++++++++------------------ rocfile/test/rocfile-test.h | 27 +++++++++++---- 4 files changed, 89 insertions(+), 79 deletions(-) diff --git a/rocfile/test/driver.cpp b/rocfile/test/driver.cpp index 3ca76e47..4d9ffcfa 100644 --- a/rocfile/test/driver.cpp +++ b/rocfile/test/driver.cpp @@ -63,9 +63,7 @@ TEST_F(RocFileDriverAdmin, HandleRegisterInitsDriver) descr.fs_ops = nullptr; ASSERT_EQ(rocFileUseCount(), 0); - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&handle, &descr), ROCFILE_SUCCESS); ASSERT_EQ(rocFileUseCount(), 1); } @@ -85,9 +83,7 @@ TEST_F(RocFileDriverAdmin, HandleRegisterGoodFD) descr.type = rocFileHandleTypeOpaqueFD; descr.fs_ops = nullptr; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileUseCount(), 0); ASSERT_EQ(rocFileHandleRegister(&handle, &descr), ROCFILE_SUCCESS); @@ -151,15 +147,13 @@ TEST_F(RocFileDriverAdmin, CloseDeregistersFile) descr.type = rocFileHandleTypeOpaqueFD; descr.fs_ops = nullptr; - EXPECT_CALL(msys, fstat).Times(2); - EXPECT_CALL(msys, fcntl).Times(2); - EXPECT_CALL(mlibmounthelper, getMountInfo).Times(2); - ASSERT_EQ(rocFileUseCount(), 0); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&handle, &descr), ROCFILE_SUCCESS); ASSERT_EQ(rocFileUseCount(), 1); ASSERT_EQ(rocFileDriverClose(), ROCFILE_SUCCESS); ASSERT_EQ(rocFileUseCount(), 0); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&handle, &descr), ROCFILE_SUCCESS); ASSERT_EQ(rocFileUseCount(), 1); } diff --git a/rocfile/test/handle.cpp b/rocfile/test/handle.cpp index 9cee9e76..cec2a334 100644 --- a/rocfile/test/handle.cpp +++ b/rocfile/test/handle.cpp @@ -8,6 +8,7 @@ #include "hipfile-warnings.h" #include "msys.h" #include "mmountinfo.h" +#include "mountinfo.h" #include "rocfile.h" #include "rocfile-test.h" #include "state.h" @@ -24,15 +25,29 @@ #include using namespace rocFile; - -using ::testing::Return; -using ::testing::StrictMock; -using ::testing::Throw; +using namespace testing; // Put tests inside the macros to suppress the global constructor // warnings HIPFILE_WARN_NO_GLOBAL_CTOR_OFF +void +expect_file_registration(MSys &msys, MLibMountHelper &mlibmounthelper) +{ + EXPECT_CALL(msys, fstat); + EXPECT_CALL(msys, fcntl(_, F_GETFL, 0)); + EXPECT_CALL(mlibmounthelper, getMountInfo); +} + +void +expect_file_registration(MSys &msys, MLibMountHelper &mlibmounthelper, struct stat statbuf, int fcntl_flags, + MountInfo mountinfo) +{ + EXPECT_CALL(msys, fstat).WillOnce(Return(statbuf)); + EXPECT_CALL(msys, fcntl(_, F_GETFL, 0)).WillOnce(Return(fcntl_flags)); + EXPECT_CALL(mlibmounthelper, getMountInfo).WillOnce(Return(mountinfo)); +} + struct RocFileHandle : public RocFileOpened {}; TEST_F(RocFileHandle, register_handle_internal_linux_fd) @@ -42,9 +57,7 @@ TEST_F(RocFileHandle, register_handle_internal_linux_fd) int fd{0xBADF00D}; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); ASSERT_NE(Context::get()->registerFile(fd), nullptr); } @@ -62,9 +75,7 @@ TEST_F(RocFileHandle, file_initialization) mountinfo.type = FilesystemType::ext4; mountinfo.options.ext4.journaling_mode = ExtJournalingMode::ordered; - EXPECT_CALL(msys, fstat(fd)).WillOnce(Return(fstat)); - EXPECT_CALL(msys, fcntl(fd, F_GETFL, 0)).WillOnce(Return(status_flags)); - EXPECT_CALL(mlibmounthelper, getMountInfo(fstat.st_dev)).WillOnce(Return(std::make_optional(mountinfo))); + expect_file_registration(msys, mlibmounthelper, fstat, status_flags, mountinfo); auto fh{Context::get()->registerFile(fd)}; auto file{Context::get()->getFile(fh)}; @@ -84,10 +95,9 @@ TEST_F(RocFileHandle, register_handle_internal_linux_fd_already_registered) StrictMock mlibmounthelper; int fd{0xBADF00D}; - EXPECT_CALL(msys, fstat).Times(2); - EXPECT_CALL(msys, fcntl).Times(2); - EXPECT_CALL(mlibmounthelper, getMountInfo).Times(2); + expect_file_registration(msys, mlibmounthelper); ASSERT_NE(Context::get()->registerFile(fd), nullptr); + expect_file_registration(msys, mlibmounthelper); ASSERT_THROW(Context::get()->registerFile(fd), FileAlreadyRegistered); } @@ -102,9 +112,7 @@ TEST_F(RocFileHandle, register_handle_linux_fd) rfd.type = rocFileHandleTypeOpaqueFD; rfd.handle.fd = 0xBADF00D; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&fh, &rfd), ROCFILE_SUCCESS); ASSERT_NE(fh, nullptr); } @@ -171,11 +179,10 @@ TEST_F(RocFileHandle, register_handle_linux_fd_already_registered) rfd.type = rocFileHandleTypeOpaqueFD; rfd.handle.fd = 0xBADF00D; - EXPECT_CALL(msys, fstat).Times(2); - EXPECT_CALL(msys, fcntl).Times(2); - EXPECT_CALL(mlibmounthelper, getMountInfo).Times(2); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&fh, &rfd), ROCFILE_SUCCESS); ASSERT_NE(fh, nullptr); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&fh, &rfd), RocFileOpError(rocFileHandleAlreadyRegistered)); } @@ -223,9 +230,7 @@ TEST_F(RocFileHandle, deregister_handle_internal) { StrictMock msys; StrictMock mlibmounthelper; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); auto fh = Context::get()->registerFile(0xBADF00D); Context::get()->deregisterFile(fh); ASSERT_THROW(Context::get()->deregisterFile(fh), FileNotRegistered); @@ -242,9 +247,7 @@ TEST_F(RocFileHandle, deregister_handle) rfd.type = rocFileHandleTypeOpaqueFD; rfd.handle.fd = 0xBADF00D; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&fh, &rfd), ROCFILE_SUCCESS); ASSERT_EQ(rocFileHandleDeregister(fh), ROCFILE_SUCCESS); ASSERT_EQ(rocFileHandleDeregister(fh), RocFileOpError(rocFileHandleNotRegistered)); @@ -255,9 +258,7 @@ TEST_F(RocFileHandle, deregister_handle_internal_fails_when_operations_are_ousta StrictMock msys; StrictMock mlibmounthelper; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); auto fh = Context::get()->registerFile(0xBADF00D); { auto file = Context::get()->getFile(fh); @@ -277,9 +278,7 @@ TEST_F(RocFileHandle, deregister_handle_fails_when_operations_are_oustanding) rfd.type = rocFileHandleTypeOpaqueFD; rfd.handle.fd = 0xBADF00D; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); - EXPECT_CALL(mlibmounthelper, getMountInfo); + expect_file_registration(msys, mlibmounthelper); ASSERT_EQ(rocFileHandleRegister(&fh, &rfd), ROCFILE_SUCCESS); { auto file = Context::get()->getFile(fh); diff --git a/rocfile/test/io.cpp b/rocfile/test/io.cpp index e89aba89..2a22e322 100644 --- a/rocfile/test/io.cpp +++ b/rocfile/test/io.cpp @@ -16,6 +16,7 @@ #include "mbuffer.h" #include "mfile.h" #include "mhip.h" +#include "mmountinfo.h" #include "mstate.h" #include "msys.h" #include "rocfile.h" @@ -118,8 +119,9 @@ struct RocFileIO : public RocFileOpened { RocFileIO() : buffer_data(1024 * 1024) { - StrictMock mhip; - StrictMock msys; + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; // Initialize DriverState::backends EXPECT_CALL(mhip, hipRuntimeGetVersion); @@ -130,8 +132,7 @@ struct RocFileIO : public RocFileOpened { Context::get()->registerBuffer(buffer_data.data(), buffer_data.size(), 0); buffer = Context::get()->getBuffer(buffer_data.data()); - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + expect_file_registration(msys, mlibmounthelper); file = Context::get()->getFile(Context::get()->registerFile(0xBADF00D)); } @@ -183,8 +184,9 @@ struct RocFileFallbackValidation : ::testing::TestWithParam { RocFileFallbackValidation() { - StrictMock mhip; - StrictMock msys; + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; assert(rocFileDriverOpen() == ROCFILE_SUCCESS); @@ -193,8 +195,7 @@ struct RocFileFallbackValidation : ::testing::TestWithParam { Context::get()->registerBuffer(buf, 4096, 0); buffer = Context::get()->getBuffer(buf); - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + expect_file_registration(msys, mlibmounthelper); file = Context::get()->getFile(Context::get()->registerFile(0xBADF00D)); } @@ -369,10 +370,10 @@ struct RocFileWrite : public RocFileIO { TEST_F(RocFileWrite, write_handles_pointer_get_attributes_error) { - StrictMock mhip; - StrictMock msys; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; + expect_file_registration(msys, mlibmounthelper); auto fh{Context::get()->registerFile(0xBADCAFE)}; EXPECT_CALL(mhip, hipPointerGetAttributes).WillOnce(testing::Throw(Hip::RuntimeError(hipErrorUnknown))); ASSERT_EQ(rocFileWrite(fh, nonnull_ptr, 0, 0, 0), -static_cast(hipErrorUnknown)); @@ -380,9 +381,10 @@ TEST_F(RocFileWrite, write_handles_pointer_get_attributes_error) TEST_F(RocFileWrite, write_handles_unsupported_hip_memory_type) { - StrictMock msys; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + StrictMock msys; + StrictMock mlibmounthelper; + + expect_file_registration(msys, mlibmounthelper); auto fh = Context::get()->registerFile(0xBADCAFE); for (const auto memoryType : UnsupportedHipMemoryTypes) { StrictMock mhip; @@ -395,13 +397,13 @@ TEST_F(RocFileWrite, write_handles_unsupported_hip_memory_type) TEST_F(RocFileWrite, write_handles_invalid_length_of_registered_buffer) { - StrictMock mhip; - StrictMock msys; - size_t buffer_length = 0; + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; + size_t buffer_length = 0; expect_buffer_registration(mhip, hipMemoryTypeDevice); Context::get()->registerBuffer(nonnull_ptr, buffer_length, 0); - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + expect_file_registration(msys, mlibmounthelper); auto fh{Context::get()->registerFile(0xBADCAFE)}; ASSERT_EQ(rocFileWrite(fh, nonnull_ptr, buffer_length + 1, 0, 0), -static_cast(rocFileInvalidValue)); @@ -693,10 +695,10 @@ struct RocFileRead : public RocFileIO { TEST_F(RocFileRead, read_handles_pointer_get_attributes_error) { - StrictMock mhip; - StrictMock msys; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; + expect_file_registration(msys, mlibmounthelper); auto fh{Context::get()->registerFile(0xBADCAFE)}; EXPECT_CALL(mhip, hipPointerGetAttributes).WillOnce(testing::Throw(Hip::RuntimeError(hipErrorUnknown))); ASSERT_EQ(rocFileRead(fh, nonnull_ptr, 0, 0, 0), -static_cast(hipErrorUnknown)); @@ -704,10 +706,10 @@ TEST_F(RocFileRead, read_handles_pointer_get_attributes_error) TEST_F(RocFileRead, read_handles_unsupported_hip_memory_type) { - StrictMock mhip; - StrictMock msys; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; + expect_file_registration(msys, mlibmounthelper); auto fh{Context::get()->registerFile(0xBADCAFE)}; for (const auto memoryType : UnsupportedHipMemoryTypes) { hipPointerAttribute_t attrs; @@ -719,10 +721,10 @@ TEST_F(RocFileRead, read_handles_unsupported_hip_memory_type) TEST_F(RocFileRead, read_handles_invalid_length_of_registered_buffer) { - StrictMock mhip; - StrictMock msys; - EXPECT_CALL(msys, fstat); - EXPECT_CALL(msys, fcntl); + StrictMock mhip; + StrictMock msys; + StrictMock mlibmounthelper; + expect_file_registration(msys, mlibmounthelper); auto fh{Context::get()->registerFile(0xBADCAFE)}; size_t buffer_length{0}; expect_buffer_registration(mhip, hipMemoryTypeDevice); diff --git a/rocfile/test/rocfile-test.h b/rocfile/test/rocfile-test.h index 86644fcd..a433dc97 100644 --- a/rocfile/test/rocfile-test.h +++ b/rocfile/test/rocfile-test.h @@ -8,17 +8,17 @@ // Common rocFile test functionality #include "magic-word.h" -#include "rocfile-test.h" - #include "mhip.h" - +#include "mmountinfo.h" +#include "msys.h" +#include "rocfile-test.h" #include "rocfile.h" -#include -#include - #include #include +#include +#include +#include // *********************************************************************** // ERRORS AND ERROR HANDLING @@ -116,6 +116,21 @@ struct RocFileUnopened : public ::testing::Test { /// @brief Set up mocks for buffer registration void expect_buffer_registration(rocFile::MHip &mhip, hipMemoryType memory_type); +// *********************************************************************** +// FILE FUNCTIONALITY +// *********************************************************************** + +/// @brief Setup mocks for file registration +/// +/// Mock methods will return default values +void expect_file_registration(rocFile::MSys &msys, rocFile::MLibMountHelper &mlibmounthelper); + +/// @brief Setup mocks for file registration +/// +/// Mock methods will return the specified values +void expect_file_registration(rocFile::MSys &msys, rocFile::MLibMountHelper &mlibmounthelper, + struct stat statbuf, int fcntl_flags, rocFile::MountInfo mountinfo); + // *********************************************************************** // ENUM VALUE HELPERS // *********************************************************************** From 4542c5d2622cdb31399087017a9d787cad3c2b74 Mon Sep 17 00:00:00 2001 From: Kurt McMillan Date: Fri, 14 Nov 2025 17:51:18 +0000 Subject: [PATCH 2/2] rocfile/test: Move mocks into RocFileHandle --- rocfile/test/handle.cpp | 46 ++++------------------------------------- 1 file changed, 4 insertions(+), 42 deletions(-) diff --git a/rocfile/test/handle.cpp b/rocfile/test/handle.cpp index cec2a334..650ade84 100644 --- a/rocfile/test/handle.cpp +++ b/rocfile/test/handle.cpp @@ -48,13 +48,13 @@ expect_file_registration(MSys &msys, MLibMountHelper &mlibmounthelper, struct st EXPECT_CALL(mlibmounthelper, getMountInfo).WillOnce(Return(mountinfo)); } -struct RocFileHandle : public RocFileOpened {}; - -TEST_F(RocFileHandle, register_handle_internal_linux_fd) -{ +struct RocFileHandle : public RocFileOpened { StrictMock msys; StrictMock mlibmounthelper; +}; +TEST_F(RocFileHandle, register_handle_internal_linux_fd) +{ int fd{0xBADF00D}; expect_file_registration(msys, mlibmounthelper); @@ -63,9 +63,6 @@ TEST_F(RocFileHandle, register_handle_internal_linux_fd) TEST_F(RocFileHandle, file_initialization) { - StrictMock msys; - StrictMock mlibmounthelper; - int fd{0x12345678}; int status_flags{0x789ABCDE}; struct stat fstat; @@ -91,9 +88,6 @@ TEST_F(RocFileHandle, file_initialization) TEST_F(RocFileHandle, register_handle_internal_linux_fd_already_registered) { - StrictMock msys; - StrictMock mlibmounthelper; - int fd{0xBADF00D}; expect_file_registration(msys, mlibmounthelper); ASSERT_NE(Context::get()->registerFile(fd), nullptr); @@ -103,9 +97,6 @@ TEST_F(RocFileHandle, register_handle_internal_linux_fd_already_registered) TEST_F(RocFileHandle, register_handle_linux_fd) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; @@ -120,9 +111,6 @@ TEST_F(RocFileHandle, register_handle_linux_fd) // If the fstat() fails during file registration return rocfileInternalError TEST_F(RocFileHandle, RocfileHandleRegisterFstatError) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; rfd.type = rocFileHandleTypeOpaqueFD; @@ -136,9 +124,6 @@ TEST_F(RocFileHandle, RocfileHandleRegisterFstatError) // If the fcntl() fails during file registration return rocfileInternalError TEST_F(RocFileHandle, RocfileHandleRegisterFcntlError) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; rfd.type = rocFileHandleTypeOpaqueFD; @@ -153,9 +138,6 @@ TEST_F(RocFileHandle, RocfileHandleRegisterFcntlError) // If getting mount information fails during file registration return rocfileInternalError TEST_F(RocFileHandle, RocfileHandleRegisterLibMountError) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; rfd.type = rocFileHandleTypeOpaqueFD; @@ -170,9 +152,6 @@ TEST_F(RocFileHandle, RocfileHandleRegisterLibMountError) TEST_F(RocFileHandle, register_handle_linux_fd_already_registered) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; @@ -188,9 +167,6 @@ TEST_F(RocFileHandle, register_handle_linux_fd_already_registered) TEST_F(RocFileHandle, register_handle_windows_handle_not_supported) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; @@ -202,9 +178,6 @@ TEST_F(RocFileHandle, register_handle_windows_handle_not_supported) TEST_F(RocFileHandle, register_handle_userspace_fs_not_supported) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; @@ -228,8 +201,6 @@ TEST_F(RocFileHandle, deregister_handle_returns_error_if_not_registered) TEST_F(RocFileHandle, deregister_handle_internal) { - StrictMock msys; - StrictMock mlibmounthelper; expect_file_registration(msys, mlibmounthelper); auto fh = Context::get()->registerFile(0xBADF00D); Context::get()->deregisterFile(fh); @@ -238,9 +209,6 @@ TEST_F(RocFileHandle, deregister_handle_internal) TEST_F(RocFileHandle, deregister_handle) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{}; @@ -255,9 +223,6 @@ TEST_F(RocFileHandle, deregister_handle) TEST_F(RocFileHandle, deregister_handle_internal_fails_when_operations_are_oustanding) { - StrictMock msys; - StrictMock mlibmounthelper; - expect_file_registration(msys, mlibmounthelper); auto fh = Context::get()->registerFile(0xBADF00D); { @@ -269,9 +234,6 @@ TEST_F(RocFileHandle, deregister_handle_internal_fails_when_operations_are_ousta TEST_F(RocFileHandle, deregister_handle_fails_when_operations_are_oustanding) { - StrictMock msys; - StrictMock mlibmounthelper; - rocFileHandle_t fh{}; rocFileDescr_t rfd{};