Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kocakosm committed Mar 30, 2014
1 parent 5d4123e commit 82bb7e1
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 31 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand Down
6 changes: 0 additions & 6 deletions src/org/kocakosm/nash/IV.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ public boolean[] getBits()
return Arrays.copyOf(bits, bits.length);
}

@Override
public String toString()
{
return Arrays.toString(bits);
}

@Override
public int hashCode()
{
Expand Down
13 changes: 0 additions & 13 deletions src/org/kocakosm/nash/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@ static boolean[] nextBits(int n)
return bits;
}

/**
* Returns a pseudo-random {@code int} value, uniformly distributed
* between 0 (inclusive) and the given value (exclusive).
*
* @param n the bound on the random number to be returned.
*
* @throws IllegalArgumentException if {@code n} is negative.
*/
static int nextInt(int n)
{
return PRNG.nextInt(n);
}

/**
* Shuffles the given values and returns them in a new array using the
* optimized version of the Fisher-Yates shuffle algorithm (Fisher,
Expand Down
63 changes: 63 additions & 0 deletions test/org/kocakosm/nash/IVTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*----------------------------------------------------------------------------*
* This file is part of Nash-Cipher. *
* Copyright (C) 2012-2013 Osman KOCAK <kocakosm@gmail.com> *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*----------------------------------------------------------------------------*/

package org.kocakosm.nash;

import static org.junit.Assert.*;

import org.junit.Test;

/**
* {@link IV}'s unit tests.
*
* @author Osman KOCAK
*/
public final class IVTest
{
@Test
public void testCreate()
{
IV iv = IV.create(32);
assertEquals(32, iv.getSize());
assertEquals(32, iv.getBits().length);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateWithNegativeSize()
{
IV.create(-1);
}

@Test
public void testEqualsAndHashCode()
{
IV iv = IV.create(32);
assertFalse(iv.equals(IV.create(16)));
assertFalse(iv.equals(IV.create(32)));
assertTrue(iv.equals(iv));
assertEquals(iv.hashCode(), iv.hashCode());
assertFalse(iv.equals((IV) (null)));
}

@Test
public void testSerialization() throws Exception
{
IV iv = IV.create(32);
IV decoded = ObjectCodec.decode(ObjectCodec.encode(iv), IV.class);
assertNotSame(iv, decoded);
assertEquals(iv, decoded);
}
}
66 changes: 66 additions & 0 deletions test/org/kocakosm/nash/KeyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*----------------------------------------------------------------------------*
* This file is part of Nash-Cipher. *
* Copyright (C) 2012-2013 Osman KOCAK <kocakosm@gmail.com> *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. *
* You should have receked a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*----------------------------------------------------------------------------*/

package org.kocakosm.nash;

import static org.junit.Assert.*;

import org.junit.Test;

/**
* {@link Key}'s unit tests.
*
* @author Osman KOCAK
*/
public final class KeyTest
{
@Test
public void testCreate()
{
Key k = Key.create(32);
assertEquals(32, k.getSize());
assertEquals(32, k.getRedBits().length);
assertEquals(32, k.getBlueBits().length);
assertEquals(32, k.getRedPermutations().length);
assertEquals(32, k.getBluePermutations().length);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateWithNegatkeSize()
{
Key.create(-1);
}

@Test
public void testEqualsAndHashCode()
{
Key k = Key.create(32);
assertFalse(k.equals(Key.create(16)));
assertFalse(k.equals(Key.create(32)));
assertTrue(k.equals(k));
assertEquals(k.hashCode(), k.hashCode());
assertFalse(k.equals((Key) (null)));
}

@Test
public void testSerialization() throws Exception
{
Key k = Key.create(32);
Key decoded = ObjectCodec.decode(ObjectCodec.encode(k), Key.class);
assertNotSame(k, decoded);
assertEquals(k, decoded);
}
}
54 changes: 46 additions & 8 deletions test/org/kocakosm/nash/NashCipherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,56 @@
*/
public final class NashCipherTest
{
private static final Random PRNG = new Random();
private final IV iv = IV.create(64);
private final Key key = Key.create(64);
private final Random prng = new Random();

@Test
public void test()
public void testCipher()
{
IV iv = IV.create(64);
Key k = Key.create(64);
NashCipher enc = new NashCipher(k, iv, NashCipher.Mode.ENCRYPTION);
NashCipher dec = new NashCipher(k, iv, NashCipher.Mode.DECRYPTION);
byte[] data = new byte[PRNG.nextInt(4096)];
PRNG.nextBytes(data);
NashCipher enc = new NashCipher(key, iv, NashCipher.Mode.ENCRYPTION);
NashCipher dec = new NashCipher(key, iv, NashCipher.Mode.DECRYPTION);
byte[] data = new byte[prng.nextInt(4096)];
prng.nextBytes(data);

assertArrayEquals(data, dec.process(enc.process(data)));
}

@Test(expected = NullPointerException.class)
public void testCreateWithNullKey()
{
new NashCipher(null, iv, NashCipher.Mode.ENCRYPTION);
}

@Test(expected = NullPointerException.class)
public void testCreateWithNullIV()
{
new NashCipher(key, null, NashCipher.Mode.ENCRYPTION);
}

@Test(expected = NullPointerException.class)
public void testCreateWithNullMode()
{
new NashCipher(key, iv, null);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateWithDifferentKeyAndIVSize()
{
new NashCipher(Key.create(4), IV.create(8), NashCipher.Mode.DECRYPTION);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testProcessWithNegativeOffset()
{
NashCipher enc = new NashCipher(key, iv, NashCipher.Mode.ENCRYPTION);
enc.process(new byte[0], -1, 0);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testProcessWithNegativeLength()
{
NashCipher enc = new NashCipher(key, iv, NashCipher.Mode.ENCRYPTION);
enc.process(new byte[0], 1, -1);
}
}
74 changes: 74 additions & 0 deletions test/org/kocakosm/nash/ObjectCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*----------------------------------------------------------------------------*
* This file is part of Nash-Cipher. *
* Copyright (C) 2012-2013 Osman KOCAK <kocakosm@gmail.com> *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*----------------------------------------------------------------------------*/

package org.kocakosm.nash;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
* Serialization utilities.
*
* @author Osman KOCAK
*/
final class ObjectCodec
{
static byte[] encode(Serializable object) throws IOException
{
ObjectOutputStream oos = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
oos = new ObjectOutputStream(out);
oos.writeObject(object);
oos.flush();
return out.toByteArray();
} finally {
close(oos);
}
}

static <T extends Serializable> T decode(byte[] object, Class<T> t)
throws IOException
{
ObjectInputStream ois = null;
ByteArrayInputStream in = new ByteArrayInputStream(object);
try {
ois = new ObjectInputStream(in);
return (T) ois.readObject();
} catch (ClassNotFoundException ex) {
throw new IOException(ex);
} finally {
close(ois);
}
}

private static void close(Closeable stream) throws IOException
{
if (stream != null) {
stream.close();
}
}

private ObjectCodec()
{
/* ... */
}
}
Loading

0 comments on commit 82bb7e1

Please sign in to comment.