• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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

Generated on Sun Aug 22 2010 14:38:31 for SMBIOS Library by  doxygen 1.7.1