From 374b8a419f69291ebc13302aa10bc773194129e4 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sat, 6 May 2017 11:37:22 -0400 Subject: Added additional tests --- validat1.cpp | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 186 insertions(+), 20 deletions(-) (limited to 'validat1.cpp') diff --git a/validat1.cpp b/validat1.cpp index 6ff27693..3659b539 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -453,6 +453,31 @@ bool TestOS_RNG() else std::cout << "passed:"; std::cout << " " << total << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; + + try + { + // Miscellaneous for code coverage + RandomNumberGenerator& prng = *rng.get(); + (void)prng.AlgorithmName(); + word32 result = prng.GenerateWord32(); + result = prng.GenerateWord32(21, 0xffffffff - 21); + prng.GenerateBlock(reinterpret_cast(&result), 4); + prng.GenerateBlock(reinterpret_cast(&result), 3); + prng.GenerateBlock(reinterpret_cast(&result), 2); + prng.GenerateBlock(reinterpret_cast(&result), 1); + prng.GenerateBlock(reinterpret_cast(&result), 0); + pass = true; + } + catch (const Exception&) + { + pass = false; + } + + if (!pass) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; } else std::cout << "\nNo operating system provided blocking random number generator, skipping test." << std::endl; @@ -478,6 +503,31 @@ bool TestOS_RNG() else std::cout << "passed:"; std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; + + try + { + // Miscellaneous for code coverage + RandomNumberGenerator& prng = *rng.get(); + (void)prng.AlgorithmName(); + word32 result = prng.GenerateWord32(); + result = prng.GenerateWord32(21, 0xffffffff - 21); + prng.GenerateBlock(reinterpret_cast(&result), 4); + prng.GenerateBlock(reinterpret_cast(&result), 3); + prng.GenerateBlock(reinterpret_cast(&result), 2); + prng.GenerateBlock(reinterpret_cast(&result), 1); + prng.GenerateBlock(reinterpret_cast(&result), 0); + pass = true; + } + catch (const Exception&) + { + pass = false; + } + + if (!pass) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; } else std::cout << "\nNo operating system provided nonblocking random number generator, skipping test." << std::endl; @@ -502,7 +552,7 @@ bool TestAutoSeeded() AutoSeededRandomPool prng; static const unsigned int ENTROPY_SIZE = 32; - bool generate = true, discard = true, incorporate = false; + bool generate = true, discard = true, incorporate = false, crop = false; MeterFilter meter(new Redirector(TheBitBucket())); RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter))); @@ -554,9 +604,33 @@ bool TestAutoSeeded() std::cout << "FAILED:"; else std::cout << "passed:"; - std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << std::endl; + std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes\n"; + + try + { + // Miscellaneous for code coverage + (void)prng.AlgorithmName(); + word32 result = prng.GenerateWord32(); + result = prng.GenerateWord32(21, 0xffffffff - 21); + prng.GenerateBlock(reinterpret_cast(&result), 4); + prng.GenerateBlock(reinterpret_cast(&result), 3); + prng.GenerateBlock(reinterpret_cast(&result), 2); + prng.GenerateBlock(reinterpret_cast(&result), 1); + prng.GenerateBlock(reinterpret_cast(&result), 0); + crop = true; + } + catch (const Exception&) + { + crop = false; + } + + if (!crop) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; - return generate && discard && incorporate; + return generate && discard && incorporate && crop; } bool TestAutoSeededX917() @@ -566,7 +640,7 @@ bool TestAutoSeededX917() AutoSeededX917RNG prng; static const unsigned int ENTROPY_SIZE = 32; - bool generate = true, discard = true, incorporate = false; + bool generate = true, discard = true, incorporate = false, crop = false; MeterFilter meter(new Redirector(TheBitBucket())); RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter))); @@ -618,9 +692,33 @@ bool TestAutoSeededX917() std::cout << "FAILED:"; else std::cout << "passed:"; - std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << std::endl; + std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes\n"; - return generate && discard && incorporate; + try + { + // Miscellaneous for code coverage + (void)prng.AlgorithmName(); + word32 result = prng.GenerateWord32(); + result = prng.GenerateWord32(21, 0xffffffff - 21); + prng.GenerateBlock(reinterpret_cast(&result), 4); + prng.GenerateBlock(reinterpret_cast(&result), 3); + prng.GenerateBlock(reinterpret_cast(&result), 2); + prng.GenerateBlock(reinterpret_cast(&result), 1); + prng.GenerateBlock(reinterpret_cast(&result), 0); + crop = true; + } + catch (const Exception&) + { + crop = false; + } + + if (!crop) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; + + return generate && discard && incorporate && crop; } #endif // NO_OS_DEPENDENCE @@ -630,7 +728,7 @@ bool TestMersenne() std::cout << "\nTesting Mersenne Twister...\n\n"; static const unsigned int ENTROPY_SIZE = 32; - bool equal = true, generate = true, discard = true, incorporate = false; + bool equal = true, generate = true, discard = true, incorporate = false, crop = false; // First 10; http://create.stephan-brumme.com/mersenne-twister/ word32 result[10], expected[10] = {0xD091BB5C, 0x22AE9EF6, @@ -702,9 +800,33 @@ bool TestMersenne() std::cout << "FAILED:"; else std::cout << "passed:"; - std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << std::endl; + std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes\n"; + + try + { + // Miscellaneous for code coverage + (void)prng.AlgorithmName(); + result[0] = prng.GenerateWord32(); + result[0] = prng.GenerateWord32(21, 0xffffffff - 21); + prng.GenerateBlock(reinterpret_cast(&result[0]), 4); + prng.GenerateBlock(reinterpret_cast(&result[0]), 3); + prng.GenerateBlock(reinterpret_cast(&result[0]), 2); + prng.GenerateBlock(reinterpret_cast(&result[0]), 1); + prng.GenerateBlock(reinterpret_cast(&result[0]), 0); + crop = true; + } + catch (const Exception&) + { + crop = false; + } + + if (!crop) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; - return equal && generate && discard && incorporate; + return equal && generate && discard && incorporate && crop; } #endif @@ -713,7 +835,7 @@ bool TestRDRAND() { // Testing on 6th generation i7 shows RDRAND needs less than 8 retries for 10K bytes. RDRAND rdrand; - bool entropy = true, compress = true, discard = true; + bool entropy = true, compress = true, discard = true, crop = true; static const unsigned int SIZE = 10000; if (HasRDRAND()) @@ -773,12 +895,34 @@ bool TestRDRAND() else std::cout << "\nRDRAND generator not available, skipping test.\n"; - // Squash code coverage warnings on unused functions - (void)rdrand.AlgorithmName(); - (void)rdrand.CanIncorporateEntropy(); - rdrand.IncorporateEntropy(NULLPTR, 0); + try + { + // Miscellaneous for code coverage + (void)rdrand.AlgorithmName(); + (void)rdrand.CanIncorporateEntropy(); + rdrand.IncorporateEntropy(NULLPTR, 0); - return entropy && compress && discard; + word32 result = rdrand.GenerateWord32(); + result = rdrand.GenerateWord32(21, 0xffffffff - 21); + rdrand.GenerateBlock(reinterpret_cast(&result), 4); + rdrand.GenerateBlock(reinterpret_cast(&result), 3); + rdrand.GenerateBlock(reinterpret_cast(&result), 2); + rdrand.GenerateBlock(reinterpret_cast(&result), 1); + rdrand.GenerateBlock(reinterpret_cast(&result), 0); + crop = true; + } + catch (const Exception&) + { + crop = false; + } + + if (!crop) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; + + return entropy && compress && discard && crop; } #endif @@ -788,7 +932,7 @@ bool TestRDSEED() // Testing on 5th generation i5 shows RDSEED needs about 128 retries for 10K bytes // on 64-bit/amd64 VM, and it needs more for an 32-bit/i686 VM. RDSEED rdseed; - bool entropy = true, compress = true, discard = true; + bool entropy = true, compress = true, discard = true, crop = true; static const unsigned int SIZE = 10000; if (HasRDSEED()) @@ -848,10 +992,32 @@ bool TestRDSEED() else std::cout << "\nRDSEED generator not available, skipping test.\n"; - // Squash code coverage warnings on unused functions - (void)rdseed.AlgorithmName(); - (void)rdseed.CanIncorporateEntropy(); - rdseed.IncorporateEntropy(NULLPTR, 0); + try + { + // Miscellaneous for code coverage + (void)rdseed.AlgorithmName(); + (void)rdseed.CanIncorporateEntropy(); + rdseed.IncorporateEntropy(NULLPTR, 0); + + word32 result = rdseed.GenerateWord32(); + result = rdseed.GenerateWord32(21, 0xffffffff - 21); + rdseed.GenerateBlock(reinterpret_cast(&result), 4); + rdseed.GenerateBlock(reinterpret_cast(&result), 3); + rdseed.GenerateBlock(reinterpret_cast(&result), 2); + rdseed.GenerateBlock(reinterpret_cast(&result), 1); + rdseed.GenerateBlock(reinterpret_cast(&result), 0); + crop = true; + } + catch (const Exception&) + { + crop = false; + } + + if (!crop) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; return entropy && compress && discard; } -- cgit v1.2.1