testRbu.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 <fstream>
00023 #include <cctype>
00024 
00025 #include "testRbu.h"
00026 
00027 // specific to unit tests. Users do not need to include this,
00028 // so it is not in testPlatform.h
00029 #include "smbios/IMemory.h"
00030 #include "smbios/ISmi.h"
00031 #include "smbios/ISmbios.h"
00032 #include "smbios/IToken.h"
00033 
00034 using namespace std;
00035 
00036 // Note:
00037 //      Except for , there are no "using namespace XXXX;" statements
00038 //      here... on purpose. We want to ensure that while reading this code that
00039 //      it is extremely obvious where each function is coming from.
00040 //
00041 //      This leads to verbose code in some instances, but that is fine for
00042 //      these purposes.
00043 
00044 // Register the test
00045 CPPUNIT_TEST_SUITE_REGISTRATION (testRbu);
00046 
00047 void copyFile( string dstFile, string srcFile )
00048 {
00049     ifstream src(srcFile.c_str(), ios_base::binary);
00050     ofstream dst(dstFile.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);
00051 
00052     char ch;
00053     while( src.get(ch)) dst.put(ch);
00054 
00055     if( !src.eof() || !dst ) throw exception();
00056 }
00057 
00058 bool fileExists(string fileName)
00059 {
00060     FILE *fh=0;
00061     fh=fopen(fileName.c_str(), "rb");
00062     if(!fh)
00063         return false;
00064 
00065     fclose(fh);
00066     return true;
00067 }
00068 
00069 void testRbu::setUp()
00070 {
00071     string testInput = getCppunitTopDirectory() + getTestDirectory() + "/testInput.xml";
00072     if(!fileExists(testInput))
00073         testInput = getTestDirectory() + "/testInput.xml"; 
00074 
00075     // copy the memdump.dat file. We do not write to it, but rw open will fail
00076     // if we do not copy it
00077     string memdumpOrigFile = getCppunitTopDirectory() + getTestDirectory() + "/memdump.dat";
00078     if(!fileExists(memdumpOrigFile))
00079         memdumpOrigFile = getTestDirectory() + "/memdump.dat";
00080     string memdumpCopyFile = getWritableDirectory() + "/memdump-copy.dat";
00081     copyFile( memdumpCopyFile, memdumpOrigFile );
00082 
00083     // copy the CMOS file. We are going to write to it and do not wan to mess up
00084     // the pristine unit test version
00085     string cmosOrigFile = getCppunitTopDirectory() + getTestDirectory() + "/cmos.dat";
00086     if(!fileExists(cmosOrigFile))
00087         cmosOrigFile = getTestDirectory() + "/cmos.dat";
00088     string cmosCopyFile = getWritableDirectory() + "/cmos-copy.dat";
00089     copyFile( cmosCopyFile, cmosOrigFile );
00090 
00091     // Smi output file.
00092     string smiOutput = getWritableDirectory() + "/smi-output.dat";
00093 
00094     // normal users of the smbios classes need not
00095     // set the four parameters below. They should all be set inside the factory
00096     // properly by default. We override stuff here to have
00097     // the smbios, cmos, etc classes use file dumps instead of
00098     // real memory/cmos/etc.
00099     smbios::SmbiosFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
00100     smbios::SmbiosFactory::getFactory()->setParameter("offset", 0);
00101     smbios::SmbiosFactory::getFactory()->setMode(smbios::SmbiosFactory::UnitTestMode);
00102 
00103     cmos::  CmosRWFactory::getFactory()->setParameter("cmosMapFile", cmosCopyFile);
00104     cmos::  CmosRWFactory::getFactory()->setMode( factory::IFactory::UnitTestMode );
00105 
00106     memory::MemoryFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
00107     memory::MemoryFactory::getFactory()->setMode( memory::MemoryFactory::UnitTestMode );
00108 
00109     smi::SmiFactory::getFactory()->setParameter("smiFile", smiOutput);
00110     smi::SmiFactory::getFactory()->setMode( smi::SmiFactory::UnitTestMode );
00111 
00112     doc = 0;
00113     parser = 0;
00114     InitXML();
00115     parser = xmlutils::getParser();
00116     compatXmlReadFile(parser, doc, testInput.c_str());
00117 }
00118 
00119 void testRbu::tearDown()
00120 {
00121     // the factory is static. If we do not reset the factory, the next
00122     // unit test may accidentally get the wrong objects.
00123     // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
00124     // object used in its construction.
00125     smbios::TokenTableFactory::getFactory()->reset();
00126 
00127     smbios::SmbiosFactory::getFactory()->reset();
00128 
00129     memory::MemoryFactory::getFactory()->reset();
00130 
00131     cmos::CmosRWFactory::getFactory()->reset();
00132 
00133     smi::SmiFactory::getFactory()->reset();
00134 
00135     if (parser)
00136         xmlFreeParser(parser);
00137 
00138     if (doc)
00139         xmlFreeDoc(doc);
00140 
00141     FiniXML();
00142 }
00143 
00144 // testInput.xml tests
00145 string testRbu::getTestInputString( string toFind, string section )
00146 {
00147     if (!doc)
00148         throw skip_test();
00149 
00150     string foundString = "";
00151 
00152     try
00153     {
00154         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domSection = xmlutils::findElement( xmlDocGetRootElement(doc), section, "", "" );
00155         if(!domSection) throw skip_test();
00156         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domElem = xmlutils::findElement( domSection, toFind, "", "" );
00157         if(!domElem) throw skip_test();
00158         foundString = xmlutils::getNodeText( domElem );
00159     }
00160     catch( const exception & )
00161     {
00162         throw skip_test();
00163     }
00164 
00165     return foundString;
00166 }
00167 
00168 
00169 //
00170 //
00171 // TABLE tests
00172 //
00173 //
00174 
00175 string stringToLower(string in)
00176 {
00177     for(unsigned int i=0;i<in.length();i++)
00178     {
00179         in[i] = tolower(in[i]);
00180     }
00181     return in;
00182 }
00183 
00184 void testRbu::testRbuBadData()
00185 {
00186     STD_TEST_START(getTestName().c_str() << "  " );
00187 
00188     ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew("nonexistent_file"), rbu::HdrFileIOError );
00189 
00190     string bad_hdr_filename = getCppunitTopDirectory() + getTestDirectory() + "/bad_hdr.hdr";
00191     if(!fileExists(bad_hdr_filename))
00192         bad_hdr_filename = getTestDirectory() + "/bad_hdr.hdr";
00193 
00194     ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew(bad_hdr_filename), rbu::InvalidHdrFile );
00195 
00196     STD_TEST_END("");
00197 }
00198 
00199 auto_ptr<rbu::IRbuHdr> testRbu::checkHdrInfo(string name)
00200 {
00201     string hdr_a_name = getCppunitTopDirectory() + getTestDirectory() + "/" + getTestInputString("filename", name);
00202     if(!fileExists(hdr_a_name))
00203         hdr_a_name = getTestDirectory() + "/" + getTestInputString("filename", name);
00204 
00205     auto_ptr<rbu::IRbuHdr> hdrA (rbu::RbuFactory::getFactory()->makeNew(hdr_a_name));
00206     string expectedBiosVer = getTestInputString("biosver", name);
00207     string actualBiosVer = stringToLower(hdrA->getBiosVersion());
00208     CPPUNIT_ASSERT_EQUAL ( expectedBiosVer, actualBiosVer );
00209 
00210     unsigned int actualMajor, actualMinor, expectedMajor, expectedMinor;
00211     hdrA->getHdrVersion(actualMajor, actualMinor);
00212     expectedMajor = strtoul(getTestInputString("hdrmajorver", name).c_str(), 0, 0);
00213     expectedMinor = strtoul(getTestInputString("hdrminorver", name).c_str(), 0, 0);
00214     CPPUNIT_ASSERT_EQUAL (expectedMajor, actualMajor);
00215     CPPUNIT_ASSERT_EQUAL (expectedMinor, actualMinor);
00216     CPPUNIT_ASSERT_EQUAL ( true, checkSystemId(*hdrA, strtoul(getTestInputString("sysid", name).c_str(), 0, 0)));
00217 
00218     return hdrA;
00219 }
00220 
00221 void testRbu::testRbuBasic()
00222 {
00223     STD_TEST_START(getTestName().c_str() << "  " );
00224 
00225     auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
00226     auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
00227     auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
00228     auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
00229     auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
00230     auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
00231     auto_ptr<rbu::IRbuHdr> hdr_1b1_990208 = checkHdrInfo("hdr_1bb_990209");
00232 
00233     STD_TEST_END("");
00234 }
00235 
00236 
00237 void testRbu::testRbuOldVerCompare()
00238 {
00239     STD_TEST_START(getTestName().c_str() << "  " );
00240 
00241     auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
00242     auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
00243     auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
00244     auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
00245 
00246     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
00247     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
00248     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
00249     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a10->getBiosVersion()));
00250 
00251 
00252     //CPPUNIT_ASSERT_EQUAL( EXPECTED, ACTUAL );
00253     CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
00254     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a09->getBiosVersion()));
00255     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_x09->getBiosVersion()));
00256     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
00257     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
00258     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
00259     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
00260 
00261     // synthetic comparisons
00262     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("P01", "Q00"));
00263     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("Q01", "P00"));
00264     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("U00", "T01"));
00265     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("Y01", "Z00"));
00266 
00267     // mixed vers
00268     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("A01", "0.2.8"));
00269     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("3.2.1", "A01"));
00270 
00271     STD_TEST_END("");
00272 }
00273 
00274 
00275 void testRbu::testRbuNewVerCompare()
00276 {
00277     STD_TEST_START(getTestName().c_str() << "  " );
00278 
00279     auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
00280     auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
00281     auto_ptr<rbu::IRbuHdr> hdr_1b1_990209 = checkHdrInfo("hdr_1bb_990209");
00282 
00283     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
00284     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
00285     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
00286 
00287     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
00288     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
00289 
00290     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
00291     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
00292     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
00293     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
00294 
00295     // synthetic comparisons
00296     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("0.2.8", "99.2.4"));
00297     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.2.8", "0.2.4"));
00298     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("1.2.8", "2.2.4"));
00299     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("1.2.8", "1.3.4"));
00300     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.4.8", "1.3.4"));
00301 
00302     STD_TEST_END("");
00303 }
00304 
00305 // not part of public API, so declare here
00306 namespace rbu {
00307 extern void splitNewVersion(std::string ver, unsigned int &maj, unsigned int &min, unsigned int &ext);
00308 }
00309 
00310 void testRbu::testRbuNewVerSplit()
00311 {
00312     STD_TEST_START(getTestName().c_str() << "  " );
00313 
00314     unsigned int maj, min, ext, expmaj, expmin, expext;
00315     string ver;
00316 
00317     // good version
00318     ver = "0.2.9";
00319     expmaj = 0;
00320     expmin = 2;
00321     expext = 9;
00322     rbu::splitNewVersion(ver, maj, min, ext);
00323     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00324     CPPUNIT_ASSERT_EQUAL( expmin, min );
00325     CPPUNIT_ASSERT_EQUAL( expext, ext );
00326 
00327     // high version
00328     ver = "99.2.9";
00329     expmaj = 99;
00330     expmin = 2;
00331     expext = 9;
00332     rbu::splitNewVersion(ver, maj, min, ext);
00333     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00334     CPPUNIT_ASSERT_EQUAL( expmin, min );
00335     CPPUNIT_ASSERT_EQUAL( expext, ext );
00336 
00337     // max legal len
00338     ver = "88.88.88";
00339     expmaj = 88;
00340     expmin = 88;
00341     expext = 88;
00342     rbu::splitNewVersion(ver, maj, min, ext);
00343     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00344     CPPUNIT_ASSERT_EQUAL( expmin, min );
00345     CPPUNIT_ASSERT_EQUAL( expext, ext );
00346 
00347     // bad: trailing period
00348     ver = "100.100.100.";
00349     expmaj = 100;
00350     expmin = 100;
00351     expext = 100;
00352     rbu::splitNewVersion(ver, maj, min, ext);
00353     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00354     CPPUNIT_ASSERT_EQUAL( expmin, min );
00355     CPPUNIT_ASSERT_EQUAL( expext, ext );
00356 
00357     // bad: missing ext
00358     ver = "100.100.";
00359     expmaj = 100;
00360     expmin = 100;
00361     expext = 0;
00362     rbu::splitNewVersion(ver, maj, min, ext);
00363     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00364     CPPUNIT_ASSERT_EQUAL( expmin, min );
00365     CPPUNIT_ASSERT_EQUAL( expext, ext );
00366 
00367     // bad: missing .ext
00368     ver = "0.2";
00369     expmaj = 0;
00370     expmin = 2;
00371     expext = 0;
00372     rbu::splitNewVersion(ver, maj, min, ext);
00373     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00374     CPPUNIT_ASSERT_EQUAL( expmin, min );
00375     CPPUNIT_ASSERT_EQUAL( expext, ext );
00376 
00377     // bad: missing min.ext
00378     ver = "100.";
00379     expmaj = 100;
00380     expmin = 0;
00381     expext = 0;
00382     rbu::splitNewVersion(ver, maj, min, ext);
00383     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00384     CPPUNIT_ASSERT_EQUAL( expmin, min );
00385     CPPUNIT_ASSERT_EQUAL( expext, ext );
00386 
00387     // bad: missing .min.ext
00388     ver = "100";
00389     expmaj = 100;
00390     expmin = 0;
00391     expext = 0;
00392     rbu::splitNewVersion(ver, maj, min, ext);
00393     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00394     CPPUNIT_ASSERT_EQUAL( expmin, min );
00395     CPPUNIT_ASSERT_EQUAL( expext, ext );
00396 
00397     // bad: trailing junk
00398     ver = "100.100.100Junk";
00399     expmaj = 100;
00400     expmin = 100;
00401     expext = 100;
00402     rbu::splitNewVersion(ver, maj, min, ext);
00403     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00404     CPPUNIT_ASSERT_EQUAL( expmin, min );
00405     CPPUNIT_ASSERT_EQUAL( expext, ext );
00406 
00407     STD_TEST_END("");
00408 }
00409 
00410 
00411 void testRbu::testRbuOutput()
00412 {
00413     STD_TEST_START(getTestName().c_str() << "  " );
00414 
00415     std::ostringstream out;
00416     auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
00417     out << *hdr_152_a09 << endl;
00418 
00419     STD_TEST_END("");
00420 }
00421 

Generated on Wed Apr 2 16:37:38 2008 for SMBIOS Library by  doxygen 1.5.1