-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
467 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
/* ... */ | ||
} | ||
} |
Oops, something went wrong.