Skip to content

Commit

Permalink
Fix X11 race conditions and add tests (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering authored May 20, 2021
1 parent 287a96d commit 38529d2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
8 changes: 3 additions & 5 deletions core/src/main/java/com/hashengineering/crypto/X11.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class X11 {

private static final Logger log = LoggerFactory.getLogger(X11.class);
private static boolean native_library_loaded = false;
private static Digest [] algorithms;

static {
try {
Expand All @@ -43,21 +42,19 @@ public class X11 {
catch(UnsatisfiedLinkError x)
{
log.info("Loading x11 failed: " + x.getMessage());
init();
}
catch(Exception e)
{
native_library_loaded = false;
log.info("Loading x11 failed: " + e.getMessage());
init();
}
}

/**
* create the hash objects only if the native library failed to load
*/
static void init() {
algorithms = new Digest[]{
static Digest [] initAlgorithms() {
return new Digest[] {
new BLAKE512(),
new BMW512(),
new Groestl512(),
Expand Down Expand Up @@ -88,6 +85,7 @@ public static byte[] x11Digest(byte[] input) {
static native byte [] x11_native(byte [] input, int offset, int length);

public static byte [] x11(byte input[], int offset, int length) {
Digest [] algorithms = initAlgorithms();
Digest algorithm = algorithms[0];
algorithm.reset();
algorithm.update(input, offset, length);
Expand Down
63 changes: 63 additions & 0 deletions core/src/test/java/com/hashengineering/crypto/X11Test.java
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
}
}
}
}

0 comments on commit 38529d2

Please sign in to comment.