testPlatform.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
00003  *
00004  * Copyright (C) 2005 Dell Inc.
00005  *  by Michael Brown <Michael_E_Brown@dell.com>
00006  * Licensed under the Open Software License version 2.1 
00007  * 
00008  * Alternatively, you can redistribute it and/or modify 
00009  * it under the terms of the GNU General Public License as published 
00010  * by the Free Software Foundation; either version 2 of the License, 
00011  * or (at your option) any later version.
00012 
00013  * This program is distributed in the hope that it will be useful, but 
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of 
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00016  * See the GNU General Public License for more details.
00017  */
00018 
00019 // compat header should always be first header if including system headers
00020 #include "smbios/compat.h"
00021 
00022 #include <iomanip>
00023 #include <fstream>
00024 
00025 #include "testPlatform.h"
00026 #include "smbios/SmbiosDefs.h"
00027 
00028 // specific to unit tests. Users do not need to include this,
00029 // so it is not in testPlatform.h
00030 #include "smbios/IMemory.h"
00031 #include "smbios/ISmi.h"
00032 #include "smbios/IObserver.h"
00033 
00034 #include "smbios/version.h"
00035 
00036 using namespace std;
00037 
00038 // Note:
00039 //      Except for , there are no "using namespace XXXX;" statements
00040 //      here... on purpose. We want to ensure that while reading this code that
00041 //      it is extremely obvious where each function is coming from.
00042 //
00043 //      This leads to verbose code in some instances, but that is fine for
00044 //      these purposes.
00045 
00046 // Register the test
00047 CPPUNIT_TEST_SUITE_REGISTRATION (testPlatform);
00048 
00049 void copyFile( string dstFile, string srcFile )
00050 {
00051     ifstream src(srcFile.c_str(), ios_base::binary);
00052     ofstream dst(dstFile.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);
00053 
00054     char ch;
00055     while( src.get(ch)) dst.put(ch);
00056 
00057     if( !src.eof() || !dst ) throw exception();
00058 }
00059 
00060 bool fileExists(string fileName)
00061 {
00062     FILE *fh=0;
00063     fh=fopen(fileName.c_str(), "rb");
00064     if(!fh)
00065         return false;
00066 
00067     fclose(fh);
00068     return true;
00069 }
00070 
00071 void testPlatform::setUp()
00072 {
00073     string programDirname = getCppunitTopDirectory();
00074     string writeDirectory = getWritableDirectory();
00075 
00076     string testInput = programDirname + getTestDirectory() + "/testInput.xml";
00077     if(!fileExists(testInput))
00078         testInput = getTestDirectory() + "/testInput.xml"; 
00079 
00080     // copy the memdump.dat file. We do not write to it, but rw open will fail
00081     // if we do not copy it
00082     string memdumpOrigFile = programDirname + getTestDirectory() + "/memdump.dat";
00083     if(!fileExists(memdumpOrigFile))
00084         memdumpOrigFile = getTestDirectory() + "/memdump.dat";
00085     string memdumpCopyFile = writeDirectory + "/memdump-copy.dat";
00086     copyFile( memdumpCopyFile, memdumpOrigFile );
00087 
00088     // copy the CMOS file. We are going to write to it and do not wan to mess up
00089     // the pristine unit test version
00090     string cmosOrigFile = programDirname + getTestDirectory() + "/cmos.dat";
00091     if(!fileExists(cmosOrigFile))
00092         cmosOrigFile = getTestDirectory() + "/cmos.dat";
00093     string cmosCopyFile = writeDirectory + "/cmos-copy.dat";
00094     copyFile( cmosCopyFile, cmosOrigFile );
00095 
00096     // Smi output file.
00097     string smiOutput = writeDirectory + "/smi-output.dat";
00098 
00099     // normal users of the smbios classes need not
00100     // set the four parameters below. They should all be set inside the factory
00101     // properly by default. We override stuff here to have
00102     // the smbios, cmos, etc classes use file dumps instead of
00103     // real memory/cmos/etc.
00104     smbios::SmbiosFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
00105     smbios::SmbiosFactory::getFactory()->setParameter("offset", 0);
00106     smbios::SmbiosFactory::getFactory()->setMode(smbios::SmbiosFactory::UnitTestMode);
00107 
00108     cmos::  CmosRWFactory::getFactory()->setParameter("cmosMapFile", cmosCopyFile);
00109     cmos::  CmosRWFactory::getFactory()->setMode( factory::IFactory::UnitTestMode );
00110 
00111     memory::MemoryFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
00112     memory::MemoryFactory::getFactory()->setMode( memory::MemoryFactory::UnitTestMode );
00113 
00114     smi::SmiFactory::getFactory()->setParameter("smiFile", smiOutput);
00115     smi::SmiFactory::getFactory()->setMode( smi::SmiFactory::UnitTestMode );
00116 
00117     doc = 0;
00118     parser = 0;
00119     InitXML();
00120     parser = xmlutils::getParser();
00121     compatXmlReadFile(parser, doc, testInput.c_str());
00122 }
00123 
00124 void testPlatform::tearDown()
00125 {
00126     // the factory is static. If we do not reset the factory, the next
00127     // unit test may accidentally get the wrong objects.
00128     // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
00129     // object used in its construction.
00130     smbios::TokenTableFactory::getFactory()->reset();
00131 
00132     smbios::SmbiosFactory::getFactory()->reset();
00133 
00134     memory::MemoryFactory::getFactory()->reset();
00135 
00136     cmos::CmosRWFactory::getFactory()->reset();
00137 
00138     smi::SmiFactory::getFactory()->reset();
00139 
00140     if (parser)
00141         xmlFreeParser(parser);
00142 
00143     if (doc)
00144         xmlFreeDoc(doc);
00145 
00146     FiniXML();
00147 }
00148 
00149 // checkSkipTest for Skipping known BIOS Bugs.
00150 void 
00151 testPlatform::checkSkipTest(string testName)
00152 {
00153     if(!doc)
00154         return;
00155 
00156     try
00157     {
00158         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *testsToSkip = xmlutils::findElement(xmlDocGetRootElement(doc),"testsToSkip","","");
00159         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *test = xmlutils::findElement(testsToSkip,"test","name",testName);
00160 
00161         if(test)
00162             throw skip_test();
00163     }
00164     catch (const skip_test &)
00165     {
00166         throw;
00167     }
00168     catch (const exception &)
00169     {
00170         //Do Nothing
00171     }
00172 }
00173 
00174 //
00175 // CMOS Token
00176 //
00177 
00178 void
00179 testPlatform::testCmosChecksum ()
00180 {
00181     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00182 
00183     smbios::TokenTableFactory *ttFactory;
00184     ttFactory = smbios::TokenTableFactory::getFactory() ;
00185     const smbios::ITokenTable *tokenTable = ttFactory->getSingleton();
00186 
00187     smbios::ITokenTable::const_iterator token = tokenTable->begin();
00188     while( token != tokenTable->end() )
00189     {
00190         (void) *token;
00191         ++token;
00192     }
00193 
00194     cmos::ICmosRW *cmos = cmos::CmosRWFactory::getFactory()->getSingleton();
00195     observer::IObservable *ob = dynamic_cast<observer::IObservable *>(cmos);
00196     bool doUpdate = false;
00197     if( ob )
00198         ob->notify(&doUpdate);
00199 
00200     STD_TEST_END("");
00201 }
00202 
00203 void
00204 testPlatform::testCmosWriting ()
00205 {
00206     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00207 
00208     smbios::TokenTableFactory *ttFactory;
00209     ttFactory = smbios::TokenTableFactory::getFactory() ;
00210     smbios::ITokenTable *tokenTable = ttFactory->getSingleton();
00211     const smbios::ITokenTable *tokenTableC = ttFactory->getSingleton();
00212 
00213     ASSERT_THROWS( (*tokenTable) ["la la la"], smbios::NotImplemented );
00214     ASSERT_THROWS( (*tokenTableC)["la la la"], smbios::NotImplemented );
00215 
00216     // test [] on const table.
00217     (void) tokenTableC[0xFE];
00218 
00219     ostringstream ost;
00220     ost << *tokenTable << endl;
00221     ost << *tokenTableC << endl;
00222 
00223     // test const iterator
00224     smbios::ITokenTable::const_iterator tokenC = tokenTableC->begin();
00225     while( tokenC != tokenTableC->end() )
00226     {
00227         (void) *tokenC;
00228         (void) tokenC->isString();
00229 
00230         tokenC++;
00231     }
00232 
00233     // refuse to write to cmos unless the checksum is correct
00234     cmos::ICmosRW *cmos = cmos::CmosRWFactory::getFactory()->getSingleton();
00235     observer::IObservable *ob = dynamic_cast<observer::IObservable *>(cmos);
00236     bool doUpdate = false;
00237     if( ob )
00238         ob->notify(&doUpdate);
00239 
00240     smbios::ITokenTable::iterator token = tokenTable->begin();
00241     while( token != tokenTable->end() )
00242     {
00243         //cout << *token << endl;
00244         if( token->isString() )
00245         {
00246             const char *testStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnop";
00247             const u8 *testStrU8 = reinterpret_cast<const u8*>(testStr);
00248             u8 *myStr=0;
00249             u8 *myStr1=0;
00250             try
00251             {
00252                 // not really a valid test anymore since SMI tokens can be
00253                 // accessed as bit or string.
00254                 //ASSERT_THROWS( token->activate(), smbios::InvalidAccessMode );
00255                 //ASSERT_THROWS( token->isActive(), smbios::InvalidAccessMode );
00256 
00257                 unsigned int size = token->getStringLength() + 1;
00258 
00259                 myStr1 = new u8[ size ];
00260                 memset( myStr1, 0, size );
00261 
00262                 try
00263                 {
00264                     token->getString( myStr1, size );
00265                 }
00266                 catch(const smi::UnhandledSmi &)
00267                 {   /* if token is a smi token, cannot unit test. */
00268                     delete [] myStr1;
00269                     goto next_token;
00270                 }
00271 
00272                 token->setString( testStrU8, strlen(testStr) + 1 );
00273 
00274                 CPPUNIT_ASSERT( size <= strlen(testStr)+1 );
00275 
00276                 myStr = new u8[ size ];
00277                 memset( myStr, 0, size );
00278                 token->getString( myStr, size );
00279 
00280                 // return might be smaller, only compare up to what was stored.
00281                 if( 0 != memcmp( testStr, reinterpret_cast<char*>(myStr), size - 1 ) )
00282                 {
00283                     // FAILED
00284                     ostringstream ost;
00285                     ost << "String set on token failed." << endl;
00286                     ost << (*token) << endl;
00287                     ost << "Size of string to compare is: " << size-1 << endl;
00288                     ost << "Original data: (" << myStr1  << ")" << endl;
00289                     ost << "Wrote        : (" << testStr << ")" << endl;
00290                     ost << "Read back    : (" << myStr   << ")" << endl;
00291                     CPPUNIT_FAIL( ost.str().c_str() );
00292                 }
00293             }
00294             catch(...)
00295             {
00296                 delete [] myStr1;
00297                 delete [] myStr;
00298                 myStr1 = 0;
00299                 myStr = 0;
00300                 throw;
00301             }
00302             delete [] myStr1;
00303             delete [] myStr;
00304             myStr1 = 0;
00305             myStr = 0;
00306         }
00307         else
00308         {
00309             try
00310             {
00311                 token->activate();
00312             }
00313             catch(const smi::UnhandledSmi &)
00314             {   /* if token is a smi token, cannot unit test. */
00315                 goto next_token;
00316             }
00317             if( ! token->isActive() )
00318             {
00319                 ostringstream ost;
00320                 ost << "Failed to SET bit token. Token data: " << endl;
00321                 ost << (*token);
00322                 CPPUNIT_FAIL( ost.str().c_str() );
00323             }
00324             // not really a valid test anymore since SMI tokens can be
00325             // accessed as bit or string.
00326             //ASSERT_THROWS( token->setString(0, 0), smbios::InvalidAccessMode );
00327             //ASSERT_THROWS( token->getStringLength(), smbios::InvalidAccessMode );
00328         }
00329 
00330 next_token:
00331         // test post-increment behaves properly
00332         smbios::ITokenTable::iterator before = token;
00333         smbios::ITokenTable::iterator after = token++;
00334         CPPUNIT_ASSERT( before == after );
00335     }
00336 
00337     // recheck the checksums.
00338     // ensure that we wrote correct checksums out
00339     if( ob )
00340         ob->notify(&doUpdate);
00341 
00342     STD_TEST_END("");
00343 }
00344 
00345 
00346 
00347 //
00348 // System Info
00349 //
00350 
00351 
00352 void
00353 testPlatform::testSystemInfo()
00354 {
00355     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00356 
00357     int   systemId   = 0;
00358     const char *systemName = 0;
00359     const char *serviceTag = 0;
00360     const char *assetTag   = 0;
00361     const char *biosVersion   = 0;
00362     const char *vendorName    = 0;
00363 
00364     try
00365     {
00366         systemId   = SMBIOSGetDellSystemId();
00367         systemName = SMBIOSGetSystemName();
00368         serviceTag = SMBIOSGetServiceTag();
00369         assetTag   = SMBIOSGetAssetTag();
00370         biosVersion   = SMBIOSGetBiosVersion();
00371         vendorName    = SMBIOSGetVendorName();
00372 
00373         int   isDell        = SMBIOSIsDellSystem();
00374 
00375         (void) systemId; //avoid unused var warning
00376         (void) isDell; //avoid unused var warning
00377 
00378         //We should at least get an empty string from these
00379         //methods.  Never a null string.
00380         CPPUNIT_ASSERT(systemId != 0);
00381         CPPUNIT_ASSERT(systemName != 0);
00382         //CPPUNIT_ASSERT(serviceTag != 0); // svc tag can legitimately be 0
00383         //CPPUNIT_ASSERT(assetTag != 0); //This fails on latitude so we comment out for now.
00384         CPPUNIT_ASSERT(biosVersion != 0);
00385         CPPUNIT_ASSERT(vendorName != 0);
00386     }
00387     catch(...)
00388     {
00389         SMBIOSFreeMemory( systemName );
00390         SMBIOSFreeMemory( serviceTag );
00391         SMBIOSFreeMemory( assetTag );
00392         SMBIOSFreeMemory( biosVersion );
00393         SMBIOSFreeMemory( vendorName );
00394 
00395         throw;
00396     }
00397 
00398     SMBIOSFreeMemory( systemName );
00399     SMBIOSFreeMemory( serviceTag );
00400     SMBIOSFreeMemory( assetTag );
00401     SMBIOSFreeMemory( biosVersion );
00402     SMBIOSFreeMemory( vendorName );
00403 
00404     STD_TEST_END("");
00405 }
00406 
00407 
00408 // testInput.xml tests
00409 string testPlatform::getTestInputString( string toFind, string section )
00410 {
00411     if (!doc)
00412         throw skip_test();
00413 
00414     string foundString = "";
00415 
00416     try
00417     {
00418         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *systeminfo = xmlutils::findElement( xmlDocGetRootElement(doc), section, "", "" );
00419         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *sysName = xmlutils::findElement( systeminfo, toFind, "", "" );
00420         foundString = xmlutils::getNodeText( sysName );
00421     }
00422     catch( const exception & )
00423     {
00424         throw skip_test();
00425     }
00426 
00427     return foundString;
00428 }
00429 
00430 
00431 void
00432 testPlatform::testIdByte()
00433 {
00434     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00435 
00436     int   systemId   = SMBIOSGetDellSystemId  ();
00437 
00438     string idStr = getTestInputString("idByte");
00439     int id  = strtol( idStr.c_str(), 0, 0);
00440 
00441     CPPUNIT_ASSERT_EQUAL ( id, systemId );
00442 
00443     STD_TEST_END("");
00444 }
00445 
00446 string safeConvertToString( const char *str )
00447 {
00448     string fromSystem = "";
00449     if( 0 != str )
00450     {
00451         fromSystem = str;
00452     }
00453     SMBIOSFreeMemory(str);
00454     return fromSystem;
00455 }
00456 
00457 void
00458 testPlatform::testSystemName()
00459 {
00460     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00461 
00462     string fromSystem = safeConvertToString( SMBIOSGetSystemName() );
00463     string testInput  = getTestInputString( "systemName" );
00464 
00465     CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
00466     STD_TEST_END("");
00467 }
00468 
00469 void
00470 testPlatform::testServiceTag()
00471 {
00472     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00473 
00474     string fromSystem = safeConvertToString( SMBIOSGetServiceTag() );
00475     string testInput  = getTestInputString( "serviceTag" );
00476 
00477     CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
00478 
00479     STD_TEST_END("");
00480 }
00481 
00482 //  not part of public API, so just wing it here so that we can do the unit test.
00483 extern char *getServiceTagFromCMOSToken();
00484 
00485 void
00486 testPlatform::testServiceTagWriting()
00487 {
00488     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00489 
00490     string fromSystem = safeConvertToString( SMBIOSGetServiceTag() );
00491     string testInput  = getTestInputString( "serviceTag" );
00492 
00493     CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
00494 
00495     string rawCMOSOrig("");
00496     try
00497     {
00498         rawCMOSOrig = safeConvertToString( getServiceTagFromCMOSToken() );
00499     }
00500     catch(const exception &)
00501     {
00502         // if service tag is not in SMBIOS, we cannot do the
00503         // tests below
00504         throw skip_test();
00505     }
00506 
00507     CPPUNIT_ASSERT_EQUAL ( testInput, rawCMOSOrig );
00508 
00509     string tagToSet, shouldBe, rawCMOSNew;
00510 
00511     // test std 5-char svc tag
00512     tagToSet = shouldBe = "NEWTG";
00513     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00514     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00515     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00516 
00517     // test std 7-char svc tag (alphabet 1/3)
00518     tagToSet = shouldBe = "BCDFGHJ";
00519     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00520     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00521     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00522 
00523     // test std 7-char svc tag (alphabet 2/3)
00524     tagToSet = shouldBe = "KLMNPQR";
00525     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00526     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00527     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00528 
00529     // test std 7-char svc tag (alphabet 3/3)
00530     tagToSet = shouldBe = "STVWXYZ";
00531     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00532     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00533     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00534 
00535     // odd size (1)
00536     tagToSet = shouldBe = "A";
00537     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00538     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00539     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00540 
00541     // odd size (2)
00542     tagToSet = shouldBe = "AB";
00543     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00544     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00545     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00546 
00547     // odd size (3)
00548     tagToSet = shouldBe = "ABC";
00549     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00550     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00551     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00552 
00553     // odd size (4)
00554     tagToSet = shouldBe = "ABCD";
00555     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00556     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00557     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00558 
00559     // odd size (6)
00560     tagToSet = "12DFGH";
00561     shouldBe = "12DFGH0";  // invalid/missing chars for 7-char svc tag 
00562                     // converted to '0'
00563     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00564     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00565     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00566 
00567     // odd size (7)
00568     tagToSet = shouldBe = "XGYZYYY";
00569     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00570     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00571     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00572 
00573     // odd size (8)
00574     tagToSet = "MNPQMNPQ";
00575     shouldBe = "MNPQMNP"; // extra chars ignored
00576     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00577     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00578     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00579 
00580     // invalid chars in 7-char tag
00581     tagToSet = "ABEIOUD";
00582     shouldBe = "AB0000D"; // invalid chars turned into '0';
00583     SMBIOSSetServiceTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00584     rawCMOSNew = safeConvertToString( getServiceTagFromCMOSToken() );
00585     CPPUNIT_ASSERT_EQUAL ( shouldBe, rawCMOSNew );
00586 
00587 
00588     STD_TEST_END("");
00589 }
00590 
00591 // not part of public API
00592 extern char *getAssetTagFromToken();
00593 
00594 void
00595 testPlatform::testAssetTag()
00596 {
00597     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00598 
00599     string fromSystem = safeConvertToString( SMBIOSGetAssetTag() );
00600     string testInput  = getTestInputString( "assetTag" );
00601     CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
00602 
00603     string tagToSet = "1234567890"; 
00604     SMBIOSSetAssetTag("", tagToSet.c_str(), strlen(tagToSet.c_str()));
00605 
00606     try
00607     {
00608         // only do this part of the test if system has CMOS tag
00609         // This will throw exception if not present
00610         fromSystem = safeConvertToString( getAssetTagFromToken() );
00611         CPPUNIT_ASSERT_EQUAL ( tagToSet, fromSystem );
00612     }
00613     catch (const exception &)
00614     {
00615     }
00616 
00617     STD_TEST_END("");
00618 }
00619 
00620 void
00621 testPlatform::testBiosVersion()
00622 {
00623     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00624 
00625     string fromSystem = safeConvertToString( SMBIOSGetBiosVersion() );
00626     string testInput  = getTestInputString( "biosVersion" );
00627 
00628     CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
00629 
00630     STD_TEST_END("");
00631 }
00632 
00633 void
00634 testPlatform::testIsDell()
00635 {
00636     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00637 
00638     int   isDell   = SMBIOSIsDellSystem  ();
00639 
00640     string strval = getTestInputString( "isDellSystem" );
00641     int isDellExpected = strtol( strval.c_str(), 0, 0);
00642 
00643     CPPUNIT_ASSERT_EQUAL ( isDell, isDellExpected );
00644 
00645     STD_TEST_END("");
00646 }
00647 
00648 void testPlatform::testVariousAccessors()
00649 {
00650     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00651 
00652     // table should not be deleted when we are finished. It is managed by the
00653     // factory. Factory will delete it for us when ->reset() is called.
00654     const smbios::ISmbiosTable *table =
00655         smbios::SmbiosFactory::getFactory()->getSingleton();
00656 
00657     smbios::ISmbiosTable::const_iterator item = (*table)[smbios::BIOS_Information] ;
00658 
00659     string vendorStr="";
00660     string versionStr="";
00661     string releaseStr="";
00662 
00663     if (!doc)
00664         throw skip_test();
00665 
00666     // pull info out of xml
00667     try
00668     {
00669         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *smbios = xmlutils::findElement( xmlDocGetRootElement(doc), "smbios", "", "" );
00670         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *biosInfo = xmlutils::findElement( smbios, "biosInformation", "", "" );
00671         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *vendor = xmlutils::findElement( biosInfo, "vendor", "", "" );
00672         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *version = xmlutils::findElement( biosInfo, "version", "", "" );
00673         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *release = xmlutils::findElement( biosInfo, "release", "", "" );
00674         vendorStr = xmlutils::getNodeText( vendor );
00675         versionStr = xmlutils::getNodeText( version );
00676         releaseStr = xmlutils::getNodeText( release );
00677 
00678     }
00679     catch( const exception & )
00680     {
00681         throw skip_test();
00682     }
00683 
00684     const string string1( getString_FromItem(*item, 4) ); // BIOS VENDOR
00685     const string string2( getString_FromItem(*item, 5) ); // BIOS VERSION
00686     const string string3( getString_FromItem(*item, 8) ); // RELEASE DATE
00687 
00688     const string string4( item->getStringByStringNumber(1) ); //BIOS VENDOR
00689     const string string5( item->getStringByStringNumber(2) ); //BIOS VERSION
00690     const string string6( item->getStringByStringNumber(3) ); //RELEASE DATE
00691 
00692     CPPUNIT_ASSERT_EQUAL( vendorStr, string1 );
00693     CPPUNIT_ASSERT_EQUAL( versionStr, string2 );
00694     CPPUNIT_ASSERT_EQUAL( releaseStr, string3 );
00695 
00696     CPPUNIT_ASSERT_EQUAL( string1, string4 );
00697     CPPUNIT_ASSERT_EQUAL( string2, string5 );
00698     CPPUNIT_ASSERT_EQUAL( string3, string6 );
00699 
00700     STD_TEST_END("");
00701 }
00702 
00703 void
00704 testPlatform::testStateBytes()
00705 {
00706     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00707 
00708     if( ! SMBIOSHasNvramStateBytes() )
00709         throw skip_test();
00710 
00711     int testValue = 0;
00712     SMBIOSGetNvramStateBytes( 0x0000 );
00713 
00714     // test DSA mode
00715     testValue = 0x1234;
00716     SMBIOSSetNvramStateBytes( testValue, 0x0000 );
00717     CPPUNIT_ASSERT_EQUAL( testValue, SMBIOSGetNvramStateBytes( 0x0000 ) ); // DSA sees real value
00718     CPPUNIT_ASSERT_EQUAL( 0x0000,    SMBIOSGetNvramStateBytes( 0x8000 ) ); // toolkit should see 0
00719     CPPUNIT_ASSERT_EQUAL( 0x0000,    SMBIOSGetNvramStateBytes( 0xF100 ) ); // other should see 0
00720 
00721     // test DSA mode (proper mask off)
00722     testValue = 0x9234; // we should not be able to set topmost bit
00723     SMBIOSSetNvramStateBytes( testValue, 0x0000 );
00724     CPPUNIT_ASSERT_EQUAL( (testValue & ~0x8000), SMBIOSGetNvramStateBytes( 0x0000 ) ); // DSA sees real value
00725     CPPUNIT_ASSERT_EQUAL( 0x0000,                SMBIOSGetNvramStateBytes( 0x8000 ) ); // toolkit should see 0
00726     CPPUNIT_ASSERT_EQUAL( 0x0000,                SMBIOSGetNvramStateBytes( 0xF100 ) ); // other should see 0
00727 
00728     // test Toolkit mode
00729     testValue = 0x0234; // we should not be able to set topmost bit
00730     SMBIOSSetNvramStateBytes( testValue, 0x8000 );
00731     CPPUNIT_ASSERT_EQUAL( testValue, SMBIOSGetNvramStateBytes( 0x8000 ) ); //toolkit sees real value
00732     CPPUNIT_ASSERT_EQUAL( 0x0000,    SMBIOSGetNvramStateBytes( 0x0000 ) ); // DSA should see 0
00733     CPPUNIT_ASSERT_EQUAL( 0x0000,    SMBIOSGetNvramStateBytes( 0xF100 ) ); // other should see 0
00734 
00735     // test Toolkit mode (proper mask off)
00736     testValue = 0x7234; // we should not be able to set topmost nibble (4 bits)
00737     SMBIOSSetNvramStateBytes( testValue, 0x8000 );
00738     CPPUNIT_ASSERT_EQUAL( (testValue & ~0xF000), SMBIOSGetNvramStateBytes( 0x8000 ) ); //toolkit sees real value
00739     CPPUNIT_ASSERT_EQUAL( 0x0000,                SMBIOSGetNvramStateBytes( 0x0000 ) ); // DSA should see 0
00740     CPPUNIT_ASSERT_EQUAL( 0x0000,                SMBIOSGetNvramStateBytes( 0xF100 ) ); // other should see 0
00741 
00742     // test other mode
00743     testValue = 0x0034; // we should not be able to set topmost byte
00744     SMBIOSSetNvramStateBytes( testValue, 0xF100 );
00745     CPPUNIT_ASSERT_EQUAL( testValue, SMBIOSGetNvramStateBytes( 0xF100 ) ); // other sees real value
00746     CPPUNIT_ASSERT_EQUAL( 0x0000,    SMBIOSGetNvramStateBytes( 0x0000 ) ); // DSA should see 0
00747     CPPUNIT_ASSERT_EQUAL( 0x0000,    SMBIOSGetNvramStateBytes( 0x8000 ) ); // DSA should see 0
00748 
00749     // test other mode (proper mask off)
00750     testValue = 0x7234; // we should not be able to set topmost byte
00751     SMBIOSSetNvramStateBytes( testValue, 0xF100 );
00752     CPPUNIT_ASSERT_EQUAL( (testValue & ~0xFF00), SMBIOSGetNvramStateBytes( 0xF100 ) ); // other sees real value
00753     CPPUNIT_ASSERT_EQUAL( 0x0000,                SMBIOSGetNvramStateBytes( 0x0000 ) ); // DSA should see 0
00754     CPPUNIT_ASSERT_EQUAL( 0x0000,                SMBIOSGetNvramStateBytes( 0x8000 ) ); // DSA should see 0
00755 
00756     STD_TEST_END("");
00757 }
00758 
00759 void
00760 testPlatform::testUpBoot()
00761 {
00762     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00763 
00764     if( ! SMBIOSHasBootToUp() )
00765         throw skip_test();
00766 
00767     SMBIOSSetBootToUp(1);
00768     CPPUNIT_ASSERT_EQUAL( 1, SMBIOSGetBootToUp() );
00769 
00770     SMBIOSSetBootToUp(0);
00771     CPPUNIT_ASSERT_EQUAL( 0, SMBIOSGetBootToUp() );
00772 
00773     SMBIOSSetBootToUp(1);
00774     CPPUNIT_ASSERT_EQUAL( 1, SMBIOSGetBootToUp() );
00775 
00776     SMBIOSSetBootToUp(0);
00777     CPPUNIT_ASSERT_EQUAL( 0, SMBIOSGetBootToUp() );
00778 
00779     STD_TEST_END("");
00780 }
00781 
00782 
00783 void
00784 testPlatform::testOutOfBounds()
00785 {
00786     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00787 
00788     const smbios::ISmbiosTable *table =
00789         smbios::SmbiosFactory::getFactory()->getSingleton();
00790 
00791     smbios::ISmbiosTable::const_iterator item = (*table)[smbios::BIOS_Information] ;
00792 
00793     // access string '0' should always throw. (string offset start at 1, per
00794     // spec)
00795     ASSERT_THROWS( item->getStringByStringNumber(0), smbios::StringUnavailable );
00796 
00797     if (!doc)
00798         throw skip_test();
00799 
00800     int numStrings = 0;
00801     // pull info out of xml
00802     try
00803     {
00804         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *smbios = xmlutils::findElement( xmlDocGetRootElement(doc), "smbios", "", "" );
00805         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *biosInfo = xmlutils::findElement( smbios, "biosInformation", "", "" );
00806         numStrings = strtoul( xmlutils::safeGetAttribute( biosInfo, "numstrings" ).c_str(), 0, 0 );
00807     }
00808     catch( const exception & )
00809     {
00810         throw skip_test();
00811     }
00812 
00813     // Should not throw (cast to void to eliminate unused var warn)
00814     if( numStrings > 0 )
00815         (void) (item->getStringByStringNumber(numStrings));
00816 
00817     ASSERT_THROWS( item->getStringByStringNumber(numStrings + 1), smbios::StringUnavailable );
00818     ASSERT_THROWS( item->getStringByStringNumber(numStrings + 2), smbios::StringUnavailable );
00819 
00820     STD_TEST_END("");
00821 }
00822 
00823 
00824 void
00825 testPlatform::testConstructionOffset1()
00826 {
00827     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00828 
00829     if (!doc)
00830         throw skip_test();
00831 
00832     u32 offset = 0;
00833     try
00834     {
00835         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *smbios = xmlutils::findElement( xmlDocGetRootElement(doc), "smbios", "", "" );
00836         offset = strtoul( xmlutils::safeGetAttribute( smbios, "offset" ).c_str(), 0, 0 );
00837         if( 0 == offset )
00838         {
00839             throw skip_test();
00840         }
00841     }
00842     catch( const exception & )
00843     {
00844         throw skip_test();
00845     }
00846 
00847     smbios::SmbiosFactory::getFactory()->setParameter("offset", offset);
00848     const smbios::ISmbiosTable *table =
00849         smbios::SmbiosFactory::getFactory()->getSingleton();
00850 
00851     int tableEntriesCounted = 0;
00852     smbios::ISmbiosTable::const_iterator item = table->begin();
00853     while( item != table->end() )
00854     {
00855         tableEntriesCounted++;
00856         (void) *item;  // do this to sniff out possible errors with deref iterator.
00857         ++item;
00858     }
00859 
00860     CPPUNIT_ASSERT( tableEntriesCounted == table->getNumberOfEntries() );
00861     STD_TEST_END("");
00862 }
00863 
00864 void
00865 testPlatform::testConstructionOffset2()
00866 {
00867     STD_TEST_START_CHECKSKIP(getTestName().c_str() << "  ");
00868 
00869     if (!doc)
00870         throw skip_test();
00871 
00872     u32 offset = 0;
00873     try
00874     {
00875         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *smbios = xmlutils::findElement( xmlDocGetRootElement(doc), "smbios", "", "" );
00876         offset = strtoul( xmlutils::safeGetAttribute( smbios, "offset" ).c_str(), 0, 0 );
00877         if( 0 == offset )
00878         {
00879             throw skip_test();
00880         }
00881     }
00882     catch( const exception & )
00883     {
00884         throw skip_test();
00885     }
00886 
00887     //
00888     // TEST BAD FILENAMES
00889     // construct our table with various invalid file names
00890     //
00891     smbios::SmbiosFactory *factory = smbios::SmbiosFactory::getFactory();
00892 
00893     // use auto_ptr so in case it does _not_ throw, we don't leak mem in the test.
00894     //  DO NOT USE ->getSingleton() here... we use ->makeNew() on purpose.
00895     //  This is an internal test and the parameters of this test mean we should _not_ use a singleton.
00896     factory->setParameter("offset", 1);
00897     ASSERT_THROWS( auto_ptr<const smbios::ISmbiosTable>p(factory->makeNew()), smbios::IException );
00898 
00899     factory->setParameter("offset", 1000);
00900     ASSERT_THROWS( auto_ptr<const smbios::ISmbiosTable>p(factory->makeNew()), smbios::IException );
00901 
00902     factory->setParameter("offset", 0xFFFFFUL ); // F_BLOCK_END (private definition)
00903     ASSERT_THROWS( auto_ptr<const smbios::ISmbiosTable>p(factory->makeNew()), smbios::IException );
00904 
00905     factory->setParameter("offset", 0xFFFFFUL - 1); // F_BLOCK_END (private definition)
00906     ASSERT_THROWS( auto_ptr<const smbios::ISmbiosTable>p(factory->makeNew()), smbios::IException );
00907 
00908     smbios::SmbiosFactory::getFactory()->setParameter("offset", offset + 1);
00909     ASSERT_THROWS( auto_ptr<const smbios::ISmbiosTable> table( factory->makeNew() ), smbios::IException );
00910 
00911     smbios::SmbiosFactory::getFactory()->setParameter("offset", offset - 1);
00912     ASSERT_THROWS( auto_ptr<const smbios::ISmbiosTable> table( factory->makeNew() ), smbios::IException );
00913 
00914     // should not be able to use no-argument constructor...
00915     // UNCOMMENT TO CHECK.
00916     // THIS TEST DOES NOT COMPILE. IT SHOULD NOT COMPILE
00917     // DUE TO THE DEFINITION OF SmbiosTable.
00918     //ASSERT_THROWS( smbios::SmbiosTableFileIo myTable1, smbios::PermissionException);
00919 
00920     STD_TEST_END("");
00921 }
00922 
00923 

Generated on Tue Feb 26 14:52:57 2008 for SMBIOS Library by  doxygen 1.4.7