Author: nextgens
Date: 2008-11-22 16:15:11 +0000 (Sat, 22 Nov 2008)
New Revision: 23805
Modified:
trunk/freenet/src/freenet/crypt/Yarrow.java
trunk/freenet/test/freenet/crypt/YarrowTest.java
Log:
Yarrow: reinstanciate the unit tests... add a new one and maybe fix the seeding
problem
Modified: trunk/freenet/src/freenet/crypt/Yarrow.java
===================================================================
--- trunk/freenet/src/freenet/crypt/Yarrow.java 2008-11-22 16:14:29 UTC (rev
23804)
+++ trunk/freenet/src/freenet/crypt/Yarrow.java 2008-11-22 16:15:11 UTC (rev
23805)
@@ -99,7 +99,7 @@
else
seedfile = null;
if(reseedOnStartup) {
- entropy_init(seed);
+ entropy_init(seed, reseedOnStartup);
seedFromExternalStuff(canBlock);
/**
* If we don't reseed at this point, we will be
predictable,
@@ -184,26 +184,27 @@
consumeString(Long.toHexString(Runtime.getRuntime().totalMemory()));
}
- private void entropy_init(File seed) {
- Properties sys = System.getProperties();
- EntropySource startupEntropy = new EntropySource();
+ private void entropy_init(File seed, boolean reseedOnStartup) {
+ if(reseedOnStartup) {
+ Properties sys = System.getProperties();
+ EntropySource startupEntropy = new EntropySource();
- // Consume the system properties list
- for(Enumeration<?> enu = sys.propertyNames();
enu.hasMoreElements();) {
- String key = (String) enu.nextElement();
- consumeString(key);
- consumeString(sys.getProperty(key));
- }
+ // Consume the system properties list
+ for(Enumeration<?> enu = sys.propertyNames();
enu.hasMoreElements();) {
+ String key = (String) enu.nextElement();
+ consumeString(key);
+ consumeString(sys.getProperty(key));
+ }
- // Consume the local IP address
- try {
- consumeString(InetAddress.getLocalHost().toString());
- } catch(Exception e) {
- // Ignore
+ // Consume the local IP address
+ try {
+
consumeString(InetAddress.getLocalHost().toString());
+ } catch(Exception e) {
+ // Ignore
+ }
+ readStartupEntropy(startupEntropy);
}
- readStartupEntropy(startupEntropy);
-
read_seed(seed);
}
Modified: trunk/freenet/test/freenet/crypt/YarrowTest.java
===================================================================
--- trunk/freenet/test/freenet/crypt/YarrowTest.java 2008-11-22 16:14:29 UTC
(rev 23804)
+++ trunk/freenet/test/freenet/crypt/YarrowTest.java 2008-11-22 16:15:11 UTC
(rev 23805)
@@ -24,66 +24,78 @@
(byte)0x1F, (byte)0xBD, (byte)0x91, (byte)0xEA, (byte)0xA2,
(byte)0xCD, (byte)0xD0, (byte)0xEB, (byte)0x37, (byte)0xF4
};
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ FileWriter fw = new FileWriter(SEED_FILE);
+ for(int i = 0; i < 256; i++)
+ fw.write(i);
+ fw.flush();
+ fw.close();
+ }
- public void testIt() {
- assertTrue(true);
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ assertTrue(SEED_FILE.delete());
}
+
+ public void testConsistencySeedFromFile() throws
NoSuchAlgorithmException, UnsupportedEncodingException {
+ Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
+ MessageDigest md = MessageDigest.getInstance("SHA-1");
+
+ byte[] bytes = new byte[SEED_SIZE];
+
+ y.nextBytes(bytes);
+ md.update(bytes);
+
+ bytes = md.digest();
+ assertEquals(new String(bytes, "UTF-8"), new
String(SEED_OUTPUT_YARROW_FILE, "UTF-8"));
+ }
+
+ public void testDouble() {
+ Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
+ ScalarSampleStatistics sample = new ScalarSampleStatistics();
+
+ for(int i = 0; i < 10000; ++i) {
+ sample.add(y.nextDouble());
+ }
+
+ assertEquals(0.5, sample.getMean(), 0.02);
+ assertEquals(1.0 / (2.0 * Math.sqrt(3.0)),
sample.getStandardDeviation(), 0.002);
+ }
+
+ public void testNextInt() {
+ Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
+ for(int n = 1; n < 20; ++n) {
+ int[] count = new int[n];
+ for(int k = 0; k < 10000; ++k) {
+ int l = y.nextInt(n);
+ ++count[l];
+ assertTrue(l >= 0);
+ assertTrue(l < n);
+ }
+ for(int i = 0; i < n; ++i) {
+ assertTrue(n * count[i] > 8800);
+ assertTrue(n * count[i] < 11100);
+ }
+ }
+ }
-// @Override
-// protected void setUp() throws Exception {
-// super.setUp();
-// FileWriter fw = new FileWriter(SEED_FILE);
-// for(int i = 0; i < 256; i++)
-// fw.write(i);
-// fw.flush();
-// fw.close();
-// }
-//
-// @Override
-// protected void tearDown() throws Exception {
-// super.tearDown();
-// assertTrue(SEED_FILE.delete());
-// }
-//
-// public void testConsistencySeedFromFile() throws
NoSuchAlgorithmException, UnsupportedEncodingException {
-// Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
-// MessageDigest md = MessageDigest.getInstance("SHA-1");
-//
-// byte[] bytes = new byte[SEED_SIZE];
-//
-// y.nextBytes(bytes);
-// md.update(bytes);
-//
-// bytes = md.digest();
-// assertEquals(new String(bytes, "UTF-8"), new
String(SEED_OUTPUT_YARROW_FILE, "UTF-8"));
-// }
-//
-// public void testDouble() {
-// Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
-// ScalarSampleStatistics sample = new ScalarSampleStatistics();
-//
-// for(int i = 0; i < 10000; ++i) {
-// sample.add(y.nextDouble());
-// }
-//
-// assertEquals(0.5, sample.getMean(), 0.02);
-// assertEquals(1.0 / (2.0 * Math.sqrt(3.0)),
sample.getStandardDeviation(), 0.002);
-// }
-//
-// public void testNextInt() {
-// Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
-// for(int n = 1; n < 20; ++n) {
-// int[] count = new int[n];
-// for(int k = 0; k < 10000; ++k) {
-// int l = y.nextInt(n);
-// ++count[l];
-// assertTrue(l >= 0);
-// assertTrue(l < n);
-// }
-// for(int i = 0; i < n; ++i) {
-// assertTrue(n * count[i] > 8800);
-// assertTrue(n * count[i] < 11100);
-// }
-// }
-// }
+ public void testNextBoolean() {
+ Yarrow y = new Yarrow(SEED_FILE, "SHA1", "Rijndael", false,
false, false);
+ int[] results = new int[2];
+ int RUNS = 1000000;
+ for(int i=0; i<RUNS; i++) {
+ if(y.nextBoolean())
+ results[0]++;
+ else
+ results[1]++;
+ }
+
+ assertEquals(RUNS, results[0]+results[1]);
+ assertTrue(results[0] > RUNS/2 - RUNS/1000);
+ assertTrue(results[1] > RUNS/2 - RUNS/1000);
+ }
}
_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs