|
| 1 | +package quickfix.mina; |
| 2 | + |
| 3 | +import org.apache.mina.util.AvailablePortFinder; |
| 4 | +import org.junit.After; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | +import quickfix.Acceptor; |
| 8 | +import quickfix.ApplicationAdapter; |
| 9 | +import quickfix.ConfigError; |
| 10 | +import quickfix.DefaultMessageFactory; |
| 11 | +import quickfix.FixVersions; |
| 12 | +import quickfix.Initiator; |
| 13 | +import quickfix.MemoryStoreFactory; |
| 14 | +import quickfix.MessageFactory; |
| 15 | +import quickfix.MessageStoreFactory; |
| 16 | +import quickfix.Session; |
| 17 | +import quickfix.SessionFactory; |
| 18 | +import quickfix.SessionID; |
| 19 | +import quickfix.SessionSettings; |
| 20 | +import quickfix.ThreadedSocketAcceptor; |
| 21 | +import quickfix.ThreadedSocketInitiator; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +/** |
| 27 | + * Performs end to end tests against SOCKS proxy server. |
| 28 | + */ |
| 29 | +public class SocksProxyTest { |
| 30 | + |
| 31 | + // maximum time to wait for session logon |
| 32 | + private static final long TIMEOUT_SECONDS = 5; |
| 33 | + |
| 34 | + private SocksProxyServer proxyServer; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setUp() { |
| 38 | + int proxyPort = AvailablePortFinder.getNextAvailable(); |
| 39 | + |
| 40 | + proxyServer = new SocksProxyServer(proxyPort); |
| 41 | + proxyServer.start(); |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + public void tearDown() { |
| 46 | + proxyServer.stop(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void shouldLoginViaSocks4Proxy() throws ConfigError { |
| 51 | + shouldLoginSocksProxy("4"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldLoginViaSocks4aProxy() throws ConfigError { |
| 56 | + shouldLoginSocksProxy("4a"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldLoginViaSocks5Proxy() throws ConfigError { |
| 61 | + shouldLoginSocksProxy("5"); |
| 62 | + } |
| 63 | + |
| 64 | + private void shouldLoginSocksProxy(String proxyVersion) throws ConfigError { |
| 65 | + int port = AvailablePortFinder.getNextAvailable(); |
| 66 | + SessionConnector acceptor = createAcceptor(port); |
| 67 | + |
| 68 | + try { |
| 69 | + acceptor.start(); |
| 70 | + |
| 71 | + SessionConnector initiator = createInitiator(proxyVersion, proxyServer.getPort(), port); |
| 72 | + |
| 73 | + try { |
| 74 | + initiator.start(); |
| 75 | + assertLoggedOn(acceptor, new SessionID(FixVersions.BEGINSTRING_FIX44, "ALICE", "BOB")); |
| 76 | + assertLoggedOn(initiator, new SessionID(FixVersions.BEGINSTRING_FIX44, "BOB", "ALICE")); |
| 77 | + } finally { |
| 78 | + initiator.stop(); |
| 79 | + } |
| 80 | + } finally { |
| 81 | + acceptor.stop(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void assertLoggedOn(SessionConnector connector, SessionID sessionID) { |
| 86 | + long startTimeNanos = System.nanoTime(); |
| 87 | + |
| 88 | + while (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTimeNanos) < TIMEOUT_SECONDS) { |
| 89 | + if (isLoggedOn(connector, sessionID)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + Thread.sleep(100); |
| 95 | + } catch (InterruptedException e) { |
| 96 | + Thread.currentThread().interrupt(); |
| 97 | + throw new RuntimeException("Interrupted", e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + throw new AssertionError("Session " + sessionID + " is not logged on"); |
| 102 | + } |
| 103 | + |
| 104 | + private boolean isLoggedOn(SessionConnector connector, SessionID sessionID) { |
| 105 | + Session session = connector.getSessionMap().get(sessionID); |
| 106 | + |
| 107 | + if (session == null) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + return session.isLoggedOn(); |
| 112 | + } |
| 113 | + |
| 114 | + private SessionConnector createAcceptor(int port) throws ConfigError { |
| 115 | + MessageStoreFactory messageStoreFactory = new MemoryStoreFactory(); |
| 116 | + MessageFactory messageFactory = new DefaultMessageFactory(); |
| 117 | + SessionSettings acceptorSettings = createAcceptorSettings("ALICE", "BOB", port); |
| 118 | + return new ThreadedSocketAcceptor(new ApplicationAdapter(), messageStoreFactory, acceptorSettings, messageFactory); |
| 119 | + } |
| 120 | + |
| 121 | + private SessionConnector createInitiator(String proxyVersion, int proxyPort, int port) throws ConfigError { |
| 122 | + MessageStoreFactory messageStoreFactory = new MemoryStoreFactory(); |
| 123 | + MessageFactory messageFactory = new DefaultMessageFactory(); |
| 124 | + SessionSettings initiatorSettings = createInitiatorSettings("BOB", "ALICE", proxyVersion, proxyPort, port); |
| 125 | + return new ThreadedSocketInitiator(new ApplicationAdapter(), messageStoreFactory, initiatorSettings, messageFactory); |
| 126 | + } |
| 127 | + |
| 128 | + private SessionSettings createAcceptorSettings(String senderId, String targetId, int port) { |
| 129 | + HashMap<Object, Object> defaults = new HashMap<>(); |
| 130 | + defaults.put(SessionFactory.SETTING_CONNECTION_TYPE, "acceptor"); |
| 131 | + defaults.put(Acceptor.SETTING_SOCKET_ACCEPT_PORT, Integer.toString(port)); |
| 132 | + defaults.put(Session.SETTING_START_TIME, "00:00:00"); |
| 133 | + defaults.put(Session.SETTING_END_TIME, "00:00:00"); |
| 134 | + defaults.put(Session.SETTING_HEARTBTINT, "30"); |
| 135 | + |
| 136 | + SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX44, senderId, targetId); |
| 137 | + |
| 138 | + SessionSettings sessionSettings = new SessionSettings(); |
| 139 | + sessionSettings.set(defaults); |
| 140 | + sessionSettings.setString(sessionID, "BeginString", FixVersions.BEGINSTRING_FIX44); |
| 141 | + sessionSettings.setString(sessionID, "DataDictionary", "FIX44.xml"); |
| 142 | + sessionSettings.setString(sessionID, "SenderCompID", senderId); |
| 143 | + sessionSettings.setString(sessionID, "TargetCompID", targetId); |
| 144 | + |
| 145 | + return sessionSettings; |
| 146 | + } |
| 147 | + |
| 148 | + private SessionSettings createInitiatorSettings(String senderId, String targetId, String proxyVersion, int proxyPort, int port) { |
| 149 | + HashMap<Object, Object> defaults = new HashMap<>(); |
| 150 | + defaults.put(SessionFactory.SETTING_CONNECTION_TYPE, "initiator"); |
| 151 | + defaults.put(Initiator.SETTING_SOCKET_CONNECT_PROTOCOL, ProtocolFactory.getTypeString(ProtocolFactory.SOCKET)); |
| 152 | + defaults.put(Initiator.SETTING_SOCKET_CONNECT_HOST, "localhost"); |
| 153 | + defaults.put(Initiator.SETTING_SOCKET_CONNECT_PORT, Integer.toString(port)); |
| 154 | + defaults.put(Initiator.SETTING_RECONNECT_INTERVAL, "2"); |
| 155 | + defaults.put(Initiator.SETTING_PROXY_HOST, "localhost"); |
| 156 | + defaults.put(Initiator.SETTING_PROXY_PORT, Integer.toString(proxyPort)); |
| 157 | + defaults.put(Initiator.SETTING_PROXY_TYPE, "socks"); |
| 158 | + defaults.put(Initiator.SETTING_PROXY_VERSION, proxyVersion); |
| 159 | + defaults.put(Initiator.SETTING_PROXY_USER, "proxy-user"); |
| 160 | + defaults.put(Initiator.SETTING_PROXY_PASSWORD, "proxy-password"); |
| 161 | + defaults.put(Session.SETTING_START_TIME, "00:00:00"); |
| 162 | + defaults.put(Session.SETTING_END_TIME, "00:00:00"); |
| 163 | + defaults.put(Session.SETTING_HEARTBTINT, "30"); |
| 164 | + |
| 165 | + SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX44, senderId, targetId); |
| 166 | + |
| 167 | + SessionSettings sessionSettings = new SessionSettings(); |
| 168 | + sessionSettings.set(defaults); |
| 169 | + sessionSettings.setString(sessionID, "BeginString", FixVersions.BEGINSTRING_FIX44); |
| 170 | + sessionSettings.setString(sessionID, "DataDictionary", "FIX44.xml"); |
| 171 | + sessionSettings.setString(sessionID, "SenderCompID", senderId); |
| 172 | + sessionSettings.setString(sessionID, "TargetCompID", targetId); |
| 173 | + |
| 174 | + return sessionSettings; |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments