SMBIOS Library
testStandalone.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 <iomanip>
23 #include <fstream>
24 
25 #include "testStandalone.h"
26 #include "smbios/SmbiosDefs.h"
27 
28 // specific to unit tests. Users do not need to include this,
29 // so it is not in testStandalone.h
30 #include "smbios/IMemory.h"
31 #include "smbios/ISmi.h"
32 #include "smbios/IObserver.h"
33 
34 #include "smbios/version.h"
35 
36 using namespace std;
37 
38 // Note:
39 // Except for , there are no "using namespace XXXX;" statements
40 // here... on purpose. We want to ensure that while reading this code that
41 // it is extremely obvious where each function is coming from.
42 //
43 // This leads to verbose code in some instances, but that is fine for
44 // these purposes.
45 
46 // Register the test
48 
49 void copyFile( string dstFile, string srcFile )
50 {
51  ifstream src(srcFile.c_str(), ios_base::binary);
52  ofstream dst(dstFile.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);
53 
54  char ch;
55  while( src.get(ch)) dst.put(ch);
56 
57  if( !src.eof() || !dst ) throw exception();
58 }
59 
60 bool fileExists(string fileName)
61 {
62  FILE *fh=0;
63  fh=fopen(fileName.c_str(), "rb");
64  if(!fh)
65  return false;
66 
67  fclose(fh);
68  return true;
69 }
70 
72 {
73  string programDirname = getCppunitTopDirectory();
74  string writeDirectory = getWritableDirectory();
75 
76  // copy the memdump.dat file. We do not write to it, but rw open will fail
77  // if we do not copy it
78  string memdumpOrigFile = programDirname + getTestDirectory() + "/memdump.dat";
79  if(!fileExists(memdumpOrigFile))
80  memdumpOrigFile = getTestDirectory() + "/memdump.dat";
81  string memdumpCopyFile = writeDirectory + "/memdump-copy.dat";
82  copyFile( memdumpCopyFile, memdumpOrigFile );
83 
84  // copy the CMOS file. We are going to write to it and do not wan to mess up
85  // the pristine unit test version
86  string cmosOrigFile = programDirname + getTestDirectory() + "/cmos.dat";
87  if(!fileExists(cmosOrigFile))
88  cmosOrigFile = getTestDirectory() + "/cmos.dat";
89  string cmosCopyFile = writeDirectory + "/cmos-copy.dat";
90  copyFile( cmosCopyFile, cmosOrigFile );
91 
92  // Smi output file.
93  string smiOutput = writeDirectory + "/smi-output.dat";
94 
95  // normal users of the smbios classes need not
96  // set the four parameters below. They should all be set inside the factory
97  // properly by default. We override stuff here to have
98  // the smbios, cmos, etc classes use file dumps instead of
99  // real memory/cmos/etc.
100  smbios::SmbiosFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
103 
104  cmos:: CmosRWFactory::getFactory()->setParameter("cmosMapFile", cmosCopyFile);
106 
107  memory::MemoryFactory::getFactory()->setParameter("memFile", memdumpCopyFile);
109 
110  smi::SmiFactory::getFactory()->setParameter("smiFile", smiOutput);
112 }
113 
115 {
116  // the factory is static. If we do not reset the factory, the next
117  // unit test may accidentally get the wrong objects.
118  // Lifetime rules: CmosTokenTable cannot live longer than the ISmbiosTable
119  // object used in its construction.
121 
123 
125 
127 
129 }
130 
131 //
132 //
133 // TABLE tests
134 //
135 //
136 
138 {}
139 
141 {}
142 
144 {}
145 
147 {}
148 
149 
151 {
152  STD_TEST_START(getTestName().c_str() << " " );
153  // PURPOSE:
154  // The purpose of this test is to test the subscript operator [].
155  // It tests these operators using string and int args and tests
156  // it outside of a loop.
157 
158  // table should not be deleted when we are finished. It is managed by the
159  // factory. Factory will delete it for us when ->reset() is called.
160  smbios::ISmbiosTable *table =
162 
165 
166  item1 = (*table)[smbios::BIOS_Information];
167  item2 = (*table)[smbios::BIOS_Information];
168 
169  // Use CPPUNIT_ASSERT_EQUAL when testing equality.
170  // It gives better diagnostics on failure.
171  CPPUNIT_ASSERT_EQUAL( item1->getHandle(), item2->getHandle() );
172  CPPUNIT_ASSERT_EQUAL( getItemHandle(*item1), item2->getHandle() );
173  CPPUNIT_ASSERT_EQUAL( item1->getLength(), item2->getLength() );
174  CPPUNIT_ASSERT_EQUAL( getItemLength(*item1), item2->getLength() );
175  CPPUNIT_ASSERT_EQUAL( item1->getType() , item2->getType() );
176  CPPUNIT_ASSERT_EQUAL( getItemType(*item1) , item2->getType() );
177 
178  CPPUNIT_ASSERT_EQUAL( (*table)[smbios::BIOS_Information]->getType(), item1->getType() );
179 
180  item1 = (*table)[smbios::System_Information];
181  item2 = (*table)[smbios::System_Information];
182 
183  CPPUNIT_ASSERT_EQUAL( item1->getHandle(), item2->getHandle() );
184  CPPUNIT_ASSERT_EQUAL( getItemHandle(*item1), item2->getHandle() );
185  CPPUNIT_ASSERT_EQUAL( item1->getLength(), item2->getLength() );
186  CPPUNIT_ASSERT_EQUAL( getItemLength(*item1), item2->getLength() );
187  CPPUNIT_ASSERT_EQUAL( item1->getType(), item2->getType() );
188  CPPUNIT_ASSERT_EQUAL( getItemType(*item1) , item2->getType() );
189 
190  CPPUNIT_ASSERT_EQUAL( (*table)[smbios::System_Information]->getType(), item1->getType() );
191 
192 
193  STD_TEST_END("");
194 }
195 
196 void
198 {
199  STD_TEST_START(getTestName().c_str() << " " );
200 
201  // do not delete. Factory manages lifetime.
202  smbios::ISmbiosTable *table =
204 
205  // test streamify() while we are at it.
206  ostringstream ost;
207  int tableEntriesCounted = 0;
208  smbios::ISmbiosTable::iterator item = table->begin();
209  while( item != table->end() )
210  {
211  tableEntriesCounted++;
212  ost << *item << endl;
213  ++item;
214  }
215 
216  CPPUNIT_ASSERT_EQUAL( tableEntriesCounted, table->getNumberOfEntries() );
217  STD_TEST_END("");
218 }
219 
220 void
222 {
223  STD_TEST_START(getTestName().c_str() << " " );
224 
225  // do not delete. Factory manages lifetime.
226  const smbios::ISmbiosTable *table =
228 
229  const smbios::ISmbiosTable *constTable = &*table;
230 
231  int tableEntriesCounted = 0;
232  smbios::ISmbiosTable::const_iterator item = constTable->begin();
233  while( item != constTable->end() )
234  {
235  tableEntriesCounted++;
236  (void) *item; // do this to sniff out possible errors with deref iterator.
237 
238  ++item;
239  }
240  CPPUNIT_ASSERT_EQUAL( tableEntriesCounted, constTable->getNumberOfEntries() );
241  STD_TEST_END("");
242 }
243 
244 void
246 {
247  STD_TEST_START(getTestName().c_str() << " " );
248 
249  // do not delete. Factory manages lifetime.
250  const smbios::ISmbiosTable *table =
252 
253  int tableEntriesCounted = 0;
254  // table[-1] is special, it returns all objects.
255  // This test should be identical to testConstIterator (both walk all entries)
256  for( smbios::ISmbiosTable::const_iterator item = (*table)[-1] ; item != table->end(); ++item)
257  {
258  tableEntriesCounted++;
259  (void) *item; // do this to sniff out possible errors with deref iterator.
260  }
261  CPPUNIT_ASSERT_EQUAL( table->getNumberOfEntries(), tableEntriesCounted );
262  STD_TEST_END("");
263 }
264 
265 void
267 {
268  STD_TEST_START(getTestName().c_str() << " " );
269 
270 // it turns out that 8450 has 3 BIOS Information blocks.
271 
272 // // do not delete. Factory manages lifetime.
273 // smbios::ISmbiosTable *table =
274 // smbios::SmbiosFactory::getFactory()->getSingleton();
275 //
276 // // There should probably be only one "BIOS Information" block. Check.
277 // // update this test if it turns out that there are actual systems with >1 Bios Info block
278 // //
279 // int tableEntriesCounted = 0;
280 // for( smbios::ISmbiosTable::iterator item = (*table)[0] ; item != table->end(); ++item)
281 // {
282 // (void) *item; // do this to sniff out possible errors with deref iterator.
283 // tableEntriesCounted++;
284 // }
285 // CPPUNIT_ASSERT_EQUAL( 1, tableEntriesCounted );
286 
287  STD_TEST_END("");
288 }
289 
290 void
292 {
293  STD_TEST_START(getTestName().c_str() << " " );
294 
295  // do not delete. Factory manages lifetime.
296  const smbios::ISmbiosTable *table =
298 
299  // There should normally be more than one "Port Connector" block. Check.
300  // "Port Connector" block is type == 8
301  // memdump-opti.txt has 12
302  // memdump-PAweb110.txt has 17
303  // memdump-PAweb110.txt has 9 *unverified...
304  // update this test if it turns out that there are actual systems with <2 Port Connector blocks
305  //
306  int tableEntriesCounted = 0;
307  for( smbios::ISmbiosTable::const_iterator item = (*table)[8] ; item != table->end(); ++item)
308  {
309  (void) *item; // do this to sniff out possible errors with deref iterator.
310  tableEntriesCounted++;
311  }
312  CPPUNIT_ASSERT( 1 < tableEntriesCounted );
313  STD_TEST_END("");
314 }
315 
316 
317 
318 //
319 //
320 // ITEM tests
321 //
322 //
323 
325 {
326  STD_TEST_START(getTestName().c_str() << " " );
327 
328  // BEGIN EXAMPLE iterator
329  // table should not be deleted when we are finished. It is managed by the
330  // factory. Factory will delete it for us when ->reset() is called.
331  smbios::ISmbiosTable *table =
333 
334  ostringstream ost;
335  ost << *table << endl;
336 
337  // iterate over all PORT INFORMATION BLOCKS
338  for( smbios::ISmbiosTable::iterator item = (*table)[8] ; item != table->end(); ++item)
339  {
340  ost << *item << endl;
341  }
342  // END EXAMPLE iterator
343 
344  STD_TEST_END("");
345 }
346 
347 void
349 {
350  STD_TEST_START(getTestName().c_str() << " " );
351  // test that when we grab things out of the
352  // table, we get copies of the same thing
353  // when we ask for the same thing, rather than
354  // separate items with the same data.
355  //
356  // we use references below to make the CPPUNIT_ASSERT
357  // a bit easier to read.
358 
359  // do not delete. Factory manages lifetime.
360  const smbios::ISmbiosTable *table =
362 
363  // grab the first bios block
365  const smbios::ISmbiosItem &item1 = *position1; //reference
366 
367  // use another iterator and grab another
369  const smbios::ISmbiosItem &item2 = *position2; //reference
370 
371  // Check that they both gave the same thing
372  // The address of each should be equal
373  CPPUNIT_ASSERT_EQUAL( &item1, &item2 ); // easiest to read
374  CPPUNIT_ASSERT_EQUAL( &(*position1), &item2 ); // same test, written differently
375  CPPUNIT_ASSERT_EQUAL( &item1 , &(*position2) ); // same test, written differently
376  CPPUNIT_ASSERT_EQUAL( &(*position1), &(*position2) ); // same test, written differently
377 
378  // use another iterator and grab another _different_ pointer.
380  const smbios::ISmbiosItem &item3 = *position3; //reference
381 
382  // Check that these are different...
383  CPPUNIT_ASSERT( &item1 != &item3 );
384  CPPUNIT_ASSERT( &item2 != &item3 );
385 
386  CPPUNIT_ASSERT_EQUAL( item1.getType(), static_cast<u8>(smbios::BIOS_Information) );
387  CPPUNIT_ASSERT_EQUAL( item2.getType(), static_cast<u8>(smbios::BIOS_Information) );
388  CPPUNIT_ASSERT_EQUAL( item3.getType(), static_cast<u8>(smbios::System_Information) );
389 
390  STD_TEST_END("");
391 }
392 
393 void
395 {
396  STD_TEST_START(getTestName().c_str() << " " );
397  // test getU{8|16}_FromItem() functions
398  // no way to test in generic table the getU32
399  // or getU64
400  //
401  // primarily to ensure that getUx_FromItem() has the endianness correct
402 
403  // do not delete. Factory manages lifetime.
404  const smbios::ISmbiosTable *table =
406 
408  while( item != table->end() )
409  {
410  u8 type1 = item->getType();
411  u8 type2 = getU8_FromItem(*item, 0);
412  CPPUNIT_ASSERT_EQUAL( type1, type2 );
413 
414  u8 len1 = item->getLength();
415  u8 len2 = getU8_FromItem(*item, 1);
416  CPPUNIT_ASSERT_EQUAL( len1, len2 );
417 
418  u16 handle1 = item->getHandle();
419  u16 handle2 = getU16_FromItem(*item, 2);
420  CPPUNIT_ASSERT_EQUAL( handle1, handle2 );
421 
422  ++item;
423  }
424 
425  STD_TEST_END("");
426 }
427 
429 {
430  STD_TEST_START(getTestName().c_str() << " " );
431 
433  factory->reset();
434 
435  //
436  // This is now a NON-XML factory.
437  // This call nails the _instance variable to a regular non-xml factory
438  // instance. Subsequent setup calls in setUp ensure it is properly set
439  // with the correct paths and whatnot.
440  tearDown();
442  setUp();
443 
444  // Ok, none of these are related to construction offset, but it is good to
445  // run these tests while we have a handy reference to a NON-XML factory.
446  auto_ptr<smbios::ISmbiosTable>p(factory->makeNew());
447  auto_ptr<const smbios::ISmbiosTable>q(factory->makeNew());
448 
451 
452  // test streamify() for non-XML SmbiosItem class while we are here.
453  ostringstream ost;
454  ost << *item1 << endl;
455 
456  CPPUNIT_ASSERT_EQUAL( item1->getHandle(), item2->getHandle() );
457  CPPUNIT_ASSERT_EQUAL( item1->getHandle(), getU16_FromItem(*item2, 2) );
458  CPPUNIT_ASSERT_EQUAL( item1->getLength(), item2->getLength() );
459  CPPUNIT_ASSERT_EQUAL( item1->getLength(), getU8_FromItem(*item2, 1) );
460  CPPUNIT_ASSERT_EQUAL( item1->getType() , item2->getType() );
461  CPPUNIT_ASSERT_EQUAL( item1->getType() , getU8_FromItem(*item2, 0) );
462 
463  STD_TEST_END("");
464 }
465 
467 {
468  STD_TEST_START(getTestName().c_str() << " " );
469  // do not delete. Factory manages lifetime.
470  const smbios::ISmbiosTable *table =
472 
473  // test if "PCI Supported" bit is set, should always be set.
475  CPPUNIT_ASSERT_EQUAL( isBitSet( &(*item), 0xA, 0x7 ), true );
476 
477  STD_TEST_END("");
478 }
479 
480 
481 
482 void
484 {
485  STD_TEST_START(getTestName().c_str() << " " );
486 
487  // do not delete. Factory manages lifetime.
488  const smbios::ISmbiosTable *table =
490 
491  for( smbios::ISmbiosTable::const_iterator item = (*table)[-1] ; item != table->end(); ++item)
492  {
493  int len = item->getLength();
494 
495  // none of these should throw.
496  (void) getU8_FromItem (*item, len - 1);
497  (void) getU16_FromItem(*item, len - 2);
498  (void) getU32_FromItem(*item, len - 4);
499 
500  // not all items are large enough. only attempt if item is at least 8 bytes
501  if( len >= 8 )
502  (void) getU64_FromItem(*item, len - 8);
503 
504  ASSERT_THROWS( getU8_FromItem (*item, len - 0), smbios::DataOutOfBounds);
505  ASSERT_THROWS( getU16_FromItem(*item, len - 1), smbios::DataOutOfBounds);
506  ASSERT_THROWS( getU32_FromItem(*item, len - 3), smbios::DataOutOfBounds);
507  ASSERT_THROWS( getU64_FromItem(*item, len - 7), smbios::DataOutOfBounds);
508  }
509 
510  STD_TEST_END("");
511 }
512 
513 
514 
515 //
516 // Memory tests
517 //
518 void
520 {
521  STD_TEST_START(getTestName().c_str() << " " );
522 
524 
525  memFact->setParameter("memFile", "");
526  ASSERT_THROWS( memFact->makeNew() , memory::AccessError);
527 
528  memFact->setParameter("memFile", "nonexistentfile");
529  ASSERT_THROWS( memFact->makeNew() , memory::AccessError);
530 
532  ASSERT_THROWS( memFact->makeNew(), smbios::NotImplemented);
533 
535 
536  STD_TEST_END("");
537 }
538 
539 void
541 {
542  STD_TEST_START(getTestName().c_str() << " " );
543 
545 
546  memory::IMemory *mem = memFact->getSingleton();
547 
548  mem->getByte( 0 );
549 
550  ASSERT_THROWS( mem->getByte( 0x10000000UL ), memory::AccessError);
551 
552  mem->putByte( 0, 254 );
553  int b = mem->getByte( 0 );
554 
555  CPPUNIT_ASSERT_EQUAL ( b, 254 );
556  STD_TEST_END("");
557 }
558 
559 
560 
561 
562 //
563 // CMOS Token
564 //
565 
566 
567 void
569 {
570  STD_TEST_START(getTestName().c_str() << " " );
571 
572  smbios::TokenTableFactory *ttFactory;
574  smbios::ITokenTable *tokenTable = ttFactory->getSingleton();
575 
576  ostringstream ost;
577 
578  smbios::ITokenTable::iterator token = tokenTable->begin();
579  while( token != tokenTable->end() )
580  {
581  ost << *token << endl;
582  ++token;
583  }
584 
585  STD_TEST_END("");
586 }
587 
588 
589 //
590 // SMI Tests
591 //
592 
593 void
595 {
596  STD_TEST_START(getTestName().c_str() << " " );
597 
598  std::auto_ptr<smi::IDellCallingInterfaceSmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI);
599 
600  smi->setClass( 0xAABB );
601  smi->setSelect( 0xCCDD );
602  smi->setArg( 0, 0xA1A2A3A4 );
603  smi->setArg( 1, 0xB1B2B3B4 );
604  smi->setArg( 2, 0xC1C2C3C4 );
605  smi->setArg( 3, 0xD1D2D3D4 );
606  try
607  { /* This is expected to fail in unit test, no good way to simulate them*/
608  smi->execute();
609  }
610  catch( const smi::UnhandledSmi & ) {}
611 
612  STD_TEST_END("");
613 }
614 
615 void
617 {
618  STD_TEST_START(getTestName().c_str() << " " );
619 
620  std::auto_ptr<smi::IDellCallingInterfaceSmi> smi = smi::SmiFactory::getFactory()->makeNew(smi::SmiFactory::DELL_CALLING_INTERFACE_SMI);
621  smi->setClass( 0xAABB );
622  smi->setSelect( 0xCCDD );
623  smi->setArgAsPhysicalAddress(0, 0);
624  smi->setArgAsPhysicalAddress(1, 1);
625  smi->setArgAsPhysicalAddress(2, 2);
626  smi->setArgAsPhysicalAddress(3, 3);
627  try
628  { /* This is expected to fail in unit test, no good way to simulate them*/
629  smi->execute();
630  }
631  catch( const smi::UnhandledSmi & ) {}
632 
633  STD_TEST_END("");
634 }
635 
636 
637 
638 string safeConvertToString( const char *str )
639 {
640  string fromSystem = "";
641  if( 0 != str )
642  {
643  fromSystem = str;
644  }
645  SMBIOSFreeMemory(str);
646  return fromSystem;
647 }
648 
649 void
651 {
652  STD_TEST_START(getTestName().c_str() << " " );
653 
654  string fromSystem = SMBIOSGetLibraryVersionString();
655  string testInput = LIBSMBIOS_RELEASE_VERSION;
656 
657  CPPUNIT_ASSERT_EQUAL ( testInput, fromSystem );
658  STD_TEST_END("");
659 }
660 
661 void
663 {
664  STD_TEST_START(getTestName().c_str() << " " );
665  std::string actual = "";
667  string source = "";
668  string expected = "";
669  foo.setParameter("foo", "happy");
670  foo.setParameter("bar", 42);
671  foo.setParameter("recursive", "%(foo)s");
672  foo.setParameter("recursiverecursive", "%(recursive)s");
673 
674  source = "The %% cat is %(foo)s. The best number is %(bar)i. %";
675  expected = "The % cat is happy. The best number is 42. %";
676  foo.setMessageString( source );
677  actual = foo.what();
678  CPPUNIT_ASSERT_EQUAL( expected, actual );
679 
680  // test copy constructor
682  actual = bar.what();
683  CPPUNIT_ASSERT_EQUAL( expected, actual );
684 
685  source = "The %% cat is %(recursive)s. The best number is %(bar)i. %";
686  foo.setMessageString( source );
687  actual = foo.what();
688  CPPUNIT_ASSERT_EQUAL( expected, actual );
689 
690  source = "The %% cat is %(recursiverecursive)s. The best number is %(bar)i. %";
691  foo.setMessageString( source );
692  actual = foo.what();
693  CPPUNIT_ASSERT_EQUAL( expected, actual );
694 
695  source = "The %% cat %is %(recursiverecursive)s. The best number is %(bar)i. %";
696  expected = "The % cat %is happy. The best number is 42. %";
697  foo.setMessageString( source );
698  actual = foo.what();
699  CPPUNIT_ASSERT_EQUAL( expected, actual );
700 
701  source = " %(a_really_long_variable_longer_than_32_characters)s";
702  expected = " %(a_really_long_variable_longer_than_32_characters)s";
703  foo.setMessageString( source );
704  actual = foo.what();
705  CPPUNIT_ASSERT_EQUAL( expected, actual );
706 
707  source = " %(no_closing_paren";
708  expected = " %(no_closing_paren";
709  foo.setMessageString( source );
710  actual = foo.what();
711  CPPUNIT_ASSERT_EQUAL( expected, actual );
712 
713  source = " %(a_var_with_no_type)";
714  expected = " %(a_var_with_no_type)";
715  foo.setMessageString( source );
716  actual = foo.what();
717  CPPUNIT_ASSERT_EQUAL( expected, actual );
718 
719  source = " %(a_var_with_no_type) ";
720  expected = " %(a_var_with_no_type) ";
721  foo.setMessageString( source );
722  actual = foo.what();
723  CPPUNIT_ASSERT_EQUAL( expected, actual );
724 
725  source = " %";
726  expected = " %";
727  foo.setMessageString( source );
728  actual = foo.what();
729  CPPUNIT_ASSERT_EQUAL( expected, actual );
730 
731  STD_TEST_END("");
732 }
733