Skip to content

Commit

Permalink
Updated tests to reduce dependency on BaseTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreet- committed Jul 24, 2018
1 parent 1f81aa4 commit 86c9c89
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/test/java/AEADTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
* This Java source file was generated by the Gradle 'init' task.
*/

import com.goterl.lazycode.lazysodium.exceptions.SodiumException;
import com.goterl.lazycode.lazysodium.interfaces.AEAD;
import com.goterl.lazycode.lazysodium.interfaces.KeyDerivation;
import com.goterl.lazycode.lazysodium.utils.DetachedDecrypt;
import com.goterl.lazycode.lazysodium.utils.DetachedEncrypt;
import com.goterl.lazycode.lazysodium.utils.Key;
Expand All @@ -21,6 +19,7 @@

public class AEADTest extends BaseTest {

private final String PASSWORD = "superSecurePassword";

@Test
public void encryptChacha() {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/AuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import junit.framework.TestCase;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class AuthTest extends BaseTest {
Expand Down
37 changes: 1 addition & 36 deletions src/test/java/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,10 @@
*/
import com.goterl.lazycode.lazysodium.LazySodiumJava;
import com.goterl.lazycode.lazysodium.SodiumJava;
import com.goterl.lazycode.lazysodium.interfaces.*;
import org.junit.BeforeClass;

import java.nio.charset.StandardCharsets;

public class BaseTest {

public static SodiumJava sodium;
public final String PASSWORD = "catdog";
public final byte[] PASSWORD_BYTES = PASSWORD.getBytes(StandardCharsets.UTF_8);
public final int PASSWORD_BYTES_LEN = PASSWORD_BYTES.length;

public static LazySodiumJava lazySodium;

public static Random random;
public static PwHash.Native pwHash;
public static PwHash.Lazy pwHashLazy;
public static SecretBox.Lazy secretBoxLazy;
public static Box.Lazy cryptoBoxLazy;
public static Sign.Lazy cryptoSignLazy;

public static KeyDerivation.Native keyDerivation;
public static KeyDerivation.Lazy keyDerivationLazy;



@BeforeClass
public static void beforeClass() {
sodium = new SodiumJava();
lazySodium = new LazySodiumJava(sodium);
random = (Random) lazySodium;
pwHash = (PwHash.Native) lazySodium;
pwHashLazy = (PwHash.Lazy) lazySodium;
keyDerivation = (KeyDerivation.Native) lazySodium;
keyDerivationLazy = (KeyDerivation.Lazy) lazySodium;
secretBoxLazy = (SecretBox.Lazy) lazySodium;
cryptoBoxLazy = (Box.Lazy) lazySodium;
cryptoSignLazy = (Sign.Lazy) lazySodium;
}

public static LazySodiumJava lazySodium = new LazySodiumJava(new SodiumJava());

}
8 changes: 8 additions & 0 deletions src/test/java/BoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.goterl.lazycode.lazysodium.utils.DetachedEncrypt;
import com.goterl.lazycode.lazysodium.utils.KeyPair;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
Expand All @@ -26,6 +27,13 @@
public class BoxTest extends BaseTest {


private Box.Lazy cryptoBoxLazy;

@Before
public void before() {
cryptoBoxLazy = (Box.Lazy) lazySodium;
}

@Test
public void generateKeyPair() throws SodiumException {
KeyPair keys = cryptoBoxLazy.cryptoBoxKeypair();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/HelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void compare() {
byte[] b1 = new byte[] { 4, 2, 2, 1 };
byte[] b2 = new byte[] { 4, 2, 2, 1 };

int r = sodium.sodium_compare(b1, b2, 4);
int r = lazySodium.getSodium().sodium_compare(b1, b2, 4);

TestCase.assertEquals(0, r);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/KeyDerivationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@
*/

import com.goterl.lazycode.lazysodium.exceptions.SodiumException;
import com.goterl.lazycode.lazysodium.interfaces.Box;
import com.goterl.lazycode.lazysodium.interfaces.KeyDerivation;
import com.goterl.lazycode.lazysodium.utils.Key;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;


public class KeyDerivationTest extends BaseTest {

private KeyDerivation.Native keyDerivation;
private KeyDerivation.Lazy keyDerivationLazy;

@Before
public void before() {
keyDerivation = (KeyDerivation.Native) lazySodium;
keyDerivationLazy = (KeyDerivation.Lazy) lazySodium;
}

@Test
public void keygen() throws SodiumException {
byte[] masterKey = new byte[KeyDerivation.MASTER_KEY_BYTES];
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/PwHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@
import com.goterl.lazycode.lazysodium.exceptions.SodiumException;
import com.goterl.lazycode.lazysodium.interfaces.PwHash;
import com.goterl.lazycode.lazysodium.interfaces.Scrypt;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class PwHashTest extends BaseTest {

private final String PASSWORD = "Password123456!!!!@@";


private PwHash.Lazy pwHashLazy;

@Before
public void before() {
pwHashLazy = (PwHash.Lazy) lazySodium;
}


@Test
public void nativeHash() throws SodiumException {
Expand Down
11 changes: 9 additions & 2 deletions src/test/java/SecretBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
import com.goterl.lazycode.lazysodium.interfaces.SecretBox;
import com.goterl.lazycode.lazysodium.utils.Key;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class SecretBoxTest extends BaseTest {


private SecretBox.Lazy secretBoxLazy;

@Before
public void before() {
secretBoxLazy = (SecretBox.Lazy) lazySodium;
}


@Test
public void encrypt() throws SodiumException {
String message = "This is a super secret message.";
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/SecretStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public void test1() throws SodiumException {
String decryptedMessage2 = lazySodium.cryptoSecretStreamPull(state2, c2, tag);
String decryptedMessage3 = lazySodium.cryptoSecretStreamPull(state2, c3, tag);



if (tag[0] == SecretStream.XCHACHA20POLY1305_TAG_FINAL) {
TestCase.assertTrue(
decryptedMessage.equals(message1) &&
Expand All @@ -53,8 +51,6 @@ public void test1() throws SodiumException {
);
}

System.out.println(String.valueOf(SecretStream.TAG_MESSAGE));

}


Expand Down
9 changes: 9 additions & 0 deletions src/test/java/SignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.goterl.lazycode.lazysodium.utils.Key;
import com.goterl.lazycode.lazysodium.utils.KeyPair;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
Expand All @@ -24,6 +25,14 @@
public class SignTest extends BaseTest {


private Sign.Lazy cryptoSignLazy;

@Before
public void before() {
cryptoSignLazy = (Sign.Lazy) lazySodium;
}


@Test
public void generateKeyPair() throws SodiumException {
KeyPair keys = cryptoSignLazy.cryptoSignKeypair();
Expand Down

0 comments on commit 86c9c89

Please sign in to comment.