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

testStandalone.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 "testStandalone.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 testStandalone.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 (testStandalone);
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 testStandalone::setUp()
00072 {
00073     string programDirname = getCppunitTopDirectory();
00074     string writeDirectory = getWritableDirectory();
00075     string xmlFile = programDirname + getXmlFile();
00076 
00077     // copy the memdump.dat file. We do not write to it, but rw open will fail
00078     // if we do not copy it
00079     string memdumpOrigFile = programDirname + getTestDirectory() + "/memdump.dat";
00080     if(!fileExists(memdumpOrigFile))
00081         memdumpOrigFile = getTestDirectory() + "/memdump.dat";
00082     string memdumpCopyFile = writeDirectory + "/memdump-copy.dat";
00083     copyFile( memdumpCopyFile, memdumpOrigFile );
00084 
00085     // copy the CMOS file. We are going to write to it and do not wan to mess up
00086     // the pristine unit test version
00087     string cmosOrigFile = programDirname + getTestDirectory() + "/cmos.dat";
00088     if(!fileExists(cmosOrigFile))
00089         cmosOrigFile = getTestDirectory() + "/cmos.dat";
00090     string cmosCopyFile = writeDirectory + "/cmos-copy.dat";
00091     copyFile( cmosCopyFile, cmosOrigFile );
00092 
00093     // Smi output file.
00094     string smiOutput = writeDirectory + "/smi-output.dat";
00095 
00096     // set up XML factory. from here on, we can just say SmbiosFactory.
00097     smbios::SmbiosXmlFactory::getFactory();
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     // The parameter below will normally need to be set by the client code.
00118     smbios::SmbiosFactory::getFactory()->setParameter("xmlFile", xmlFile);
00119 }
00120 
00121 void testStandalone::resetFactoryToBuiltinXml()
00122 {
00123     smbios::SmbiosFactory::getFactory()->setParameter("xmlFile", "");
00124 }
00125 
00126 void testStandalone::tearDown()
00127 {
00128     // the factory is static. If we do not reset the factory, the next
00129     // unit test may accidentally get the wrong objects.
00130     // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
00131     // object used in its construction.
00132     smbios::TokenTableFactory::getFactory()->reset();
00133 
00134     smbios::SmbiosFactory::getFactory()->reset();
00135 
00136     memory::MemoryFactory::getFactory()->reset();
00137 
00138     cmos::CmosRWFactory::getFactory()->reset();
00139 
00140     smi::SmiFactory::getFactory()->reset();
00141 }
00142 
00143 //
00144 //
00145 // TABLE tests
00146 //
00147 //
00148 
00149 void testStandalone::testTable_Subscript()
00150 {
00151     STD_TEST_START(getTestName().c_str() << "  " );
00152     // PURPOSE:
00153     //      The purpose of this test is to test the subscript operator [].
00154     //      It tests these operators using string and int args and tests
00155     //      it outside of a loop.
00156 
00157     // table should not be deleted when we are finished. It is managed by the
00158     // factory. Factory will delete it for us when ->reset() is called.
00159     smbios::ISmbiosTable *table =
00160         smbios::SmbiosFactory::getFactory()->getSingleton();
00161 
00162     smbios::ISmbiosTable::iterator item1;
00163     smbios::ISmbiosTable::iterator item2;
00164 
00165     item1 = (*table)["BIOS Information"];
00166     item2 = (*table)[smbios::BIOS_Information];
00167 
00168     // Use CPPUNIT_ASSERT_EQUAL when testing equality.
00169     // It gives better diagnostics on failure.
00170     CPPUNIT_ASSERT_EQUAL( item1->getHandle(),    item2->getHandle() );
00171     CPPUNIT_ASSERT_EQUAL( getItemHandle(*item1), item2->getHandle() );
00172     CPPUNIT_ASSERT_EQUAL( item1->getLength(),    item2->getLength() );
00173     CPPUNIT_ASSERT_EQUAL( getItemLength(*item1), item2->getLength() );
00174     CPPUNIT_ASSERT_EQUAL( item1->getType()  ,    item2->getType()   );
00175     CPPUNIT_ASSERT_EQUAL( getItemType(*item1)  , item2->getType()   );
00176 
00177     CPPUNIT_ASSERT_EQUAL( (*table)[smbios::BIOS_Information]->getType(), item1->getType() );
00178     CPPUNIT_ASSERT_EQUAL( (*table)["BIOS Information"]->getType(), item1->getType() );
00179 
00180     item1 = (*table)["System Information"];
00181     item2 = (*table)[smbios::System_Information];
00182 
00183     CPPUNIT_ASSERT_EQUAL( item1->getHandle(),    item2->getHandle() );
00184     CPPUNIT_ASSERT_EQUAL( getItemHandle(*item1), item2->getHandle() );
00185     CPPUNIT_ASSERT_EQUAL( item1->getLength(),    item2->getLength() );
00186     CPPUNIT_ASSERT_EQUAL( getItemLength(*item1), item2->getLength() );
00187     CPPUNIT_ASSERT_EQUAL( item1->getType(),      item2->getType()   );
00188     CPPUNIT_ASSERT_EQUAL( getItemType(*item1)  , item2->getType()   );
00189 
00190     CPPUNIT_ASSERT_EQUAL( (*table)[smbios::System_Information]->getType(), item1->getType() );
00191     CPPUNIT_ASSERT_EQUAL( (*table)["System Information"]->getType(), item1->getType() );
00192 
00193 
00194     STD_TEST_END("");
00195 }
00196 
00197 void testStandalone::testTable_Subscript_builtinXml()
00198 {
00199     resetFactoryToBuiltinXml();
00200     testTable_Subscript();
00201 }
00202 
00203 void
00204 testStandalone::testEntryCount ()
00205 {
00206     STD_TEST_START(getTestName().c_str() << "  " );
00207 
00208     // do not delete. Factory manages lifetime.
00209     smbios::ISmbiosTable *table =
00210         smbios::SmbiosFactory::getFactory()->getSingleton();
00211 
00212     // test streamify() while we are at it.
00213     ostringstream ost;
00214     int tableEntriesCounted = 0;
00215     smbios::ISmbiosTable::iterator item = table->begin();
00216     while( item != table->end() )
00217     {
00218         tableEntriesCounted++;
00219         ost << *item << endl;
00220         ++item;
00221     }
00222 
00223     CPPUNIT_ASSERT_EQUAL( tableEntriesCounted, table->getNumberOfEntries() );
00224     STD_TEST_END("");
00225 }
00226 
00227 void testStandalone::testEntryCount_builtinXml ()
00228 {
00229     resetFactoryToBuiltinXml();
00230     testEntryCount();
00231 }
00232 
00233 void
00234 testStandalone::testConstIterator ()
00235 {
00236     STD_TEST_START(getTestName().c_str() << "  " );
00237 
00238     // do not delete. Factory manages lifetime.
00239     smbios::ISmbiosTable *table =
00240         smbios::SmbiosFactory::getFactory()->getSingleton();
00241 
00242     const smbios::ISmbiosTable *constTable = &*table;
00243 
00244     int tableEntriesCounted = 0;
00245     smbios::ISmbiosTable::const_iterator item = constTable->begin();
00246     while( item != constTable->end() )
00247     {
00248         tableEntriesCounted++;
00249         (void) *item;  // do this to sniff out possible errors with deref iterator.
00250 
00251         ++item;
00252     }
00253     CPPUNIT_ASSERT_EQUAL( tableEntriesCounted, constTable->getNumberOfEntries() );
00254     STD_TEST_END("");
00255 }
00256 
00257 void testStandalone::testConstIterator_builtinXml ()
00258 {
00259     resetFactoryToBuiltinXml();
00260     testConstIterator();
00261 }
00262 
00263 void
00264 testStandalone::testSubscriptOperator1 ()
00265 {
00266     STD_TEST_START(getTestName().c_str() << "  " );
00267 
00268     // do not delete. Factory manages lifetime.
00269     smbios::ISmbiosTable *table =
00270         smbios::SmbiosFactory::getFactory()->getSingleton();
00271 
00272     int tableEntriesCounted = 0;
00273     // table[-1] is special, it returns all objects.
00274     // This test should be identical to testConstIterator (both walk all entries)
00275     for( smbios::ISmbiosTable::iterator item = (*table)[-1] ; item != table->end(); ++item)
00276     {
00277         tableEntriesCounted++;
00278         (void) *item;  // do this to sniff out possible errors with deref iterator.
00279     }
00280     CPPUNIT_ASSERT_EQUAL( table->getNumberOfEntries(), tableEntriesCounted );
00281     STD_TEST_END("");
00282 }
00283 
00284 void testStandalone::testSubscriptOperator1_builtinXml ()
00285 {
00286     resetFactoryToBuiltinXml();
00287     testSubscriptOperator1();
00288 }
00289 
00290 void
00291 testStandalone::testSubscriptOperator2 ()
00292 {
00293     STD_TEST_START(getTestName().c_str() << "  " );
00294 
00295 // it turns out that 8450 has 3 BIOS Information blocks.
00296 
00297 //    // do not delete. Factory manages lifetime.
00298 //    smbios::ISmbiosTable *table =
00299 //        smbios::SmbiosFactory::getFactory()->getSingleton();
00300 //
00301 //    // There should probably be only one "BIOS Information" block. Check.
00302 //    //  update this test if it turns out that there are actual systems with >1 Bios Info block
00303 //    //
00304 //    int tableEntriesCounted = 0;
00305 //    for( smbios::ISmbiosTable::iterator item = (*table)[0] ; item != table->end(); ++item)
00306 //    {
00307 //        (void) *item;  // do this to sniff out possible errors with deref iterator.
00308 //        tableEntriesCounted++;
00309 //    }
00310 //    CPPUNIT_ASSERT_EQUAL( 1, tableEntriesCounted );
00311 
00312     STD_TEST_END("");
00313 }
00314 
00315 void testStandalone::testSubscriptOperator2_builtinXml ()
00316 {
00317     resetFactoryToBuiltinXml();
00318     testSubscriptOperator2();
00319 }
00320 
00321 void
00322 testStandalone::testSubscriptOperator3 ()
00323 {
00324     STD_TEST_START(getTestName().c_str() << "  " );
00325 
00326     // do not delete. Factory manages lifetime.
00327     smbios::ISmbiosTable *table =
00328         smbios::SmbiosFactory::getFactory()->getSingleton();
00329 
00330     // There should normally be more than one "Port Connector" block. Check.
00331     //                               "Port Connector" block is type == 8
00332     //   memdump-opti.txt     has 12
00333     //   memdump-PAweb110.txt has 17
00334     //   memdump-PAweb110.txt has 9  *unverified...
00335     //  update this test if it turns out that there are actual systems with <2 Port Connector blocks
00336     //
00337     int tableEntriesCounted = 0;
00338     for( smbios::ISmbiosTable::iterator item = (*table)[8] ; item != table->end(); ++item)
00339     {
00340         (void) *item;  // do this to sniff out possible errors with deref iterator.
00341         tableEntriesCounted++;
00342     }
00343     CPPUNIT_ASSERT( 1 < tableEntriesCounted );
00344     STD_TEST_END("");
00345 }
00346 
00347 void testStandalone::testSubscriptOperator3_builtinXml ()
00348 {
00349     resetFactoryToBuiltinXml();
00350     testSubscriptOperator3();
00351 }
00352 
00353 
00354 
00355 //
00356 //
00357 // ITEM tests
00358 //
00359 //
00360 
00361 void testStandalone::testStreamify()
00362 {
00363     STD_TEST_START(getTestName().c_str() << "  " );
00364 
00365     // BEGIN EXAMPLE iterator
00366     // table should not be deleted when we are finished. It is managed by the
00367     // factory. Factory will delete it for us when ->reset() is called.
00368     smbios::ISmbiosTable *table =
00369         smbios::SmbiosFactory::getFactory()->getSingleton();
00370 
00371     ostringstream ost;
00372     ost << *table << endl;
00373 
00374     // iterate over all PORT INFORMATION BLOCKS
00375     for( smbios::ISmbiosTable::iterator item = (*table)[8] ; item != table->end(); ++item)
00376     {
00377         ost << *item << endl;
00378     }
00379     // END EXAMPLE iterator
00380 
00381     STD_TEST_END("");
00382 }
00383 
00384 void
00385 testStandalone::testItemIdentity ()
00386 {
00387     STD_TEST_START(getTestName().c_str() << "  " );
00388     // test that when we grab things out of the
00389     // table, we get copies of the same thing
00390     // when we ask for the same thing, rather than
00391     // separate items with the same data.
00392     //
00393     // we use references below to make the CPPUNIT_ASSERT
00394     // a bit easier to read.
00395 
00396     // do not delete. Factory manages lifetime.
00397     smbios::ISmbiosTable *table =
00398         smbios::SmbiosFactory::getFactory()->getSingleton();
00399 
00400     // grab the first bios block
00401     smbios::ISmbiosTable::iterator position1 = (*table)[smbios::BIOS_Information];
00402     smbios::ISmbiosItem &item1 = *position1; //reference
00403 
00404     // use another iterator and grab another
00405     smbios::ISmbiosTable::iterator position2 = (*table)[smbios::BIOS_Information];
00406     smbios::ISmbiosItem &item2 = *position2; //reference
00407 
00408     // Check that they both gave the same thing
00409     // The address of each should be equal
00410     CPPUNIT_ASSERT_EQUAL(  &item1, &item2 );  // easiest to read
00411     CPPUNIT_ASSERT_EQUAL(  &(*position1), &item2 ); // same test, written differently
00412     CPPUNIT_ASSERT_EQUAL(  &item1       , &(*position2) ); // same test, written differently
00413     CPPUNIT_ASSERT_EQUAL(  &(*position1), &(*position2) ); // same test, written differently
00414 
00415     // use another iterator and grab another _different_ pointer.
00416     smbios::ISmbiosTable::iterator position3 = (*table)[smbios::System_Information];
00417     smbios::ISmbiosItem &item3 = *position3; //reference
00418 
00419     // Check that these are different...
00420     CPPUNIT_ASSERT( &item1 != &item3 );
00421     CPPUNIT_ASSERT( &item2 != &item3 );
00422 
00423     CPPUNIT_ASSERT_EQUAL( item1.getType(), static_cast<u8>(smbios::BIOS_Information) );
00424     CPPUNIT_ASSERT_EQUAL( item2.getType(), static_cast<u8>(smbios::BIOS_Information) );
00425     CPPUNIT_ASSERT_EQUAL( item3.getType(), static_cast<u8>(smbios::System_Information) );
00426 
00427     STD_TEST_END("");
00428 }
00429 
00430 void testStandalone::testItemIdentity_builtinXml ()
00431 {
00432     resetFactoryToBuiltinXml();
00433     testItemIdentity();
00434 }
00435 
00436 void
00437 testStandalone::testEachItemAccessors ()
00438 {
00439     STD_TEST_START(getTestName().c_str() << "  " );
00440     // test getU{8|16}_FromItem() functions
00441     // no way to test in generic table the getU32
00442     // or getU64
00443     //
00444     // primarily to ensure that getUx_FromItem() has the endianness correct
00445 
00446     // do not delete. Factory manages lifetime.
00447     smbios::ISmbiosTable *table =
00448         smbios::SmbiosFactory::getFactory()->getSingleton();
00449 
00450     smbios::ISmbiosTable::iterator item = table->begin();
00451     while( item != table->end() )
00452     {
00453         u8 type1 = item->getType();
00454         u8 type2 = getU8_FromItem(*item, 0);
00455         CPPUNIT_ASSERT_EQUAL( type1, type2 );
00456 
00457         u8 len1 = item->getLength();
00458         u8 len2 = getU8_FromItem(*item, 1);
00459         CPPUNIT_ASSERT_EQUAL( len1, len2 );
00460 
00461         u16 handle1 = item->getHandle();
00462         u16 handle2 = getU16_FromItem(*item, 2);
00463         CPPUNIT_ASSERT_EQUAL( handle1, handle2 );
00464 
00465         ++item;
00466     }
00467 
00468     STD_TEST_END("");
00469 }
00470 
00471 void testStandalone::testEachItemAccessors_builtinXml ()
00472 {
00473     resetFactoryToBuiltinXml();
00474     testStandalone::testEachItemAccessors();
00475 }
00476 
00477 void testStandalone::testItem_GetBiosInfo()
00478 {
00479     STD_TEST_START(getTestName().c_str() << "  " );
00480     // Purpose:
00481     //      The purpose of this test is to exercise all of the getXX()
00482     //      functions that are avalable using std SMBIOS tables that are
00483     //      available on all dumps. Do not rely on any specific value in any
00484     //      table in these tests as this test will be run against many
00485     //      tables.
00486     //
00487 
00488     // BEGIN EXAMPLE factory
00489     // table should not be deleted when we are finished. It is managed by the
00490     // factory. Factory will delete it for us when ->reset() is called.
00491     smbios::ISmbiosTable *table =
00492         smbios::SmbiosFactory::getFactory()->getSingleton();
00493 
00494     smbios::ISmbiosTable::iterator item1 = (*table)["BIOS Information"];
00495 
00496     //
00497     // here is an example XML for BIOS Information Block, which we test below.
00498     //
00499     //<FIELD offset="0h" name="Type" length="BYTE" usage="STRUCTURE_TYPE"/>
00500     //<FIELD offset="1h" name="Length" length="BYTE" usage="SIZE"/>
00501     //<FIELD offset="2h" name="Handle" length="WORD" usage="HANDLE"/>
00502     //<FIELD offset="4h" name="Vendor" length="BYTE" usage="STRING"/>
00503     //<FIELD offset="5h" name="BIOS Version" length="BYTE" usage="STRING"/>
00504     //<FIELD offset="6h" name="BIOS Starting Address Segment" length="WORD" usage="ADDRESS"/>
00505     //<FIELD offset="8h" name="BIOS Release Date" length="BYTE" usage="STRING"/>
00506     //<FIELD offset="9h" name="BIOS ROM Size" length="BYTE" usage="SIZE"/>
00507     //<FIELD offset="Ah" name="BIOS Characteristics" length="QWORD" usage="BITFIELD">
00508     //
00509 
00510     u64 ull1, ull2;
00511     u16 v1, v2, v3;
00512     u8  u1, u2, u3;
00513     int int1, int2;
00514     string str1, str2;
00515 
00516     // Test Philosophy:
00517     //      There are multiple ways to access any particular item in the table.
00518     //      If you have an XML smbios file, you can access by string name and
00519     //      the code will look the name up in the XML.
00520     //
00521     //      We play the different access modes off each other below to ensure
00522     //      that each access mode returns the exact same data.
00523     u3=0;
00524     u1 = getU8_FromItem(*item1, "Type");
00525     u2 = item1->getType();
00526     getData(*item1, 0, u3);
00527     CPPUNIT_ASSERT_EQUAL (u1, u2);
00528     CPPUNIT_ASSERT_EQUAL (u1, u3);
00529 
00530     u3=0;
00531     u1 = getU8_FromItem(*item1, "Length");
00532     u2 = item1->getLength();
00533     getData(*item1, 1, u3);
00534     CPPUNIT_ASSERT_EQUAL (u1, u2);
00535     CPPUNIT_ASSERT_EQUAL (u1, u3);
00536 
00537     v3=0;
00538     v1 = getU16_FromItem(*item1, "Handle");
00539     v2 = item1->getHandle();
00540     getData(*item1, 2, v3);
00541     CPPUNIT_ASSERT_EQUAL (v1, v2);
00542     CPPUNIT_ASSERT_EQUAL (v1, v3);
00543 
00544     str1 = getString_FromItem(*item1, "Vendor") ;
00545     str2 = getString_FromItem(*item1, 0x4 ) ;
00546     CPPUNIT_ASSERT_EQUAL (str1, str2);
00547 
00548     str1 = getString_FromItem(*item1, "BIOS Version") ;
00549     str2 = getString_FromItem(*item1, 0x5 ) ;
00550     CPPUNIT_ASSERT_EQUAL (str1, str2);
00551 
00552     v3=0;
00553     v1 = getU16_FromItem(*item1, "BIOS Starting Address Segment");
00554     v2 = getU16_FromItem(*item1, 0x6 );
00555     getData(*item1, 0x6, v3);
00556     CPPUNIT_ASSERT_EQUAL (v1, v2);
00557     CPPUNIT_ASSERT_EQUAL (v1, v3);
00558 
00559     str1 = getString_FromItem(*item1, "BIOS Release Date");
00560     str2 = getString_FromItem(*item1, 0x8 );
00561     CPPUNIT_ASSERT_EQUAL (str1, str2);
00562 
00563     int1 = getU8_FromItem(*item1, "BIOS ROM Size");
00564     int2 = getU8_FromItem(*item1, 0x9);
00565     CPPUNIT_ASSERT_EQUAL (int1, int2);
00566 
00567     ull1 = getU64_FromItem(*item1, "BIOS Characteristics");
00568     ull2 = getU64_FromItem(*item1, 0xA);
00569     // will not compile on VC++
00570     //    CPPUNIT_ASSERT_EQUAL (ull1, ull2);
00571 
00572     //First get some bits from the BIOS Characteristics bitfield and compare to U64
00573     u32 bitfield = 0;
00574     getBits_FromItem(*item1, 0xA, &bitfield, 0, 15);
00575     CPPUNIT_ASSERT_EQUAL (static_cast<u32>(ull2 & 0x000000000000FFFFL), bitfield);
00576 
00577     // cannot access bits > 64
00578     ASSERT_THROWS( getBits_FromItem(*item1, 0xA, &bitfield, 48, 96 ),  smbios::DataOutOfBounds );
00579 
00580     //Get some bits from the BIOS Characteristics bitfield using the other method
00581     //and compare it to the U64
00582     u32 tempval=0;
00583     bitfield  = 0;
00584 
00585     getBits_FromItem(*item1, "BIOS Characteristics", "Reserved0", &tempval);
00586     bitfield  = tempval;
00587 
00588     getBits_FromItem(*item1, "BIOS Characteristics", "Reserved1", &tempval);
00589     bitfield |= (tempval << 1);
00590 
00591     getBits_FromItem(*item1, "BIOS Characteristics", "Unknown", &tempval);
00592     bitfield |= (tempval << 2);
00593 
00594     getBits_FromItem(*item1, "BIOS Characteristics", "BIOS Characteristics Not Supported", &tempval);
00595     bitfield |= (tempval << 3);
00596 
00597     getBits_FromItem(*item1, "BIOS Characteristics", "ISA is supported", &tempval);
00598     bitfield |= (tempval << 4);
00599 
00600     getBits_FromItem(*item1, "BIOS Characteristics", "MCA is supported", &tempval);
00601     bitfield |= (tempval << 5);
00602 
00603     getBits_FromItem(*item1, "BIOS Characteristics", "EISA is supported", &tempval);
00604     bitfield |= (tempval << 6);
00605 
00606     getBits_FromItem(*item1, "BIOS Characteristics", "PCI is supported", &tempval);
00607     bitfield |= (tempval << 7);
00608 
00609     getBits_FromItem(*item1, "BIOS Characteristics", "PC Card (PCMCIA) is supported", &tempval);
00610     bitfield |= (tempval << 8);
00611 
00612     getBits_FromItem(*item1, "BIOS Characteristics", "Plug and Play is supported", &tempval);
00613     bitfield |= (tempval << 9);
00614 
00615     getBits_FromItem(*item1, "BIOS Characteristics", "APM is supported", &tempval);
00616     bitfield |= (tempval << 10);
00617 
00618     getBits_FromItem(*item1, "BIOS Characteristics", "BIOS is Upgradeable (Flash)", &tempval);
00619     bitfield |= (tempval << 11);
00620 
00621     getBits_FromItem(*item1, "BIOS Characteristics", "BIOS shadowing is allowed", &tempval);
00622     bitfield |= (tempval << 12);
00623 
00624     getBits_FromItem(*item1, "BIOS Characteristics", "VL-VESA is supported", &tempval);
00625     bitfield |= (tempval << 13);
00626 
00627     getBits_FromItem(*item1, "BIOS Characteristics", "ESCD support is available", &tempval);
00628     bitfield |= (tempval << 14);
00629 
00630     getBits_FromItem(*item1, "BIOS Characteristics", "Boot from CD is supported", &tempval);
00631     bitfield |= (tempval << 15);
00632 
00633     //cout << "BIOS Characteristics Bitfield (0x" << hex << bitfield << ")" << dec << endl;
00634     //Compare the lower 15 bits with the bitfield we just retrieved
00635     CPPUNIT_ASSERT_EQUAL (static_cast<u32>(ull2 & 0x000000000000FFFFL), bitfield);
00636 
00637     STD_TEST_END("");
00638 }
00639 
00640 void testStandalone::testItem_GetBiosInfo_builtinXml ()
00641 {
00642     resetFactoryToBuiltinXml();
00643     testItem_GetBiosInfo();
00644 }
00645 
00646 void testStandalone::testNonXml()
00647 {
00648     STD_TEST_START(getTestName().c_str() << "  " );
00649 
00650     smbios::SmbiosFactory *factory = smbios::SmbiosFactory::getFactory();
00651     factory->reset();
00652 
00653     //
00654     // This is now a NON-XML factory.
00655     // This call nails the _instance variable to a regular non-xml factory
00656     // instance. Subsequent setup calls in setUp ensure it is properly set
00657     // with the correct paths and whatnot.
00658     tearDown();
00659     factory = smbios::SmbiosFactory::getFactory();
00660     setUp();
00661 
00662     // Ok, none of these are related to construction offset, but it is good to
00663     // run these tests while we have a handy reference to a NON-XML factory.
00664     auto_ptr<smbios::ISmbiosTable>p(factory->makeNew());
00665     auto_ptr<const smbios::ISmbiosTable>q(factory->makeNew());
00666     ASSERT_THROWS( (*p)["BIOS Information"], smbios::NotImplemented );
00667     ASSERT_THROWS( (*q)["BIOS Information"], smbios::NotImplemented );
00668 
00669     smbios::ISmbiosTable::iterator item1 = (*p)[smbios::BIOS_Information];
00670     smbios::ISmbiosTable::const_iterator item2 = (*q)[smbios::BIOS_Information];
00671 
00672     // test streamify() for non-XML SmbiosItem class while we are here.
00673     ostringstream ost;
00674     ost << *item1 << endl;
00675 
00676     CPPUNIT_ASSERT_EQUAL( item1->getHandle(), item2->getHandle() );
00677     CPPUNIT_ASSERT_EQUAL( item1->getHandle(), getU16_FromItem(*item2, 2) );
00678     CPPUNIT_ASSERT_EQUAL( item1->getLength(), item2->getLength() );
00679     CPPUNIT_ASSERT_EQUAL( item1->getLength(), getU8_FromItem(*item2, 1) );
00680     CPPUNIT_ASSERT_EQUAL( item1->getType()  , item2->getType()   );
00681     CPPUNIT_ASSERT_EQUAL( item1->getType()  , getU8_FromItem(*item2, 0)   );
00682 
00683     ASSERT_THROWS( getU8_FromItem(*item1, "BIOS Version"), smbios::NotImplemented);
00684     ASSERT_THROWS( getU8_FromItem(*item1, "BIOS Version"), smbios::NotImplemented);
00685     ASSERT_THROWS( getU8_FromItem(*item1, "BIOS Version"), smbios::NotImplemented);
00686     ASSERT_THROWS( getU8_FromItem(*item1, "BIOS Version"), smbios::NotImplemented);
00687     ASSERT_THROWS( getString_FromItem(*item1, "BIOS Version"), smbios::NotImplemented);
00688 
00689     STD_TEST_END("");
00690 }
00691 
00692 void testStandalone::testItem_GetSystemInfo()
00693 {
00694     STD_TEST_START(getTestName().c_str() << "  " );
00695     // PURPOSE:
00696     //      Same purpose as testGet_BiosInfo()
00697 
00698     // do not delete. Factory manages lifetime.
00699     smbios::ISmbiosTable *table =
00700         smbios::SmbiosFactory::getFactory()->getSingleton();
00701 
00702     smbios::ISmbiosTable::iterator item1 = ( *table )["System Information"];
00703 
00704     //
00705     // here is an example XML for System Information Block, which we test below.
00706     //
00707     // <FIELD offset="0h" name="Type" length="BYTE" usage="STRUCTURE_TYPE"/>
00708     // <FIELD offset="1h" name="Length" length="BYTE" usage="SIZE"/>
00709     // <FIELD offset="2h" name="Handle" length="WORD" usage="HANDLE"/>
00710     // <FIELD offset="4h" name="Manufacturer" length="BYTE" usage="STRING"/>
00711     // <FIELD offset="5h" name="Product Name" length="BYTE" usage="STRING"/>
00712     // <FIELD offset="6h" name="Version" length="BYTE" usage="STRING"/>
00713     // <FIELD offset="7h" name="Serial Number" length="BYTE" usage="STRING"/>
00714     // <FIELD offset="8h" version="2.1" name="UUID" length="BYTE" count="16" usage="NUMBER"/>
00715     // <FIELD offset="18h" version="2.1" name="Wake-up Type" length="BYTE" usage="ENUM">
00716     //
00717     //
00718     string str1, str2;
00719     int int1, int2;
00720 
00721     int1 = getU8_FromItem(*item1, "Type");
00722     int2 = item1->getType();
00723     CPPUNIT_ASSERT_EQUAL (int1, int2);
00724 
00725     int1 = getU8_FromItem(*item1, "Length");
00726     int2 = item1->getLength();
00727     CPPUNIT_ASSERT_EQUAL (int1, int2);
00728 
00729     int1 = getU16_FromItem(*item1, "Handle");
00730     int2 = item1->getHandle();
00731     CPPUNIT_ASSERT_EQUAL (int1, int2);
00732 
00733     str1 = getString_FromItem(*item1, "Manufacturer") ;
00734     str2 = getString_FromItem(*item1, 0x4 ) ;
00735     CPPUNIT_ASSERT_EQUAL (str1, str2);
00736 
00737     str1 = getString_FromItem(*item1, "Product Name") ;
00738     str2 = getString_FromItem(*item1, 0x5 ) ;
00739     CPPUNIT_ASSERT_EQUAL (str1, str2);
00740 
00741 #if 0
00742     //
00743     // This is not a good test case because several
00744     // of our BIOSs have a '\0' in the header for this
00745     // string, which means this string does not
00746     // exist. Lowlevel code will throw an exception.
00747     //
00748     str1 = getString_FromItem(*item1, "Version") ;
00749     str2 = getString_FromItem(*item1, 0x6 ) ;
00750     CPPUNIT_ASSERT_EQUAL( str1, str2 );
00751 #endif
00752 
00753     try
00754     {
00755         str1 = getString_FromItem(*item1, "Serial Number") ;
00756         str2 = getString_FromItem(*item1, 0x7 ) ;
00757         CPPUNIT_ASSERT_EQUAL (str1, str2);
00758     }
00759     catch( const exception & )
00760     {
00761         // 4G systems do not support Serial Number.
00762     }
00763 
00764 #if 0
00765     //
00766     // These are not good test cases because they are SMBIOS 2.3
00767     // additions and are not guaranteed to be present.
00768     //
00769     u8 val1, val2;
00770     val1 =  item1->getU8("UUID") ;
00771     val2 = item1->getU8( 0x8 ) ;
00772     CPPUNIT_ASSERT_EQUAL( val1, val2 );
00773 
00774     val1 = item1->getU8("Wake-up Type") ;
00775     val2 = item1->getU8( 0x9 ) ;
00776     CPPUNIT_ASSERT_EQUAL( val1, val2 );
00777 #endif
00778 
00779     STD_TEST_END("");
00780 }
00781 
00782 void testStandalone::testItem_GetSystemInfo_builtinXml ()
00783 {
00784     resetFactoryToBuiltinXml();
00785     testItem_GetSystemInfo();
00786 }
00787 
00788 // helper because this isn't external API
00789 extern const string getStringForType(const XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc, const int searchForType );
00790 #include "SmbiosXmlImpl.h"
00791 
00792 void testStandalone::testSmbiosXml()
00793 {
00794     STD_TEST_START(getTestName().c_str() << "  " );
00795     // do not delete. Factory manages lifetime.
00796     smbios::ISmbiosTable *table =
00797             smbios::SmbiosFactory::getFactory()->getSingleton();
00798     
00799     // test reverse name mapping function
00800     string bios = dynamic_cast<smbios::SmbiosTableXml *>(table)->getStringForType(0);
00801     string expected = "BIOS Information";
00802     CPPUNIT_ASSERT_EQUAL ( bios, expected );
00803 
00804     // test if "PCI Supported" bit is set, should always be set.
00805     smbios::ISmbiosTable::iterator item = (*table)["BIOS Information"];
00806     CPPUNIT_ASSERT_EQUAL( isBitSet( &(*item), 0xA,  0x7 ), true );
00807     
00808     STD_TEST_END("");
00809 }
00810 
00811 void testStandalone::testTypeMismatch()
00812 {
00813     STD_TEST_START(getTestName().c_str() << "  " );
00814     // PURPOSE:
00815     //      The purpose of this test is to test all types of invalid item
00816     //      access. The getXX(string) methods all validate that the field
00817     //      passed to them is actually of type XX. If a mismatch is found, an
00818     //      exception is thrown.
00819     //
00820     //      Each test validates that an exception is thrown and the type.
00821 
00822     // do not delete. Factory manages lifetime.
00823     smbios::ISmbiosTable *table =
00824         smbios::SmbiosFactory::getFactory()->getSingleton();
00825 
00826     smbios::ISmbiosTable::iterator item1 = (*table)["BIOS Information"];
00827 
00828     //
00829     // refer to testGet_BiosInfo() for XML sample for BIOS INFORMATION BLOCK
00830     //
00831 
00832     // ASSERT_THROWS is not a CPPUNIT macro, it is our custom macro.
00833     ASSERT_THROWS( getU8_FromItem (*item1, "Handle"), smbios::ParseException );
00834     ASSERT_THROWS( getU16_FromItem(*item1, "Type"), smbios::ParseException );
00835     ASSERT_THROWS( getU32_FromItem(*item1, "Type"), smbios::ParseException );
00836     ASSERT_THROWS( getU64_FromItem(*item1, "Type"), smbios::ParseException );
00837     ASSERT_THROWS( getString_FromItem(*item1, "Type"), smbios::ParseException );
00838     ASSERT_THROWS( getBits_FromItem(*item1, "Type", "foo", NULL), smbios::ParseException );
00839 
00840     STD_TEST_END("");
00841 }
00842 
00843 void testStandalone::testTypeMismatch_builtinXml ()
00844 {
00845     resetFactoryToBuiltinXml();
00846     testStandalone::testTypeMismatch();
00847 }
00848 
00849 
00850 void
00851 testStandalone::testGetBoundaries()
00852 {
00853     STD_TEST_START(getTestName().c_str() << "  " );
00854 
00855     // do not delete. Factory manages lifetime.
00856     smbios::ISmbiosTable *table =
00857         smbios::SmbiosFactory::getFactory()->getSingleton();
00858 
00859     for( smbios::ISmbiosTable::iterator item = (*table)[-1] ; item != table->end(); ++item)
00860     {
00861         int len = item->getLength();
00862 
00863         // none of these should throw.
00864         (void) getU8_FromItem (*item, len - 1);
00865         (void) getU16_FromItem(*item, len - 2);
00866         (void) getU32_FromItem(*item, len - 4);
00867 
00868         // not all items are large enough. only attempt if item is at least 8 bytes
00869         if( len >= 8 )
00870             (void) getU64_FromItem(*item, len - 8);
00871 
00872         ASSERT_THROWS( getU8_FromItem (*item, len - 0), smbios::DataOutOfBounds);
00873         ASSERT_THROWS( getU16_FromItem(*item, len - 1), smbios::DataOutOfBounds);
00874         ASSERT_THROWS( getU32_FromItem(*item, len - 3), smbios::DataOutOfBounds);
00875         ASSERT_THROWS( getU64_FromItem(*item, len - 7), smbios::DataOutOfBounds);
00876     }
00877 
00878     STD_TEST_END("");
00879 }
00880 
00881 void testStandalone::testGetBoundaries_builtinXml ()
00882 {
00883     resetFactoryToBuiltinXml();
00884     testStandalone::testGetBoundaries();
00885 }
00886 
00887 
00888 //
00889 // Memory tests
00890 //
00891 void
00892 testStandalone::testMemoryBadFiles ()
00893 {
00894     STD_TEST_START(getTestName().c_str() << "  " );
00895 
00896     memory::MemoryFactory *memFact = memory::MemoryFactory::getFactory();
00897 
00898     memFact->setParameter("memFile", "");
00899     ASSERT_THROWS( memFact->makeNew() , memory::AccessError);
00900 
00901     memFact->setParameter("memFile", "nonexistentfile");
00902     ASSERT_THROWS( memFact->makeNew() , memory::AccessError);
00903 
00904     memory::MemoryFactory::getFactory()->setMode( 2 );
00905     ASSERT_THROWS( memFact->makeNew(), smbios::NotImplemented);
00906 
00907     memory::MemoryFactory::getFactory()->setMode( memory::MemoryFactory::UnitTestMode );
00908 
00909     STD_TEST_END("");
00910 }
00911 
00912 void
00913 testStandalone::testMemoryFuncs ()
00914 {
00915     STD_TEST_START(getTestName().c_str() << "  " );
00916 
00917     memory::MemoryFactory *memFact = memory::MemoryFactory::getFactory();
00918 
00919     memory::IMemory *mem =  memFact->getSingleton();
00920 
00921     mem->getByte( 0 );
00922 
00923     ASSERT_THROWS( mem->getByte( 0x10000000UL ), memory::AccessError);
00924 
00925     mem->putByte( 0, 254 );
00926     int b = mem->getByte( 0 );
00927 
00928     CPPUNIT_ASSERT_EQUAL ( b, 254 );
00929     STD_TEST_END("");
00930 }
00931 
00932 
00933 
00934 
00935 //
00936 // CMOS Token
00937 //
00938 
00939 
00940 void
00941 testStandalone::testCmosConstructor ()
00942 {
00943     STD_TEST_START(getTestName().c_str() << "  " );
00944 
00945     smbios::TokenTableFactory *ttFactory;
00946     ttFactory = smbios::TokenTableFactory::getFactory() ;
00947     smbios::ITokenTable *tokenTable = ttFactory->getSingleton();
00948 
00949     ostringstream ost;
00950 
00951     smbios::ITokenTable::iterator token = tokenTable->begin();
00952     while( token != tokenTable->end() )
00953     {
00954         ost << *token << endl;
00955         ++token;
00956     }
00957 
00958     STD_TEST_END("");
00959 }
00960 
00961 
00962 //
00963 // SMI Tests
00964 //
00965 
00966 void
00967 testStandalone::testSmi_callingInterface()
00968 {
00969     STD_TEST_START(getTestName().c_str() << "  " );
00970 
00971     std::auto_ptr<smi::ISmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI_RAW);
00972     smi->setCommandIOMagic( 0x1234, 0x56 );
00973 
00974     smi::IDellCallingInterfaceSmi *ci = dynamic_cast<smi::IDellCallingInterfaceSmi *>(&*smi);
00975     ci->setClass( 0xAABB );
00976     ci->setSelect( 0xCCDD );
00977     ci->setArg( 0, 0xA1A2A3A4 );
00978     ci->setArg( 1, 0xB1B2B3B4 );
00979     ci->setArg( 2, 0xC1C2C3C4 );
00980     ci->setArg( 3, 0xD1D2D3D4 );
00981     try
00982     {   /* This is expected to fail in unit test, no good way to simulate them*/
00983         ci->execute();
00984     }
00985     catch( const smi::UnhandledSmi & ) {}
00986 
00987     STD_TEST_END("");
00988 }
00989 
00990 void
00991 testStandalone::testSmi_callingInterface_physaddr ()
00992 {
00993     STD_TEST_START(getTestName().c_str() << "  " );
00994 
00995     std::auto_ptr<smi::ISmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI_RAW);
00996     smi->setCommandIOMagic( 0x1234, 0x56 );
00997 
00998     smi::IDellCallingInterfaceSmi *ci = dynamic_cast<smi::IDellCallingInterfaceSmi *>(&*smi);
00999     ci->setClass( 0xAABB );
01000     ci->setSelect( 0xCCDD );
01001     ci->setArgAsPhysicalAddress(0, 0);
01002     ci->setArgAsPhysicalAddress(1, 1);
01003     ci->setArgAsPhysicalAddress(2, 2);
01004     ci->setArgAsPhysicalAddress(3, 3);
01005     try
01006     {   /* This is expected to fail in unit test, no good way to simulate them*/
01007         ci->execute();
01008     }
01009     catch( const smi::UnhandledSmi & ) {}
01010 
01011     STD_TEST_END("");
01012 }
01013 
01014 
01015 
01016 string safeConvertToString( const char *str )
01017 {
01018     string fromSystem = "";
01019     if( 0 != str )
01020     {
01021         fromSystem = str;
01022     }
01023     SMBIOSFreeMemory(str);
01024     return fromSystem;
01025 }
01026 
01027 void
01028 testStandalone::testLibraryVersion()
01029 {
01030     STD_TEST_START(getTestName().c_str() << "  " );
01031 
01032     string fromSystem = SMBIOSGetLibraryVersionString();
01033     string testInput  = LIBSMBIOS_RELEASE_VERSION;
01034 
01035     CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
01036     STD_TEST_END("");
01037 }
01038 
01039 void
01040 testStandalone::testException()
01041 {
01042     STD_TEST_START(getTestName().c_str() << "  " );
01043     std::string actual = "";
01044     smbios::Exception<smbios::IException> foo;
01045     string source = "";
01046     string expected = "";
01047     foo.setParameter("foo", "happy");
01048     foo.setParameter("bar", 42);
01049     foo.setParameter("recursive", "%(foo)s");
01050     foo.setParameter("recursiverecursive", "%(recursive)s");
01051 
01052     source = "The %% cat is %(foo)s. The best number is %(bar)i. %";
01053     expected = "The % cat is happy. The best number is 42. %";
01054     foo.setMessageString( source );
01055     actual = foo.what();
01056     CPPUNIT_ASSERT_EQUAL( expected, actual );
01057 
01058     // test copy constructor
01059     smbios::Exception<smbios::IException> bar = foo;
01060     actual = bar.what();
01061     CPPUNIT_ASSERT_EQUAL( expected, actual );
01062 
01063     source = "The %% cat is %(recursive)s. The best number is %(bar)i. %";
01064     foo.setMessageString( source );
01065     actual = foo.what();
01066     CPPUNIT_ASSERT_EQUAL( expected, actual );
01067 
01068     source = "The %% cat is %(recursiverecursive)s. The best number is %(bar)i. %";
01069     foo.setMessageString( source );
01070     actual = foo.what();
01071     CPPUNIT_ASSERT_EQUAL( expected, actual );
01072 
01073     source = "The %% cat %is %(recursiverecursive)s. The best number is %(bar)i. %";
01074     expected = "The % cat %is happy. The best number is 42. %";
01075     foo.setMessageString( source );
01076     actual = foo.what();
01077     CPPUNIT_ASSERT_EQUAL( expected, actual );
01078 
01079     source = " %(a_really_long_variable_longer_than_32_characters)s";
01080     expected = " %(a_really_long_variable_longer_than_32_characters)s";
01081     foo.setMessageString( source );
01082     actual = foo.what();
01083     CPPUNIT_ASSERT_EQUAL( expected, actual );
01084 
01085     source = " %(no_closing_paren";
01086     expected = " %(no_closing_paren";
01087     foo.setMessageString( source );
01088     actual = foo.what();
01089     CPPUNIT_ASSERT_EQUAL( expected, actual );
01090 
01091     source = " %(a_var_with_no_type)";
01092     expected = " %(a_var_with_no_type)";
01093     foo.setMessageString( source );
01094     actual = foo.what();
01095     CPPUNIT_ASSERT_EQUAL( expected, actual );
01096 
01097     source = " %(a_var_with_no_type)  ";
01098     expected = " %(a_var_with_no_type)  ";
01099     foo.setMessageString( source );
01100     actual = foo.what();
01101     CPPUNIT_ASSERT_EQUAL( expected, actual );
01102 
01103     source = " %";
01104     expected = " %";
01105     foo.setMessageString( source );
01106     actual = foo.what();
01107     CPPUNIT_ASSERT_EQUAL( expected, actual );
01108 
01109     STD_TEST_END("");
01110 }
01111 

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