diff options
author | Jay Berkenbilt <ejb@ql.org> | 2013-11-30 18:02:56 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2013-12-14 21:17:35 +0100 |
commit | 5e3bad2f86665b35155095b91a2d672fc7335870 (patch) | |
tree | 984d4830ee0dd3b8e90bae913f411a84c8e876d4 /libtests/random.cc | |
parent | e9a319fb9536347aeab076cdb18e1ff97eb66c07 (diff) | |
download | qpdf-5e3bad2f86665b35155095b91a2d672fc7335870.tar.zst |
Refactor random data generation
Add new RandomDataProvider object and implement existing random number
generation in terms of that. This enables end users to supply their
own random data providers.
Diffstat (limited to 'libtests/random.cc')
-rw-r--r-- | libtests/random.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/libtests/random.cc b/libtests/random.cc new file mode 100644 index 00000000..644fdd91 --- /dev/null +++ b/libtests/random.cc @@ -0,0 +1,65 @@ +#include <qpdf/QUtil.hh> +#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() +{ + 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"; + } + 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"; + } + BogusRandomDataProvider brdp; + QUtil::setRandomDataProvider(&brdp); + 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"; + } + std::cout << "random: end of tests\n"; + return 0; +} |