From 65d97e289247c3dd7dc72ef5147b3ce9fedcbd08 Mon Sep 17 00:00:00 2001 From: Brian Cox Date: Sat, 8 Jul 2017 16:29:34 -0700 Subject: [PATCH] Rework fileutil_t test to not require /etc/hosts; tweak twutil_t test to work properly if run as root --- src/twtest/fcopropvector_t.cpp | 8 ++++---- src/twtest/fileutil_t.cpp | 32 +++++++++++++++++++++++--------- src/twtest/twutil_t.cpp | 7 +++++-- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/twtest/fcopropvector_t.cpp b/src/twtest/fcopropvector_t.cpp index 00f19eb..e82d470 100644 --- a/src/twtest/fcopropvector_t.cpp +++ b/src/twtest/fcopropvector_t.cpp @@ -99,7 +99,7 @@ void TestFCOPropVector() d.TraceDetail("Object manipulation tests are not successful\n"); return; -}//end TestPropVector +} static bool init (cFCOPropVector &testV) { @@ -111,7 +111,7 @@ static bool init (cFCOPropVector &testV) return false; } //end for return true; -} //end init +} 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. return out; -}//end addRemove +} static bool objManip (cFCOPropVector &testV, cDebug& d) { @@ -201,4 +201,4 @@ static bool objManip (cFCOPropVector &testV, cDebug& d) TEST((v1 ^ v2) == v3); return out; -}//end objManip +} diff --git a/src/twtest/fileutil_t.cpp b/src/twtest/fileutil_t.cpp index 3f132f6..05d2936 100644 --- a/src/twtest/fileutil_t.cpp +++ b/src/twtest/fileutil_t.cpp @@ -38,6 +38,8 @@ #include "util/stdutil.h" #include "util/fileutil.h" #include "core/debug.h" +#include "test.h" +#include using namespace std; @@ -47,15 +49,27 @@ using namespace std; void TestFileUtil() { - if(cFileUtil::FileExists("/etc/hosts")) - { - TSTRING source, dest; + TSTRING source = TEMP_DIR; + source += _T("/copy_src"); - source = _T("/etc/hosts"); - dest = _T("/tmp/dest"); - bool blah = cFileUtil::Copy(source, dest); - (void)blah; - // TCOUT << _T("<") << wstr3 << _T(">") << std::endl; - } + //Create a temporary file for testing: + FILE* testStream; + testStream = _tfopen( source.c_str(), _T("w+b")); + TEST(testStream); + + 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()); } diff --git a/src/twtest/twutil_t.cpp b/src/twtest/twutil_t.cpp index fe38d53..d844585 100644 --- a/src/twtest/twutil_t.cpp +++ b/src/twtest/twutil_t.cpp @@ -80,7 +80,10 @@ void TestTWUtil() // make the dir read only and make sure write tests false // windows fails this test, perhaps because I am an administrator? 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); // create the file @@ -91,7 +94,7 @@ void TestTWUtil() // test a read only file chmod(tmpFN.c_str(), 0400); - TEST(cFileUtil::FileWritable(tmpFN) == false); + TEST(cFileUtil::FileWritable(tmpFN) == is_root); // test a writable file chmod(tmpFN.c_str(), 0666);