propagate from branch 'i2p.i2p' (head 3002b47d5d20180f84fb6a4f161823bc751989be)

to branch 'i2p.i2p.unittests' (head 97dc8de19916c3d0c7ec42790800c1e23f9ce9e8)
This commit is contained in:
str4d
2012-03-30 05:10:53 +00:00
13 changed files with 434 additions and 139 deletions

View File

@@ -28,86 +28,94 @@ import net.i2p.data.Hash;
*/
public class DatagramTest extends TestCase {
private I2PClient _client;
public void setUp(){
}
protected void tearDown() {
System.gc();
}
public void testDatagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramMaker dm = new I2PDatagramMaker(session);
byte[] dg = dm.makeI2PDatagram("What's the deal with 42?".getBytes());
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(dg);
byte[] x = dd.getPayload();
assertTrue(DataHelper.eq(x, "What's the deal with 42?".getBytes()));
x = dd.extractPayload();
assertTrue(DataHelper.eq(x, "What's the deal with 42?".getBytes()));
assertEquals(d, dd.getSender());
assertEquals(d, dd.extractSender());
}
/*public void testMakeNullDatagram() throws Exception{
public void testMakeNullDatagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramMaker dm = new I2PDatagramMaker(session);
byte[] dg = dm.makeI2PDatagram(null);
assertNull(dg);
}*/
/*public void testExtractNullDatagram() throws Exception{
}
public void testExtractNullDatagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(null);
}*/
}
public void testBadagram() throws Exception{
ByteArrayOutputStream out = new ByteArrayOutputStream();
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(out);
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
DSAEngine dsaEng = DSAEngine.getInstance();
ByteArrayOutputStream dout = new ByteArrayOutputStream();
d.writeBytes(dout);
dsaEng.sign(Hash.FAKE_HASH.toByteArray(), session.getPrivateKey()).writeBytes(dout);
dout.write("blah".getBytes());
byte[] data = dout.toByteArray();
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(data);
boolean error = false;
try{
dd.getPayload();
}catch(I2PInvalidDatagramException i2pide){
error = true;
}
assertTrue(error);
error = false;
try{
dd.getSender();
}catch(I2PInvalidDatagramException i2pide){
error = true;
}
assertTrue(error);
error = false;
try{
dd.getHash();
}catch(I2PInvalidDatagramException i2pide){
error = true;
}
assertTrue(error);
}
}
}

View File

@@ -0,0 +1,65 @@
package net.i2p.client.naming;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import junit.framework.TestCase;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
public class BlockfileNamingServiceTest extends TestCase {
BlockfileNamingService _bns;
List<String> _names;
public void setUp() {
I2PAppContext ctx = new I2PAppContext();
_bns = new BlockfileNamingService(ctx);
_names = null;
Properties props = new Properties();
try {
DataHelper.loadProps(props, new File("../../installer/resources/hosts.txt"), true);
_names = new ArrayList(props.keySet());
Collections.shuffle(_names);
} catch (IOException ioe) {
_bns.shutdown();
return;
}
}
public void tearDown() {
_bns.shutdown();
File f = new File("hostsdb.blockfile");
f.delete();
}
public void testRepeatedLookup() throws Exception{
int found = 0;
int notfound = 0;
int rfound = 0;
int rnotfound = 0;
for (String name : _names) {
Destination dest = _bns.lookup(name);
if (dest != null) {
found++;
String reverse = _bns.reverseLookup(dest);
if (reverse != null)
rfound++;
else
rnotfound++;
} else {
notfound++;
}
}
assertEquals(0, notfound);
assertEquals(0, rnotfound);
}
}

View File

@@ -0,0 +1,45 @@
package net.i2p.client.naming;
import junit.framework.TestCase;
import net.i2p.I2PAppContext;
import net.i2p.data.Destination;
public class DummyNamingServiceTest extends TestCase {
private I2PAppContext _context;
public void setUp() {
_context = new I2PAppContext();
}
public void testLookup() throws Exception{
// The good b64 and b32 are the destination of www.i2p2.i2p =)
String goodB64 = "-KR6qyfPWXoN~F3UzzYSMIsaRy4quickbrownfoxXSzUQXQdi2Af1TV2UMH3PpPuNu-GwrqihwmLSkPFg4fv4yQQY3E10VeQVuI67dn5vlan3NGMsjqxoXTSHHt7C3nX3szXK90JSoO~tRMDl1xyqtKm94-RpIyNcLXofd0H6b02683CQIjb-7JiCpDD0zharm6SU54rhdisIUVXpi1xYgg2pKVpssL~KCp7RAGzpt2rSgz~RHFsecqGBeFwJdiko-6CYW~tcBcigM8ea57LK7JjCFVhOoYTqgk95AG04-hfehnmBtuAFHWklFyFh88x6mS9sbVPvi-am4La0G0jvUJw9a3wQ67jMr6KWQ~w~bFe~FDqoZqVXl8t88qHPIvXelvWw2Y8EMSF5PJhWw~AZfoWOA5VQVYvcmGzZIEKtFGE7bgQf3rFtJ2FAtig9XXBsoLisHbJgeVb29Ew5E7bkwxvEe9NYkIqvrKvUAt1i55we0Nkt6xlEdhBqg6xXOyIAAAA";
String goodB32 = "rjxwbsw4zjhv4zsplma6jmf5nr24e4ymvvbycd3swgiinbvg7oga.b32.i2p";
// TODO: Come up with an actual bad b64 and b32
String badB64 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
String badB32 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.b32.i2p";
DummyNamingService ns = new DummyNamingService(_context);
assertNull(ns.lookup(""));
// TODO: Could this case ever come up?
//assertNull(ns.lookup(null));
Destination dGoodB64 = ns.lookup(goodB64);
assertNotNull(dGoodB64);
// TODO: Check that the b64 is preserved.
Destination dGoodB32 = ns.lookup(goodB32);
assertNotNull(dGoodB32);
// TODO: Check that the b32 is preserved.
// TODO: Come up with an actual bad b64 and b32
//assertNull(ns.lookup(badB64));
//assertNull(ns.lookup(badB32));
// DummyNameService only handles b64 and b32 addresses
assertNull(ns.lookup("www.i2p2.i2p"));
}
}

View File

@@ -0,0 +1,66 @@
package net.i2p.client.naming;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import junit.framework.TestCase;
import java.io.File;
import java.util.Collections;
import net.i2p.I2PAppContext;
import net.i2p.data.Destination;
public class SingleFileNamingServiceTest extends TestCase {
private I2PAppContext _context;
public void setUp() {
_context = new I2PAppContext();
}
public void tearDown() {
File f = new File("testhosts.txt");
f.delete();
}
public void testAddRemoveLookup() throws Exception{
String testB64 = "-KR6qyfPWXoN~F3UzzYSMIsaRy4quickbrownfoxXSzUQXQdi2Af1TV2UMH3PpPuNu-GwrqihwmLSkPFg4fv4yQQY3E10VeQVuI67dn5vlan3NGMsjqxoXTSHHt7C3nX3szXK90JSoO~tRMDl1xyqtKm94-RpIyNcLXofd0H6b02683CQIjb-7JiCpDD0zharm6SU54rhdisIUVXpi1xYgg2pKVpssL~KCp7RAGzpt2rSgz~RHFsecqGBeFwJdiko-6CYW~tcBcigM8ea57LK7JjCFVhOoYTqgk95AG04-hfehnmBtuAFHWklFyFh88x6mS9sbVPvi-am4La0G0jvUJw9a3wQ67jMr6KWQ~w~bFe~FDqoZqVXl8t88qHPIvXelvWw2Y8EMSF5PJhWw~AZfoWOA5VQVYvcmGzZIEKtFGE7bgQf3rFtJ2FAtig9XXBsoLisHbJgeVb29Ew5E7bkwxvEe9NYkIqvrKvUAt1i55we0Nkt6xlEdhBqg6xXOyIAAAA";
Destination testDest = new Destination();
testDest.fromBase64(testB64);
String testB642 = "24SmhWiRDm-GzpV5Gq2sXhuvPpa1OihY7rkxQO4aHy5qKjr6zmEnZ3xQXdkFJJ0Z1lKy73XRmgCyys02G25Hl3cuxlZ2fNbp6KhOzlRKpOIAWFdSWZNF4Fp7sos0x-a-9fxOWnwwQ9MFcRYwixE~iCZf4JG~-Pd-MHgAuDhIX0P3~GmfUvo~9xPjof1ZsnaOV1zC0XUkHxZA5D6V0Bse~Ptfb66lPNcgBxIEntCStBAy~rTjaA3SdAufG29IRWDscpFq1-D4XPaXHnlXu7n7WdpFEM8WWd3ebUMqnq8XvLL1eqoWYzKCe3aaavC3W6~pJp8cxKl2IKrhvSFatHZ0chRg3B4~ja1Cxmw1psisplSkJqMnF921E6pury0i6GH52XAVoj4iiDY~EAvqDhzG-ThwlzTs~2JKzslwxOrD2ejd-dcKdi4i9xvi2JQ4Ib2Mw2ktaQhuAw3Y9EkqAs7oriQQN8N8dwIoYkJLfvh7ousm0iKJJvMt3s55PccM46SoAAAA";
Destination testDest2 = new Destination();
testDest2.fromBase64(testB642);
SingleFileNamingService ns = new SingleFileNamingService(_context, "testhosts.txt");
// testhosts.txt is empty.
assertThat(ns.size(), is(equalTo(0)));
assertThat(ns.getEntries(), is(equalTo(Collections.EMPTY_MAP)));
assertThat(ns.lookup("test.i2p"), is(nullValue()));
assertThat(ns.reverseLookup(testDest), is(nullValue()));
// First put should add the hostname.
ns.put("test.i2p", testDest);
assertThat(ns.size(), is(equalTo(1)));
assertThat(ns.getEntries().size(), is(equalTo(1)));
assertThat(ns.lookup("test.i2p"), is(equalTo(testDest)));
assertThat(ns.reverseLookup(testDest), is(equalTo("test.i2p")));
// Second put should replace the first.
ns.put("test.i2p", testDest2);
assertThat(ns.size(), is(equalTo(1)));
assertThat(ns.lookup("test.i2p"), is(equalTo(testDest2)));
assertThat(ns.reverseLookup(testDest2), is(equalTo("test.i2p")));
assertThat(ns.lookup("test.i2p"), is(not(equalTo(testDest))));
assertThat(ns.reverseLookup(testDest), is(nullValue()));
// Removing the hostname should give an empty file again.
ns.remove("test.i2p");
assertThat(ns.lookup("test.i2p"), is(nullValue()));
assertThat(ns.reverseLookup(testDest2), is(nullValue()));
// Odd quirk - the above lookups don't update size, but getEntries() does...
assertThat(ns.size(), is(equalTo(1)));
assertThat(ns.getEntries(), is(equalTo(Collections.EMPTY_MAP)));
assertThat(ns.size(), is(equalTo(0)));
}
}

View File

@@ -24,7 +24,6 @@ public class CryptoTestSuite {
suite.addTestSuite(AESInputStreamTest.class);
suite.addTestSuite(CryptixAESEngineTest.class);
suite.addTestSuite(CryptixRijndael_AlgorithmTest.class);
suite.addTestSuite(DHSessionKeyBuilderTest.class);
suite.addTestSuite(DSATest.class);
suite.addTestSuite(ElGamalTest.class);
suite.addTestSuite(HMACSHA256Test.class);
@@ -35,4 +34,4 @@ public class CryptoTestSuite {
return suite;
}
}
}

View File

@@ -1,49 +0,0 @@
package net.i2p.crypto;
/*
* free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain
* with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk.
*
*/
import java.math.BigInteger;
import junit.framework.TestCase;
import net.i2p.I2PAppContext;
import net.i2p.data.SessionKey;
import net.i2p.util.RandomSource;
public class DHSessionKeyBuilderTest extends TestCase {
public void testDHSessionKeyBuilder(){
I2PAppContext ctx = new I2PAppContext();
for (int i = 0; i < 5; i++) {
DHSessionKeyBuilder builder1 = new DHSessionKeyBuilder();
DHSessionKeyBuilder builder2 = new DHSessionKeyBuilder();
BigInteger pub1 = builder1.getMyPublicValue();
BigInteger pub2 = builder2.getMyPublicValue();
try {
builder2.setPeerPublicValue(pub1);
builder1.setPeerPublicValue(pub2);
} catch (DHSessionKeyBuilder.InvalidPublicParameterException ippe) {
assertTrue(ippe.getMessage(), true);
}
SessionKey key1 = builder1.getSessionKey();
SessionKey key2 = builder2.getSessionKey();
assertEquals(key1, key2);
byte iv[] = new byte[16];
RandomSource.getInstance().nextBytes(iv);
String origVal = "1234567890123456"; // 16 bytes max using AESEngine
byte enc[] = new byte[16];
byte dec[] = new byte[16];
ctx.aes().encrypt(origVal.getBytes(), 0, enc, 0, key1, iv, 16);
ctx.aes().decrypt(enc, 0, dec, 0, key2, iv, 16);
String tranVal = new String(dec);
assertEquals(origVal, tranVal);
}
}
}

View File

@@ -372,8 +372,10 @@ public class ElGamalTest extends TestCase{
public void testYKGen(){
RandomSource.getInstance().nextBoolean();
I2PAppContext context = new I2PAppContext();
YKGenerator ykgen = new YKGenerator(context);
for (int i = 0; i < 5; i++) {
YKGenerator.getNextYK();
ykgen.getNextYK();
}
}
}
}

View File

@@ -28,7 +28,8 @@ public class LeaseTest extends StructureTest {
return lease;
}
public DataStructure createStructureToRead() { return new Lease(); }
/* TODO: Delete this if Lease.getNumSuccess() / getNumFailure() get deleted
public void testNumSuccessFail() throws Exception{
Lease lease = new Lease();
lease.setEndDate(new Date(1000*60*2));
@@ -40,7 +41,8 @@ public class LeaseTest extends StructureTest {
lease.getNumSuccess();
lease.getNumFailure();
}
*/
public void testExpiration() throws Exception{
Lease lease = new Lease();
assertTrue(lease.isExpired());

View File

@@ -33,10 +33,37 @@ public class RouterAddressTest extends StructureTest {
return addr;
}
public DataStructure createStructureToRead() { return new RouterAddress(); }
public void testSetNullOptions(){
RouterAddress addr = new RouterAddress();
boolean error = false;
try{
addr.setOptions(null);
}catch(NullPointerException dfe){
error = true;
}
assertTrue(error);
}
public void testSetOptionsAgain(){
RouterAddress addr = new RouterAddress();
Properties options = new Properties();
options.setProperty("hostname", "localhost");
options.setProperty("portnum", "1234");
addr.setOptions(options);
options.setProperty("portnum", "2345");
boolean error = false;
try{
addr.setOptions(options);
}catch(IllegalStateException dfe){
error = true;
}
assertTrue(error);
}
public void testBadWrite() throws Exception{
RouterAddress addr = new RouterAddress();
boolean error = true;
boolean error = false;
try{
addr.writeBytes(new ByteArrayOutputStream());
}catch(DataFormatException dfe){
@@ -44,7 +71,7 @@ public class RouterAddressTest extends StructureTest {
}
assertTrue(error);
}
public void testNullEquals(){
RouterAddress addr = new RouterAddress();
byte data[] = new byte[32];
@@ -60,7 +87,7 @@ public class RouterAddressTest extends StructureTest {
assertFalse(addr.equals(null));
assertFalse(addr.equals(""));
}
public void testToString(){
RouterAddress addr = new RouterAddress();
byte data[] = new byte[32];
@@ -73,8 +100,7 @@ public class RouterAddressTest extends StructureTest {
options.setProperty("portnum", "1234");
addr.setOptions(options);
addr.setTransportStyle("Blah");
addr.toString();
addr.setOptions(null);
addr.toString();
String ret = addr.toString();
assertEquals("[RouterAddress: \n\tTransportStyle: Blah\n\tCost: 42\n\tExpiration: Fri Jan 02 00:00:00 UTC 1970\n\tOptions: #: 2\n\t\t[hostname] = [localhost]\n\t\t[portnum] = [1234]]", ret);
}
}

View File

@@ -6,6 +6,30 @@ import junit.framework.TestCase;
public class RateStatTest extends TestCase {
public void testGettersEtc() throws Exception{
long emptyArray[] = new long[0];
RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", emptyArray);
// Test basic getters
assertEquals("test", rs.getName());
assertEquals("tests", rs.getGroupName());
assertEquals("test RateStat getters etc", rs.getDescription());
// There should be no periods, so other getters should return defaults
// TODO: Fix this so it checks that the array is empty rather than comparing objects
//assertEquals(rs.getPeriods(), emptyArray);
assertEquals(0.0, rs.getLifetimeAverageValue());
assertEquals(0, rs.getLifetimeEventCount());
assertNull(rs.getRate(2000));
// Test adding and removing a period
assertFalse(rs.containsRate(1000));
rs.addRate(1000);
assertTrue(rs.containsRate(1000));
rs.removeRate(1000);
assertFalse(rs.containsRate(1000));
}
public void testRateStat() throws Exception{
RateStat rs = new RateStat("moo", "moo moo moo", "cow trueisms", new long[] { 60 * 1000, 60 * 60 * 1000,
24 * 60 * 60 * 1000});
@@ -30,4 +54,4 @@ public class RateStatTest extends TestCase {
assertEquals(rs, loadedRs);
}
}
}