File: testRbu.cpp

package info (click to toggle)
libsmbios 2.0.3.dfsg-1.1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 3,768 kB
  • ctags: 2,016
  • sloc: cpp: 14,292; sh: 9,408; xml: 3,820; makefile: 454; ansic: 402; python: 157
file content (421 lines) | stat: -rw-r--r-- 15,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
 *
 * Copyright (C) 2005 Dell Inc.
 *  by Michael Brown <Michael_E_Brown@dell.com>
 * Licensed under the Open Software License version 2.1 
 * 
 * Alternatively, you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published 
 * by the Free Software Foundation; either version 2 of the License, 
 * or (at your option) any later version.

 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */

// compat header should always be first header if including system headers
#include "smbios/compat.h"

#include <fstream>
#include <cctype>

#include "testRbu.h"

// specific to unit tests. Users do not need to include this,
// so it is not in testPlatform.h
#include "smbios/IMemory.h"
#include "smbios/ISmi.h"
#include "smbios/ISmbios.h"
#include "smbios/IToken.h"

using namespace std;

// Note:
//      Except for , there are no "using namespace XXXX;" statements
//      here... on purpose. We want to ensure that while reading this code that
//      it is extremely obvious where each function is coming from.
//
//      This leads to verbose code in some instances, but that is fine for
//      these purposes.

// Register the test
CPPUNIT_TEST_SUITE_REGISTRATION (testRbu);

void copyFile( string dstFile, string srcFile )
{
    ifstream src(srcFile.c_str(), ios_base::binary);
    ofstream dst(dstFile.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);

    char ch;
    while( src.get(ch)) dst.put(ch);

    if( !src.eof() || !dst ) throw exception();
}

bool fileExists(string fileName)
{
    FILE *fh=0;
    fh=fopen(fileName.c_str(), "rb");
    if(!fh)
        return false;

    fclose(fh);
    return true;
}

void testRbu::setUp()
{
    string testInput = getCppunitTopDirectory() + getTestDirectory() + "/testInput.xml";
    if(!fileExists(testInput))
        testInput = getTestDirectory() + "/testInput.xml"; 

    // copy the memdump.dat file. We do not write to it, but rw open will fail
    // if we do not copy it
    string memdumpOrigFile = getCppunitTopDirectory() + getTestDirectory() + "/memdump.dat";
    if(!fileExists(memdumpOrigFile))
        memdumpOrigFile = getTestDirectory() + "/memdump.dat";
    string memdumpCopyFile = getWritableDirectory() + "/memdump-copy.dat";
    copyFile( memdumpCopyFile, memdumpOrigFile );

    // copy the CMOS file. We are going to write to it and do not wan to mess up
    // the pristine unit test version
    string cmosOrigFile = getCppunitTopDirectory() + getTestDirectory() + "/cmos.dat";
    if(!fileExists(cmosOrigFile))
        cmosOrigFile = getTestDirectory() + "/cmos.dat";
    string cmosCopyFile = getWritableDirectory() + "/cmos-copy.dat";
    copyFile( cmosCopyFile, cmosOrigFile );

    // Smi output file.
    string smiOutput = getWritableDirectory() + "/smi-output.dat";

    // normal users of the smbios classes need not
    // set the four parameters below. They should all be set inside the factory
    // properly by default. We override stuff here to have
    // the smbios, cmos, etc classes use file dumps instead of
    // real memory/cmos/etc.
    smbios::SmbiosFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
    smbios::SmbiosFactory::getFactory()->setParameter("offset", 0);
    smbios::SmbiosFactory::getFactory()->setMode(smbios::SmbiosFactory::UnitTestMode);

    cmos::  CmosRWFactory::getFactory()->setParameter("cmosMapFile", cmosCopyFile);
    cmos::  CmosRWFactory::getFactory()->setMode( factory::IFactory::UnitTestMode );

    memory::MemoryFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
    memory::MemoryFactory::getFactory()->setMode( memory::MemoryFactory::UnitTestMode );

    smi::SmiFactory::getFactory()->setParameter("smiFile", smiOutput);
    smi::SmiFactory::getFactory()->setMode( smi::SmiFactory::UnitTestMode );

    doc = 0;
    parser = 0;
    InitXML();
    parser = xmlutils::getParser();
    compatXmlReadFile(parser, doc, testInput.c_str());
}

void testRbu::tearDown()
{
    // the factory is static. If we do not reset the factory, the next
    // unit test may accidentally get the wrong objects.
    // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
    // object used in its construction.
    smbios::TokenTableFactory::getFactory()->reset();

    smbios::SmbiosFactory::getFactory()->reset();

    memory::MemoryFactory::getFactory()->reset();

    cmos::CmosRWFactory::getFactory()->reset();

    smi::SmiFactory::getFactory()->reset();

    if (parser)
        xmlFreeParser(parser);

    if (doc)
        xmlFreeDoc(doc);

    FiniXML();
}

// testInput.xml tests
string testRbu::getTestInputString( string toFind, string section )
{
    if (!doc)
        throw skip_test();

    string foundString = "";

    try
    {
        XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domSection = xmlutils::findElement( xmlDocGetRootElement(doc), section, "", "" );
        if(!domSection) throw skip_test();
        XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domElem = xmlutils::findElement( domSection, toFind, "", "" );
        if(!domElem) throw skip_test();
        foundString = xmlutils::getNodeText( domElem );
    }
    catch( const exception & )
    {
        throw skip_test();
    }

    return foundString;
}


//
//
// TABLE tests
//
//

string stringToLower(string in)
{
    for(unsigned int i=0;i<in.length();i++)
    {
        in[i] = tolower(in[i]);
    }
    return in;
}

void testRbu::testRbuBadData()
{
    STD_TEST_START(getTestName().c_str() << "  " );

    ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew("nonexistent_file"), rbu::HdrFileIOError );

    string bad_hdr_filename = getCppunitTopDirectory() + getTestDirectory() + "/bad_hdr.hdr";
    if(!fileExists(bad_hdr_filename))
        bad_hdr_filename = getTestDirectory() + "/bad_hdr.hdr";

    ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew(bad_hdr_filename), rbu::InvalidHdrFile );

    STD_TEST_END("");
}

auto_ptr<rbu::IRbuHdr> testRbu::checkHdrInfo(string name)
{
    string hdr_a_name = getCppunitTopDirectory() + getTestDirectory() + "/" + getTestInputString("filename", name);
    if(!fileExists(hdr_a_name))
        hdr_a_name = getTestDirectory() + "/" + getTestInputString("filename", name);

    auto_ptr<rbu::IRbuHdr> hdrA (rbu::RbuFactory::getFactory()->makeNew(hdr_a_name));
    string expectedBiosVer = getTestInputString("biosver", name);
    string actualBiosVer = stringToLower(hdrA->getBiosVersion());
    CPPUNIT_ASSERT_EQUAL ( expectedBiosVer, actualBiosVer );

    unsigned int actualMajor, actualMinor, expectedMajor, expectedMinor;
    hdrA->getHdrVersion(actualMajor, actualMinor);
    expectedMajor = strtoul(getTestInputString("hdrmajorver", name).c_str(), 0, 0);
    expectedMinor = strtoul(getTestInputString("hdrminorver", name).c_str(), 0, 0);
    CPPUNIT_ASSERT_EQUAL (expectedMajor, actualMajor);
    CPPUNIT_ASSERT_EQUAL (expectedMinor, actualMinor);
    CPPUNIT_ASSERT_EQUAL ( true, checkSystemId(*hdrA, strtoul(getTestInputString("sysid", name).c_str(), 0, 0)));

    return hdrA;
}

void testRbu::testRbuBasic()
{
    STD_TEST_START(getTestName().c_str() << "  " );

    auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
    auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
    auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
    auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
    auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
    auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
    auto_ptr<rbu::IRbuHdr> hdr_1b1_990208 = checkHdrInfo("hdr_1bb_990209");

    STD_TEST_END("");
}


void testRbu::testRbuOldVerCompare()
{
    STD_TEST_START(getTestName().c_str() << "  " );

    auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
    auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
    auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
    auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");

    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a10->getBiosVersion()));


    //CPPUNIT_ASSERT_EQUAL( EXPECTED, ACTUAL );
    CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_x09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_x09->getBiosVersion()));

    // synthetic comparisons
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("P01", "Q00"));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("Q01", "P00"));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("U00", "T01"));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("Y01", "Z00"));

    // mixed vers
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("A01", "0.2.8"));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("3.2.1", "A01"));

    STD_TEST_END("");
}


void testRbu::testRbuNewVerCompare()
{
    STD_TEST_START(getTestName().c_str() << "  " );

    auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
    auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
    auto_ptr<rbu::IRbuHdr> hdr_1b1_990209 = checkHdrInfo("hdr_1bb_990209");

    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));

    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));

    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));

    // synthetic comparisons
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("0.2.8", "99.2.4"));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.2.8", "0.2.4"));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("1.2.8", "2.2.4"));
    CPPUNIT_ASSERT_EQUAL(  1, rbu::compareBiosVersion("1.2.8", "1.3.4"));
    CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.4.8", "1.3.4"));

    STD_TEST_END("");
}

// not part of public API, so declare here
namespace rbu {
extern void splitNewVersion(std::string ver, unsigned int &maj, unsigned int &min, unsigned int &ext);
}

void testRbu::testRbuNewVerSplit()
{
    STD_TEST_START(getTestName().c_str() << "  " );

    unsigned int maj, min, ext, expmaj, expmin, expext;
    string ver;

    // good version
    ver = "0.2.9";
    expmaj = 0;
    expmin = 2;
    expext = 9;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // high version
    ver = "99.2.9";
    expmaj = 99;
    expmin = 2;
    expext = 9;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // max legal len
    ver = "88.88.88";
    expmaj = 88;
    expmin = 88;
    expext = 88;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // bad: trailing period
    ver = "100.100.100.";
    expmaj = 100;
    expmin = 100;
    expext = 100;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // bad: missing ext
    ver = "100.100.";
    expmaj = 100;
    expmin = 100;
    expext = 0;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // bad: missing .ext
    ver = "0.2";
    expmaj = 0;
    expmin = 2;
    expext = 0;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // bad: missing min.ext
    ver = "100.";
    expmaj = 100;
    expmin = 0;
    expext = 0;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // bad: missing .min.ext
    ver = "100";
    expmaj = 100;
    expmin = 0;
    expext = 0;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    // bad: trailing junk
    ver = "100.100.100Junk";
    expmaj = 100;
    expmin = 100;
    expext = 100;
    rbu::splitNewVersion(ver, maj, min, ext);
    CPPUNIT_ASSERT_EQUAL( expmaj, maj );
    CPPUNIT_ASSERT_EQUAL( expmin, min );
    CPPUNIT_ASSERT_EQUAL( expext, ext );

    STD_TEST_END("");
}


void testRbu::testRbuOutput()
{
    STD_TEST_START(getTestName().c_str() << "  " );

    std::ostringstream out;
    auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
    out << *hdr_152_a09 << endl;

    STD_TEST_END("");
}