#include "DemoTest.h" #include #include // for cout #include // for setprecision( int p ) #include // For auto_ptr #include #include #include // For system independent path support namespace bf = boost::filesystem; using namespace std; const int testDemoLength = 90; // Should match the default in DemoAndLicensing.cpp CPPUNIT_TEST_SUITE_REGISTRATION( DemoTest ); DemoTest::DemoTest() { paths.push_back( "/tmp/lic1" ); // paths.push_back( "~/.testlic" ); // we can re-enable this when we have support for tildes in filenames paths.push_back( "/tmp/lic2/lic3" ); } void DemoTest::setUpTester() { tester = new CERT::DemoAndLicensing_Test(); tester->setAllowedPaths( paths ); tester->setFilename( ".demotest" ); } void DemoTest::setUp() { setUpTester(); CPPUNIT_ASSERT( tester->clearPaths() ); } /** */ void DemoTest::tearDown() { tester->clearPaths(); if( tester ) { delete tester; tester = NULL; } } /** Rotate an RImage and compare. * Rotate the input RImage and compare with one we know is properly rotated. */ void DemoTest::testBasicUse() { tester->createDirectories(); CPPUNIT_ASSERT( !tester->isDemo() ); CPPUNIT_ASSERT( tester->startDemo() ); CPPUNIT_ASSERT( tester->isDemo() ); // make sure the end date is correct bg::date today = bg::day_clock::local_day(); bg::date endDate = tester->demoEndDate(); today += bg::days( tester->getDemoLength() ); CPPUNIT_ASSERT_MESSAGE( "Demo end date does not seem to be correct.", today == endDate ); } /** Test for failure if all paths are blocked. * It may not be possible to create any files for storing demo info because of permissions * or because a directory exists with the same name as the file. */ void DemoTest::testBlockedPaths() { CPPUNIT_ASSERT( !tester->isDemo() ); tester->blockPaths(); CPPUNIT_ASSERT( !tester->startDemo() ); CPPUNIT_ASSERT( !tester->isDemo() ); } /** Test for an expired demo. */ void DemoTest::testExpiredDemo() { tester->createDirectories(); CPPUNIT_ASSERT( !tester->isDemo() ); bg::date aDate = bg::day_clock::local_day(); aDate -= bg::days( testDemoLength+1 ); tester->writeGivenDate( aDate ); CPPUNIT_ASSERT( tester->isDemo() ); CPPUNIT_ASSERT( tester->hasDemoEnded() ); delete tester; setUpTester(); CPPUNIT_ASSERT( tester->isDemo() ); CPPUNIT_ASSERT( tester->hasDemoEnded() ); } /** Test asking to write a future date. * This is mostly testing the demos date validation procedure. */ void DemoTest::testBadDate1() { tester->createDirectories(); CPPUNIT_ASSERT( !tester->isDemo() ); bg::date aDate = bg::day_clock::local_day(); aDate += bg::days( 1 ); CPPUNIT_ASSERT( !tester->writeGivenDate( aDate ) ); CPPUNIT_ASSERT( !tester->isDemo() ); } /** Test for various bad dates. * Forcibly write various dates to a file and make sure we handle them correctly. */ void DemoTest::testBadDate2() { // One day in the past, should be good testOffsetDate( -1, true, false ); } void DemoTest::testBadDate3() { // One day in the future, should be bad testOffsetDate( 1, false, true ); } void DemoTest::testBadDate4() { // Today, should be good testOffsetDate( 0, true, false ); } void DemoTest::testBadDate5() { // 1 day less than the demo length, in the past, should be good testOffsetDate( -(testDemoLength-1), true, false ); } void DemoTest::testBadDate6() { // demoLength days in the past, should be good testOffsetDate( -testDemoLength, true, false ); } void DemoTest::testBadDate7() { // One day longer than the demo length testOffsetDate( -(testDemoLength+1), true, true ); } void DemoTest::testBadDate8() { // 2 days longer than the demo length testOffsetDate( -(testDemoLength+2), true, true ); } void DemoTest::testBadDate9() { // many days in the past, should be bad testOffsetDate( -30000, true, true ); } void DemoTest::testBadDate10() { // many days in the future, should be bad testOffsetDate( 3000, false, true ); } /** Test with a given offset from today. */ void DemoTest::testOffsetDate( int offset, bool valid, bool ended ) { tester->createDirectories(); bg::date aDate = bg::day_clock::local_day(); aDate += bg::days( offset ); std::vector paths = tester->getAllowedPaths(); bf::path bfPath( paths[0], bf::native ); bfPath /= bf::path( tester->getFilename(), bf::native ); CPPUNIT_ASSERT( writeDateToFile( aDate, bfPath ) ); CPPUNIT_ASSERT( tester->isDemo() == valid ); // if this date offset is supposed to produce a valid demo and it does then test the demo has ended. if( valid ) CPPUNIT_ASSERT( tester->hasDemoEnded() == ended ); } bool DemoTest::writeDateToFile( const bg::date &dt, const bf::path &bfPath ) { std::ofstream ofs( bfPath.native_file_string().c_str() ); if( ofs.good() ) { tm myTM = to_tm( dt ); time_t startTime = mktime( &myTM ); startTime = obfuscate( startTime ); ofs << startTime; return true; } return false; } void DemoTest::testObfuscation2() { testObfuscation( -1 ); } void DemoTest::testObfuscation3() { testObfuscation( 0 ); } void DemoTest::testObfuscation1() { testObfuscation( 1 ); } void DemoTest::testObfuscation4() { testObfuscation( -1000 ); } void DemoTest::testObfuscation5() { testObfuscation( 1000 ); } void DemoTest::testObfuscation6() { testObfuscation( 1197935187 ); } void DemoTest::testObfuscation7() { testObfuscation( std::numeric_limits::min() ); } void DemoTest::testObfuscation8() { testObfuscation( std::numeric_limits::max() ); } void DemoTest::testObfuscationAll() { int min = std::numeric_limits::min(); int max = std::numeric_limits::max(); std::vector badObfuscate; std::vector badDeobfuscate; for( int cnt = min; cnt < max; ++cnt ) { // if( !(cnt % 100000) ) // { // std::cout << " " << cnt << std::endl; // std::cout.flush(); // } int res = obfuscate( cnt ); if( res == cnt ) badObfuscate.push_back(cnt); res = deobfuscate( res ); if( res != cnt ) badDeobfuscate.push_back(cnt); } // badObfuscate.push_back( 0 ); // badObfuscate.push_back( 1 ); // badObfuscate.push_back( -3 ); // badDeobfuscate.push_back( 99); // badDeobfuscate.push_back( -99); std::stringstream badStream; if( badObfuscate.size() > 0 ) badStream << "Obfuscation errors: "; for( std::vector::iterator iter = badObfuscate.begin(); iter != badObfuscate.end(); ++iter ) { badStream << *iter << ", "; } if( badObfuscate.size() > 0 ) { badStream << std::endl; } std::stringstream badStream2; if( badDeobfuscate.size() > 0 ) badStream2 << " Deobfuscation errors: "; for( std::vector::iterator iter = badDeobfuscate.begin(); iter != badDeobfuscate.end(); ++iter ) { badStream2 << *iter << ", "; } if( badDeobfuscate.size() > 0 ) { badStream2 << std::endl; } std::string errMessage = badStream.str() + badStream2.str(); CPPUNIT_ASSERT_MESSAGE( errMessage.c_str(), ((badObfuscate.size() + badDeobfuscate.size()) == 0) ); } void DemoTest::testObfuscation( int num ) { int res = obfuscate( num ); std::stringstream message; message << "Obfuscation error: " << res << " == " << num; CPPUNIT_ASSERT_MESSAGE( message.str().c_str(), res != num ); res = deobfuscate( res ); message.str( "" ); message << "Deobfuscation error: " << res << " != " << num; CPPUNIT_ASSERT_MESSAGE( message.str().c_str(), res == num ); }