Compare commits

..

4 Commits

Author SHA1 Message Date
0c4611c0bd Revert ee06171a2f
They were due to an autoformatting of the editor and not necessary for the merge request.
2021-01-20 17:48:49 +01:00
44c3d8cc0c ConvertToHashMockTest: Clean up after using TestContext 2021-01-19 23:18:18 +01:00
d888eee6d1 Use TestContext to replace existing I2PAppContext
This allows us to mock parts of the I2PAppContext as we like.
The mocking is done when `testContext` is created in the constructor,
 which replaces the existing global context.
2021-01-19 22:58:45 +01:00
Zlatin Balevsky
ee06171a2f example usage of TestContext 2021-01-19 22:58:45 +01:00
8 changed files with 72 additions and 311 deletions

View File

@@ -46,7 +46,7 @@
</td>
</tr>
<tr>
<th id="upnpconfig"><%=intl._t("UPnP Configuration")%>&nbsp;<a href="peers?tx=upnp">[<%=intl._t("UPnP Status")%>]</a></th>
<th id="upnpconfig"><%=intl._t("UPnP Configuration")%>&nbsp;<a href="peers#upnp">[<%=intl._t("UPnP Status")%>]</a></th>
</tr>
<tr>
<td>

View File

@@ -0,0 +1,17 @@
package net.i2p;
public class TestContext extends I2PAppContext {
public TestContext() {
TestContext.setGlobalContext(this);
}
/**
* Allows overriding the existing I2PAppContext with a test context who's fields we may mock as we like
*
* @param ctx Our test context to replace the global context with
*/
public static void setGlobalContext(TestContext ctx){
_globalAppContext = ctx;
}
}

View File

@@ -1,195 +0,0 @@
package net.i2p.socks;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import sun.net.util.IPAddressUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import static net.i2p.socks.SOCKS4Constants.SOCKS_VERSION_4;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;
/**
* @since 0.9.49
*/
public class SOCKS4ClientTest {
ByteArrayInputStream inputStream;
ByteArrayOutputStream outputStream;
@Before
public void openStreams(){
outputStream = new ByteArrayOutputStream();
}
@After
public void closeStreams() throws IOException {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
/**
* A successful connection to an IPv4 host
*/
@Test
public void connect() throws IOException {
_testConnect(false);
}
/**
* A successful connection to an IPv4 host using a socket
*/
@Test
public void connect__withSocket() throws IOException {
_testConnect(true);
}
private void _testConnect(boolean useSocket) throws IOException {
String hostIPv4 = "11.22.33.44";
int connectionPort = 8080;
byte[] hostIPv4Bin = IPAddressUtil.textToNumericFormatV4(hostIPv4);
// Build sequence of bytes to be expected
ByteArrayOutputStream expectedByteStream = new ByteArrayOutputStream();
DataOutputStream writerStream = new DataOutputStream(expectedByteStream);
writerStream.writeByte(SOCKS_VERSION_4);
writerStream.writeByte(SOCKS4Constants.Command.CONNECT);
writerStream.writeShort(connectionPort);
writerStream.write(hostIPv4Bin);
writerStream.write((byte) 0);
inputStream = new ByteArrayInputStream(new byte[]{
0, // dummy
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
0, 0, 0, 0, 0, 0 // filler
});
outputStream = new ByteArrayOutputStream();
// Test overloaded function
try {
if (useSocket) {
Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
SOCKS4Client.connect(socket, hostIPv4, connectionPort);
} else {
SOCKS4Client.connect(inputStream, outputStream, hostIPv4, connectionPort);
}
} finally {
writerStream.close();
}
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
}
/**
* Connect proxy with a domain name
*/
@Test
public void connect__host() throws IOException {
String host = "stats.i2p";
int connectionPort = 80;
// Build sequence of bytes to be expected
ByteArrayOutputStream expectedByteStream = new ByteArrayOutputStream();
DataOutputStream writerStream = new DataOutputStream(expectedByteStream);
writerStream.writeByte(SOCKS_VERSION_4);
writerStream.writeByte(SOCKS4Constants.Command.CONNECT);
writerStream.writeShort(connectionPort);
writerStream.write(new byte[]{0,0,0,1}); // 0.0.0.1
writerStream.write((byte) 0); // empty userID
writerStream.write(host.getBytes(StandardCharsets.ISO_8859_1));
writerStream.write((byte) 0);
inputStream = new ByteArrayInputStream(new byte[]{
0, // dummy
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
0, 0, 0, 0, 0, 0 // filler
});
try {
SOCKS4Client.connect(SOCKS4ClientTest.this.inputStream, outputStream, host, connectionPort);
} finally {
expectedByteStream.close();
}
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
}
/**
* Run into IOException while trying to connect due to no input/response
*/
@Test
public void connect__ioException() {
inputStream = new ByteArrayInputStream(new byte[]{});
assertThrows(IOException.class, () -> {
SOCKS4Client.connect(
inputStream,
outputStream,
"127.0.0.1",
80);
});
}
/**
* Run into IOException while trying to connect due to closed input stream
*/
@Test
public void connect__ioExceptionWithSocket() {
inputStream = new ByteArrayInputStream(new byte[]{});
assertThrows(IOException.class, () -> {
Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
SOCKS4Client.connect(
socket,
"127.0.0.1",
80
);
});
}
/**
* Check that CONNECTION_REFUSED throws exception
*/
@Test
public void connect__responseCONNECTION_REFUSED() throws IOException {
inputStream = new ByteArrayInputStream(new byte[]{
0, // dummy
SOCKS4Constants.Reply.CONNECTION_REFUSED, // Connection succeeded
});
assertThrows(SOCKSException.class, () -> {
SOCKS4Client.connect(inputStream,
outputStream,
"1.1.1.1",
80
);
});
}
/**
* IPv6 is not supported by this SOCKS client so it just throws an exception
*/
@Test
public void connect__IPv6() {
inputStream = new ByteArrayInputStream(new byte[]{});
assertThrows(SOCKSException.class, () -> {
SOCKS4Client.connect(
inputStream,
outputStream,
"::1",
80);
});
}
}

View File

@@ -0,0 +1,51 @@
package net.i2p.util;
import net.i2p.TestContext;
import net.i2p.client.naming.NamingService;
import net.i2p.data.Destination;
import net.i2p.data.Hash;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
public class ConvertToHashMockTest{
@Mock private NamingService namingService;
@Mock private Destination destination;
@Mock private Hash hash;
@InjectMocks TestContext testContext;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
}
/**
* Reset the global context after all tests in the class are done.
*
* We would otherwise pollute the other tests that depend on I2PAppContext
*/
@AfterClass
public static void afterClass(){
TestContext.setGlobalContext(null);
}
@Test
public void testMockedDestination() {
when(namingService.lookup("zzz.i2p")).thenReturn(destination);
when(destination.calculateHash()).thenReturn(hash);
assertSame(hash, ConvertToHash.getHash("zzz.i2p"));
verify(namingService).lookup("zzz.i2p");
verify(destination).calculateHash();
}
}

View File

@@ -1,69 +0,0 @@
package net.i2p.util;
import net.i2p.data.Hash;
import org.junit.Test;
import static org.junit.Assert.*;
public class ConvertToHashTest {
private static final String zzzDotI2pBase32Hash = "lhbd7ojcaiofbfku7ixh47qj537g572zmhdc4oilvugzxdpdghua";
private static final String zzzDotI2pBase64Hash = "WcI~uSICHFCVVPoufn4J7v5u~1lhxi45C60Nm43jMeg=";
private static final String zzzDotI2pBase64Dest = "GKapJ8koUcBj~jmQzHsTYxDg2tpfWj0xjQTzd8BhfC9c3OS5fwPBNajgF-eOD6eCjFTqTlorlh7Hnd8kXj1qblUGXT-tDoR9~YV8dmXl51cJn9MVTRrEqRWSJVXbUUz9t5Po6Xa247Vr0sJn27R4KoKP8QVj1GuH6dB3b6wTPbOamC3dkO18vkQkfZWUdRMDXk0d8AdjB0E0864nOT~J9Fpnd2pQE5uoFT6P0DqtQR2jsFvf9ME61aqLvKPPWpkgdn4z6Zkm-NJOcDz2Nv8Si7hli94E9SghMYRsdjU-knObKvxiagn84FIwcOpepxuG~kFXdD5NfsH0v6Uri3usE3XWD7Pw6P8qVYF39jUIq4OiNMwPnNYzy2N4mDMQdsdHO3LUVh~DEppOy9AAmEoHDjjJxt2BFBbGxfdpZCpENkwvmZeYUyNCCzASqTOOlNzdpne8cuesn3NDXIpNnqEE6Oe5Qm5YOJykrX~Vx~cFFT3QzDGkIjjxlFBsjUJyYkFjBQAEAAcAAA==";
@Test
public void getHashNullPeer() {
assertNull(ConvertToHash.getHash(null));
}
@Test
public void getHashB64() {
Hash hash = ConvertToHash.getHash(zzzDotI2pBase64Hash);
assertNotNull(hash);
assertEquals(hash.toBase64(), zzzDotI2pBase64Hash);
}
@Test
public void getHashB64DotI2P() {
Hash hash = ConvertToHash.getHash(zzzDotI2pBase64Hash + ".i2p");
assertNotNull(hash);
assertEquals(hash.toBase64(), zzzDotI2pBase64Hash);
}
@Test
public void getHashDestinationB64() {
Hash hash = ConvertToHash.getHash(zzzDotI2pBase64Dest);
assertNotNull(hash);
assertEquals(hash.toBase64(), zzzDotI2pBase64Hash);
}
@Test
public void getHashDestinationB64DotI2P() {
Hash hash = ConvertToHash.getHash(zzzDotI2pBase64Dest + ".i2p");
assertNotNull(hash);
assertEquals(hash.toBase64(), zzzDotI2pBase64Hash);
}
@Test
public void getHashB32() {
Hash hash = ConvertToHash.getHash(zzzDotI2pBase32Hash);
assertNotNull(hash);
assertEquals(hash.toBase32(), zzzDotI2pBase32Hash + ".b32.i2p");
}
@Test
public void getHashB32DotI2P() {
String zzzB32I2P = zzzDotI2pBase32Hash + ".b32.i2p";
Hash hash = ConvertToHash.getHash(zzzB32I2P);
assertNotNull(hash);
assertEquals(hash.toBase32(), zzzB32I2P);
}
/**
* The case where a destination cannot be resolved at all
*/
@Test
public void getHashResolveDestinationFail() {
assertNull(ConvertToHash.getHash("unknown.i2p"));
}
}

View File

@@ -1,22 +1,3 @@
2021-01-20 zzz
* Console: Fix link to UPnP status
* SSU: Fix deadlock with router restart
2021-01-14 zzz
* Router:
- Change default encryption type to ECIES-X25519 (proposal 156)
- Move Sybil subsystem from console to router
- Limit max addresses in RI
2021-01-13 zzz
* Jetty: Fix URI in request logs
2021-01-12 zzz
* i2psnark: Don't decrement downloaded counter after receiving bad piece
2021-01-11 zzz
* Console: Delete rrd files for no-longer-configured stats at startup
2021-01-08 zzz
* i2ptunnel: Disable shared clients (DSA) (part 2)
* SSU: Fix bandwidth estimator deadlock (ticket #2798)

View File

@@ -18,7 +18,7 @@ public class RouterVersion {
/** deprecated */
public final static String ID = "Monotone";
public final static String VERSION = CoreVersion.VERSION;
public final static long BUILD = 14;
public final static long BUILD = 13;
/** for example "-test" */
public final static String EXTRA = "";

View File

@@ -2455,7 +2455,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
// warning, this calls back into us with allowRebuildRouterInfo = false,
// via CSFI.createAddresses->TM.getAddresses()->updateAddress()->REA
if (allowRebuildRouterInfo)
rebuildRouterInfo();
_context.router().rebuildRouterInfo();
} else {
addr = null;
}
@@ -2510,35 +2510,11 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
// warning, this calls back into us with allowRebuildRouterInfo = false,
// via CSFI.createAddresses->TM.getAddresses()->updateAddress()->REA
if (allowRebuildRouterInfo)
rebuildRouterInfo();
_context.router().rebuildRouterInfo();
}
}
}
/**
* Avoid deadlocks part 999
* @since 0.9.49
*/
private void rebuildRouterInfo() {
(new RebuildEvent()).schedule(0);
}
/**
* @since 0.9.49
*/
private class RebuildEvent extends SimpleTimer2.TimedEvent {
/**
* Caller must schedule
*/
public RebuildEvent() {
super(_context.simpleTimer2());
}
public void timeReached() {
_context.router().rebuildRouterInfo(true);
}
}
/**
* Simple fetch of stored IP and port, since
* we don't put them in the real, published RouterAddress anymore