Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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

Generated on Wed Apr 11 16:25:10 2007 for SMBIOS Library by doxygen 1.3.5