1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

Use assertThrows() in Socks5ByteStreamManagerTest

This commit is contained in:
Florian Schmaus 2019-06-02 11:00:37 +02:00
parent 839e347676
commit 58fc39714f

View file

@ -22,6 +22,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.io.IOException; import java.io.IOException;
@ -140,20 +141,17 @@ public class Socks5ByteStreamManagerTest {
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
try { FeatureNotSupportedException e = assertThrows(FeatureNotSupportedException.class, () -> {
// build empty discover info as reply if targets features are queried // build empty discover info as reply if targets features are queried
DiscoverInfo discoverInfo = new DiscoverInfo(); DiscoverInfo discoverInfo = new DiscoverInfo();
protocol.addResponse(discoverInfo); protocol.addResponse(discoverInfo);
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID); byteStreamManager.establishSession(targetJID);
});
fail("exception should be thrown"); assertTrue(e.getFeature().equals("SOCKS5 Bytestream"));
} assertTrue(e.getJid().equals(targetJID));
catch (FeatureNotSupportedException e) {
assertTrue(e.getFeature().equals("SOCKS5 Bytestream"));
assertTrue(e.getJid().equals(targetJID));
}
} }
/** /**
@ -196,16 +194,15 @@ public class Socks5ByteStreamManagerTest {
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver,
Verification.requestTypeGET); Verification.requestTypeGET);
try { SmackException e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
fail("exception should be thrown"); fail("exception should be thrown");
} });
catch (SmackException e) {
protocol.verifyAll(); protocol.verifyAll();
assertTrue(e.getMessage().contains("no SOCKS5 proxies available")); assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
} }
/** /**
@ -261,16 +258,13 @@ public class Socks5ByteStreamManagerTest {
protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver, protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver,
Verification.requestTypeGET); Verification.requestTypeGET);
try { SmackException e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
});
fail("exception should be thrown"); protocol.verifyAll();
} assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
catch (SmackException e) {
protocol.verifyAll();
assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
} }
/** /**
@ -325,16 +319,15 @@ public class Socks5ByteStreamManagerTest {
protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver, protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver,
Verification.requestTypeGET); Verification.requestTypeGET);
try { SmackException e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
fail("exception should be thrown"); fail("exception should be thrown");
} });
catch (SmackException e) {
protocol.verifyAll(); protocol.verifyAll();
assertTrue(e.getMessage().contains("no SOCKS5 proxies available")); assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
/* retry to establish SOCKS5 Bytestream */ /* retry to establish SOCKS5 Bytestream */
@ -344,20 +337,16 @@ public class Socks5ByteStreamManagerTest {
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver,
Verification.requestTypeGET); Verification.requestTypeGET);
try { e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
});
fail("exception should be thrown"); /*
} * #verifyAll() tests if the number of requests and responses corresponds and should
catch (SmackException e) { * fail if the invalid proxy is queried again
/* */
* #verifyAll() tests if the number of requests and responses corresponds and should protocol.verifyAll();
* fail if the invalid proxy is queried again assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
*/
protocol.verifyAll();
assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
} }
/** /**
@ -431,16 +420,13 @@ public class Socks5ByteStreamManagerTest {
protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver, protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver,
Verification.requestTypeSET); Verification.requestTypeSET);
try { XMPPErrorException e = assertThrows(XMPPErrorException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
});
fail("exception should be thrown"); protocol.verifyAll();
} assertEquals(rejectPacket.getError(), e.getStanzaError());
catch (XMPPErrorException e) {
protocol.verifyAll();
assertEquals(rejectPacket.getError(), e.getStanzaError());
}
} }
/** /**
@ -516,16 +502,13 @@ public class Socks5ByteStreamManagerTest {
protocol.addResponse(streamHostUsedPacket, Verification.correspondingSenderReceiver, protocol.addResponse(streamHostUsedPacket, Verification.correspondingSenderReceiver,
Verification.requestTypeSET); Verification.requestTypeSET);
try { SmackException e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
});
fail("exception should be thrown"); protocol.verifyAll();
} assertTrue(e.getMessage().contains("Remote user responded with unknown host"));
catch (SmackException e) {
protocol.verifyAll();
assertTrue(e.getMessage().contains("Remote user responded with unknown host"));
}
} }
/** /**
@ -609,18 +592,15 @@ public class Socks5ByteStreamManagerTest {
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET); }, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
try { IOException e = assertThrows(IOException.class, () -> {
// start SOCKS5 Bytestream // start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID); byteStreamManager.establishSession(targetJID, sessionID);
});
fail("exception should be thrown"); // initiator can't connect to proxy because it is not running
} protocol.verifyAll();
catch (IOException e) { Throwable actualCause = e.getCause().getCause();
// initiator can't connect to proxy because it is not running assertEquals(ConnectException.class, actualCause.getClass());
protocol.verifyAll();
Throwable actualCause = e.getCause().getCause();
assertEquals(ConnectException.class, actualCause.getClass());
}
} }
/** /**