This library provides a simple and nice C++ wrapper around these libraries, so that programmers can concentrate on functionality. It offers general support for PCSC-lite, OpenSSL, PKCS#11, plus specific functionality for the SuisseID.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

111 lines
3.5 KiB

/*! @file
@id $Id$
*/
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#include <cryptoki.hxx>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/XmlOutputter.h>
#include <fstream>
using namespace cryptoki;
template <typename T> class SharedPointerInside: public SharedPointer<T> {
public:
SharedPointerInside() {}
SharedPointerInside(const SharedPointerInside& o):
SharedPointer<T>(o) {}
SharedPointerInside(T* p): SharedPointer<T>(p) {}
~SharedPointerInside() {
SmartResource::destruct();
}
SharedPointerInside& operator=(const SharedPointerInside& o) {
return reset(o), *this;
}
SharedPointerInside& operator=(T* p) {
return reset(p), *this;
}
int cnt() {
return SharedPointer<T>::cnt();
}
static int freed() {
return _free;
}
protected:
void free() {
++_free;
SharedPointer<T>::free();
}
private:
static int _free;
};
template<typename T> int SharedPointerInside<T>::_free(0);
class SharedPointerTest: public CppUnit::TestFixture {
public:
void check() {
{
SharedPointerInside<int> i, j(new int);
CPPUNIT_ASSERT_EQUAL(0, SharedPointerInside<int>::freed());
CPPUNIT_ASSERT_EQUAL(1, i.cnt());
CPPUNIT_ASSERT_EQUAL(1, j.cnt());
i = j;
CPPUNIT_ASSERT_EQUAL(1, SharedPointerInside<int>::freed());
CPPUNIT_ASSERT_EQUAL(i.get(), j.get());
CPPUNIT_ASSERT_EQUAL(2, i.cnt());
CPPUNIT_ASSERT_EQUAL(2, j.cnt());
*i = 5;
CPPUNIT_ASSERT_EQUAL(1, SharedPointerInside<int>::freed());
CPPUNIT_ASSERT_EQUAL(5, *j);
CPPUNIT_ASSERT_EQUAL(2, i.cnt());
CPPUNIT_ASSERT_EQUAL(2, j.cnt());
int* old(j.get());
i = new int(6);
CPPUNIT_ASSERT_EQUAL(1, SharedPointerInside<int>::freed());
CPPUNIT_ASSERT_EQUAL(5, *j);
CPPUNIT_ASSERT_EQUAL(6, *i);
CPPUNIT_ASSERT(i.get()!=j.get());
CPPUNIT_ASSERT_EQUAL(1, i.cnt());
CPPUNIT_ASSERT_EQUAL(1, j.cnt());
i = new int(8);
CPPUNIT_ASSERT_EQUAL(2, SharedPointerInside<int>::freed());
CPPUNIT_ASSERT_EQUAL(5, *j);
CPPUNIT_ASSERT_EQUAL(8, *i);
CPPUNIT_ASSERT(i.get()!=j.get());
CPPUNIT_ASSERT_EQUAL(1, i.cnt());
CPPUNIT_ASSERT_EQUAL(1, j.cnt());
{
SharedPointerInside<int> k(j);
CPPUNIT_ASSERT_EQUAL(1, i.cnt());
CPPUNIT_ASSERT_EQUAL(2, j.cnt());
CPPUNIT_ASSERT_EQUAL(2, k.cnt());
}
CPPUNIT_ASSERT_EQUAL(2, SharedPointerInside<int>::freed());
CPPUNIT_ASSERT_EQUAL(1, i.cnt());
CPPUNIT_ASSERT_EQUAL(1, j.cnt());
}
CPPUNIT_ASSERT_EQUAL(4, SharedPointerInside<int>::freed());
}
CPPUNIT_TEST_SUITE(SharedPointerTest);
CPPUNIT_TEST(check);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(SharedPointerTest);
int main(int argc, char** argv) try {
std::ofstream ofs((*argv+std::string(".xml")).c_str());
CppUnit::TextUi::TestRunner runner;
runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), ofs));
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
return runner.run() ? 0 : 1;
} catch (std::exception& e) {
std::cerr<<"***Exception: "<<e.what()<<std::endl;
return 1;
}