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

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/ISmbiosXml.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 xmlFile = getCppunitTopDirectory() + getXmlFile();
00072 
00073     string testInput = getCppunitTopDirectory() + getTestDirectory() + "/testInput.xml";
00074     if(!fileExists(testInput))
00075         testInput = getTestDirectory() + "/testInput.xml"; 
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 = getCppunitTopDirectory() + getTestDirectory() + "/memdump.dat";
00080     if(!fileExists(memdumpOrigFile))
00081         memdumpOrigFile = getTestDirectory() + "/memdump.dat";
00082     string memdumpCopyFile = getWritableDirectory() + "/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 = getCppunitTopDirectory() + getTestDirectory() + "/cmos.dat";
00088     if(!fileExists(cmosOrigFile))
00089         cmosOrigFile = getTestDirectory() + "/cmos.dat";
00090     string cmosCopyFile = getWritableDirectory() + "/cmos-copy.dat";
00091     copyFile( cmosCopyFile, cmosOrigFile );
00092 
00093     // Smi output file.
00094     string smiOutput = getWritableDirectory() + "/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     doc = 0;
00121     parser = 0;
00122     InitXML();
00123     parser = xmlutils::getParser();
00124     compatXmlReadFile(parser, doc, testInput.c_str());
00125 }
00126 
00127 void testRbu::tearDown()
00128 {
00129     // the factory is static. If we do not reset the factory, the next
00130     // unit test may accidentally get the wrong objects.
00131     // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
00132     // object used in its construction.
00133     smbios::TokenTableFactory::getFactory()->reset();
00134 
00135     smbios::SmbiosFactory::getFactory()->reset();
00136 
00137     memory::MemoryFactory::getFactory()->reset();
00138 
00139     cmos::CmosRWFactory::getFactory()->reset();
00140 
00141     smi::SmiFactory::getFactory()->reset();
00142 
00143     if (parser)
00144         xmlFreeParser(parser);
00145 
00146     if (doc)
00147         xmlFreeDoc(doc);
00148 
00149     FiniXML();
00150 }
00151 
00152 // testInput.xml tests
00153 string testRbu::getTestInputString( string toFind, string section )
00154 {
00155     if (!doc)
00156         throw skip_test();
00157 
00158     string foundString = "";
00159 
00160     try
00161     {
00162         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domSection = xmlutils::findElement( xmlDocGetRootElement(doc), section, "", "" );
00163         if(!domSection) throw skip_test();
00164         XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domElem = xmlutils::findElement( domSection, toFind, "", "" );
00165         if(!domElem) throw skip_test();
00166         foundString = xmlutils::getNodeText( domElem );
00167     }
00168     catch( const exception & )
00169     {
00170         throw skip_test();
00171     }
00172 
00173     return foundString;
00174 }
00175 
00176 
00177 //
00178 //
00179 // TABLE tests
00180 //
00181 //
00182 
00183 string stringToLower(string in)
00184 {
00185     for(unsigned int i=0;i<in.length();i++)
00186     {
00187         in[i] = tolower(in[i]);
00188     }
00189     return in;
00190 }
00191 
00192 void testRbu::testRbuBadData()
00193 {
00194     STD_TEST_START(getTestName().c_str() << "  " );
00195 
00196     ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew("nonexistent_file"), rbu::HdrFileIOError );
00197 
00198     string bad_hdr_filename = getCppunitTopDirectory() + getTestDirectory() + "/bad_hdr.hdr";
00199     if(!fileExists(bad_hdr_filename))
00200         bad_hdr_filename = getTestDirectory() + "/bad_hdr.hdr";
00201 
00202     ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew(bad_hdr_filename), rbu::InvalidHdrFile );
00203 
00204     STD_TEST_END("");
00205 }
00206 
00207 auto_ptr<rbu::IRbuHdr> testRbu::checkHdrInfo(string name)
00208 {
00209     string hdr_a_name = getCppunitTopDirectory() + getTestDirectory() + "/" + getTestInputString("filename", name);
00210     if(!fileExists(hdr_a_name))
00211         hdr_a_name = getTestDirectory() + "/" + getTestInputString("filename", name);
00212 
00213     auto_ptr<rbu::IRbuHdr> hdrA (rbu::RbuFactory::getFactory()->makeNew(hdr_a_name));
00214     string expectedBiosVer = getTestInputString("biosver", name);
00215     string actualBiosVer = stringToLower(hdrA->getBiosVersion());
00216     CPPUNIT_ASSERT_EQUAL ( expectedBiosVer, actualBiosVer );
00217 
00218     unsigned int actualMajor, actualMinor, expectedMajor, expectedMinor;
00219     hdrA->getHdrVersion(actualMajor, actualMinor);
00220     expectedMajor = strtoul(getTestInputString("hdrmajorver", name).c_str(), 0, 0);
00221     expectedMinor = strtoul(getTestInputString("hdrminorver", name).c_str(), 0, 0);
00222     CPPUNIT_ASSERT_EQUAL (expectedMajor, actualMajor);
00223     CPPUNIT_ASSERT_EQUAL (expectedMinor, actualMinor);
00224     CPPUNIT_ASSERT_EQUAL ( true, checkSystemId(*hdrA, strtoul(getTestInputString("sysid", name).c_str(), 0, 0)));
00225 
00226     return hdrA;
00227 }
00228 
00229 void testRbu::testRbuBasic()
00230 {
00231     STD_TEST_START(getTestName().c_str() << "  " );
00232 
00233     auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
00234     auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
00235     auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
00236     auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
00237     auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
00238     auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
00239     auto_ptr<rbu::IRbuHdr> hdr_1b1_990208 = checkHdrInfo("hdr_1bb_990209");
00240 
00241     STD_TEST_END("");
00242 }
00243 
00244 
00245 void testRbu::testRbuOldVerCompare()
00246 {
00247     STD_TEST_START(getTestName().c_str() << "  " );
00248 
00249     auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
00250     auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
00251     auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
00252     auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
00253 
00254     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
00255     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
00256     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
00257     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a10->getBiosVersion()));
00258 
00259 
00260     //CPPUNIT_ASSERT_EQUAL( EXPECTED, ACTUAL );
00261     CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
00262     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a09->getBiosVersion()));
00263     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_x09->getBiosVersion()));
00264     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
00265     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
00266     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
00267     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
00268 
00269     // synthetic comparisons
00270     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("P01", "Q00"));
00271     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("Q01", "P00"));
00272     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("U00", "T01"));
00273     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("Y01", "Z00"));
00274 
00275     // mixed vers
00276     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("A01", "0.2.8"));
00277     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("3.2.1", "A01"));
00278 
00279     STD_TEST_END("");
00280 }
00281 
00282 
00283 void testRbu::testRbuNewVerCompare()
00284 {
00285     STD_TEST_START(getTestName().c_str() << "  " );
00286 
00287     auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
00288     auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
00289     auto_ptr<rbu::IRbuHdr> hdr_1b1_990209 = checkHdrInfo("hdr_1bb_990209");
00290 
00291     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
00292     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
00293     CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
00294 
00295     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
00296     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
00297 
00298     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
00299     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
00300     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
00301     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
00302 
00303     // synthetic comparisons
00304     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("0.2.8", "99.2.4"));
00305     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.2.8", "0.2.4"));
00306     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("1.2.8", "2.2.4"));
00307     CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("1.2.8", "1.3.4"));
00308     CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.4.8", "1.3.4"));
00309 
00310     STD_TEST_END("");
00311 }
00312 
00313 // not part of public API, so declare here
00314 namespace rbu {
00315 extern void splitNewVersion(std::string ver, unsigned int &maj, unsigned int &min, unsigned int &ext);
00316 }
00317 
00318 void testRbu::testRbuNewVerSplit()
00319 {
00320     STD_TEST_START(getTestName().c_str() << "  " );
00321 
00322     unsigned int maj, min, ext, expmaj, expmin, expext;
00323     string ver;
00324 
00325     // good version
00326     ver = "0.2.9";
00327     expmaj = 0;
00328     expmin = 2;
00329     expext = 9;
00330     rbu::splitNewVersion(ver, maj, min, ext);
00331     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00332     CPPUNIT_ASSERT_EQUAL( expmin, min );
00333     CPPUNIT_ASSERT_EQUAL( expext, ext );
00334 
00335     // high version
00336     ver = "99.2.9";
00337     expmaj = 99;
00338     expmin = 2;
00339     expext = 9;
00340     rbu::splitNewVersion(ver, maj, min, ext);
00341     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00342     CPPUNIT_ASSERT_EQUAL( expmin, min );
00343     CPPUNIT_ASSERT_EQUAL( expext, ext );
00344 
00345     // max legal len
00346     ver = "88.88.88";
00347     expmaj = 88;
00348     expmin = 88;
00349     expext = 88;
00350     rbu::splitNewVersion(ver, maj, min, ext);
00351     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00352     CPPUNIT_ASSERT_EQUAL( expmin, min );
00353     CPPUNIT_ASSERT_EQUAL( expext, ext );
00354 
00355     // bad: trailing period
00356     ver = "100.100.100.";
00357     expmaj = 100;
00358     expmin = 100;
00359     expext = 100;
00360     rbu::splitNewVersion(ver, maj, min, ext);
00361     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00362     CPPUNIT_ASSERT_EQUAL( expmin, min );
00363     CPPUNIT_ASSERT_EQUAL( expext, ext );
00364 
00365     // bad: missing ext
00366     ver = "100.100.";
00367     expmaj = 100;
00368     expmin = 100;
00369     expext = 0;
00370     rbu::splitNewVersion(ver, maj, min, ext);
00371     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00372     CPPUNIT_ASSERT_EQUAL( expmin, min );
00373     CPPUNIT_ASSERT_EQUAL( expext, ext );
00374 
00375     // bad: missing .ext
00376     ver = "0.2";
00377     expmaj = 0;
00378     expmin = 2;
00379     expext = 0;
00380     rbu::splitNewVersion(ver, maj, min, ext);
00381     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00382     CPPUNIT_ASSERT_EQUAL( expmin, min );
00383     CPPUNIT_ASSERT_EQUAL( expext, ext );
00384 
00385     // bad: missing min.ext
00386     ver = "100.";
00387     expmaj = 100;
00388     expmin = 0;
00389     expext = 0;
00390     rbu::splitNewVersion(ver, maj, min, ext);
00391     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00392     CPPUNIT_ASSERT_EQUAL( expmin, min );
00393     CPPUNIT_ASSERT_EQUAL( expext, ext );
00394 
00395     // bad: missing .min.ext
00396     ver = "100";
00397     expmaj = 100;
00398     expmin = 0;
00399     expext = 0;
00400     rbu::splitNewVersion(ver, maj, min, ext);
00401     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00402     CPPUNIT_ASSERT_EQUAL( expmin, min );
00403     CPPUNIT_ASSERT_EQUAL( expext, ext );
00404 
00405     // bad: trailing junk
00406     ver = "100.100.100Junk";
00407     expmaj = 100;
00408     expmin = 100;
00409     expext = 100;
00410     rbu::splitNewVersion(ver, maj, min, ext);
00411     CPPUNIT_ASSERT_EQUAL( expmaj, maj );
00412     CPPUNIT_ASSERT_EQUAL( expmin, min );
00413     CPPUNIT_ASSERT_EQUAL( expext, ext );
00414 
00415     STD_TEST_END("");
00416 }
00417 
00418 
00419 void testRbu::testRbuOutput()
00420 {
00421     STD_TEST_START(getTestName().c_str() << "  " );
00422 
00423     std::ostringstream out;
00424     auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
00425     out << *hdr_152_a09 << endl;
00426 
00427     STD_TEST_END("");
00428 }
00429 

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