Skip to content

Commit

Permalink
#1158 Unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Nov 19, 2013
1 parent bec3d78 commit a49eba7
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 296 deletions.
8 changes: 0 additions & 8 deletions src/main/java/org/lantern/ProxySocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
Expand All @@ -32,13 +31,6 @@ public class ProxySocketFactory extends SocketFactory {
private static final Pattern RESPONSE_PATTERN = Pattern
.compile("HTTP/\\S+\\s(\\d+)\\s(.*)\\s*");

private final LanternSocketsUtil socketsUtil;

@Inject
public ProxySocketFactory(final LanternSocketsUtil socketsUtil) {
this.socketsUtil = socketsUtil;
}

@Override
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/lantern/AllCensored.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.lantern;

import java.io.IOException;

public class AllCensored implements Censored {

@Override
public boolean isExportRestricted(String string) throws IOException {
return true;
}

@Override
public boolean isCountryCodeCensored(String cc) {
return true;
}

@Override
public boolean isCensored(Country country) {
return true;
}

@Override
public boolean isCensored() {
return true;
}

}
48 changes: 27 additions & 21 deletions src/test/java/org/lantern/DefaultXmppHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.lantern;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.util.concurrent.Callable;

import org.jivesoftware.smack.SASLAuthentication;
import org.junit.Ignore;
import org.junit.Test;
import org.lantern.event.ClosedBetaEvent;
import org.lantern.event.Events;
Expand Down Expand Up @@ -55,26 +56,31 @@ public void testControllerMessages() throws Exception {
settings.setRefreshToken(TestingUtils.getRefreshToken());
settings.setUseGoogleOAuth2(true);


final XmppHandler handler = TestingUtils.newXmppHandler(censored, model);
handler.start();
// The handler could have already been created and connected, so
// make sure we disconnect.
handler.disconnect();
handler.connect();

assertTrue(handler.isLoggedIn());

LOG.debug("Checking for proxies in settings: {}", settings);
int count = 0;
while (closedBetaEvent == null && count < 200) {
Thread.sleep(120);
count++;
}
TestingUtils.doWithWithGetModeProxy(new Callable<Void>() {
@Override
public Void call() throws Exception {
final XmppHandler handler = TestingUtils.newXmppHandler(censored, model);
handler.start();
// The handler could have already been created and connected, so
// make sure we disconnect.
handler.disconnect();
handler.connect();

assertTrue(handler.isLoggedIn());

LOG.debug("Checking for proxies in settings: {}", settings);
int count = 0;
while (closedBetaEvent == null && count < 200) {
Thread.sleep(120);
count++;
}

handler.stop();
return null;
}
});

assertTrue("Should have received event from the controller",
this.closedBetaEvent != null);

handler.stop();
this.closedBetaEvent != null);
}
}
32 changes: 16 additions & 16 deletions src/test/java/org/lantern/GoogleOauth2CallbackServletTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.lantern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import org.junit.Test;
import org.lantern.http.GoogleOauth2CallbackServlet;
Expand All @@ -15,21 +16,20 @@ public class GoogleOauth2CallbackServletTest {

@Test
public void testGoogleApis() throws Exception {
final LanternKeyStoreManager ksm = TestingUtils.newKeyStoreManager();
final LanternTrustStore trustStore = new LanternTrustStore(ksm);
final LanternSocketsUtil socketsUtil =
new LanternSocketsUtil(null, trustStore);

final Censored censored = new DefaultCensored();
final HttpClientFactory factory =
new HttpClientFactory(socketsUtil, censored, null);
final GoogleOauth2CallbackServlet servlet =
new GoogleOauth2CallbackServlet(null, null, null, null, null,
null, factory, null, new Messages(new Model()));

final Map<String, String> allToks = new HashMap<String, String>();
allToks.put("access_token", "invalidcode");
final int code = servlet.fetchEmail(allToks, factory.newClient());
int code = TestingUtils.doWithWithGetModeProxy(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
final Censored censored = new DefaultCensored();
final HttpClientFactory factory = new HttpClientFactory(censored);
final GoogleOauth2CallbackServlet servlet =
new GoogleOauth2CallbackServlet(null, null, null, null, null,
null, factory, null, new Messages(new Model()));

final Map<String, String> allToks = new HashMap<String, String>();
allToks.put("access_token", "invalidcode");
return servlet.fetchEmail(allToks, factory.newClient());
}
});

// Expected to be unauthorized with a bogus token -- we want to
// make sure it gets there.
Expand Down
76 changes: 38 additions & 38 deletions src/test/java/org/lantern/HttpClientFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
Expand All @@ -43,33 +43,33 @@ public void testFallbackProxyConnection() throws Exception {
//System.setProperty("javax.net.debug", "ssl");

Launcher.configureCipherSuites();
final LanternKeyStoreManager ksm = TestingUtils.newKeyStoreManager();
final LanternTrustStore trustStore = new LanternTrustStore(ksm);
final LanternSocketsUtil socketsUtil =
new LanternSocketsUtil(null, trustStore);

final Censored censored = new DefaultCensored();
final HttpClientFactory factory =
new HttpClientFactory(socketsUtil, censored, null);

final HttpHost proxy = new HttpHost("54.254.96.14", 16589, "https");

final HttpClient httpClient = factory.newClient(proxy, true);

httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 8000);
TestingUtils.doWithWithGetModeProxy(new Callable<Void>() {
@Override
public Void call() throws Exception {
final HttpClientFactory factory =
new HttpClientFactory(new AllCensored());

// Because we are censored, this should use the local proxy
final HttpClient httpClient = factory.newClient();
TestingUtils.assertIsUsingGetModeProxy(httpClient);

httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 8000);

final HttpHead head = new HttpHead("https://www.google.com");

log.debug("About to execute get!");
final HttpResponse response = httpClient.execute(head);
final StatusLine line = response.getStatusLine();
final int code = line.getStatusCode();
if (code < 200 || code > 299) {
//log.error("Head request failed?\n"+line);
fail("Could not proxy");
}
head.reset();
final HttpHead head = new HttpHead("https://www.google.com");

log.debug("About to execute get!");
final HttpResponse response = httpClient.execute(head);
final StatusLine line = response.getStatusLine();
final int code = line.getStatusCode();
if (code < 200 || code > 299) {
//log.error("Head request failed?\n"+line);
fail("Could not proxy");
}
head.reset();
return null;
}
});
}

/**
Expand All @@ -88,19 +88,19 @@ public void testFallbackProxyConnection() throws Exception {
*/
@Test
public void testAllInternallyProxiedSites() throws Exception {
final LanternKeyStoreManager ksm = TestingUtils.newKeyStoreManager();
final LanternTrustStore trustStore = new LanternTrustStore(ksm);
final LanternSocketsUtil socketsUtil =
new LanternSocketsUtil(null, trustStore);

final Censored censored = new DefaultCensored();
final HttpClientFactory factory =
new HttpClientFactory(socketsUtil, censored, TestingUtils.newProxyTracker());
final HttpClientFactory factory = new HttpClientFactory(new AllCensored());
final HttpClient client = factory.newClient();
TestingUtils.assertIsUsingGetModeProxy(client);

testExceptional(client);
testGoogleDocs(factory);
testStats(client);
TestingUtils.doWithWithGetModeProxy(new Callable<Void>() {
@Override
public Void call() throws Exception {
testExceptional(client);
testGoogleDocs(factory);
testStats(client);
return null;
}
});
}

private void testStats(final HttpClient client) throws Exception {
Expand Down
Loading

0 comments on commit a49eba7

Please sign in to comment.