Enable & (at least partly) fix charutil, displayencoder, & policyparser tests, which weren't running until now.

This commit is contained in:
Brian Cox 2017-03-26 20:16:37 -07:00
parent be0b374cad
commit cdb7310dae
4 changed files with 338 additions and 331 deletions

View File

@ -37,34 +37,23 @@
#include "core/stdcore.h"
#ifdef TSS_TEST
#include "core/charutil.h"
#include "core/debug.h"
#include "core/errorbucketimpl.h"
#include "test/utx.h"
#include "charutil.h"
#include "debug.h"
#include "errorbucketimpl.h"
///////////////////////////////////////////////////////////////////////////////
// cCharEncoderTest
///////////////////////////////////////////////////////////////////////////////
class cCharEncoderTest
{
public:
void PrintChars( const TSTRING& str )
{
const TCHAR* cur = str.begin();
const TCHAR* end = str.end();
const TCHAR* first = NULL;
const TCHAR* last = NULL;
TSTRING::const_iterator cur = str.begin();
TSTRING::const_iterator end = str.end();
TSTRING::const_iterator first, last;
while( cCharUtil::PopNextChar( cur, end, first, last ) )
{
TCOUT << _T("char length: ") << (int)(last - first) << std::endl;
TCOUT << _T("char: <");
for( const TCHAR* at = first; at != last; at++ )
for( TSTRING::const_iterator at = first; at != last; at++ )
{
if( at != first )
TCOUT << _T(",");
@ -79,7 +68,7 @@ public:
///////////////////////////////////////////////////////////////////////////
// Basic
///////////////////////////////////////////////////////////////////////////
void Basic( tss::TestContext& ctx )
void TestCharUtilBasic()
{
try
{
@ -92,14 +81,12 @@ public:
ASSERT(false);
}
}
};
/*
TSS_BeginTestSuiteFrom( cCharEncoderTest )
TSS_AddTestCase( Basic );
TSS_EndTestSuite( cCharEncoderTest )
#endif // TSS_TEST
// eof: charutil_t.cpp
*/

View File

@ -37,14 +37,12 @@
#include "core/stdcore.h"
#ifdef TSS_TEST
#include "test/utx.h"
#include "displayencoder.h"
#include "debug.h"
#include "twlocale.h"
#include "errorbucketimpl.h"
#include "core/displayencoder.h"
#include "core/debug.h"
#include "core/twlocale.h"
#include "core/errorbucketimpl.h"
#include "twtest/test.h"
///////////////////////////////////////////////////////////////////////////////
// UTIL
@ -53,7 +51,7 @@
#define TSS_TCHAR_MIN CHAR_MIN
#define TSS_TCHAR_MAX CHAR_MAX
/*
template< class CharT > bool IsPrintable( const std::basic_string< CharT >& str )
{
const std::ctype< CharT > *pct = 0, &ct = tss::GetFacet( std::locale(), pct );
@ -65,6 +63,7 @@ template< class CharT > bool IsPrintable( const std::basic_string< CharT >& str
return true;
}
*/
static void util_TestUnprintable( const TSTRING& strCUnprintable )
{
@ -80,109 +79,98 @@ static void util_TestUnprintable( const TSTRING& strCUnprintable )
}
///////////////////////////////////////////////////////////////////////////////
// cDisplayEncoderTest
///////////////////////////////////////////////////////////////////////////////
class cDisplayEncoderTest
{
public:
///////////////////////////////////////////////////////////////////////////
// TestCharToHex
///////////////////////////////////////////////////////////////////////////
void TestCharToHex( tss::TestContext& ctx )
void test_char_to_hex(char ch, const TSTRING& expected)
{
TCHAR ch;
TSTRING str;
const std::ctype< TCHAR > *pct = 0, &ct = tss::GetFacet( std::locale(), pct );
// only use lowercase strings with this define
#define TSS_CHAR_TO_HEX_TEST( s ) \
ch = 0x ## s; \
str = cCharEncoderUtil::char_to_hex( ch ); \
ct.tolower( str.begin(), str.end() ); \
ASSERT( str == _T( #s ) );
TSS_CHAR_TO_HEX_TEST( fefe );
TSS_CHAR_TO_HEX_TEST( 0000 );
TSS_CHAR_TO_HEX_TEST( 1234 );
TSS_CHAR_TO_HEX_TEST( ffff );
TSS_CHAR_TO_HEX_TEST( 0001 );
TSS_CHAR_TO_HEX_TEST( 543c );
TSS_CHAR_TO_HEX_TEST( cccc );
TSS_CHAR_TO_HEX_TEST( 9999 );
TSS_CHAR_TO_HEX_TEST( abcd );
TSTRING observed = cCharEncoderUtil::char_to_hex( ch );
TEST(expected == observed);
}
void TestCharToHex()
{
test_char_to_hex( 0xfe, "fe");
test_char_to_hex( 0xff, "ff");
test_char_to_hex( 0x00, "00");
test_char_to_hex( 0x01, "01");
test_char_to_hex( 0x7f, "7f");
test_char_to_hex( 0x80, "80");
}
///////////////////////////////////////////////////////////////////////////
// TestHexToChar
///////////////////////////////////////////////////////////////////////////
void TestHexToChar( tss::TestContext& ctx )
void test_hex_to_char(const TSTRING& str, char expected)
{
TCHAR ch;
TSTRING str;
// only use lowercase strings with this define
#define TSS_HEX_TO_CHAR_TEST( s ) \
str = _T( #s ); \
ch = cCharEncoderUtil::hex_to_char( str.begin(), str.end() ); \
ASSERT( ch == 0x ## s );
TSS_HEX_TO_CHAR_TEST( fefe );
TSS_HEX_TO_CHAR_TEST( 0000 );
TSS_HEX_TO_CHAR_TEST( 1234 );
TSS_HEX_TO_CHAR_TEST( ffff );
TSS_HEX_TO_CHAR_TEST( 0001 );
TSS_HEX_TO_CHAR_TEST( 543c );
TSS_HEX_TO_CHAR_TEST( cccc );
TSS_HEX_TO_CHAR_TEST( 9999 );
TSS_HEX_TO_CHAR_TEST( abcd );
char observed = cCharEncoderUtil::hex_to_char( str.begin(), str.end() );
TEST(expected == observed);
}
void TestHexToChar()
{
TCERR << "\nTODO: TestHexToChar in displayencoder_t.cpp needs to be fixed; currently disabled." << std::endl;
#if 0
test_hex_to_char( "fe", 0xfe );
test_hex_to_char( "ff", 0xff );
test_hex_to_char( "00", 0x00 );
test_hex_to_char( "01", 0x01 );
test_hex_to_char( "7f", 0x7f );
test_hex_to_char( "80", 0x80 );
test_hex_to_char( "100", 0); // should throw
test_hex_to_char( "-01", 0); // should throw
#endif
}
//////////////////////////////////////////////////////////////////////////
// TestStringToHex -- locale specific test -- only works in ASCII
///////////////////////////////////////////////////////////////////////////
void TestStringToHex( tss::TestContext& ctx )
void test_string_to_hex(const std::string& str, const std::string& expected)
{
TSTRING str;
const std::ctype< TCHAR > *pct = 0, &ct = tss::GetFacet( std::locale(), pct );
// only use lowercase strings with this define
#define TSS_STRING_TO_HEX_TEST( s, n ) \
str = cCharEncoderUtil::CharStringToHexValue( _T( #s ) ); \
ct.tolower( str.begin(), str.end() ); \
ASSERT( str == _T( #n ) );
TSS_STRING_TO_HEX_TEST( \n, 000a );
TSS_STRING_TO_HEX_TEST( \r, 000d );
TSS_STRING_TO_HEX_TEST( \r\n, 000d000a );
TSS_STRING_TO_HEX_TEST( a\r\nb, 0061000d000a0062 );
std::string observed = cCharEncoderUtil::CharStringToHexValue(str);
TEST(expected == observed);
}
void TestStringToHex()
{
test_string_to_hex( "\n", "0a" );
test_string_to_hex( "\r", "0d" );
test_string_to_hex( "\r\n", "0d0a" );
test_string_to_hex( "a\r\nb", "610d0a62" );
}
//////////////////////////////////////////////////////////////////////////
// TestHexToString -- locale specific test -- only works in Unicode
///////////////////////////////////////////////////////////////////////////
void TestHexToString( tss::TestContext& ctx )
void test_hex_to_string(const std::string& str, const std::string& expected)
{
TSTRING str;
const std::ctype< TCHAR > *pct = 0, &ct = tss::GetFacet( std::locale(), pct );
// only use lowercase strings with this define
#define TSS_HEX_TO_STRING_TEST( s, n ) \
str = cCharEncoderUtil::HexValueToCharString( _T( #n ) ); \
ct.tolower( str.begin(), str.end() ); \
ASSERT( str == _T( #s ) );
TSS_HEX_TO_STRING_TEST( \n, 000a );
TSS_HEX_TO_STRING_TEST( \r, 000d );
TSS_HEX_TO_STRING_TEST( \r\n, 000d000a );
TSS_HEX_TO_STRING_TEST( a\r\nb, 0061000d000a0062 );
std::string observed = cCharEncoderUtil::HexValueToCharString(str);
TEST(expected == observed);
}
void TestHexToString()
{
TCERR << "\nTODO: TestHexToString in displayencoder_t.cpp needs to be fixed; currently disabled." << std::endl;
#if 0
test_hex_to_string( "0a", "\n");
test_hex_to_string( "0d", "\r");
test_hex_to_string( "0d0a", "\r\n");
test_hex_to_string( "610d0a62", "a\r\nb");
#endif
}
#if 0
//////////////////////////////////////////////////////////////////////////
// TestUnconvertable -- locale specific test -- only works in Unicode
///////////////////////////////////////////////////////////////////////////
void TestUnconvertable( tss::TestContext& ctx )
void TestUnconvertable()
{
cDisplayEncoder e( cDisplayEncoder::ROUNDTRIP );
const std::ctype< TCHAR > *pct = 0, &ct = tss::GetFacet( std::locale(), pct );
@ -194,7 +182,7 @@ public:
ch = 0x ## n; \
str = ch; \
e.Encode( str ); \
ct.tolower( str.begin(), str.end() ); \
// ct.tolower( str.begin(), str.end() ); \
ASSERT( str == _T("\\x") _T( #n ) _T("x") );
TSS_UNCONVERTABLE_TEST( fefe );
@ -211,7 +199,7 @@ public:
//////////////////////////////////////////////////////////////////////////
// TestUnprintable -- locale specific test -- only works in Unicode
///////////////////////////////////////////////////////////////////////////
void TestUnprintable( tss::TestContext& ctx )
void TestUnprintable()
{
cDisplayEncoder e( cDisplayEncoder::ROUNDTRIP );
const std::ctype< TCHAR > *pct = 0, &ct = tss::GetFacet( std::locale(), pct );
@ -223,17 +211,18 @@ public:
ch = 0x ## n; \
str = ch; \
e.Encode( str ); \
ct.tolower( str.begin(), str.end() ); \
// ct.tolower( str.begin(), str.end() ); \
ASSERT( str == _T("\\x") _T( #n ) _T("x") );
TSS_UNPRINTABLE_TEST( 000a );
TSS_UNPRINTABLE_TEST( 000d );
}
#endif
//////////////////////////////////////////////////////////////////////////
// TestQuoteAndBackSlash
///////////////////////////////////////////////////////////////////////////
void TestQuoteAndBackSlash( tss::TestContext& ctx )
void TestQuoteAndBackSlash()
{
cDisplayEncoder e( cDisplayEncoder::ROUNDTRIP );
TSTRING str;
@ -253,7 +242,7 @@ public:
///////////////////////////////////////////////////////////////////////////
// Basic
///////////////////////////////////////////////////////////////////////////
void Basic( tss::TestContext& ctx )
void TestDisplayEncoderBasic()
{
try
{
@ -335,9 +324,8 @@ public:
ASSERT(false);
}
}
};
TSS_BeginTestSuiteFrom( cDisplayEncoderTest )
/*TSS_BeginTestSuiteFrom( cDisplayEncoderTest )
TSS_AddTestCase( Basic );
TSS_AddTestCase( TestHexToChar );
@ -348,8 +336,6 @@ TSS_BeginTestSuiteFrom( cDisplayEncoderTest )
TSS_AddTestCase( TestUnprintable );
TSS_AddTestCase( TestQuoteAndBackSlash );
TSS_EndTestSuite( cDisplayEncoderTest )
TSS_EndTestSuite( cDisplayEncoderTest )*/
#endif // TSS_TEST
// eof: displayencoder_t.cpp

View File

@ -44,6 +44,7 @@
#include "fs/fspropset.h"
#include "fco/fcospeclist.h"
#include "twtest/test.h"
#include "util/fileutil.h"
#include <fstream>
// helper class that checks output of each fcospec
@ -52,19 +53,29 @@ public:
static bool VerifyNameAndStartPoint(iFCOSpec *pfsspec, TSTRING &name);
};
void TestPolicyParser()
TSTRING get_test_file_dir()
{
cDebug d("TestPolicyParser()");
if(cFileUtil::IsDir("../src/parser/testfiles"))
return "../src/parser/testfiles";
//
// file: pol.txt
//
if(cFileUtil::IsDir("src/parser/testfiles"))
return "src/parser/testfiles";
return "";
}
void test_policy_file(const std::string& polfile)
{
try
{
cDebug::AddOutTarget(cDebug::OUT_STDOUT);
TSTRING pol_path = get_test_file_dir();
pol_path.append("/");
pol_path.append(polfile);
std::ifstream in;
in.open( "../twparser/testfiles/pol.txt" );
in.open(pol_path);
if( ! in.good() )
throw eParserHelper( _T("couldn't open test file") );
@ -81,7 +92,6 @@ void TestPolicyParser()
parser.Execute( policy, &errorQ );
return;
}
catch(eError& e)
{
@ -89,7 +99,24 @@ void TestPolicyParser()
return;
}
TCERR << "Parsed policy test file " << polfile << std::endl;
return;
}
void TestPolicyParser()
{
cDebug d("TestPolicyParser()");
test_policy_file("pol.txt");
// test_policy_file("directives.txt"); //fails unless you substitute your hostname for 'your_host' in this file
// TODO: test currently segfaults if you create more than one policy parser in a process. (Not a real world scenario).
/* test_policy_file("poleasy.txt");
test_policy_file("polhard.txt");
test_policy_file("polruleattr.txt"); */
}

View File

@ -39,11 +39,7 @@
#include "core/core.h"
#include "db/db.h"
#include "twcrypto/twcrypto.h"
#ifdef PARSER_PRESENT
#include "parser/parser.h"
#endif
#include "twparser/twparser.h"
#include "tw/tw.h"
#include "fco/fco.h"
@ -104,9 +100,7 @@ void TestHashTable();
void TestFCONameTbl();
void TestConfigFile();
#ifdef PARSER_PRESENT
void TestPolicyParser();
#endif//PARSER_PRESENT
void TestFCOSpecHelper();
void TestCrypto();
@ -131,9 +125,7 @@ void TestIntegrityCheck();
void TestFCODatabaseFile();
void TestWchar16();
void TestStringEncoder();
#ifdef TSS_TEST
void TestDisplayEncoder();
#endif
//void TestDisplayEncoder();
void TestGrowHeap();
void TestPlatform();
//void TestBlockFile();
@ -143,6 +135,17 @@ void TestTWLocale();
void TestFileUtil();
void TestFCONameTranslator();
void TestCodeConverter();
void TestCharToHex();
void TestHexToChar();
void TestStringToHex();
void TestHexToString();
//void TestUnconvertable();
//void TestUnprintable();
void TestQuoteAndBackSlash();
void TestDisplayEncoderBasic();
void TestCharUtilBasic();
/// This is easier than all the (cpp) files and declarations
#include "stringutil_t.h"
@ -203,9 +206,7 @@ static void Test(int testID)
case 43: TestTCHAR(); break;
case 44: TestUnixFSServices(); break;
case 46: TestConfigFile(); break;
#ifdef PARSER_PRESENT
case 47: TestPolicyParser(); break;
#endif//PARSER_PRESENT
case 48: TestKeyFile(); break;
case 49: TestTWUtil(); break;
case 50: TestFSPropDisplayer(); break;
@ -219,9 +220,7 @@ static void Test(int testID)
//case 59: TestIntegrityCheck(); break;
case 65: TestWchar16(); break;
case 66: TestStringEncoder(); break;
#ifdef TSS_TEST
case 67: TestDisplayEncoder(); break;
#endif
//case 67: TestDisplayEncoder(); break;
case 69: TestGrowHeap(); break;
case 70: TestPlatform(); break;
//case 71: TestBlockFile(); break;
@ -232,6 +231,16 @@ static void Test(int testID)
case 76: TestFCONameTranslator(); break;
case 77: TestStringUtil(); break;
case 78: TestCodeConverter(); break;
case 79: TestCharToHex(); break;
case 80: TestHexToChar(); break;
case 81: TestStringToHex(); break;
case 82: TestHexToString(); break;
// case 83: TestUnconvertable(); break;
// case 84: TestUnprintable(); break;
case 85: TestQuoteAndBackSlash(); break;
case 86: TestDisplayEncoderBasic(); break;
case 87: TestCharUtilBasic(); break;
}
}
@ -245,9 +254,7 @@ cTest::cTest()
TSS_Dependency( cCore );
TSS_Dependency( cDb );
TSS_Dependency( cTWCrypto );
#ifdef PARSER_PRESENT
TSS_Dependency( cParser );
#endif//PARSER_PRESENT
TSS_Dependency( cTWParser );
TSS_Dependency( cTW );
TSS_Dependency( cFCO );
TSS_Dependency( cFS );