Modify canRead and CanWrite functions of fsaccess class
Signed-off-by: Mayank Raghuwanshi <mayank.raghuwanshi@intel.com>
This commit is contained in:
parent
12ab2961b1
commit
52e9dd26a6
|
@ -168,23 +168,25 @@ ze_result_t FsAccess::write(const std::string file, const std::string val) {
|
|||
}
|
||||
|
||||
ze_result_t FsAccess::canRead(const std::string file) {
|
||||
if (access(file.c_str(), F_OK)) {
|
||||
struct stat sb;
|
||||
if (statSyscall(file.c_str(), &sb) != 0) {
|
||||
return ZE_RESULT_ERROR_UNKNOWN;
|
||||
}
|
||||
if (access(file.c_str(), R_OK)) {
|
||||
return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
|
||||
if (sb.st_mode & S_IRUSR) {
|
||||
return ZE_RESULT_SUCCESS;
|
||||
}
|
||||
return ZE_RESULT_SUCCESS;
|
||||
return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
|
||||
}
|
||||
|
||||
ze_result_t FsAccess::canWrite(const std::string file) {
|
||||
if (access(file.c_str(), F_OK)) {
|
||||
struct stat sb;
|
||||
if (statSyscall(file.c_str(), &sb) != 0) {
|
||||
return ZE_RESULT_ERROR_UNKNOWN;
|
||||
}
|
||||
if (access(file.c_str(), W_OK)) {
|
||||
return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
|
||||
if (sb.st_mode & S_IWUSR) {
|
||||
return ZE_RESULT_SUCCESS;
|
||||
}
|
||||
return ZE_RESULT_SUCCESS;
|
||||
return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS;
|
||||
}
|
||||
|
||||
bool FsAccess::fileExists(const std::string file) {
|
||||
|
|
|
@ -55,6 +55,7 @@ class FsAccess {
|
|||
protected:
|
||||
FsAccess();
|
||||
decltype(&NEO::SysCalls::access) accessSyscall = NEO::SysCalls::access;
|
||||
decltype(&stat) statSyscall = stat;
|
||||
};
|
||||
|
||||
class ProcfsAccess : private FsAccess {
|
||||
|
|
|
@ -134,6 +134,7 @@ class SysmanMultiDeviceFixture : public MultiDeviceFixture, public ::testing::Te
|
|||
class PublicFsAccess : public L0::FsAccess {
|
||||
public:
|
||||
using FsAccess::accessSyscall;
|
||||
using FsAccess::statSyscall;
|
||||
};
|
||||
|
||||
class PublicSysfsAccess : public L0::SysfsAccess {
|
||||
|
|
|
@ -20,6 +20,20 @@ inline static int mockAccessSuccess(const char *pathname, int mode) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
inline static int mockStatFailure(const char *pathname, struct stat *sb) noexcept {
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline static int mockStatSuccess(const char *pathname, struct stat *sb) noexcept {
|
||||
sb->st_mode = S_IWUSR | S_IRUSR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline static int mockStatNoPermissions(const char *pathname, struct stat *sb) noexcept {
|
||||
sb->st_mode = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenValidDeviceHandleInSysmanImpCreationWhenAllSysmanInterfacesAreAssignedToNullThenExpectSysmanDeviceModuleContextsAreNull) {
|
||||
ze_device_handle_t hSysman = device->toHandle();
|
||||
SysmanDeviceImp *sysmanImp = new SysmanDeviceImp(hSysman);
|
||||
|
@ -161,6 +175,58 @@ TEST_F(SysmanDeviceFixture, GivenPublicSysfsAccessClassWhenCallingDirectoryExist
|
|||
delete tempSysfsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingCanWriteWithUserHavingWritePermissionsThenSuccessIsReturned) {
|
||||
PublicFsAccess *tempFsAccess = new PublicFsAccess();
|
||||
tempFsAccess->statSyscall = mockStatSuccess;
|
||||
char cwd[PATH_MAX];
|
||||
std::string path = getcwd(cwd, PATH_MAX);
|
||||
EXPECT_EQ(ZE_RESULT_SUCCESS, tempFsAccess->canWrite(path));
|
||||
delete tempFsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingCanReadWithUserHavingReadPermissionsThenSuccessIsReturned) {
|
||||
PublicFsAccess *tempFsAccess = new PublicFsAccess();
|
||||
tempFsAccess->statSyscall = mockStatSuccess;
|
||||
char cwd[PATH_MAX];
|
||||
std::string path = getcwd(cwd, PATH_MAX);
|
||||
EXPECT_EQ(ZE_RESULT_SUCCESS, tempFsAccess->canRead(path));
|
||||
delete tempFsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingCanWriteWithUserNotHavingWritePermissionsThenInsufficientIsReturned) {
|
||||
PublicFsAccess *tempFsAccess = new PublicFsAccess();
|
||||
tempFsAccess->statSyscall = mockStatNoPermissions;
|
||||
char cwd[PATH_MAX];
|
||||
std::string path = getcwd(cwd, PATH_MAX);
|
||||
EXPECT_EQ(ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS, tempFsAccess->canWrite(path));
|
||||
delete tempFsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingCanReadWithUserNotHavingReadPermissionsThenInsufficientIsReturned) {
|
||||
PublicFsAccess *tempFsAccess = new PublicFsAccess();
|
||||
tempFsAccess->statSyscall = mockStatNoPermissions;
|
||||
char cwd[PATH_MAX];
|
||||
std::string path = getcwd(cwd, PATH_MAX);
|
||||
EXPECT_EQ(ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS, tempFsAccess->canRead(path));
|
||||
delete tempFsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingCanReadWithInvalidPathThenErrorIsReturned) {
|
||||
PublicFsAccess *tempFsAccess = new PublicFsAccess();
|
||||
tempFsAccess->statSyscall = mockStatFailure;
|
||||
std::string path = "invalidPath";
|
||||
EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, tempFsAccess->canRead(path));
|
||||
delete tempFsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingCanWriteWithInvalidPathThenErrorIsReturned) {
|
||||
PublicFsAccess *tempFsAccess = new PublicFsAccess();
|
||||
tempFsAccess->statSyscall = mockStatFailure;
|
||||
std::string path = "invalidPath";
|
||||
EXPECT_EQ(ZE_RESULT_ERROR_UNKNOWN, tempFsAccess->canRead(path));
|
||||
delete tempFsAccess;
|
||||
}
|
||||
|
||||
TEST_F(SysmanDeviceFixture, GivenValidPathnameWhenCallingFsAccessExistsThenSuccessIsReturned) {
|
||||
auto FsAccess = pLinuxSysmanImp->getFsAccess();
|
||||
|
||||
|
|
Loading…
Reference in New Issue