all basic tests pass!

This commit is contained in:
Dave
2015-07-04 14:46:22 +02:00
parent 1c1a6012bf
commit cd1c7bdc69
9 changed files with 248 additions and 101 deletions

View File

@@ -4,6 +4,7 @@ import java.security.PublicKey;
import java.util.Collection; import java.util.Collection;
import nodash.exceptions.NoAdapterException; import nodash.exceptions.NoAdapterException;
import nodash.exceptions.NoSessionExpiredException;
import nodash.exceptions.NoUserAlreadyOnlineException; import nodash.exceptions.NoUserAlreadyOnlineException;
import nodash.exceptions.NoUserNotValidException; import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoByteSet; import nodash.models.NoByteSet;
@@ -19,11 +20,11 @@ public interface NoAdapter {
public byte[][] exportHashes() throws NoAdapterException; public byte[][] exportHashes() throws NoAdapterException;
public long hashCount() throws NoAdapterException; public long hashCount() throws NoAdapterException;
public void goOnline(byte[] hash) throws NoAdapterException, NoUserAlreadyOnlineException; public void goOnline(byte[] hash) throws NoAdapterException, NoUserAlreadyOnlineException;
public boolean isOnline(byte[] hash) throws NoAdapterException; public boolean isOnline(byte[] hash) throws NoAdapterException;
public void goOffline(byte[] hash) throws NoAdapterException; public void goOffline(byte[] hash) throws NoAdapterException;
public void addNoSession(NoSession session) throws NoAdapterException; public void addNoSession(NoSession session) throws NoAdapterException;
@@ -32,12 +33,13 @@ public interface NoAdapter {
public void shredNoSession(byte[] encryptedUuid) throws NoAdapterException; public void shredNoSession(byte[] encryptedUuid) throws NoAdapterException;
public NoSession getNoSession(byte[] encryptedUuid) throws NoAdapterException; public NoSession getNoSession(byte[] encryptedUuid) throws NoAdapterException,
NoSessionExpiredException;
public Collection<NoByteSet> pollNoByteSets(PublicKey address) throws NoAdapterException; public Collection<NoByteSet> pollNoByteSets(PublicKey address) throws NoAdapterException;
public void addNoByteSet(NoByteSet byteSet, PublicKey address) throws NoAdapterException; public void addNoByteSet(NoByteSet byteSet, PublicKey address) throws NoAdapterException;
public void addNoByteSets(Collection<NoByteSet> byteSets, PublicKey address) public void addNoByteSets(Collection<NoByteSet> byteSets, PublicKey address)
throws NoAdapterException; throws NoAdapterException;
} }

View File

@@ -17,6 +17,8 @@
package nodash.core; package nodash.core;
import org.apache.commons.codec.binary.Base64;
import nodash.exceptions.NoAdapterException; import nodash.exceptions.NoAdapterException;
import nodash.exceptions.NoByteSetBadDecryptionException; import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
@@ -59,7 +61,7 @@ public final class NoCore {
public byte[] login(byte[] data, char[] password) throws NoUserNotValidException, public byte[] login(byte[] data, char[] password) throws NoUserNotValidException,
NoUserAlreadyOnlineException, NoSessionExpiredException { NoUserAlreadyOnlineException, NoSessionExpiredException {
NoSession session = new NoSession(adapter, data, password); NoSession session = new NoSession(data, password);
/* 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 {
@@ -129,6 +131,7 @@ public final class NoCore {
byte[] userFile; byte[] userFile;
try { try {
userFile = save(cookie, password); userFile = save(cookie, password);
adapter.goOnline(user.createHash());
} catch (NoSessionExpiredException e) { } catch (NoSessionExpiredException e) {
throw new NoDashFatalException("Session expired despite just being created."); throw new NoDashFatalException("Session expired despite just being created.");
} catch (NoSessionConfirmedException e) { } catch (NoSessionConfirmedException e) {
@@ -139,6 +142,10 @@ public final class NoCore {
} catch (NoSessionAlreadyAwaitingConfirmationException e) { } catch (NoSessionAlreadyAwaitingConfirmationException e) {
throw new NoDashFatalException( throw new NoDashFatalException(
"Session already waiting confirmation despite just being created."); "Session already waiting confirmation despite just being created.");
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not go online.", e);
} catch (NoUserAlreadyOnlineException e) {
throw new NoDashFatalException("User with same hash is already online.");
} }
return new NoRegister(cookie, userFile); return new NoRegister(cookie, userFile);
@@ -196,29 +203,44 @@ public final class NoCore {
} }
try { try {
adapter.goOffline(newHash); adapter.shredNoSession(cookie);
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not shred session.", e);
}
try {
adapter.goOffline(session.getNoUserSafe().createHash());
} catch (NoAdapterException e) { } catch (NoAdapterException e) {
throw new NoDashFatalException("Could not go offline.", e); throw new NoDashFatalException("Could not go offline.", e);
} }
if (!session.isNewUser()) {
try {
adapter.removeHash(oldHash);
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not remove old hash.", e);
}
}
}
public void shred(byte[] cookie) throws NoSessionExpiredException {
NoSession session = getNoSession(cookie);
try { try {
adapter.shredNoSession(cookie); adapter.shredNoSession(cookie);
} catch (NoAdapterException e) { } catch (NoAdapterException e) {
throw new NoDashFatalException("Could not shred session.", e); throw new NoDashFatalException("Could not shred session.", e);
} }
try { try {
adapter.removeHash(oldHash); adapter.addNoByteSets(session.getIncomingSafe(), session.getNoUserSafe().getRsaPublicKey());
} catch (NoAdapterException e) { } catch (NoAdapterException e) {
throw new NoDashFatalException("Could not remove old hash.", e); throw new NoDashFatalException("Could not add bytesets back into pool.");
} }
}
public void shred(byte[] cookie) {
try { try {
adapter.shredNoSession(cookie); adapter.goOffline(session.getNoUserSafe().createHash());
} catch (NoAdapterException e) { } catch (NoAdapterException e) {
throw new NoDashFatalException("Could not shred session.", e); throw new NoDashFatalException("Could not go offline.", e);
} }
} }

View File

@@ -44,19 +44,21 @@ public class NoDefaultAdapter implements NoAdapter {
private static synchronized byte[] alterFile(byte[] hashToAdd, byte[] hashToRemove) private static synchronized byte[] alterFile(byte[] hashToAdd, byte[] hashToRemove)
throws IOException { throws IOException {
if (!(hashToAdd == null ^ hashToRemove == null) && Arrays.equals(hashToAdd, hashToRemove)) {
throw new IllegalArgumentException("Hashes to add and remove cannot be the same.");
}
File file = new File(HASH_FILE); File file = new File(HASH_FILE);
if (!file.exists()) {
Files.createFile(file.toPath());
}
byte[] originalFileBytes = Files.readAllBytes(file.toPath()); byte[] originalFileBytes = Files.readAllBytes(file.toPath());
if (hashToAdd == null && hashToRemove == null) { if (hashToAdd == null && hashToRemove == null) {
return originalFileBytes; return originalFileBytes;
} }
if (!(hashToAdd == null ^ hashToRemove == null) && Arrays.equals(hashToAdd, hashToRemove)) {
throw new IllegalArgumentException("Hashes to add and remove cannot be the same.");
}
int hashes = originalFileBytes.length / 64; int hashes = originalFileBytes.length / 64;
List<Byte> newFile = new ArrayList<Byte>(); List<Byte> newFile = new ArrayList<Byte>();
for (int x = 0; x < hashes; x++) { for (int x = 0; x < hashes; x++) {
byte[] hash = Arrays.copyOfRange(originalFileBytes, x * 64, x * 64 + 64); byte[] hash = Arrays.copyOfRange(originalFileBytes, x * 64, x * 64 + 64);
@@ -66,7 +68,7 @@ public class NoDefaultAdapter implements NoAdapter {
} }
} }
if (hashToAdd != null || Arrays.equals(hash, hashToAdd)) { if (hashToAdd != null && Arrays.equals(hash, hashToAdd)) {
hashToAdd = null; hashToAdd = null;
} }
} }
@@ -152,7 +154,7 @@ public class NoDefaultAdapter implements NoAdapter {
public boolean containsNoSession(byte[] encryptedUuid) { public boolean containsNoSession(byte[] encryptedUuid) {
String uuid; String uuid;
try { try {
uuid = Base64.encodeBase64String(NoUtil.decrypt(encryptedUuid)); uuid = Base64.encodeBase64URLSafeString(NoUtil.decrypt(encryptedUuid));
} catch (IllegalBlockSizeException | BadPaddingException e) { } catch (IllegalBlockSizeException | BadPaddingException e) {
throw new NoDashFatalException("Could not decrypt given UUID.", e); throw new NoDashFatalException("Could not decrypt given UUID.", e);
} }
@@ -163,7 +165,6 @@ public class NoDefaultAdapter implements NoAdapter {
@Override @Override
public void shredNoSession(byte[] encryptedUuid) { public void shredNoSession(byte[] encryptedUuid) {
NoSession session = getNoSession(encryptedUuid); NoSession session = getNoSession(encryptedUuid);
addNoByteSets(session.getIncomingSafe(), session.getNoUserSafe().getRsaPublicKey());
sessions.remove(session.getUuid()); sessions.remove(session.getUuid());
} }
@@ -172,7 +173,7 @@ public class NoDefaultAdapter implements NoAdapter {
if (containsNoSession(encryptedUuid)) { if (containsNoSession(encryptedUuid)) {
String uuid; String uuid;
try { try {
uuid = Base64.encodeBase64String(NoUtil.decrypt(encryptedUuid)); uuid = Base64.encodeBase64URLSafeString(NoUtil.decrypt(encryptedUuid));
} catch (IllegalBlockSizeException | BadPaddingException e) { } catch (IllegalBlockSizeException | BadPaddingException e) {
throw new NoDashFatalException("Could not decrypt given UUID.", e); throw new NoDashFatalException("Could not decrypt given UUID.", e);
} }
@@ -206,6 +207,14 @@ public class NoDefaultAdapter implements NoAdapter {
@Override @Override
public void addNoByteSets(Collection<NoByteSet> addedByteSets, PublicKey address) { public void addNoByteSets(Collection<NoByteSet> addedByteSets, PublicKey address) {
if (addedByteSets == null) {
return;
}
if (address == null) {
throw new NullPointerException("Address cannot be null.");
}
if (byteSets.containsKey(address)) { if (byteSets.containsKey(address)) {
byteSets.get(address).addAll(addedByteSets); byteSets.get(address).addAll(addedByteSets);
} else { } else {
@@ -215,7 +224,7 @@ public class NoDefaultAdapter implements NoAdapter {
@Override @Override
public void goOnline(byte[] hash) throws NoUserAlreadyOnlineException { public void goOnline(byte[] hash) throws NoUserAlreadyOnlineException {
String hashString = Base64.encodeBase64String(hash); String hashString = Base64.encodeBase64URLSafeString(hash);
if (online.contains(hashString)) { if (online.contains(hashString)) {
throw new NoUserAlreadyOnlineException(); throw new NoUserAlreadyOnlineException();
} }
@@ -224,13 +233,13 @@ public class NoDefaultAdapter implements NoAdapter {
@Override @Override
public boolean isOnline(byte[] hash) { public boolean isOnline(byte[] hash) {
String hashString = Base64.encodeBase64String(hash); String hashString = Base64.encodeBase64URLSafeString(hash);
return online.contains(hashString); return online.contains(hashString);
} }
@Override @Override
public void goOffline(byte[] hash) { public void goOffline(byte[] hash) {
String hashString = Base64.encodeBase64String(hash); String hashString = Base64.encodeBase64URLSafeString(hash);
online.remove(hashString); online.remove(hashString);
} }

View File

@@ -3,6 +3,7 @@ package nodash.models;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@@ -32,13 +33,12 @@ public final class NoSession implements Serializable {
private NoUser original; private NoUser original;
private NoState state; private NoState state;
private final long expiry; private final long expiry;
private boolean newUserSession;
private Collection<NoByteSet> incoming; private Collection<NoByteSet> incoming;
private NoUser current; private NoUser current;
private String uuid; private String uuid;
public NoSession() { private NoSession() {
this.state = NoState.IDLE; this.state = NoState.IDLE;
this.expiry = System.currentTimeMillis() + NoSession.SESSION_DURATION; this.expiry = System.currentTimeMillis() + NoSession.SESSION_DURATION;
this.uuid = UUID.randomUUID().toString(); this.uuid = UUID.randomUUID().toString();
@@ -46,18 +46,25 @@ public final class NoSession implements Serializable {
public NoSession(NoUser newUser) { public NoSession(NoUser newUser) {
this(); this();
if (newUser == null) {
throw new NullPointerException("Session cannot be created with null user.");
}
this.state = NoState.MODIFIED; this.state = NoState.MODIFIED;
this.original = null; this.original = null;
this.current = newUser; this.current = newUser;
this.newUserSession = true;
} }
public NoSession(NoAdapter adapter, byte[] data, char[] password) throws NoUserNotValidException { public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
this(); this();
this.newUserSession = false;
this.state = NoState.IDLE; this.state = NoState.IDLE;
try { try {
this.original = NoUser.createUserFromFile(data, password); byte[] originalData = Arrays.copyOf(data, data.length);
char[] originalPassword = Arrays.copyOf(password, password.length);
this.original = NoUser.createUserFromFile(originalData, originalPassword);
this.current = NoUser.createUserFromFile(data, password);
NoUtil.wipeBytes(data);
NoUtil.wipeChars(password);
this.uuid = UUID.randomUUID().toString();
} catch (IOException e) { } catch (IOException e) {
throw new NoUserNotValidException(); throw new NoUserNotValidException();
} catch (IllegalBlockSizeException e) { } catch (IllegalBlockSizeException e) {
@@ -80,7 +87,7 @@ public final class NoSession implements Serializable {
public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException { public NoState touchState() throws NoSessionConfirmedException, NoSessionExpiredException {
check(); check();
if (this.newUserSession) { if (this.original == null) {
if (this.state != NoState.AWAITING_CONFIRMATION) { if (this.state != NoState.AWAITING_CONFIRMATION) {
this.state = NoState.MODIFIED; this.state = NoState.MODIFIED;
} }
@@ -133,8 +140,6 @@ public final class NoSession implements Serializable {
this.incoming = new ArrayList<NoByteSet>(); this.incoming = new ArrayList<NoByteSet>();
List<NoAction> actions = this.current.getNoActions(); List<NoAction> actions = this.current.getNoActions();
this.incoming = null; this.incoming = null;
this.original = null;
this.current = null;
/* 5.2.4: execute NoActions */ /* 5.2.4: execute NoActions */
for (NoAction action : actions) { for (NoAction action : actions) {
/* /*
@@ -181,7 +186,7 @@ public final class NoSession implements Serializable {
} }
public byte[] getOriginalHash() { public byte[] getOriginalHash() {
if (this.original != null) { if (!isNewUser()) {
return this.original.createHash(); return this.original.createHash();
} else { } else {
return null; return null;
@@ -200,4 +205,8 @@ public final class NoSession implements Serializable {
public void close() { public void close() {
this.state = NoState.CLOSED; this.state = NoState.CLOSED;
} }
public boolean isNewUser() {
return this.original == null;
}
} }

View File

@@ -195,7 +195,21 @@ public class NoUser implements Serializable {
} }
public String createHashString() { public String createHashString() {
return Base64.encodeBase64String(this.createHash()); return Base64.encodeBase64URLSafeString(this.createHash());
} }
@Override
public boolean equals(Object otherUser) {
if (otherUser == null) {
return false;
}
if (!otherUser.getClass().equals(getClass())) {
return false;
}
return this.privateKey.equals(((NoUser) otherUser).privateKey);
}
} }

View File

@@ -9,6 +9,7 @@ import nodash.core.NoCore;
import nodash.core.NoDefaultAdapter; import nodash.core.NoDefaultAdapter;
import nodash.core.NoRegister; import nodash.core.NoRegister;
import nodash.exceptions.NoAdapterException; import nodash.exceptions.NoAdapterException;
import nodash.exceptions.NoDashFatalException;
import nodash.exceptions.NoDashSessionBadUuidException; import nodash.exceptions.NoDashSessionBadUuidException;
import nodash.exceptions.NoSessionAlreadyAwaitingConfirmationException; import nodash.exceptions.NoSessionAlreadyAwaitingConfirmationException;
import nodash.exceptions.NoSessionConfirmedException; import nodash.exceptions.NoSessionConfirmedException;
@@ -18,7 +19,9 @@ import nodash.exceptions.NoSessionNotChangedException;
import nodash.exceptions.NoUserAlreadyOnlineException; import nodash.exceptions.NoUserAlreadyOnlineException;
import nodash.exceptions.NoUserNotValidException; import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoUser; import nodash.models.NoUser;
import nodash.models.NoSession.NoState;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test; import org.junit.Test;
public class NoCoreTest { public class NoCoreTest {
@@ -36,7 +39,7 @@ public class NoCoreTest {
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(registration2.data, registration2.data)); assertFalse(Arrays.equals(registration1.data, registration2.data));
NoUser user3 = new NoUser(); NoUser user3 = new NoUser();
try { try {
@@ -64,7 +67,8 @@ public class NoCoreTest {
@Test @Test
public void testSaveAndConfirm() throws NoSessionExpiredException, NoSessionConfirmedException, public void testSaveAndConfirm() throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoDashSessionBadUuidException, NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException, NoAdapterException { NoDashSessionBadUuidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException, NoAdapterException {
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter);
@@ -74,7 +78,7 @@ public class NoCoreTest {
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 NoUser();
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);
@@ -82,27 +86,36 @@ public class NoCoreTest {
core.confirm(registration.cookie, "badpassword".toCharArray(), newUserBadPassFile); core.confirm(registration.cookie, "badpassword".toCharArray(), newUserBadPassFile);
fail("Confirmed with a bad password without throwing an exception."); fail("Confirmed with a bad password without throwing an exception.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, true // Do nothing, true
}
byte[] badCookie = Arrays.copyOf(registration.cookie, registration.cookie.length);
badCookie[0] = (byte) (badCookie[0] == 'A' ? 'B' : 'A');
try {
core.confirm(badCookie, "password".toCharArray(), newUserBadPassFile);
fail("Confirmed on bad cookie without throwing fatal exception.");
} catch (NoSessionExpiredException e) {
// Do nothing, correct
} }
try { try {
core.confirm(new byte[] {'b', 'a', 'd', 'c', 'o', 'o', 'k', 'i', 'e'}, "password".toCharArray(), core.confirm(new byte[] {'b', 'a', 'd', 'c', 'o', 'o', 'k', 'i', 'e'},
newUserBadPassFile); "password".toCharArray(), newUserBadPassFile);
fail("Confirmed on bad cookie without throwing exception."); fail("Confirmed on bad cookie without throwing exception.");
} catch (NoSessionExpiredException e) { } catch (NoDashFatalException e) {
// Do nothing, true // Do nothing, true
} }
NoUser oldUser = new NoUser(); NoUser oldUser = new NoUser();
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);
oldUserFile = Arrays.copyOf(registration.data, registration.data.length); oldUserFile = Arrays.copyOf(registration.data, registration.data.length);
byte[] oldUserCookie = core.login(oldUserFile, "password".toCharArray()); byte[] oldUserCookie = core.login(oldUserFile, "password".toCharArray());
assertNotNull(adapter.getNoSession(oldUserCookie)); assertNotNull(adapter.getNoSession(oldUserCookie));
oldUser.createFile("password".toCharArray()); // Touch the randomizer oldUser.createFile("password".toCharArray()); // Touch the randomizer
NoUser oldUserRevisited = core.getNoUser(oldUserCookie); NoUser oldUserRevisited = core.getNoUser(oldUserCookie);
byte[] currentHash = oldUserRevisited.createHash(); byte[] currentHash = oldUserRevisited.createHash();
oldUserRevisited.createFile("password".toCharArray()); oldUserRevisited.createFile("password".toCharArray());
@@ -121,18 +134,79 @@ public class NoCoreTest {
@Test @Test
public void testGetUser() { public void testGetUser() throws NoSessionExpiredException, NoSessionConfirmedException,
fail("Not yet implemented"); NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoUserAlreadyOnlineException {
NoCore core = new NoCore(new NoDefaultAdapter());
NoUser user = new NoUser();
NoRegister registration = core.register(user, "password".toCharArray());
byte[] file = Arrays.copyOf(registration.data, registration.data.length);
core.confirm(registration.cookie, "password".toCharArray(), file);
file = Arrays.copyOf(registration.data, registration.data.length);
byte[] cookie = core.login(file, "password".toCharArray());
byte[] badCookie = Arrays.copyOf(cookie, cookie.length);
badCookie[0] = (byte) (badCookie[0] == 'A' ? 'B' : 'A');
NoUser user2 = core.getNoUser(cookie);
assertNotNull(user2);
assertEquals(user, user2);
try {
core.getNoUser(badCookie);
fail("Did not fail when given a bad cookie.");
} catch (NoSessionExpiredException e) {
// Correct, do nothing.
}
} }
@Test @Test
public void testGetSessionState() { public void testGetSessionState() throws NoSessionExpiredException, NoSessionConfirmedException,
fail("Not yet implemented"); NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
NoCore core = new NoCore(new NoDefaultAdapter());
NoUser user = new NoUser();
NoRegister registration = core.register(user, "password".toCharArray());
assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION);
byte[] file = Arrays.copyOf(registration.data, registration.data.length);
core.confirm(registration.cookie, "password".toCharArray(), file);
file = Arrays.copyOf(registration.data, registration.data.length);
byte[] cookie = core.login(file, "password".toCharArray());
assertEquals(core.getSessionState(cookie), NoState.IDLE);
user = core.getNoUser(cookie);
user.createFile("password".toCharArray()); // touch randomizer
assertEquals(core.getSessionState(cookie), NoState.MODIFIED);
file = core.save(cookie, "password".toCharArray());
assertEquals(core.getSessionState(cookie), NoState.AWAITING_CONFIRMATION);
core.confirm(cookie, "password".toCharArray(), file);
try {
core.getSessionState(cookie);
fail("Didn't fail on getting session after confirm.");
} catch (NoSessionExpiredException e) {
// Do nothing, correct
}
} }
@Test @Test
public void testShred() { public void testShred() throws NoAdapterException, NoSessionConfirmedException, NoSessionExpiredException {
fail("Not yet implemented"); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter);
NoUser user = new NoUser();
NoRegister registration = core.register(user, "password".toCharArray());
assertTrue(adapter.isOnline(user.createHash()));
assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION);
core.shred(registration.cookie);
assertFalse(adapter.isOnline(user.createHash()));
try {
core.getNoUser(registration.cookie);
fail("Returned a user object after shredding.");
} catch (NoSessionExpiredException e) {
// Do nothing, correct
}
} }
} }

View File

@@ -0,0 +1,11 @@
package nodash.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({NoCoreTest.class, NoSessionTest.class, NoUserTest.class, NoUtilTest.class})
public class NoDashBasicTests {
}

View File

@@ -1,65 +1,71 @@
package nodash.test; package nodash.test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Arrays;
import nodash.exceptions.NoSessionConfirmedException; import nodash.exceptions.NoSessionConfirmedException;
import nodash.exceptions.NoSessionExpiredException; import nodash.exceptions.NoSessionExpiredException;
import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoSession; import nodash.models.NoSession;
import nodash.models.NoSession.NoState;
import nodash.models.NoUser;
import org.junit.Test; import org.junit.Test;
public class NoSessionTest { public class NoSessionTest {
@Test @Test
public void testNoSession() throws NoSessionConfirmedException, NoSessionExpiredException { public void testNoSessionNoUser() throws NoSessionConfirmedException, NoSessionExpiredException {
NoSession session = new NoSession(); NoUser user = new NoUser();
NoSession session = new NoSession(user);
assertNotNull(session.getNoUser());
assertNotNull(session.getUuid()); assertNotNull(session.getUuid());
assertNull(session.getNoUser()); assertNotNull(session.getEncryptedUuid());
assertNull(session.getIncoming());
assertNull(session.getOriginalHash());
assertEquals(session.getNoUser(), user);
assertEquals(session.getNoState(), NoState.MODIFIED);
try {
new NoSession(null);
fail("Did not throw NullPointerException when given a null user.");
} catch (NullPointerException e) {
// Do nothing, correct
}
} }
@Test @Test
public void testNoSessionNoUser() { public void testNoSessionByteArrayCharArray() throws NoUserNotValidException,
fail("Not yet implemented"); NoSessionExpiredException, NoSessionConfirmedException {
} NoUser user = new NoUser();
final byte[] userFile1 = user.createFile("password".toCharArray());
@Test byte[] userFile2 = Arrays.copyOf(userFile1, userFile1.length);
public void testNoSessionByteArrayCharArray() { char[] userPassword = "password".toCharArray();
fail("Not yet implemented"); NoSession session = new NoSession(userFile2, userPassword);
} assertFalse(Arrays.equals(userFile1, userFile2));
assertFalse(Arrays.equals("password".toCharArray(), userPassword));
@Test assertNotNull(session.getNoUser());
public void testCheck() { assertNotNull(session.getOriginalHash());
fail("Not yet implemented"); assertNotNull(session.getUuid());
} assertNull(session.getIncoming());
assertEquals(session.getNoUser(), user);
@Test assertEquals(session.getNoState(), NoState.IDLE);
public void testTouchState() {
fail("Not yet implemented"); byte[] badUserFile = Arrays.copyOf(userFile1, userFile1.length);
} badUserFile[0] = (byte) (badUserFile[0] == 'A' ? 'B' : 'A');
try {
@Test new NoSession(badUserFile, "password".toCharArray());
public void testInitiateSaveAttempt() { fail("Did not throw NoUserNotValidException when given bad file.");
fail("Not yet implemented"); } catch (NoUserNotValidException e) {
} // Do nothing, correct
}
@Test
public void testConfirmSave() { try {
fail("Not yet implemented"); new NoSession(Arrays.copyOf(userFile2, userFile2.length), "badpassword".toCharArray());
} fail("Did not throw NoUserNotValidException when given bad password.");
} catch (NoUserNotValidException e) {
@Test // Do nothing, correct
public void testDecryptUuid() { }
fail("Not yet implemented");
}
@Test
public void testConsume() {
fail("Not yet implemented");
}
@Test
public void testClose() {
fail("Not yet implemented");
} }
} }

View File

@@ -59,7 +59,7 @@ public class NoUserTest {
byte[] hash = user.createHash(); byte[] hash = user.createHash();
String hashString = user.createHashString(); String hashString = user.createHashString();
assertEquals(Base64.encodeBase64String(hash), hashString); assertEquals(Base64.encodeBase64URLSafeString(hash), hashString);
} }
@Test @Test