// Test that the random number generator actually works by seeing // if it generates non-zero byte streams. There is a small non-zero // probability that a working random number generator will in fact // generate an all-zero stream, but since that is about 1^-128, the // chances of it happening by accident are quite rare. public void TestRNGWorks() { if (!CryptoTestCase.RandomWorks()) { Fail("random number generator does not work"); } }
// Check that "GetNonZeroBytes" does indeed do that. We generate // a large amount of random material to increase the chance that // the underlying generator will generate a zero which needs to // be corrected by "GetNonZeroBytes". If the random number // generator doesn't work at all, we avoid running this test so // that we don't get an infinite loop inside the library. public void TestRNGNonZeroWorks() { if (CryptoTestCase.RandomWorks()) { int round; int index; byte[] rand = new byte [16384]; RandomNumberGenerator rng = RandomNumberGenerator.Create(); for (round = 0; round < 32; ++round) { rng.GetNonZeroBytes(rand); for (index = 0; index < 16; ++index) { if (rand[index] == 0x00) { Fail("zero byte found in generated stream"); } } } } }