diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-10-23 19:39:45 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2017-11-11 08:11:03 +0000 |
commit | cfad4e298f4d65fe26c3a4109d19839bdfcd30c2 (patch) | |
tree | f8fb986455c844a355e634de02e0421cbae59fc6 /tests | |
parent | af456842e13ab83cfeb44f3638b62652b201281c (diff) |
QRandomGenerator: add securelySeeded(), to ensure appropriate seeding
Since we don't document how many bytes one needs (it's 2496), it's
difficult for the caller to provide just enough data in the seed
sequence. Moreover, since std::mt19937 doesn't make it easy to provide
the ideal size either, we can't actually write code that operates
optimally given a quint32 range either -- we only provide it via
std::seed_seq, which is inefficient.
However, we can do it internally by passing QRandomGenerator to the
std::mersenne_twister_engine constructor, as it's designed to work.
Change-Id: Icaa86fc7b54d4b368c0efffd14f0613c10998321
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp b/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp index f9b3ce5390..9c1828d1dd 100644 --- a/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp +++ b/tests/auto/corelib/global/qrandomgenerator/tst_qrandomgenerator.cpp @@ -77,6 +77,7 @@ private slots: void copyingGlobal(); void copyingSystem(); void systemRng(); + void securelySeeding(); void generate32_data(); void generate32(); @@ -161,6 +162,9 @@ void tst_QRandomGenerator::basics() rng = std::move(rng64); rng64 = std::move(rng); + rng = QRandomGenerator64::securelySeeded(); + rng64 = QRandomGenerator::securelySeeded(); + // access global QRandomGenerator *global = QRandomGenerator::global(); QRandomGenerator globalCopy = *global; @@ -254,6 +258,21 @@ void tst_QRandomGenerator::systemRng() #endif } +void tst_QRandomGenerator::securelySeeding() +{ + QRandomGenerator rng1 = QRandomGenerator::securelySeeded(); + QRandomGenerator rng2 = QRandomGenerator::securelySeeded(); + + quint32 samples[20]; + rng1.fillRange(samples); + + // should NOT produce the same sequence, whichever it was + int sameCount = 0; + for (quint32 x : samples) + sameCount += (rng2() == x); + QVERIFY(sameCount < 20); +} + void tst_QRandomGenerator::generate32_data() { QTest::addColumn<uint>("control"); |