Skip to content

Commit

Permalink
Change rightsSet to Rightset and add a constructor with an int as input.
Browse files Browse the repository at this point in the history
  • Loading branch information
herve-er committed Apr 25, 2024
1 parent f12af12 commit c435db8
Showing 1 changed file with 10 additions and 32 deletions.
42 changes: 10 additions & 32 deletions test/libcommonserver/io/testchecksetgetrights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,14 @@ using namespace CppUnit;

namespace KDC {

static struct rightsSet {
static struct RightsSet {
RightsSet(int rights) : read(rights & 4), write(rights & 2), execute(rights & 1){};
RightsSet(bool read, bool write, bool execute) : read(read), write(write), execute(execute){};
bool read;
bool write;
bool execute;
};

static void rightsSetFromInt(int rights, rightsSet& rightsSet) {
rightsSet.read = rights & 4;
rightsSet.write = rights & 2;
rightsSet.execute = rights & 1;
}

static int rightsSetToInt(rightsSet rightsSet) {
int rights = 0;
if (rightsSet.read) {
rights += 4;
}
if (rightsSet.write) {
rights += 2;
}
if (rightsSet.execute) {
rights += 1;
}

return rights;
}

void TestIo::testCheckSetAndGetRights() {
#ifdef _WIN32
CPPUNIT_ASSERT(Utility::init()); // Initialize the utility library, needed to access/change the permissions on Windows
Expand All @@ -71,7 +52,7 @@ void TestIo::testCheckSetAndGetRights() {
bool isExecutable = false;
bool exists = false;

rightsSet rightsSet = {false, false, false};
RightsSet rightsSet(false, false, false);

// For a directory

Expand All @@ -97,8 +78,7 @@ void TestIo::testCheckSetAndGetRights() {

for (int baseRigths = 0; baseRigths < 7; baseRigths++) {
for (int targetRigths = baseRigths + 1; targetRigths < 8; targetRigths++) {
rightsSetFromInt(baseRigths, rightsSet);

rightsSet = RightsSet(baseRigths);
bool result = IoHelper::setRights(path, rightsSet.read, rightsSet.write, rightsSet.execute, ioError);
result &= ioError == IoErrorSuccess;
if (!result) {
Expand All @@ -118,8 +98,7 @@ void TestIo::testCheckSetAndGetRights() {
CPPUNIT_ASSERT(false /* Set base rights mismatch with get base rights */);
}

rightsSetFromInt(targetRigths, rightsSet);

rightsSet = RightsSet(targetRigths);
result = IoHelper::setRights(path, rightsSet.read, rightsSet.write, rightsSet.execute, ioError);
result &= ioError == IoErrorSuccess;
if (!result) {
Expand Down Expand Up @@ -160,14 +139,14 @@ void TestIo::testCheckSetAndGetRights() {
bool isExecutable = false;
bool exists = false;

rightsSet rightsSet = {false, false, false};
RightsSet rightsSet = {false, false, false};

// For a directory
for (int baseRigths = 0; baseRigths < 7;
baseRigths++) { // Test all the possible rights and the all the possible order of rights modification
for (int targetRigths = baseRigths + 1; targetRigths < 8; targetRigths++) {
rightsSetFromInt(baseRigths, rightsSet);

rightsSet = RightsSet(baseRigths);
bool result = IoHelper::setRights(filepath, rightsSet.read, rightsSet.write, rightsSet.execute, ioError);
result &= ioError == IoErrorSuccess;
if (!result) {
Expand All @@ -187,8 +166,7 @@ void TestIo::testCheckSetAndGetRights() {
CPPUNIT_ASSERT(false /* Set base rights mismatch with get base rights */);
}

rightsSetFromInt(targetRigths, rightsSet);

rightsSet = RightsSet(targetRigths);
result = IoHelper::setRights(filepath, rightsSet.read, rightsSet.write, rightsSet.execute, ioError);
result &= ioError == IoErrorSuccess;
if (!result) {
Expand Down Expand Up @@ -228,7 +206,7 @@ void TestIo::testCheckSetAndGetRights() {
CPPUNIT_ASSERT(IoHelper::createDirectory(subFolderPath, ioError));
CPPUNIT_ASSERT_EQUAL(IoErrorSuccess, ioError);

std::ofstream file(subFilePath.string());
std::ofstream file(subFilePath);
CPPUNIT_ASSERT(file.is_open());
file << "testCheckSetAndGetRights";
file.close();
Expand Down

0 comments on commit c435db8

Please sign in to comment.