SMBIOS Library
testRbu.cpp
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
3  *
4  * Copyright (C) 2005 Dell Inc.
5  * by Michael Brown <Michael_E_Brown@dell.com>
6  * Licensed under the Open Software License version 2.1
7  *
8  * Alternatively, you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  */
18 
19 // compat header should always be first header if including system headers
20 #include "smbios/compat.h"
21 
22 #include <fstream>
23 #include <cctype>
24 
25 #include "testRbu.h"
26 
27 // specific to unit tests. Users do not need to include this,
28 // so it is not in testPlatform.h
29 #include "smbios/IMemory.h"
30 #include "smbios/ISmi.h"
31 #include "smbios/ISmbios.h"
32 #include "smbios/IToken.h"
33 
34 using namespace std;
35 
36 // Note:
37 // Except for , there are no "using namespace XXXX;" statements
38 // here... on purpose. We want to ensure that while reading this code that
39 // it is extremely obvious where each function is coming from.
40 //
41 // This leads to verbose code in some instances, but that is fine for
42 // these purposes.
43 
44 // Register the test
46 
47 void copyFile( string dstFile, string srcFile )
48 {
49  ifstream src(srcFile.c_str(), ios_base::binary);
50  ofstream dst(dstFile.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);
51 
52  char ch;
53  while( src.get(ch)) dst.put(ch);
54 
55  if( !src.eof() || !dst ) throw exception();
56 }
57 
58 bool fileExists(string fileName)
59 {
60  FILE *fh=0;
61  fh=fopen(fileName.c_str(), "rb");
62  if(!fh)
63  return false;
64 
65  fclose(fh);
66  return true;
67 }
68 
70 {
71  string testInput = getCppunitTopDirectory() + getTestDirectory() + "/testInput.xml";
72  if(!fileExists(testInput))
73  testInput = getTestDirectory() + "/testInput.xml";
74 
75  // copy the memdump.dat file. We do not write to it, but rw open will fail
76  // if we do not copy it
77  string memdumpOrigFile = getCppunitTopDirectory() + getTestDirectory() + "/memdump.dat";
78  if(!fileExists(memdumpOrigFile))
79  memdumpOrigFile = getTestDirectory() + "/memdump.dat";
80  string memdumpCopyFile = getWritableDirectory() + "/memdump-copy.dat";
81  copyFile( memdumpCopyFile, memdumpOrigFile );
82 
83  // copy the CMOS file. We are going to write to it and do not wan to mess up
84  // the pristine unit test version
85  string cmosOrigFile = getCppunitTopDirectory() + getTestDirectory() + "/cmos.dat";
86  if(!fileExists(cmosOrigFile))
87  cmosOrigFile = getTestDirectory() + "/cmos.dat";
88  string cmosCopyFile = getWritableDirectory() + "/cmos-copy.dat";
89  copyFile( cmosCopyFile, cmosOrigFile );
90 
91  // Smi output file.
92  string smiOutput = getWritableDirectory() + "/smi-output.dat";
93 
94  // normal users of the smbios classes need not
95  // set the four parameters below. They should all be set inside the factory
96  // properly by default. We override stuff here to have
97  // the smbios, cmos, etc classes use file dumps instead of
98  // real memory/cmos/etc.
99  smbios::SmbiosFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
102 
103  cmos:: CmosRWFactory::getFactory()->setParameter("cmosMapFile", cmosCopyFile);
105 
106  memory::MemoryFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
108 
109  smi::SmiFactory::getFactory()->setParameter("smiFile", smiOutput);
111 
112  doc = 0;
113  parser = 0;
114  InitXML();
115  parser = xmlutils::getParser();
116  compatXmlReadFile(parser, doc, testInput.c_str());
117 }
118 
120 {
121  // the factory is static. If we do not reset the factory, the next
122  // unit test may accidentally get the wrong objects.
123  // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
124  // object used in its construction.
126 
128 
130 
132 
134 
135  if (parser)
136  xmlFreeParser(parser);
137 
138  if (doc)
139  xmlFreeDoc(doc);
140 
141  FiniXML();
142 }
143 
144 // testInput.xml tests
145 string testRbu::getTestInputString( string toFind, string section )
146 {
147  if (!doc)
148  throw skip_test();
149 
150  string foundString = "";
151 
152  try
153  {
155  if(!domSection) throw skip_test();
156  XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *domElem = xmlutils::findElement( domSection, toFind, "", "" );
157  if(!domElem) throw skip_test();
158  foundString = xmlutils::getNodeText( domElem );
159  }
160  catch( const exception & )
161  {
162  throw skip_test();
163  }
164 
165  return foundString;
166 }
167 
168 
169 //
170 //
171 // TABLE tests
172 //
173 //
174 
175 string stringToLower(string in)
176 {
177  for(unsigned int i=0;i<in.length();i++)
178  {
179  in[i] = tolower(in[i]);
180  }
181  return in;
182 }
183 
185 {
186  STD_TEST_START(getTestName().c_str() << " " );
187 
188  ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew("nonexistent_file"), rbu::HdrFileIOError );
189 
190  string bad_hdr_filename = getCppunitTopDirectory() + getTestDirectory() + "/bad_hdr.hdr";
191  if(!fileExists(bad_hdr_filename))
192  bad_hdr_filename = getTestDirectory() + "/bad_hdr.hdr";
193 
194  ASSERT_THROWS( rbu::RbuFactory::getFactory()->makeNew(bad_hdr_filename), rbu::InvalidHdrFile );
195 
196  STD_TEST_END("");
197 }
198 
199 auto_ptr<rbu::IRbuHdr> testRbu::checkHdrInfo(string name)
200 {
201  string hdr_a_name = getCppunitTopDirectory() + getTestDirectory() + "/" + getTestInputString("filename", name);
202  if(!fileExists(hdr_a_name))
203  hdr_a_name = getTestDirectory() + "/" + getTestInputString("filename", name);
204 
205  auto_ptr<rbu::IRbuHdr> hdrA (rbu::RbuFactory::getFactory()->makeNew(hdr_a_name));
206  string expectedBiosVer = getTestInputString("biosver", name);
207  string actualBiosVer = stringToLower(hdrA->getBiosVersion());
208  CPPUNIT_ASSERT_EQUAL ( expectedBiosVer, actualBiosVer );
209 
210  unsigned int actualMajor, actualMinor, expectedMajor, expectedMinor;
211  hdrA->getHdrVersion(actualMajor, actualMinor);
212  expectedMajor = strtoul(getTestInputString("hdrmajorver", name).c_str(), 0, 0);
213  expectedMinor = strtoul(getTestInputString("hdrminorver", name).c_str(), 0, 0);
214  CPPUNIT_ASSERT_EQUAL (expectedMajor, actualMajor);
215  CPPUNIT_ASSERT_EQUAL (expectedMinor, actualMinor);
216  CPPUNIT_ASSERT_EQUAL ( true, checkSystemId(*hdrA, strtoul(getTestInputString("sysid", name).c_str(), 0, 0)));
217 
218  return hdrA;
219 }
220 
222 {
223  STD_TEST_START(getTestName().c_str() << " " );
224 
225  auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
226  auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
227  auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
228  auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
229  auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
230  auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
231  auto_ptr<rbu::IRbuHdr> hdr_1b1_990208 = checkHdrInfo("hdr_1bb_990209");
232 
233  STD_TEST_END("");
234 }
235 
236 
238 {
239  STD_TEST_START(getTestName().c_str() << " " );
240 
241  auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
242  auto_ptr<rbu::IRbuHdr> hdr_152_x09 = checkHdrInfo("hdr_152_x09");
243  auto_ptr<rbu::IRbuHdr> hdr_152_p09 = checkHdrInfo("hdr_152_p09");
244  auto_ptr<rbu::IRbuHdr> hdr_152_a10 = checkHdrInfo("hdr_152_a10");
245 
246  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
247  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
248  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
249  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a10->getBiosVersion()));
250 
251 
252  //CPPUNIT_ASSERT_EQUAL( EXPECTED, ACTUAL );
253  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
254  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_a09->getBiosVersion()));
255  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_a10->getBiosVersion(), hdr_152_x09->getBiosVersion()));
256  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_p09->getBiosVersion()));
257  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_a09->getBiosVersion(), hdr_152_a10->getBiosVersion()));
258  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_x09->getBiosVersion(), hdr_152_a09->getBiosVersion()));
259  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_152_p09->getBiosVersion(), hdr_152_x09->getBiosVersion()));
260 
261  // synthetic comparisons
262  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("P01", "Q00"));
263  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion("Q01", "P00"));
264  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("U00", "T01"));
265  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion("Y01", "Z00"));
266 
267  // mixed vers
268  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion("A01", "0.2.8"));
269  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("3.2.1", "A01"));
270 
271  STD_TEST_END("");
272 }
273 
274 
276 {
277  STD_TEST_START(getTestName().c_str() << " " );
278 
279  auto_ptr<rbu::IRbuHdr> hdr_1b1_000208 = checkHdrInfo("hdr_1b1_000208");
280  auto_ptr<rbu::IRbuHdr> hdr_1b1_000209 = checkHdrInfo("hdr_1bb_000209");
281  auto_ptr<rbu::IRbuHdr> hdr_1b1_990209 = checkHdrInfo("hdr_1bb_990209");
282 
283  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
284  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
285  CPPUNIT_ASSERT_EQUAL( 0, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
286 
287  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
288  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
289 
290  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000208->getBiosVersion()));
291  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion(hdr_1b1_990209->getBiosVersion(), hdr_1b1_000209->getBiosVersion()));
292  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000208->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
293  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion(hdr_1b1_000209->getBiosVersion(), hdr_1b1_990209->getBiosVersion()));
294 
295  // synthetic comparisons
296  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("0.2.8", "99.2.4"));
297  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.2.8", "0.2.4"));
298  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion("1.2.8", "2.2.4"));
299  CPPUNIT_ASSERT_EQUAL( 1, rbu::compareBiosVersion("1.2.8", "1.3.4"));
300  CPPUNIT_ASSERT_EQUAL( -1, rbu::compareBiosVersion("1.4.8", "1.3.4"));
301 
302  STD_TEST_END("");
303 }
304 
305 // not part of public API, so declare here
306 namespace rbu {
307 extern void splitNewVersion(std::string ver, unsigned int &maj, unsigned int &min, unsigned int &ext);
308 }
309 
311 {
312  STD_TEST_START(getTestName().c_str() << " " );
313 
314  unsigned int maj, min, ext, expmaj, expmin, expext;
315  string ver;
316 
317  // good version
318  ver = "0.2.9";
319  expmaj = 0;
320  expmin = 2;
321  expext = 9;
322  rbu::splitNewVersion(ver, maj, min, ext);
323  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
324  CPPUNIT_ASSERT_EQUAL( expmin, min );
325  CPPUNIT_ASSERT_EQUAL( expext, ext );
326 
327  // high version
328  ver = "99.2.9";
329  expmaj = 99;
330  expmin = 2;
331  expext = 9;
332  rbu::splitNewVersion(ver, maj, min, ext);
333  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
334  CPPUNIT_ASSERT_EQUAL( expmin, min );
335  CPPUNIT_ASSERT_EQUAL( expext, ext );
336 
337  // max legal len
338  ver = "88.88.88";
339  expmaj = 88;
340  expmin = 88;
341  expext = 88;
342  rbu::splitNewVersion(ver, maj, min, ext);
343  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
344  CPPUNIT_ASSERT_EQUAL( expmin, min );
345  CPPUNIT_ASSERT_EQUAL( expext, ext );
346 
347  // bad: trailing period
348  ver = "100.100.100.";
349  expmaj = 100;
350  expmin = 100;
351  expext = 100;
352  rbu::splitNewVersion(ver, maj, min, ext);
353  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
354  CPPUNIT_ASSERT_EQUAL( expmin, min );
355  CPPUNIT_ASSERT_EQUAL( expext, ext );
356 
357  // bad: missing ext
358  ver = "100.100.";
359  expmaj = 100;
360  expmin = 100;
361  expext = 0;
362  rbu::splitNewVersion(ver, maj, min, ext);
363  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
364  CPPUNIT_ASSERT_EQUAL( expmin, min );
365  CPPUNIT_ASSERT_EQUAL( expext, ext );
366 
367  // bad: missing .ext
368  ver = "0.2";
369  expmaj = 0;
370  expmin = 2;
371  expext = 0;
372  rbu::splitNewVersion(ver, maj, min, ext);
373  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
374  CPPUNIT_ASSERT_EQUAL( expmin, min );
375  CPPUNIT_ASSERT_EQUAL( expext, ext );
376 
377  // bad: missing min.ext
378  ver = "100.";
379  expmaj = 100;
380  expmin = 0;
381  expext = 0;
382  rbu::splitNewVersion(ver, maj, min, ext);
383  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
384  CPPUNIT_ASSERT_EQUAL( expmin, min );
385  CPPUNIT_ASSERT_EQUAL( expext, ext );
386 
387  // bad: missing .min.ext
388  ver = "100";
389  expmaj = 100;
390  expmin = 0;
391  expext = 0;
392  rbu::splitNewVersion(ver, maj, min, ext);
393  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
394  CPPUNIT_ASSERT_EQUAL( expmin, min );
395  CPPUNIT_ASSERT_EQUAL( expext, ext );
396 
397  // bad: trailing junk
398  ver = "100.100.100Junk";
399  expmaj = 100;
400  expmin = 100;
401  expext = 100;
402  rbu::splitNewVersion(ver, maj, min, ext);
403  CPPUNIT_ASSERT_EQUAL( expmaj, maj );
404  CPPUNIT_ASSERT_EQUAL( expmin, min );
405  CPPUNIT_ASSERT_EQUAL( expext, ext );
406 
407  STD_TEST_END("");
408 }
409 
410 
412 {
413  STD_TEST_START(getTestName().c_str() << " " );
414 
415  std::ostringstream out;
416  auto_ptr<rbu::IRbuHdr> hdr_152_a09 = checkHdrInfo("hdr_152_a09");
417  out << *hdr_152_a09 << endl;
418 
419  STD_TEST_END("");
420 }
421