-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix X11 race conditions and add tests (#110)
- Loading branch information
1 parent
287a96d
commit 38529d2
Showing
2 changed files
with
66 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
core/src/test/java/com/hashengineering/crypto/X11Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.hashengineering.crypto; | ||
|
||
import org.bouncycastle.util.Arrays; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class X11Test { | ||
@Test | ||
public void x11Test() { | ||
byte [] message = "Hello World!".getBytes(); | ||
|
||
byte [] digestOne = X11.x11Digest(message); | ||
byte [] digestTwo = X11.x11Digest(message, 0, message.length); | ||
|
||
assertArrayEquals(digestTwo, digestOne); | ||
} | ||
|
||
@Test | ||
public void x11ThreadTest() { | ||
final byte [] message = "Hello World!".getBytes(); | ||
byte [] expectedHash = X11.x11Digest(message); | ||
|
||
class X11Thread extends Thread { | ||
public Boolean result = true; | ||
X11Thread() { | ||
super(); | ||
} | ||
|
||
public void run() { | ||
for (int i = 0; i < 10; ++i) { | ||
byte[] digestOne = X11.x11Digest(message); | ||
byte[] digestTwo = X11.x11Digest(message, 0, message.length); | ||
|
||
result = Arrays.compareUnsigned(expectedHash, digestOne) == 0; | ||
result &= Arrays.compareUnsigned(expectedHash, digestTwo) == 0; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
X11Thread [] threads = new X11Thread [10]; | ||
|
||
for (int i = 0; i < 10; ++i) { | ||
threads[i] = new X11Thread(); | ||
} | ||
|
||
for (int i = 0; i < 10; ++i) { | ||
threads[i].start(); | ||
} | ||
|
||
for (int i = 0; i < 10; ++i) { | ||
try { | ||
threads[i].join(); | ||
assertTrue(threads[i].result); | ||
} catch (InterruptedException x) { | ||
// do nothing | ||
} | ||
} | ||
} | ||
} |