Test Socks4Client
Confirms that the SOCKS4a CONNECT spec was implemented correctly. Once the format of the tests is confirmed and the MR merged, the SOCKS5 client and then the server components can be tested - that is, if the server doesn't make too much use of private static variables.
Merge request reports
Activity
assigned to @zzz
3 import org.junit.Test; 4 import org.mockito.Mockito; 5 import sun.net.util.IPAddressUtil; 6 7 import java.io.ByteArrayInputStream; 8 import java.io.ByteArrayOutputStream; 9 import java.io.DataOutputStream; 10 import java.io.IOException; 11 import java.net.Socket; 12 import java.nio.charset.StandardCharsets; 13 14 import static net.i2p.socks.SOCKS4Constants.SOCKS_VERSION_4; 15 import static org.junit.Assert.assertArrayEquals; 16 import static org.junit.Assert.assertThrows; 17 18 public class SOCKS4ClientTest { 82 writerStream.writeShort(connectionPort); 83 writerStream.write(new byte[]{0,0,0,1}); // 0.0.0.1 84 writerStream.write((byte) 0); // empty userID 85 writerStream.write(host.getBytes(StandardCharsets.ISO_8859_1)); 86 writerStream.write((byte) 0); 87 88 ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{ 89 0, // dummy 90 SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded 91 0, 0, 0, 0, 0, 0 // filler 92 }); 93 ByteArrayOutputStream ops = new ByteArrayOutputStream(); 94 95 SOCKS4Client.connect(ips, ops, host, connectionPort); 96 assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray()); 97 } You're not closing the socket or streams here as the SOCKS4Client javadocs say you should. I guess it doesn't really matter because they are BAIS/BAOS or a mocked socket, but it's still good practice to close everything in a finally{} block. This also applies to all the other tests below.
lets ask @zlatinb
Modified. Used
@After
andtry/finally
where necessary in 1c1b1e87.Could you please review the changes?
Edited by LoveIsGrief
Also please look at the comments at the bottom of recently-merged MR !15 (merged) about how the mocking dependencies weren't added to build.xml. let's make sure this one gets tested before merging.
added 2 commits