2013-11-30 17:02:56 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2013-11-30 17:25:01 +00:00
|
|
|
#include <qpdf/qpdf-config.h>
|
2013-11-30 17:02:56 +00:00
|
|
|
#include <qpdf/InsecureRandomDataProvider.hh>
|
|
|
|
#include <qpdf/SecureRandomDataProvider.hh>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
class BogusRandomDataProvider: public RandomDataProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~BogusRandomDataProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
BogusRandomDataProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void provideRandomData(unsigned char* data, size_t len)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
data[i] = static_cast<unsigned char>(i & 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2013-12-16 21:03:54 +00:00
|
|
|
RandomDataProvider* orig_rdp = QUtil::getRandomDataProvider();
|
2013-11-30 17:02:56 +00:00
|
|
|
long r1 = QUtil::random();
|
|
|
|
long r2 = QUtil::random();
|
|
|
|
if (r1 == r2)
|
|
|
|
{
|
|
|
|
std::cout << "fail: two randoms were the same\n";
|
|
|
|
}
|
|
|
|
InsecureRandomDataProvider irdp;
|
|
|
|
irdp.provideRandomData(reinterpret_cast<unsigned char*>(&r1), 4);
|
|
|
|
irdp.provideRandomData(reinterpret_cast<unsigned char*>(&r2), 4);
|
|
|
|
if (r1 == r2)
|
|
|
|
{
|
|
|
|
std::cout << "fail: two insecure randoms were the same\n";
|
|
|
|
}
|
2013-11-30 17:25:01 +00:00
|
|
|
#ifndef SKIP_OS_SECURE_RANDOM
|
2013-11-30 17:02:56 +00:00
|
|
|
SecureRandomDataProvider srdp;
|
|
|
|
srdp.provideRandomData(reinterpret_cast<unsigned char*>(&r1), 4);
|
|
|
|
srdp.provideRandomData(reinterpret_cast<unsigned char*>(&r2), 4);
|
|
|
|
if (r1 == r2)
|
|
|
|
{
|
|
|
|
std::cout << "fail: two secure randoms were the same\n";
|
|
|
|
}
|
2013-11-30 17:25:01 +00:00
|
|
|
#endif
|
2013-11-30 17:02:56 +00:00
|
|
|
BogusRandomDataProvider brdp;
|
|
|
|
QUtil::setRandomDataProvider(&brdp);
|
2013-12-16 21:03:54 +00:00
|
|
|
if (QUtil::getRandomDataProvider() != &brdp)
|
|
|
|
{
|
|
|
|
std::cout << "fail: getRandomDataProvider didn't"
|
|
|
|
" return our provider\n";
|
|
|
|
}
|
2013-11-30 17:02:56 +00:00
|
|
|
r1 = QUtil::random();
|
|
|
|
r2 = QUtil::random();
|
|
|
|
if (r1 != r2)
|
|
|
|
{
|
|
|
|
std::cout << "fail: two bogus randoms were different\n";
|
|
|
|
}
|
|
|
|
unsigned char buf[4];
|
|
|
|
QUtil::initializeWithRandomBytes(buf, 4);
|
|
|
|
if (! ((buf[0] == 0) &&
|
|
|
|
(buf[1] == 1) &&
|
|
|
|
(buf[2] == 2) &&
|
|
|
|
(buf[3] == 3)))
|
|
|
|
{
|
|
|
|
std::cout << "fail: bogus random didn't provide correct bytes\n";
|
|
|
|
}
|
2013-12-16 21:03:54 +00:00
|
|
|
QUtil::setRandomDataProvider(0);
|
|
|
|
if (QUtil::getRandomDataProvider() != orig_rdp)
|
|
|
|
{
|
|
|
|
std::cout << "fail: passing null to setRandomDataProvider "
|
|
|
|
"didn't reset the random data provider\n";
|
|
|
|
}
|
2013-11-30 17:02:56 +00:00
|
|
|
std::cout << "random: end of tests\n";
|
|
|
|
return 0;
|
|
|
|
}
|