Robustify symlink reading in cFSPropCalc, & add a unit test for it. Tweak other unit tests to use a test directory we control, rather than dumping stuff in /tmp

This commit is contained in:
Brian Cox 2017-08-23 23:36:21 -07:00
parent fafa681bce
commit f5e76827be
21 changed files with 136 additions and 76 deletions

View File

@ -14,5 +14,6 @@ uninstall-hook:
check:
rm -Rf $(top_srcdir)/src/test-harness/twtest
rm -Rf $(top_srcdir)/bin/TWTestData
cd $(top_srcdir)/src/test-harness && perl ./twtest.pl
$(top_srcdir)/bin/twtest all
cd $(top_srcdir)/bin && ./twtest all

View File

@ -809,8 +809,9 @@ uninstall-hook:
check:
rm -Rf $(top_srcdir)/src/test-harness/twtest
rm -Rf $(top_srcdir)/bin/TWTestData
cd $(top_srcdir)/src/test-harness && perl ./twtest.pl
$(top_srcdir)/bin/twtest all
cd $(top_srcdir)/bin && ./twtest all
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.

View File

@ -85,21 +85,33 @@ static bool NeedsStat(const cFCOPropVector& v)
///////////////////////////////////////////////////////////////////////////////
static bool GetSymLinkStr(const TSTRING& strName, cArchive& arch)
bool cFSPropCalc::GetSymLinkStr(const TSTRING& strName, cArchive& arch, size_t size)
{
char buf[1024]; // TODO: is this big enough?
std::vector<char> data(size+1);
char* buf = &data[0];
#if defined(O_PATH)
int fd = open(strName.c_str(), (O_PATH | O_NOFOLLOW | O_NOATIME));
int rtn = readlinkat(fd, 0, buf, 1024);
int rtn = readlinkat(fd, 0, buf, size);
close(fd);
#else
int rtn = readlink( strName.c_str(), buf, 1024 );
int rtn = readlink( strName.c_str(), buf, size );
#endif
if(rtn == -1)
return false;
// the return value is the number of characters written.
//Sadly if buf isn't big enough readlink 'succeeds' by truncating the string, so the only
// clue your buffer might be too small is if you maxed it out. So we try again, within reason.
if((size_t)rtn == size)
{
if(size < 128*TW_PATH_SIZE)
return GetSymLinkStr(strName, arch, size*2);
return false;
}
// the return value is the number of characters written.
arch.WriteBlob(buf, rtn);
return true;

View File

@ -53,6 +53,12 @@
#include "core/archive.h"
#include "fspropset.h"
#ifdef PATH_MAX
# define TW_PATH_SIZE PATH_MAX
#else
# define TW_PATH_SIZE 1024
#endif
TSS_FILE_EXCEPTION( eFSPropCalc, eFileError )
//TSS_EXCEPTION( eFSPropCalcResetAccessTime, eFSPropCalc ) // this was never used
@ -79,7 +85,9 @@ public:
virtual int GetCalcFlags() const;
virtual void SetCalcFlags( int i );
static bool GetSymLinkStr(const TSTRING& strName, cArchive& arch, size_t size = TW_PATH_SIZE);
private:
cFSPropCalc( const cFSPropCalc& );
void operator =( const cFSPropCalc& );

View File

@ -104,8 +104,8 @@ void TestArchive()
// cLockedTemporaryFileArchive
TSTRING lockedFileName = TEMP_DIR;
lockedFileName += _T("/inaccessable_file.bin");
TSTRING lockedFileName = TwTestPath("inaccessable_file.bin");
// lockedFileName += _T("/inaccessable_file.bin");
cLockedTemporaryFileArchive lockedArch;
@ -130,8 +130,8 @@ void TestArchive()
lockedArch.Close();
// cFileArchive
TSTRING fileName = TEMP_DIR;
fileName += _T("/archive_test.bin");
TSTRING fileName = TwTestPath("archive_test.bin");
//fileName += _T("/archive_test.bin");
cFileArchive filearch;
filearch.OpenReadWrite(fileName.c_str());

View File

@ -70,7 +70,7 @@ void TestCryptoArchive()
d.TraceDetail("Encrypting using symmetric key\n");
cFileArchive outFile;
outFile.OpenReadWrite(TEMP_DIR _T("/crypted.bin"));
outFile.OpenReadWrite(TwTestPath("crypted.bin"));
idea.SetKey(iCipher::ENCRYPT, ideaKey);
cCryptoArchive outCrypt;
@ -90,7 +90,7 @@ void TestCryptoArchive()
d.TraceDetail("Decrypting using symmetric key\n");
cFileArchive inFile;
inFile.OpenRead(TEMP_DIR _T("/crypted.bin"));
inFile.OpenRead(TwTestPath("crypted.bin"));
idea.SetKey(iCipher::DECRYPT, ideaKey);
cCryptoArchive inCrypt;
@ -129,7 +129,7 @@ void TestCryptoArchive()
d.TraceDetail("Signing using asymmetric key\n");
cFileArchive outFile;
outFile.OpenReadWrite(TEMP_DIR _T("/rsacrypted.bin"));
outFile.OpenReadWrite(TwTestPath("rsacrypted.bin").c_str());
cElGamalSigArchive outCrypt;
outCrypt.SetWrite(&outFile, privateKey);
@ -149,7 +149,7 @@ void TestCryptoArchive()
d.TraceDetail("Verifying using asymmetric key\n");
cFileArchive inFile;
inFile.OpenRead(TEMP_DIR _T("/rsacrypted.bin"));
inFile.OpenRead(TwTestPath("rsacrypted.bin").c_str());
cElGamalSigArchive inCrypt;
inCrypt.SetRead(&inFile, publicKey);
@ -206,7 +206,7 @@ void TestCryptoArchive()
d.TraceDetail("Encrypting using asymmetric key\n");
cFileArchive outFile;
outFile.OpenReadWrite(TEMP_DIR _T("/rsacrypted.bin"));
outFile.OpenReadWrite(TwTestPath("rsacrypted.bin").c_str());
cRSAArchive outCrypt;
outCrypt.SetWrite(&outFile, publicKey);
@ -226,7 +226,7 @@ void TestCryptoArchive()
d.TraceDetail("Decrypting using asymmetric key\n");
cFileArchive inFile;
inFile.OpenRead(TEMP_DIR _T("/rsacrypted.bin"));
inFile.OpenRead(TwTestPath("rsacrypted.bin").c_str());
cRSAArchive inCrypt;
inCrypt.SetRead(&inFile, privateKey);
@ -252,7 +252,7 @@ void TestCryptoArchive()
d.TraceDetail("Signing using asymmetric key\n");
cFileArchive outFile;
outFile.OpenReadWrite(TEMP_DIR _T("/rsacrypted.bin"));
outFile.OpenReadWrite(TwTestPath("rsacrypted.bin").c_str());
cRSAArchive outCrypt;
outCrypt.SetWrite(&outFile, privateKey);
@ -272,7 +272,7 @@ void TestCryptoArchive()
d.TraceDetail("Verifying using asymmetric key\n");
cFileArchive inFile;
inFile.OpenRead(TEMP_DIR _T("/rsacrypted.bin"));
inFile.OpenRead(TwTestPath("rsacrypted.bin").c_str());
cRSAArchive inCrypt;
inCrypt.SetRead(&inFile, publicKey);

View File

@ -73,8 +73,7 @@ void TestDebug()
// set up an output file...use the temp file in test.h
std::string str = TEMP_DIR_N;
str += "/debug.out";
std::string str = TwTestPath("debug.out");
#ifdef DEBUG
TEST(cDebug::SetOutputFile(str.c_str()));

View File

@ -62,18 +62,17 @@ static void PrintProps(const iFCO* pFCO)
void TestFCOCompare()
{
const TCHAR* FILE_NAME = TEMP_DIR _T("/dog.txt");
const char* FILE_NAME_N = TEMP_DIR_N "/dog.txt";
std::string filename = TwTestPath("dog.txt");
cDebug d("TestFCOCompare");
d.TraceDebug("Entering...\n");
// first, create an fco to compare with...
TOFSTREAM fstr(FILE_NAME_N);
TOFSTREAM fstr(filename);
if(fstr.bad())
{
d.TraceError("Unable to create test file %s!\n", FILE_NAME);
d.TraceError("Unable to create test file %s!\n", filename.c_str());
TEST(false);
return;
}
@ -83,7 +82,7 @@ void TestFCOCompare()
// create the test FCO
cFSDataSourceIter ds;
ds.SeekToFCO(cFCOName(FILE_NAME), false);
ds.SeekToFCO(cFCOName(filename), false);
iFCO* pFCO = ds.CreateFCO();
TEST(pFCO);
@ -110,10 +109,10 @@ void TestFCOCompare()
// change the file...
d.TraceDebug("Changing the file...\n");
fstr.open(FILE_NAME);
fstr.open(filename);
if(fstr.bad())
{
d.TraceError("Unable to reopen %s!\n", FILE_NAME_N);
d.TraceError("Unable to reopen %s!\n", filename.c_str());
TEST(false);
return;
}
@ -123,7 +122,7 @@ void TestFCOCompare()
//need a new data source iter, otherwise the existing FCO gets updated & you get a ref to it,
// and the resulting FCOs always match.
cFSDataSourceIter ds2;
ds2.SeekToFCO(cFCOName(FILE_NAME), false);
ds2.SeekToFCO(cFCOName(filename), false);
iFCO* pFCO2 = ds2.CreateFCO();
TEST(pFCO2);
pFCO2->AcceptVisitor(&propCalc);
@ -137,7 +136,7 @@ void TestFCOCompare()
//result.mPropVector.TraceContents();
cFSDataSourceIter ds3;
ds3.SeekToFCO(cFCOName(FILE_NAME), false);
ds3.SeekToFCO(cFCOName(filename), false);
// try testing properties that weren't calculated...
d.TraceDebug("Comparing FCOs with different properties calculated\n");
iFCO* pFCO3 = ds3.CreateFCO();

View File

@ -39,8 +39,7 @@
void TestFile()
{
TSTRING fileName = TEMP_DIR;
fileName += _T("/file_test.bin");
TSTRING fileName = TwTestPath("file_test.bin");
//Create a temporary file for testing:
FILE* testStream;

View File

@ -49,8 +49,7 @@ using namespace std;
void TestFileUtil()
{
TSTRING source = TEMP_DIR;
source += _T("/copy_src");
TSTRING source = TwTestPath("copy_src");
//Create a temporary file for testing:
FILE* testStream;
@ -64,8 +63,7 @@ void TestFileUtil()
fwrite( testString.c_str(), sizeof(TCHAR), iTestStringLength, testStream );
fclose( testStream );
TSTRING dest = TEMP_DIR;
dest += "/copy_dest";
TSTRING dest = TwTestPath("copy_dest");
TEST(cFileUtil::Copy(source, dest));

View File

@ -43,6 +43,8 @@
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
///////////////////////////////////////////////////////////////////////////////
// PrintProps -- prints out all the valid property names and values as pairs...
@ -67,8 +69,7 @@ void TestFSPropCalc()
{
cDebug d("TestFSPropCalc");
cFSDataSourceIter ds;
TSTRING foo_bin = TEMP_DIR;
foo_bin.append("/foo.bin");
TSTRING foo_bin = TwTestPath("foo.bin");
//iFSServices* pFSServices = iFSServices::GetInstance();
@ -138,3 +139,20 @@ void TestFSPropCalc()
return;
}
void TestGetSymLinkStr()
{
std::string file = TwTestPath("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
std::string link = TwTestPath("linky");
int fd = creat(file.c_str(), 0777);
close(fd);
symlink(file.c_str(), link.c_str());
cMemoryArchive arch(1024*1024);
TEST(cFSPropCalc::GetSymLinkStr(link, arch, 8));
TEST(arch.Length() == (int64)file.size());
}

View File

@ -73,9 +73,9 @@ void TestFCOSpecImpl()
cFSDataSourceIter dataSrc;
// test AllChildStopPoint fcos...
d.TraceDebug("Now testing a spec whose start point is the only thing it maps to (%s)\n", TEMP_DIR);
cFCOSpecImpl* pSpec2 = new cFCOSpecImpl(TEMP_DIR, &dataSrc, new cFCOSpecNoChildren);
pSpec2->SetStartPoint(cFCOName(TEMP_DIR));
d.TraceDebug("Now testing a spec whose start point is the only thing it maps to (%s)\n", TwTestDir().c_str());
cFCOSpecImpl* pSpec2 = new cFCOSpecImpl(TwTestDir(), &dataSrc, new cFCOSpecNoChildren);
pSpec2->SetStartPoint(cFCOName(TwTestDir()));
dataSrc.SeekToFCO(pSpec2->GetStartPoint(), false);
iFCO* pFCO = dataSrc.CreateFCO();
TEST(pFCO);

View File

@ -114,11 +114,11 @@ void TestKeyFile()
// save to and read from disk
d.TraceDebug("Read/Write to file...\n");
{
keyfile.WriteFile(TEMP_DIR _T("/keyfile.key"));
keyfile.WriteFile(TwTestPath("keyfile.key").c_str());
cKeyFile keyfile2;
TEST(!keyfile2.KeysLoaded());
keyfile2.ReadFile(TEMP_DIR _T("/keyfile.key"));
keyfile2.ReadFile(TwTestPath("keyfile.key").c_str());
TEST(keyfile2.KeysLoaded());
cElGamalSig elGamal(*keyfile2.GetPublicKey());

View File

@ -119,7 +119,7 @@ void TestSerializerImpl()
// writing
{
cFileArchive file;
file.OpenReadWrite(TEMP_DIR _T("/tmp.bin"));
file.OpenReadWrite(TwTestPath("tmp.bin").c_str());
cSerializerImpl serializer(file, cSerializerImpl::S_WRITE);
serializer.Init();
@ -127,16 +127,16 @@ void TestSerializerImpl()
cSerializerTestObject testobj;
testobj.Write(&serializer);
db.TraceAlways(" Writeing object 1...\n");
db.TraceAlways(" Writing object 1...\n");
serializer.WriteObject(&testobj);
db.TraceAlways(" Writeing object 2...\n");
db.TraceAlways(" Writing object 2...\n");
serializer.WriteObject(&testobj);
db.TraceAlways(" Writeing object 3...\n");
db.TraceAlways(" Writing object 3...\n");
serializer.WriteObject(&testobj);
db.TraceAlways(" Writeing object 4...\n");
db.TraceAlways(" Writing object 4...\n");
serializer.WriteObject(&testobj);
serializer.Finit();
@ -145,7 +145,7 @@ void TestSerializerImpl()
// reading
{
cFileArchive file;
file.OpenRead(TEMP_DIR _T("/tmp.bin"));
file.OpenRead(TwTestPath("tmp.bin").c_str());
cSerializerImpl serializer(file, cSerializerImpl::S_READ);
serializer.Init();

View File

@ -87,8 +87,7 @@ void TestSignature()
//sha : Oia1aljHD793tfj7M55tND+3OG/
//haval : BL6bFSo0EP5zf8lGSueeed
TSTRING sigFileName = TEMP_DIR;
sigFileName += TSTRING( _T("/signature_test.bin") );
TSTRING sigFileName = TwTestPath("signature_test.bin");
cFileArchive fileArc;
fileArc.OpenReadWrite(sigFileName.c_str());

View File

@ -39,6 +39,8 @@
#include "core/debug.h"
#endif
#include "test.h"
TSTRING test_wost(int, const TSTRING&);
void test_wist(const TSTRING&, cDebug& d);
@ -87,8 +89,8 @@ void TestTCHAR()
//Testing file streams
//explict constructors of 'TIFSTREAM' and "TOFSTREAM' take char*
const char* inputfile = "fun";
const char* outputfile = "mo'fun";
std::string inputfile = TwTestPath("fun");
std::string outputfile = TwTestPath("mo'fun");
//Set up the input file.
TOFSTREAM out;

View File

@ -42,8 +42,6 @@
#include "twparser/twparser.h"
#include "tw/tw.h"
#include "fco/fco.h"
#include "fs/fs.h"
#include "util/util.h"
@ -51,6 +49,7 @@
#include "core/debug.h"
#include "core/error.h"
#include "core/twlocale.h"
#include "core/fsservices.h"
#include "test.h"
#include "core/errorbucketimpl.h"
#include "tw/twinit.h"
@ -62,6 +61,8 @@
#include "db/blockrecordarray.h"
#include "db/hierdatabase.h"
#include <unistd.h>
#include <sys/stat.h>
// the test routines
void TestFCOName();
@ -99,7 +100,7 @@ void TestTextReportViewer();
void TestFCONameTbl();
void TestConfigFile();
void TestResources();
void TestGetSymLinkStr();
void TestPolicyParser();
void TestFCOSpecHelper();
@ -187,7 +188,7 @@ static void Test(int testID)
case 14: TestFCOPropVector(); break;
case 15: TestFCOPropImpl(); break;
case 16: TestFCOReport(); break;
case 17: TestGetSymLinkStr(); break;
case 18: TestFCOSetImpl(); break;
case 19: TestFCOSpec(); break;
case 20: TestFCOSpecAttr(); break;
@ -294,6 +295,31 @@ static void Test(int testID)
TCERR << std::endl << "=== test ID #" << testID << " currently unused ===" << std::endl;
}
std::string TwTestDir()
{
static std::string dir;
if(dir.empty())
{
iFSServices::GetInstance()->GetCurrentDir(dir);
dir.append("/TWTestData");
TCERR << "Using test directory: " << dir << std::endl;
mkdir(dir.c_str(), 0777);
}
return dir;
}
std::string TwTestPath(const std::string& child)
{
std::stringstream sstr;
sstr << TwTestDir();
if (child[0] != '/')
sstr << '/';
sstr << child;
return sstr.str();
}
///////////////////////////////////////////////////////////////////////////////
// cTest
///////////////////////////////////////////////////////////////////////////////

View File

@ -75,9 +75,9 @@ TSS_EndPackage( cTest )
}
///////////////////////////////////////////////////////////////////////////////
// Platform dependancies
#define TEMP_DIR _T("/tmp")
#define TEMP_DIR_N "/tmp"
std::string TwTestDir();
std::string TwTestPath(const std::string& child);
#endif // __TEST_H

View File

@ -380,7 +380,7 @@ void TestTextReportViewer()
d.TraceDebug("Read in serialized report:\n");
//TraceReport(inReport, d);
trv.PrintTextReport(TSTRING( TEMP_DIR _T( "/test2.txt" ) ) );
trv.PrintTextReport(TSTRING( TwTestPath("test2.txt" ) ) );
//TODO: this does not work any more
//trv.LaunchEditorOnFile( TSTRING( TEMP_DIR _T("/test2.txt") ), _T("") );

View File

@ -58,10 +58,8 @@ void TestTWUtil()
// assuming the current dir is writable, this test should succeed
TEST(cFileUtil::FileWritable(_T("afilethatdoesnotexist.tmp")) == true);
TSTRING tmpDir = TEMP_DIR;
tmpDir += _T("/fileexistdir");
TSTRING tmpFN = tmpDir;
tmpFN += _T("/fileexiststest.tmp");
TSTRING tmpDir = TwTestPath("fileexistdir");
TSTRING tmpFN = TwTestPath("fileexiststest.tmp");
// make a subdir in the TEMP_DIR
mkdir(tmpDir.c_str(), 0700);
@ -77,14 +75,14 @@ void TestTWUtil()
TEST(cFileUtil::FileWritable(tmpFN) == true)
TEST(cFileUtil::FileExists(tmpFN) == false);
// 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);
bool is_root = (0 == getuid());
TEST(cFileUtil::FileWritable(tmpFN) == is_root);
chmod(tmpDir.c_str(), 0700);
// 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);
// TODO - is this valid now that we don't use /tmp?
// TEST(cFileUtil::FileWritable(tmpFN) == is_root);
// chmod(tmpDir.c_str(), 0700);
// create the file
{

View File

@ -54,9 +54,9 @@ void TestUnixFSServices()
iFSServices* pFSServices = iFSServices::GetInstance();
// working primarily with the temp dir.
cFCOName name(_T("/tmp"));
cFCOName name(TwTestDir());
// Check to make sure /tmp is a dir
// Check to make sure test dir is a dir
//TEST(pFSServices->GetFileType(name) == cFSStatArgs::TY_DIR);
// get directory contents (test readdir)
@ -80,7 +80,7 @@ void TestUnixFSServices()
cFSStatArgs stat;
//TO DO: use archive to create this file
TSTRING testfile = "/tmp/tmp.tmp";
TSTRING testfile = TwTestPath("tmp.tmp");
cFileArchive filearch;
filearch.OpenReadWrite(testfile.c_str());
filearch.Seek(0, cBidirArchive::BEGINNING);
@ -150,7 +150,7 @@ void TestUnixFSServices()
// test Rename
d.TraceDetail("Testing Rename:\n");
TSTRING newtestfile = _T("/tmp/new.tmp");
TSTRING newtestfile = TwTestPath("new.tmp");
TEST( pFSServices->Rename( testfile, newtestfile ) );
// test FileDelete