00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "smbios/compat.h"
00021
00022 #include <iomanip>
00023 #include <fstream>
00024
00025 #include "testStandalone.h"
00026 #include "smbios/SmbiosDefs.h"
00027
00028
00029
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
00039
00040
00041
00042
00043
00044
00045
00046
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
00076
00077
00078 string memdumpOrigFile = programDirname + getTestDirectory() + "/memdump.dat";
00079 if(!fileExists(memdumpOrigFile))
00080 memdumpOrigFile = getTestDirectory() + "/memdump.dat";
00081 string memdumpCopyFile = writeDirectory + "/memdump-copy.dat";
00082 copyFile( memdumpCopyFile, memdumpOrigFile );
00083
00084
00085
00086 string cmosOrigFile = programDirname + getTestDirectory() + "/cmos.dat";
00087 if(!fileExists(cmosOrigFile))
00088 cmosOrigFile = getTestDirectory() + "/cmos.dat";
00089 string cmosCopyFile = writeDirectory + "/cmos-copy.dat";
00090 copyFile( cmosCopyFile, cmosOrigFile );
00091
00092
00093 string smiOutput = writeDirectory + "/smi-output.dat";
00094
00095
00096
00097
00098
00099
00100 smbios::SmbiosFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
00101 smbios::SmbiosFactory::getFactory()->setParameter("offset", 0);
00102 smbios::SmbiosFactory::getFactory()->setMode(smbios::SmbiosFactory::UnitTestMode);
00103
00104 cmos:: CmosRWFactory::getFactory()->setParameter("cmosMapFile", cmosCopyFile);
00105 cmos:: CmosRWFactory::getFactory()->setMode( factory::IFactory::UnitTestMode );
00106
00107 memory::MemoryFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
00108 memory::MemoryFactory::getFactory()->setMode( memory::MemoryFactory::UnitTestMode );
00109
00110 smi::SmiFactory::getFactory()->setParameter("smiFile", smiOutput);
00111 smi::SmiFactory::getFactory()->setMode( smi::SmiFactory::UnitTestMode );
00112 }
00113
00114 void testStandalone::tearDown()
00115 {
00116
00117
00118
00119
00120 smbios::TokenTableFactory::getFactory()->reset();
00121
00122 smbios::SmbiosFactory::getFactory()->reset();
00123
00124 memory::MemoryFactory::getFactory()->reset();
00125
00126 cmos::CmosRWFactory::getFactory()->reset();
00127
00128 smi::SmiFactory::getFactory()->reset();
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 void testStandalone::testSmbiosTableBase()
00138 {}
00139
00140 void testStandalone::testSmbiosTableBase_iterNextItem()
00141 {}
00142
00143 void testStandalone::testSmbiosTableBase_findItemByHandle()
00144 {}
00145
00146 void testStandalone::testSmbiosTableBase_findItemByType()
00147 {}
00148
00149
00150 void testStandalone::testTable_Subscript()
00151 {
00152 STD_TEST_START(getTestName().c_str() << " " );
00153
00154
00155
00156
00157
00158
00159
00160 smbios::ISmbiosTable *table =
00161 smbios::SmbiosFactory::getFactory()->getSingleton();
00162
00163 smbios::ISmbiosTable::iterator item1;
00164 smbios::ISmbiosTable::const_iterator item2;
00165
00166 item1 = (*table)[smbios::BIOS_Information];
00167 item2 = (*table)[smbios::BIOS_Information];
00168
00169
00170
00171 CPPUNIT_ASSERT_EQUAL( item1->getHandle(), item2->getHandle() );
00172 CPPUNIT_ASSERT_EQUAL( getItemHandle(*item1), item2->getHandle() );
00173 CPPUNIT_ASSERT_EQUAL( item1->getLength(), item2->getLength() );
00174 CPPUNIT_ASSERT_EQUAL( getItemLength(*item1), item2->getLength() );
00175 CPPUNIT_ASSERT_EQUAL( item1->getType() , item2->getType() );
00176 CPPUNIT_ASSERT_EQUAL( getItemType(*item1) , item2->getType() );
00177
00178 CPPUNIT_ASSERT_EQUAL( (*table)[smbios::BIOS_Information]->getType(), item1->getType() );
00179
00180 item1 = (*table)[smbios::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
00192
00193 STD_TEST_END("");
00194 }
00195
00196 void
00197 testStandalone::testEntryCount ()
00198 {
00199 STD_TEST_START(getTestName().c_str() << " " );
00200
00201
00202 smbios::ISmbiosTable *table =
00203 smbios::SmbiosFactory::getFactory()->getSingleton();
00204
00205
00206 ostringstream ost;
00207 int tableEntriesCounted = 0;
00208 smbios::ISmbiosTable::iterator item = table->begin();
00209 while( item != table->end() )
00210 {
00211 tableEntriesCounted++;
00212 ost << *item << endl;
00213 ++item;
00214 }
00215
00216 CPPUNIT_ASSERT_EQUAL( tableEntriesCounted, table->getNumberOfEntries() );
00217 STD_TEST_END("");
00218 }
00219
00220 void
00221 testStandalone::testConstIterator ()
00222 {
00223 STD_TEST_START(getTestName().c_str() << " " );
00224
00225
00226 const smbios::ISmbiosTable *table =
00227 smbios::SmbiosFactory::getFactory()->getSingleton();
00228
00229 const smbios::ISmbiosTable *constTable = &*table;
00230
00231 int tableEntriesCounted = 0;
00232 smbios::ISmbiosTable::const_iterator item = constTable->begin();
00233 while( item != constTable->end() )
00234 {
00235 tableEntriesCounted++;
00236 (void) *item;
00237
00238 ++item;
00239 }
00240 CPPUNIT_ASSERT_EQUAL( tableEntriesCounted, constTable->getNumberOfEntries() );
00241 STD_TEST_END("");
00242 }
00243
00244 void
00245 testStandalone::testSubscriptOperator1 ()
00246 {
00247 STD_TEST_START(getTestName().c_str() << " " );
00248
00249
00250 const smbios::ISmbiosTable *table =
00251 smbios::SmbiosFactory::getFactory()->getSingleton();
00252
00253 int tableEntriesCounted = 0;
00254
00255
00256 for( smbios::ISmbiosTable::const_iterator item = (*table)[-1] ; item != table->end(); ++item)
00257 {
00258 tableEntriesCounted++;
00259 (void) *item;
00260 }
00261 CPPUNIT_ASSERT_EQUAL( table->getNumberOfEntries(), tableEntriesCounted );
00262 STD_TEST_END("");
00263 }
00264
00265 void
00266 testStandalone::testSubscriptOperator2 ()
00267 {
00268 STD_TEST_START(getTestName().c_str() << " " );
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 STD_TEST_END("");
00288 }
00289
00290 void
00291 testStandalone::testSubscriptOperator3 ()
00292 {
00293 STD_TEST_START(getTestName().c_str() << " " );
00294
00295
00296 const smbios::ISmbiosTable *table =
00297 smbios::SmbiosFactory::getFactory()->getSingleton();
00298
00299
00300
00301
00302
00303
00304
00305
00306 int tableEntriesCounted = 0;
00307 for( smbios::ISmbiosTable::const_iterator item = (*table)[8] ; item != table->end(); ++item)
00308 {
00309 (void) *item;
00310 tableEntriesCounted++;
00311 }
00312 CPPUNIT_ASSERT( 1 < tableEntriesCounted );
00313 STD_TEST_END("");
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 void testStandalone::testStreamify()
00325 {
00326 STD_TEST_START(getTestName().c_str() << " " );
00327
00328
00329
00330
00331 smbios::ISmbiosTable *table =
00332 smbios::SmbiosFactory::getFactory()->getSingleton();
00333
00334 ostringstream ost;
00335 ost << *table << endl;
00336
00337
00338 for( smbios::ISmbiosTable::iterator item = (*table)[8] ; item != table->end(); ++item)
00339 {
00340 ost << *item << endl;
00341 }
00342
00343
00344 STD_TEST_END("");
00345 }
00346
00347 void
00348 testStandalone::testItemIdentity ()
00349 {
00350 STD_TEST_START(getTestName().c_str() << " " );
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 const smbios::ISmbiosTable *table =
00361 smbios::SmbiosFactory::getFactory()->getSingleton();
00362
00363
00364 smbios::ISmbiosTable::const_iterator position1 = (*table)[smbios::BIOS_Information];
00365 const smbios::ISmbiosItem &item1 = *position1;
00366
00367
00368 smbios::ISmbiosTable::const_iterator position2 = (*table)[smbios::BIOS_Information];
00369 const smbios::ISmbiosItem &item2 = *position2;
00370
00371
00372
00373 CPPUNIT_ASSERT_EQUAL( &item1, &item2 );
00374 CPPUNIT_ASSERT_EQUAL( &(*position1), &item2 );
00375 CPPUNIT_ASSERT_EQUAL( &item1 , &(*position2) );
00376 CPPUNIT_ASSERT_EQUAL( &(*position1), &(*position2) );
00377
00378
00379 smbios::ISmbiosTable::const_iterator position3 = (*table)[smbios::System_Information];
00380 const smbios::ISmbiosItem &item3 = *position3;
00381
00382
00383 CPPUNIT_ASSERT( &item1 != &item3 );
00384 CPPUNIT_ASSERT( &item2 != &item3 );
00385
00386 CPPUNIT_ASSERT_EQUAL( item1.getType(), static_cast<u8>(smbios::BIOS_Information) );
00387 CPPUNIT_ASSERT_EQUAL( item2.getType(), static_cast<u8>(smbios::BIOS_Information) );
00388 CPPUNIT_ASSERT_EQUAL( item3.getType(), static_cast<u8>(smbios::System_Information) );
00389
00390 STD_TEST_END("");
00391 }
00392
00393 void
00394 testStandalone::testEachItemAccessors ()
00395 {
00396 STD_TEST_START(getTestName().c_str() << " " );
00397
00398
00399
00400
00401
00402
00403
00404 const smbios::ISmbiosTable *table =
00405 smbios::SmbiosFactory::getFactory()->getSingleton();
00406
00407 smbios::ISmbiosTable::const_iterator item = table->begin();
00408 while( item != table->end() )
00409 {
00410 u8 type1 = item->getType();
00411 u8 type2 = getU8_FromItem(*item, 0);
00412 CPPUNIT_ASSERT_EQUAL( type1, type2 );
00413
00414 u8 len1 = item->getLength();
00415 u8 len2 = getU8_FromItem(*item, 1);
00416 CPPUNIT_ASSERT_EQUAL( len1, len2 );
00417
00418 u16 handle1 = item->getHandle();
00419 u16 handle2 = getU16_FromItem(*item, 2);
00420 CPPUNIT_ASSERT_EQUAL( handle1, handle2 );
00421
00422 ++item;
00423 }
00424
00425 STD_TEST_END("");
00426 }
00427
00428 void testStandalone::testNonXml()
00429 {
00430 STD_TEST_START(getTestName().c_str() << " " );
00431
00432 smbios::SmbiosFactory *factory = smbios::SmbiosFactory::getFactory();
00433 factory->reset();
00434
00435
00436
00437
00438
00439
00440 tearDown();
00441 factory = smbios::SmbiosFactory::getFactory();
00442 setUp();
00443
00444
00445
00446 auto_ptr<smbios::ISmbiosTable>p(factory->makeNew());
00447 auto_ptr<const smbios::ISmbiosTable>q(factory->makeNew());
00448
00449 smbios::ISmbiosTable::iterator item1 = (*p)[smbios::BIOS_Information];
00450 smbios::ISmbiosTable::const_iterator item2 = (*q)[smbios::BIOS_Information];
00451
00452
00453 ostringstream ost;
00454 ost << *item1 << endl;
00455
00456 CPPUNIT_ASSERT_EQUAL( item1->getHandle(), item2->getHandle() );
00457 CPPUNIT_ASSERT_EQUAL( item1->getHandle(), getU16_FromItem(*item2, 2) );
00458 CPPUNIT_ASSERT_EQUAL( item1->getLength(), item2->getLength() );
00459 CPPUNIT_ASSERT_EQUAL( item1->getLength(), getU8_FromItem(*item2, 1) );
00460 CPPUNIT_ASSERT_EQUAL( item1->getType() , item2->getType() );
00461 CPPUNIT_ASSERT_EQUAL( item1->getType() , getU8_FromItem(*item2, 0) );
00462
00463 STD_TEST_END("");
00464 }
00465
00466 void testStandalone::testSmbiosXml()
00467 {
00468 STD_TEST_START(getTestName().c_str() << " " );
00469
00470 const smbios::ISmbiosTable *table =
00471 smbios::SmbiosFactory::getFactory()->getSingleton();
00472
00473
00474 smbios::ISmbiosTable::const_iterator item = (*table)[smbios::BIOS_Information];
00475 CPPUNIT_ASSERT_EQUAL( isBitSet( &(*item), 0xA, 0x7 ), true );
00476
00477 STD_TEST_END("");
00478 }
00479
00480
00481
00482 void
00483 testStandalone::testGetBoundaries()
00484 {
00485 STD_TEST_START(getTestName().c_str() << " " );
00486
00487
00488 const smbios::ISmbiosTable *table =
00489 smbios::SmbiosFactory::getFactory()->getSingleton();
00490
00491 for( smbios::ISmbiosTable::const_iterator item = (*table)[-1] ; item != table->end(); ++item)
00492 {
00493 int len = item->getLength();
00494
00495
00496 (void) getU8_FromItem (*item, len - 1);
00497 (void) getU16_FromItem(*item, len - 2);
00498 (void) getU32_FromItem(*item, len - 4);
00499
00500
00501 if( len >= 8 )
00502 (void) getU64_FromItem(*item, len - 8);
00503
00504 ASSERT_THROWS( getU8_FromItem (*item, len - 0), smbios::DataOutOfBounds);
00505 ASSERT_THROWS( getU16_FromItem(*item, len - 1), smbios::DataOutOfBounds);
00506 ASSERT_THROWS( getU32_FromItem(*item, len - 3), smbios::DataOutOfBounds);
00507 ASSERT_THROWS( getU64_FromItem(*item, len - 7), smbios::DataOutOfBounds);
00508 }
00509
00510 STD_TEST_END("");
00511 }
00512
00513
00514
00515
00516
00517
00518 void
00519 testStandalone::testMemoryBadFiles ()
00520 {
00521 STD_TEST_START(getTestName().c_str() << " " );
00522
00523 memory::MemoryFactory *memFact = memory::MemoryFactory::getFactory();
00524
00525 memFact->setParameter("memFile", "");
00526 ASSERT_THROWS( memFact->makeNew() , memory::AccessError);
00527
00528 memFact->setParameter("memFile", "nonexistentfile");
00529 ASSERT_THROWS( memFact->makeNew() , memory::AccessError);
00530
00531 memory::MemoryFactory::getFactory()->setMode( 2 );
00532 ASSERT_THROWS( memFact->makeNew(), smbios::NotImplemented);
00533
00534 memory::MemoryFactory::getFactory()->setMode( memory::MemoryFactory::UnitTestMode );
00535
00536 STD_TEST_END("");
00537 }
00538
00539 void
00540 testStandalone::testMemoryFuncs ()
00541 {
00542 STD_TEST_START(getTestName().c_str() << " " );
00543
00544 memory::MemoryFactory *memFact = memory::MemoryFactory::getFactory();
00545
00546 memory::IMemory *mem = memFact->getSingleton();
00547
00548 mem->getByte( 0 );
00549
00550 ASSERT_THROWS( mem->getByte( 0x10000000UL ), memory::AccessError);
00551
00552 mem->putByte( 0, 254 );
00553 int b = mem->getByte( 0 );
00554
00555 CPPUNIT_ASSERT_EQUAL ( b, 254 );
00556 STD_TEST_END("");
00557 }
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 void
00568 testStandalone::testCmosConstructor ()
00569 {
00570 STD_TEST_START(getTestName().c_str() << " " );
00571
00572 smbios::TokenTableFactory *ttFactory;
00573 ttFactory = smbios::TokenTableFactory::getFactory() ;
00574 smbios::ITokenTable *tokenTable = ttFactory->getSingleton();
00575
00576 ostringstream ost;
00577
00578 smbios::ITokenTable::iterator token = tokenTable->begin();
00579 while( token != tokenTable->end() )
00580 {
00581 ost << *token << endl;
00582 ++token;
00583 }
00584
00585 STD_TEST_END("");
00586 }
00587
00588
00589
00590
00591
00592
00593 void
00594 testStandalone::testSmi_callingInterface()
00595 {
00596 STD_TEST_START(getTestName().c_str() << " " );
00597
00598 std::auto_ptr<smi::IDellCallingInterfaceSmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI);
00599
00600 smi->setClass( 0xAABB );
00601 smi->setSelect( 0xCCDD );
00602 smi->setArg( 0, 0xA1A2A3A4 );
00603 smi->setArg( 1, 0xB1B2B3B4 );
00604 smi->setArg( 2, 0xC1C2C3C4 );
00605 smi->setArg( 3, 0xD1D2D3D4 );
00606 try
00607 {
00608 smi->execute();
00609 }
00610 catch( const smi::UnhandledSmi & ) {}
00611
00612 STD_TEST_END("");
00613 }
00614
00615 void
00616 testStandalone::testSmi_callingInterface_physaddr ()
00617 {
00618 STD_TEST_START(getTestName().c_str() << " " );
00619
00620 std::auto_ptr<smi::IDellCallingInterfaceSmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI);
00621 smi->setClass( 0xAABB );
00622 smi->setSelect( 0xCCDD );
00623 smi->setArgAsPhysicalAddress(0, 0);
00624 smi->setArgAsPhysicalAddress(1, 1);
00625 smi->setArgAsPhysicalAddress(2, 2);
00626 smi->setArgAsPhysicalAddress(3, 3);
00627 try
00628 {
00629 smi->execute();
00630 }
00631 catch( const smi::UnhandledSmi & ) {}
00632
00633 STD_TEST_END("");
00634 }
00635
00636
00637
00638 string safeConvertToString( const char *str )
00639 {
00640 string fromSystem = "";
00641 if( 0 != str )
00642 {
00643 fromSystem = str;
00644 }
00645 SMBIOSFreeMemory(str);
00646 return fromSystem;
00647 }
00648
00649 void
00650 testStandalone::testLibraryVersion()
00651 {
00652 STD_TEST_START(getTestName().c_str() << " " );
00653
00654 string fromSystem = SMBIOSGetLibraryVersionString();
00655 string testInput = LIBSMBIOS_RELEASE_VERSION;
00656
00657 CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
00658 STD_TEST_END("");
00659 }
00660
00661 void
00662 testStandalone::testException()
00663 {
00664 STD_TEST_START(getTestName().c_str() << " " );
00665 std::string actual = "";
00666 smbios::Exception<smbios::IException> foo;
00667 string source = "";
00668 string expected = "";
00669 foo.setParameter("foo", "happy");
00670 foo.setParameter("bar", 42);
00671 foo.setParameter("recursive", "%(foo)s");
00672 foo.setParameter("recursiverecursive", "%(recursive)s");
00673
00674 source = "The %% cat is %(foo)s. The best number is %(bar)i. %";
00675 expected = "The % cat is happy. The best number is 42. %";
00676 foo.setMessageString( source );
00677 actual = foo.what();
00678 CPPUNIT_ASSERT_EQUAL( expected, actual );
00679
00680
00681 smbios::Exception<smbios::IException> bar = foo;
00682 actual = bar.what();
00683 CPPUNIT_ASSERT_EQUAL( expected, actual );
00684
00685 source = "The %% cat is %(recursive)s. The best number is %(bar)i. %";
00686 foo.setMessageString( source );
00687 actual = foo.what();
00688 CPPUNIT_ASSERT_EQUAL( expected, actual );
00689
00690 source = "The %% cat is %(recursiverecursive)s. The best number is %(bar)i. %";
00691 foo.setMessageString( source );
00692 actual = foo.what();
00693 CPPUNIT_ASSERT_EQUAL( expected, actual );
00694
00695 source = "The %% cat %is %(recursiverecursive)s. The best number is %(bar)i. %";
00696 expected = "The % cat %is happy. The best number is 42. %";
00697 foo.setMessageString( source );
00698 actual = foo.what();
00699 CPPUNIT_ASSERT_EQUAL( expected, actual );
00700
00701 source = " %(a_really_long_variable_longer_than_32_characters)s";
00702 expected = " %(a_really_long_variable_longer_than_32_characters)s";
00703 foo.setMessageString( source );
00704 actual = foo.what();
00705 CPPUNIT_ASSERT_EQUAL( expected, actual );
00706
00707 source = " %(no_closing_paren";
00708 expected = " %(no_closing_paren";
00709 foo.setMessageString( source );
00710 actual = foo.what();
00711 CPPUNIT_ASSERT_EQUAL( expected, actual );
00712
00713 source = " %(a_var_with_no_type)";
00714 expected = " %(a_var_with_no_type)";
00715 foo.setMessageString( source );
00716 actual = foo.what();
00717 CPPUNIT_ASSERT_EQUAL( expected, actual );
00718
00719 source = " %(a_var_with_no_type) ";
00720 expected = " %(a_var_with_no_type) ";
00721 foo.setMessageString( source );
00722 actual = foo.what();
00723 CPPUNIT_ASSERT_EQUAL( expected, actual );
00724
00725 source = " %";
00726 expected = " %";
00727 foo.setMessageString( source );
00728 actual = foo.what();
00729 CPPUNIT_ASSERT_EQUAL( expected, actual );
00730
00731 STD_TEST_END("");
00732 }
00733