Compare commits

9 Commits

Author SHA1 Message Date
Dave
6e84c0e76e formatting 2015-12-05 18:01:07 +02:00
Dave
86a5d724e3 added null check to nested user hash 2015-12-05 14:21:23 +02:00
Dave
ee92afd7da changed hashing method to use toString, bytearray may have proved
inconsistent
2015-12-05 14:05:09 +02:00
Dave
d0e2b2d69a Changed from static system var to explicit constructor
(consider changing to a generic NoCore/NoSession type in future)
2015-11-29 15:50:26 +02:00
Dave
d230b610da added exception to ClassNotFound error in setupNoUserClass 2015-11-29 15:27:56 +02:00
Dave
495a227da1 clean up this. 2015-11-29 11:53:53 +02:00
Dave
9108b2720a stopped ignoring actions on Hash (they are lifetime incremetors) 2015-11-29 11:36:30 +02:00
Dave
24c44e08d6 NoUser is now abstract 2015-11-29 11:24:47 +02:00
Dave
44d2ee3cc5 added @NoHash annotation to the correct fields in TestNoUser 2015-11-29 11:09:55 +02:00
9 changed files with 110 additions and 118 deletions

View File

@@ -43,6 +43,7 @@ import nodash.models.NoSession.NoState;
*/ */
public final class NoCore { public final class NoCore {
private NoAdapter adapter; private NoAdapter adapter;
private Class<? extends NoUser> userClass;
/** /**
* Instantiates an instance of the NoCore, using the given adapter to interact with saved hashes, * Instantiates an instance of the NoCore, using the given adapter to interact with saved hashes,
@@ -50,8 +51,9 @@ public final class NoCore {
* *
* @param adapter an object implementing the NoAdapter interface. * @param adapter an object implementing the NoAdapter interface.
*/ */
public NoCore(NoAdapter adapter) { public NoCore(NoAdapter adapter, Class<? extends NoUser> userClass) {
this.adapter = adapter; this.adapter = adapter;
this.userClass = userClass;
} }
/** /**
@@ -92,7 +94,7 @@ public final class NoCore {
*/ */
public byte[] login(byte[] data, char[] password) throws NoUserNotValidException, public byte[] login(byte[] data, char[] password) throws NoUserNotValidException,
NoUserAlreadyOnlineException { NoUserAlreadyOnlineException {
NoSession session = new NoSession(data, password); NoSession session = new NoSession(data, password, userClass);
/* 1. Check that user is a valid user of the system based on their hash. */ /* 1. Check that user is a valid user of the system based on their hash. */
try { try {
@@ -286,7 +288,7 @@ public final class NoCore {
byte[] newHash = session.getNoUserSafe().createHash(); byte[] newHash = session.getNoUserSafe().createHash();
try { try {
session.confirmSave(adapter, data, password); session.confirmSave(adapter, data, password, userClass);
try { try {
adapter.insertHash(newHash); adapter.insertHash(newHash);

View File

@@ -38,7 +38,6 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
import nodash.models.NoUser;
public final class NoUtil { public final class NoUtil {
public static final SecretKey SECRET_KEY = setupSecretKey(); public static final SecretKey SECRET_KEY = setupSecretKey();
@@ -53,19 +52,7 @@ public final class NoUtil {
public static final int RSA_STRENGTH = setupRsaStrength(); public static final int RSA_STRENGTH = setupRsaStrength();
public static final int AES_STRENGTH = 256; public static final int AES_STRENGTH = 256;
public static final byte BLANK_BYTE = 'A'; public static final byte BLANK_BYTE = 'A';
public static final Class<? extends NoUser> NO_USER_CLASS = setupNoUserClass();
private static Class<? extends NoUser> setupNoUserClass() {
String secretEnv = System.getenv("NODASH_USER_CLASS");
try {
@SuppressWarnings("unchecked")
Class<? extends NoUser> clazz = (Class<? extends NoUser>) Class.forName(secretEnv);
return clazz;
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can't find NODASH_USER_CLASS");
}
}
private static SecretKey setupSecretKey() { private static SecretKey setupSecretKey() {
String secretEnv = System.getenv("NODASH_SECRET"); String secretEnv = System.getenv("NODASH_SECRET");
if (secretEnv == null) { if (secretEnv == null) {

View File

@@ -50,14 +50,15 @@ public final class NoSession implements Serializable {
this.current = newUser; this.current = newUser;
} }
public NoSession(byte[] data, char[] password) throws NoUserNotValidException { public NoSession(byte[] data, char[] password, Class<? extends NoUser> userClass)
throws NoUserNotValidException {
this(); this();
this.state = NoState.IDLE; this.state = NoState.IDLE;
byte[] originalData = Arrays.copyOf(data, data.length); byte[] originalData = Arrays.copyOf(data, data.length);
char[] originalPassword = Arrays.copyOf(password, password.length); char[] originalPassword = Arrays.copyOf(password, password.length);
this.original = this.original =
NoUser.createUserFromFile(originalData, originalPassword, NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(originalData, originalPassword, userClass);
this.current = NoUser.createUserFromFile(data, password, NoUtil.NO_USER_CLASS); this.current = NoUser.createUserFromFile(data, password, userClass);
NoUtil.wipeBytes(data); NoUtil.wipeBytes(data);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);
this.uuid = UUID.randomUUID().toString(); this.uuid = UUID.randomUUID().toString();
@@ -78,32 +79,32 @@ public final class NoSession implements Serializable {
public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException { public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException {
check(); check();
if (this.original == null) { if (original == null) {
if (this.state != NoState.AWAITING_CONFIRMATION) { if (state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED; state = NoState.MODIFIED;
} }
} else { } else {
String originalHash = this.original.createHashString(); String originalHash = this.original.createHashString();
String currentHash = this.current.createHashString(); String currentHash = this.current.createHashString();
if (originalHash.equals(currentHash)) { if (originalHash.equals(currentHash)) {
this.state = NoState.IDLE; state = NoState.IDLE;
} else if (this.state != NoState.AWAITING_CONFIRMATION) { } else if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED; state = NoState.MODIFIED;
} }
} }
return this.state; return state;
} }
public byte[] initiateSaveAttempt(char[] password) public byte[] initiateSaveAttempt(char[] password)
throws NoSessionConfirmedException, NoSessionExpiredException { throws NoSessionConfirmedException, NoSessionExpiredException {
touchState(); touchState();
this.state = NoState.AWAITING_CONFIRMATION; this.state = NoState.AWAITING_CONFIRMATION;
byte[] file = this.current.createFile(password); byte[] file = current.createFile(password);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);
return file; return file;
} }
public void confirmSave(NoAdapter adapter, byte[] confirmData, char[] password) public void confirmSave(NoAdapter adapter, byte[] confirmData, char[] password, Class<? extends NoUser> userClass)
throws NoSessionConfirmedException, NoSessionExpiredException, throws NoSessionConfirmedException, NoSessionExpiredException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException { NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
check(); check();
@@ -111,7 +112,7 @@ public final class NoSession implements Serializable {
throw new NoSessionNotAwaitingConfirmationException(); throw new NoSessionNotAwaitingConfirmationException();
} }
NoUser confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS); NoUser confirmed = NoUser.createUserFromFile(confirmData, password, userClass);
NoUtil.wipeBytes(confirmData); NoUtil.wipeBytes(confirmData);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);

View File

@@ -18,9 +18,6 @@
package nodash.models; package nodash.models;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.math.BigInteger; import java.math.BigInteger;
@@ -41,8 +38,6 @@ import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
@@ -53,7 +48,7 @@ import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
import nodash.exceptions.NoUserNotValidException; import nodash.exceptions.NoUserNotValidException;
public class NoUser implements Serializable { public abstract class NoUser implements Serializable {
private static final long serialVersionUID = 7132405837081692211L; private static final long serialVersionUID = 7132405837081692211L;
@NoHash @NoHash
private RSAPublicKeyImpl publicKey; private RSAPublicKeyImpl publicKey;
@@ -64,7 +59,7 @@ public class NoUser implements Serializable {
@NoHash @NoHash
private int influences; private int influences;
@NoHash @NoHash
private int actions; private int actions;
@@ -88,10 +83,10 @@ public class NoUser implements Serializable {
} }
KeyPair keyPair = kpg.generateKeyPair(); KeyPair keyPair = kpg.generateKeyPair();
this.publicKey = (RSAPublicKeyImpl) keyPair.getPublic(); publicKey = (RSAPublicKeyImpl) keyPair.getPublic();
this.privateKey = (RSAPrivateCrtKeyImpl) keyPair.getPrivate(); privateKey = (RSAPrivateCrtKeyImpl) keyPair.getPrivate();
this.influences = 0; influences = 0;
this.actions = 0; actions = 0;
touchRandomizer(); touchRandomizer();
} }
@@ -107,8 +102,7 @@ public class NoUser implements Serializable {
public final byte[] createFile(char[] password) { public final byte[] createFile(char[] password) {
List<NoAction> tempActions = outgoing; List<NoAction> tempActions = outgoing;
int tempActionCount = actions;
touchRandomizer(); touchRandomizer();
outgoing = new ArrayList<NoAction>(); outgoing = new ArrayList<NoAction>();
actions = 0; actions = 0;
@@ -117,7 +111,6 @@ public class NoUser implements Serializable {
byte[] json = NoUtil.toBytes(gson.toJson(this)); byte[] json = NoUtil.toBytes(gson.toJson(this));
byte[] encrypted = NoUtil.encrypt(json, password); byte[] encrypted = NoUtil.encrypt(json, password);
actions = tempActionCount;
outgoing = tempActions; outgoing = tempActions;
return encrypted; return encrypted;
} }
@@ -125,48 +118,48 @@ public class NoUser implements Serializable {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final byte[] createHash() { public final byte[] createHash() {
try { try {
List<Object> items = new ArrayList<Object>();
Comparator<Field> fieldComp = new Comparator<Field>() { Comparator<Field> fieldComp = new Comparator<Field>() {
@Override @Override
public int compare(Field o1, Field o2) { public int compare(Field o1, Field o2) {
return o1.getName().compareTo(o2.getName()); return o1.getName().compareTo(o2.getName());
} }
}; };
Class<? extends NoUser> userClass = getClass(); Class<? extends NoUser> userClass = getClass();
StringBuilder toString = new StringBuilder();
while (userClass != null) { while (userClass != null) {
Field[] noHashFields = userClass.getDeclaredFields(); Field[] noHashFields = userClass.getDeclaredFields();
Arrays.sort(noHashFields, fieldComp); Arrays.sort(noHashFields, fieldComp);
for (Field field : noHashFields) { for (Field field : noHashFields) {
if (field.isAnnotationPresent(NoHash.class)) { if (field.isAnnotationPresent(NoHash.class)) {
field.setAccessible(true); field.setAccessible(true);
items.add(field.get(this)); toString.append("|");
Object item = field.get(this);
if (item != null) {
toString.append(field.get(this).toString());
}
} }
} }
if (userClass == NoUser.class) { if (userClass == NoUser.class) {
userClass = null; userClass = null;
} else { } else {
userClass = (Class<? extends NoUser>) userClass.getSuperclass(); userClass = (Class<? extends NoUser>) userClass.getSuperclass();
} }
} }
byte[] itemBytes = toString.toString().getBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(items);
byte[] itemBytes = baos.toByteArray();
return NoUtil.getHashFromByteArray(itemBytes); return NoUtil.getHashFromByteArray(itemBytes);
} catch (IOException e) {
throw new NoDashFatalException("IO Exception encountered while generating user hash.", e);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new NoDashFatalException("IllegalArgument Exception encountered while generating user hash.", e); throw new NoDashFatalException(
"IllegalArgument Exception encountered while generating user hash.", e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new NoDashFatalException("IllegalAccess Exception encountered while generating user hash.", e); throw new NoDashFatalException(
"IllegalAccess Exception encountered while generating user hash.", e);
} }
} }
@@ -192,12 +185,12 @@ public class NoUser implements Serializable {
} }
public final void addAction(NoAction action) { public final void addAction(NoAction action) {
this.outgoing.add(action); outgoing.add(action);
actions++; actions++;
} }
public final List<NoAction> getNoActions() { public final List<NoAction> getNoActions() {
return this.outgoing; return outgoing;
} }
public final BigInteger getPublicExponent() { public final BigInteger getPublicExponent() {
@@ -222,30 +215,30 @@ public class NoUser implements Serializable {
private final byte[] decryptRsa(byte[] data) private final byte[] decryptRsa(byte[] data)
throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
return NoUtil.decryptRsa(data, this.privateKey); return NoUtil.decryptRsa(data, privateKey);
} }
public static NoUser createUserFromFile(byte[] data, char[] password, Class<? extends NoUser> clazz) public static NoUser createUserFromFile(byte[] data, char[] password,
throws NoUserNotValidException { Class<? extends NoUser> clazz) throws NoUserNotValidException {
byte[] decrypted; byte[] decrypted;
try { try {
decrypted = NoUtil.decrypt(data, password); decrypted = NoUtil.decrypt(data, password);
} catch (IllegalBlockSizeException | BadPaddingException e) { } catch (IllegalBlockSizeException | BadPaddingException e) {
throw new NoUserNotValidException(e); throw new NoUserNotValidException(e);
} }
Gson gson = new Gson(); Gson gson = new Gson();
String json = NoUtil.fromBytes(decrypted); String json = NoUtil.fromBytes(decrypted);
try { try {
NoUser noUser = gson.fromJson(json, clazz); NoUser noUser = gson.fromJson(json, clazz);
return noUser; return noUser;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
throw new NoUserNotValidException(e); throw new NoUserNotValidException(e);
} }
} }
public String createHashString() { public final String createHashString() {
return Base64.encodeBase64URLSafeString(createHash()); return NoUtil.fromBytes(createHash());
} }
@Override @Override
@@ -253,13 +246,12 @@ public class NoUser implements Serializable {
if (otherUser == null) { if (otherUser == null) {
return false; return false;
} }
if (!NoUser.class.isAssignableFrom(otherUser.getClass())) { if (!NoUser.class.isAssignableFrom(otherUser.getClass())) {
return false; return false;
} }
return this.privateKey.getModulus().equals(((NoUser) otherUser).privateKey.getModulus()); return privateKey.getModulus().equals(((NoUser) otherUser).privateKey.getModulus());
} }
} }

View File

@@ -38,6 +38,7 @@ import nodash.exceptions.NoUserAlreadyOnlineException;
import nodash.exceptions.NoUserNotValidException; import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoRegister; import nodash.models.NoRegister;
import nodash.models.NoUser; import nodash.models.NoUser;
import nodash.test.functional.implementations.TestNoUser;
import nodash.models.NoSession.NoState; import nodash.models.NoSession.NoState;
import org.junit.Test; import org.junit.Test;
@@ -46,20 +47,20 @@ public class NoCoreTest {
@Test @Test
public void testRegister() { public void testRegister() {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoUser user1 = new NoUser(); NoUser user1 = new TestNoUser("Test");
NoRegister registration1 = core.register(user1, "password".toCharArray()); NoRegister registration1 = core.register(user1, "password".toCharArray());
assertNotNull(registration1.cookie); assertNotNull(registration1.cookie);
assertNotNull(registration1.data); assertNotNull(registration1.data);
NoUser user2 = new NoUser(); NoUser user2 = new TestNoUser("Test");
NoRegister registration2 = core.register(user2, "password".toCharArray()); NoRegister registration2 = core.register(user2, "password".toCharArray());
assertFalse(Arrays.equals(registration1.cookie, registration2.cookie)); assertFalse(Arrays.equals(registration1.cookie, registration2.cookie));
assertFalse(Arrays.equals(registration1.data, registration2.data)); assertFalse(Arrays.equals(registration1.data, registration2.data));
NoUser user3 = new NoUser(); NoUser user3 = new TestNoUser("Test");
try { try {
core.register(null, "password".toCharArray()); core.register(null, "password".toCharArray());
fail("Did not throw NullPointerException."); fail("Did not throw NullPointerException.");
@@ -89,16 +90,16 @@ public class NoCoreTest {
NoSessionAlreadyAwaitingConfirmationException, NoAdapterException, NoSuchMethodException, NoSessionAlreadyAwaitingConfirmationException, NoAdapterException, NoSuchMethodException,
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter, TestNoUser.class);
NoUser newUser = new NoUser(); NoUser newUser = new TestNoUser("Test");
NoRegister registration = core.register(newUser, "password".toCharArray()); NoRegister registration = core.register(newUser, "password".toCharArray());
byte[] newUserFile = Arrays.copyOf(registration.data, registration.data.length); byte[] newUserFile = Arrays.copyOf(registration.data, registration.data.length);
core.confirm(registration.cookie, "password".toCharArray(), newUserFile); core.confirm(registration.cookie, "password".toCharArray(), newUserFile);
byte[] newUserHash = newUser.createHash(); byte[] newUserHash = newUser.createHash();
adapter.checkHash(newUserHash); adapter.checkHash(newUserHash);
NoUser newUserBadPass = new NoUser(); NoUser newUserBadPass = new TestNoUser("Test");
registration = core.register(newUserBadPass, "password".toCharArray()); registration = core.register(newUserBadPass, "password".toCharArray());
byte[] newUserBadPassFile = Arrays.copyOf(registration.data, registration.data.length); byte[] newUserBadPassFile = Arrays.copyOf(registration.data, registration.data.length);
try { try {
@@ -125,7 +126,7 @@ public class NoCoreTest {
// Do nothing, true // Do nothing, true
} }
NoUser oldUser = new NoUser(); NoUser oldUser = new TestNoUser("Test");
registration = core.register(oldUser, "password".toCharArray()); registration = core.register(oldUser, "password".toCharArray());
byte[] oldUserFile = Arrays.copyOf(registration.data, registration.data.length); byte[] oldUserFile = Arrays.copyOf(registration.data, registration.data.length);
core.confirm(registration.cookie, "password".toCharArray(), oldUserFile); core.confirm(registration.cookie, "password".toCharArray(), oldUserFile);
@@ -161,8 +162,8 @@ public class NoCoreTest {
public void testGetUser() throws NoSessionExpiredException, NoSessionConfirmedException, public void testGetUser() throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoUserAlreadyOnlineException { NoUserAlreadyOnlineException {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
NoRegister registration = core.register(user, "password".toCharArray()); NoRegister registration = core.register(user, "password".toCharArray());
byte[] file = Arrays.copyOf(registration.data, registration.data.length); byte[] file = Arrays.copyOf(registration.data, registration.data.length);
core.confirm(registration.cookie, "password".toCharArray(), file); core.confirm(registration.cookie, "password".toCharArray(), file);
@@ -187,8 +188,8 @@ public class NoCoreTest {
public void testGetSessionState() throws NoSessionExpiredException, NoSessionConfirmedException, public void testGetSessionState() throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException { NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
NoRegister registration = core.register(user, "password".toCharArray()); NoRegister registration = core.register(user, "password".toCharArray());
assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION); assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION);
byte[] file = Arrays.copyOf(registration.data, registration.data.length); byte[] file = Arrays.copyOf(registration.data, registration.data.length);
@@ -216,8 +217,8 @@ public class NoCoreTest {
@Test @Test
public void testShred() throws NoAdapterException, NoSessionConfirmedException, NoSessionExpiredException { public void testShred() throws NoAdapterException, NoSessionConfirmedException, NoSessionExpiredException {
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter, TestNoUser.class);
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
NoRegister registration = core.register(user, "password".toCharArray()); NoRegister registration = core.register(user, "password".toCharArray());
assertTrue(adapter.isOnline(user.createHash())); assertTrue(adapter.isOnline(user.createHash()));
assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION); assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION);

View File

@@ -27,6 +27,7 @@ import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoSession; import nodash.models.NoSession;
import nodash.models.NoSession.NoState; import nodash.models.NoSession.NoState;
import nodash.models.NoUser; import nodash.models.NoUser;
import nodash.test.functional.implementations.TestNoUser;
import org.junit.Test; import org.junit.Test;
@@ -34,7 +35,7 @@ public class NoSessionTest {
@Test @Test
public void testNoSessionNoUser() throws NoSessionConfirmedException, NoSessionExpiredException { public void testNoSessionNoUser() throws NoSessionConfirmedException, NoSessionExpiredException {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
NoSession session = new NoSession(user); NoSession session = new NoSession(user);
assertNotNull(session.getNoUser()); assertNotNull(session.getNoUser());
assertNotNull(session.getUuid()); assertNotNull(session.getUuid());
@@ -43,7 +44,7 @@ public class NoSessionTest {
assertNull(session.getOriginalHash()); assertNull(session.getOriginalHash());
assertEquals(session.getNoUser(), user); assertEquals(session.getNoUser(), user);
assertEquals(session.getNoState(), NoState.MODIFIED); assertEquals(session.getNoState(), NoState.MODIFIED);
try { try {
new NoSession(null); new NoSession(null);
fail("Did not throw NullPointerException when given a null user."); fail("Did not throw NullPointerException when given a null user.");
@@ -51,14 +52,15 @@ public class NoSessionTest {
// Do nothing, correct // Do nothing, correct
} }
} }
@Test @Test
public void testNoSessionByteArrayCharArray() throws NoUserNotValidException, public void testNoSessionByteArrayCharArray()
NoSessionExpiredException, NoSessionConfirmedException { throws NoUserNotValidException, NoSessionExpiredException, NoSessionConfirmedException {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
final byte[] userFile1 = user.createFile("password".toCharArray()); final byte[] userFile1 = user.createFile("password".toCharArray());
byte[] userFile2 = Arrays.copyOf(userFile1, userFile1.length); byte[] userFile2 = Arrays.copyOf(userFile1, userFile1.length);
char[] userPassword = "password".toCharArray(); char[] userPassword = "password".toCharArray();
NoSession session = new NoSession(userFile2, userPassword); NoSession session = new NoSession(userFile2, userPassword, TestNoUser.class);
assertFalse(Arrays.equals(userFile1, userFile2)); assertFalse(Arrays.equals(userFile1, userFile2));
assertFalse(Arrays.equals("password".toCharArray(), userPassword)); assertFalse(Arrays.equals("password".toCharArray(), userPassword));
assertNotNull(session.getNoUser()); assertNotNull(session.getNoUser());
@@ -67,18 +69,19 @@ public class NoSessionTest {
assertNull(session.getIncoming()); assertNull(session.getIncoming());
assertEquals(session.getNoUser(), user); assertEquals(session.getNoUser(), user);
assertEquals(session.getNoState(), NoState.IDLE); assertEquals(session.getNoState(), NoState.IDLE);
byte[] badUserFile = Arrays.copyOf(userFile1, userFile1.length); byte[] badUserFile = Arrays.copyOf(userFile1, userFile1.length);
badUserFile[0] = (byte) (badUserFile[0] == 'A' ? 'B' : 'A'); badUserFile[0] = (byte) (badUserFile[0] == 'A' ? 'B' : 'A');
try { try {
new NoSession(badUserFile, "password".toCharArray()); new NoSession(badUserFile, "password".toCharArray(), TestNoUser.class);
fail("Did not throw NoUserNotValidException when given bad file."); fail("Did not throw NoUserNotValidException when given bad file.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
new NoSession(Arrays.copyOf(userFile2, userFile2.length), "badpassword".toCharArray()); new NoSession(Arrays.copyOf(userFile2, userFile2.length), "badpassword".toCharArray(),
TestNoUser.class);
fail("Did not throw NoUserNotValidException when given bad password."); fail("Did not throw NoUserNotValidException when given bad password.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct

View File

@@ -28,15 +28,15 @@ import javax.crypto.IllegalBlockSizeException;
import nodash.core.NoUtil; import nodash.core.NoUtil;
import nodash.exceptions.NoUserNotValidException; import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoUser; import nodash.models.NoUser;
import nodash.test.functional.implementations.TestNoUser;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test; import org.junit.Test;
public class NoUserTest { public class NoUserTest {
@Test @Test
public void testNoUser() { public void testNoUser() {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
assertNotNull(user.getNoActions()); assertNotNull(user.getNoActions());
assertEquals(user.getNoActions().size(), 0); assertEquals(user.getNoActions().size(), 0);
@@ -48,7 +48,7 @@ public class NoUserTest {
@Test @Test
public void testCreateFile() { public void testCreateFile() {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
byte[] file = user.createFile("password".toCharArray()); byte[] file = user.createFile("password".toCharArray());
assertNotNull(file); assertNotNull(file);
@@ -61,7 +61,7 @@ public class NoUserTest {
@Test @Test
public void testCreateHash() { public void testCreateHash() {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
byte[] hash = user.createHash(); byte[] hash = user.createHash();
assertNotNull(hash); assertNotNull(hash);
@@ -74,18 +74,18 @@ public class NoUserTest {
@Test @Test
public void testCreateHashString() { public void testCreateHashString() {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
byte[] hash = user.createHash(); byte[] hash = user.createHash();
String hashString = user.createHashString(); String hashString = user.createHashString();
assertEquals(Base64.encodeBase64URLSafeString(hash), hashString); assertEquals(NoUtil.fromBytes(hash), hashString);
} }
@Test @Test
public void testCreateUserFromFile() public void testCreateUserFromFile()
throws IllegalBlockSizeException, BadPaddingException, throws IllegalBlockSizeException, BadPaddingException,
ClassNotFoundException, IOException, NoUserNotValidException { ClassNotFoundException, IOException, NoUserNotValidException {
NoUser user = new NoUser(); NoUser user = new TestNoUser("Test");
final byte[] originalFile = user.createFile("password".toCharArray()); final byte[] originalFile = user.createFile("password".toCharArray());
byte[] file = Arrays.copyOf(originalFile, originalFile.length); byte[] file = Arrays.copyOf(originalFile, originalFile.length);
byte[] hash = user.createHash(); byte[] hash = user.createHash();
@@ -93,34 +93,34 @@ public class NoUserTest {
user = null; user = null;
try { try {
user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), NoUtil.NO_USER_CLASS); user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), TestNoUser.class);
fail("Should have thrown an error when given wrong password."); fail("Should have thrown an error when given wrong password.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct
} }
file = Arrays.copyOf(originalFile, originalFile.length); file = Arrays.copyOf(originalFile, originalFile.length);
user = NoUser.createUserFromFile(file, "password".toCharArray(), NoUtil.NO_USER_CLASS); user = NoUser.createUserFromFile(file, "password".toCharArray(), TestNoUser.class);
assertTrue(Arrays.equals(hash, user.createHash())); assertTrue(Arrays.equals(hash, user.createHash()));
assertEquals(hashString, user.createHashString()); assertEquals(hashString, user.createHashString());
file = Arrays.copyOf(originalFile, originalFile.length); file = Arrays.copyOf(originalFile, originalFile.length);
try { try {
NoUser.createUserFromFile(file, null, NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(file, null, TestNoUser.class);
fail("Should have thrown a NullPointerException."); fail("Should have thrown a NullPointerException.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUser.createUserFromFile(null, "password".toCharArray(), NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(null, "password".toCharArray(), TestNoUser.class);
fail("Should have thrown a IllegalArgumentException."); fail("Should have thrown a IllegalArgumentException.");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUser.createUserFromFile(null, null, NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(null, null, TestNoUser.class);
fail("Should have thrown a IllegalArgumentException."); fail("Should have thrown a IllegalArgumentException.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct

View File

@@ -36,7 +36,7 @@ public class NoRoutineTest {
private byte[] registerAndConfirm(TestNoUser user, String password) private byte[] registerAndConfirm(TestNoUser user, String password)
throws NoSessionExpiredException, NoSessionConfirmedException, throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException { NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoRegister registration = core.register(user, password.toCharArray()); NoRegister registration = core.register(user, password.toCharArray());
core.confirm(getCopy(registration.cookie), password.toCharArray(), getCopy(registration.data)); core.confirm(getCopy(registration.cookie), password.toCharArray(), getCopy(registration.data));
return getCopy(registration.data); return getCopy(registration.data);
@@ -50,7 +50,7 @@ public class NoRoutineTest {
final byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password"); final byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password");
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter, TestNoUser.class);
byte[] cookie = core.login(getCopy(userFile), "password".toCharArray()); byte[] cookie = core.login(getCopy(userFile), "password".toCharArray());
TestNoUser userRegistered = (TestNoUser) core.getNoUser(Arrays.copyOf(cookie, cookie.length)); TestNoUser userRegistered = (TestNoUser) core.getNoUser(Arrays.copyOf(cookie, cookie.length));
@@ -84,7 +84,7 @@ public class NoRoutineTest {
NoUserAlreadyOnlineException, NoSessionNotChangedException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException { // testing TestJustTouchStaticField NoSessionAlreadyAwaitingConfirmationException { // testing TestJustTouchStaticField
byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password"); byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password");
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
int touchCount = TestJustTouchStaticField.touchMe; int touchCount = TestJustTouchStaticField.touchMe;
@@ -118,7 +118,7 @@ public class NoRoutineTest {
NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException, NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds
byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password"); byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password");
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray()); byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray());
TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie)); TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie));
@@ -137,7 +137,7 @@ public class NoRoutineTest {
NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException, NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds
byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password"); byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password");
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray()); byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray());
TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie)); TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie));
@@ -161,7 +161,7 @@ public class NoRoutineTest {
NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFunds NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFunds
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1"); byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2"); byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2");
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray()); byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie); TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
@@ -211,7 +211,7 @@ public class NoRoutineTest {
NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFundsReceipted NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFundsReceipted
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1"); byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2"); byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2");
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray()); byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie); TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
@@ -268,7 +268,7 @@ public class NoRoutineTest {
NoUserNotValidException, NoUserAlreadyOnlineException { // testing TestSendFundsSourced NoUserNotValidException, NoUserAlreadyOnlineException { // testing TestSendFundsSourced
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1"); byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2"); byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2");
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray()); byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie); TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);

View File

@@ -3,6 +3,7 @@ package nodash.test.functional.implementations;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import nodash.models.NoHash;
import nodash.models.NoUser; import nodash.models.NoUser;
public class TestNoUser extends NoUser { public class TestNoUser extends NoUser {
@@ -11,8 +12,13 @@ public class TestNoUser extends NoUser {
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@NoHash
private String username; private String username;
@NoHash
private int money; private int money;
@NoHash
private List<String> receipts; private List<String> receipts;
public TestNoUser(String username) { public TestNoUser(String username) {