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

Fix shouldPreserveAddressOrderOnInsertions test

The test failed because the ArrayList - in contrast
to the underlying Set - did not check for duplicates
on insert. Under certain circumstances this lead to
an index out of bounds exception because the list in
the test contained duplicated entries which were not
present in the set of the Socks5Proxy.
I fixed the issue by only inserting the address when
it was not in the list before.
This commit is contained in:
vanitasvitae 2017-07-03 17:26:57 +02:00
parent 12a8645864
commit 99c1c93c2a

View file

@ -30,6 +30,8 @@ import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
@ -102,13 +104,11 @@ public class Socks5ProxyTest {
@Test @Test
public void shouldPreserveAddressOrderOnInsertions() { public void shouldPreserveAddressOrderOnInsertions() {
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy(); Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
List<String> addresses = new ArrayList<>(proxy.getLocalAddresses());
LinkedHashSet<String> addresses = new LinkedHashSet<>(proxy.getLocalAddresses());
for (int i = 1 ; i <= 3; i++) { for (int i = 1 ; i <= 3; i++) {
String addr = Integer.toString(i); addresses.add(Integer.toString(i));
if (!addresses.contains(addr)) {
addresses.add(addr);
}
} }
for (String address : addresses) { for (String address : addresses) {
@ -116,8 +116,10 @@ public class Socks5ProxyTest {
} }
List<String> localAddresses = proxy.getLocalAddresses(); List<String> localAddresses = proxy.getLocalAddresses();
Iterator<String> iterator = addresses.iterator();
for (int i = 0; i < addresses.size(); i++) { for (int i = 0; i < addresses.size(); i++) {
assertEquals(addresses.get(i), localAddresses.get(i)); assertEquals(iterator.next(), localAddresses.get(i));
} }
} }
@ -249,7 +251,7 @@ public class Socks5ProxyTest {
// send valid SOCKS5 message // send valid SOCKS5 message
out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01, out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01,
(byte) 0xAA, (byte) 0x00, (byte) 0x00 }); (byte) 0xAA, (byte) 0x00, (byte) 0x00 });
// verify error message // verify error message
assertEquals((byte) 0x05, (byte) in.read()); assertEquals((byte) 0x05, (byte) in.read());
@ -297,7 +299,7 @@ public class Socks5ProxyTest {
// send valid SOCKS5 message // send valid SOCKS5 message
out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01, out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01,
(byte) 0xAA, (byte) 0x00, (byte) 0x00 }); (byte) 0xAA, (byte) 0x00, (byte) 0x00 });
// verify response // verify response
assertEquals((byte) 0x05, (byte) in.read()); assertEquals((byte) 0x05, (byte) in.read());