blob: afd2ed6c572ac041740c4679befaf6224fd0bd97 (
plain) (
blame)
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
|
#include <qpdf/InsecureRandomDataProvider.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/SecureRandomDataProvider.hh>
#include <iostream>
class BogusRandomDataProvider: public RandomDataProvider
{
public:
~BogusRandomDataProvider() override = default;
BogusRandomDataProvider() = default;
void
provideRandomData(unsigned char* data, size_t len) override
{
for (size_t i = 0; i < len; ++i) {
data[i] = static_cast<unsigned char>(i & 0xff);
}
}
};
int
main()
{
RandomDataProvider* orig_rdp = QUtil::getRandomDataProvider();
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";
}
#ifndef SKIP_OS_SECURE_RANDOM
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";
}
#endif
BogusRandomDataProvider brdp;
QUtil::setRandomDataProvider(&brdp);
if (QUtil::getRandomDataProvider() != &brdp) {
std::cout << "fail: getRandomDataProvider didn't"
" return our provider\n";
}
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";
}
QUtil::setRandomDataProvider(nullptr);
if (QUtil::getRandomDataProvider() != orig_rdp) {
std::cout << "fail: passing null to setRandomDataProvider "
"didn't reset the random data provider\n";
}
std::cout << "random: end of tests\n";
return 0;
}
|