Rework fileutil_t test to not require /etc/hosts; tweak twutil_t test to work properly if run as root

This commit is contained in:
Brian Cox 2017-07-08 16:29:34 -07:00
parent 122010acc2
commit 65d97e2892
3 changed files with 32 additions and 15 deletions

View File

@ -99,7 +99,7 @@ void TestFCOPropVector()
d.TraceDetail("Object manipulation tests are not successful\n"); d.TraceDetail("Object manipulation tests are not successful\n");
return; return;
}//end TestPropVector }
static bool init (cFCOPropVector &testV) static bool init (cFCOPropVector &testV)
{ {
@ -111,7 +111,7 @@ static bool init (cFCOPropVector &testV)
return false; return false;
} //end for } //end for
return true; return true;
} //end init }
static bool addRemove (cFCOPropVector &test1, cFCOPropVector &test2, cDebug& d) static bool addRemove (cFCOPropVector &test1, cFCOPropVector &test2, cDebug& d)
@ -141,7 +141,7 @@ static bool addRemove (cFCOPropVector &test1, cFCOPropVector &test2, cDebug& d)
out &= local; //and-ing of results. out &= local; //and-ing of results.
return out; return out;
}//end addRemove }
static bool objManip (cFCOPropVector &testV, cDebug& d) static bool objManip (cFCOPropVector &testV, cDebug& d)
{ {
@ -201,4 +201,4 @@ static bool objManip (cFCOPropVector &testV, cDebug& d)
TEST((v1 ^ v2) == v3); TEST((v1 ^ v2) == v3);
return out; return out;
}//end objManip }

View File

@ -38,6 +38,8 @@
#include "util/stdutil.h" #include "util/stdutil.h"
#include "util/fileutil.h" #include "util/fileutil.h"
#include "core/debug.h" #include "core/debug.h"
#include "test.h"
#include <unistd.h>
using namespace std; using namespace std;
@ -47,15 +49,27 @@ using namespace std;
void TestFileUtil() void TestFileUtil()
{ {
if(cFileUtil::FileExists("/etc/hosts")) TSTRING source = TEMP_DIR;
{ source += _T("/copy_src");
TSTRING source, dest;
source = _T("/etc/hosts"); //Create a temporary file for testing:
dest = _T("/tmp/dest"); FILE* testStream;
bool blah = cFileUtil::Copy(source, dest); testStream = _tfopen( source.c_str(), _T("w+b"));
(void)blah; TEST(testStream);
// TCOUT << _T("<") << wstr3 << _T(">") << std::endl;
} TSTRING testString( _T("This is a test") );
int iTestStringLength = testString.length();
//Write some data to the stream...
fwrite( testString.c_str(), sizeof(TCHAR), iTestStringLength, testStream );
fclose( testStream );
TSTRING dest = TEMP_DIR;
dest += "/copy_dest";
TEST(cFileUtil::Copy(source, dest));
unlink(dest.c_str());
unlink(source.c_str());
} }

View File

@ -80,7 +80,10 @@ void TestTWUtil()
// make the dir read only and make sure write tests false // make the dir read only and make sure write tests false
// windows fails this test, perhaps because I am an administrator? // windows fails this test, perhaps because I am an administrator?
chmod(tmpDir.c_str(), 0500); chmod(tmpDir.c_str(), 0500);
TEST(cFileUtil::FileWritable(tmpFN) == false); bool is_root = (0 == getuid());
TEST(cFileUtil::FileWritable(tmpFN) == is_root);
chmod(tmpDir.c_str(), 0700); chmod(tmpDir.c_str(), 0700);
// create the file // create the file
@ -91,7 +94,7 @@ void TestTWUtil()
// test a read only file // test a read only file
chmod(tmpFN.c_str(), 0400); chmod(tmpFN.c_str(), 0400);
TEST(cFileUtil::FileWritable(tmpFN) == false); TEST(cFileUtil::FileWritable(tmpFN) == is_root);
// test a writable file // test a writable file
chmod(tmpFN.c_str(), 0666); chmod(tmpFN.c_str(), 0666);