PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/org/lantern/TestUtils.java

https://gitlab.com/kidaa/lantern
Java | 411 lines | 296 code | 80 blank | 35 comment | 29 complexity | fc652ae592ed7aae7ebf959de4aa8ad4 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0
  1. package org.lantern;
  2. import static org.mockito.Mockito.mock;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Properties;
  10. import javax.security.auth.login.CredentialException;
  11. import org.apache.commons.io.IOUtils;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.jivesoftware.smack.XMPPConnection;
  14. import org.lantern.geoip.GeoIpLookupService;
  15. import org.lantern.http.JettyLauncher;
  16. import org.lantern.oauth.LanternGoogleOAuth2Credentials;
  17. import org.lantern.privacy.EncryptedFileService;
  18. import org.lantern.privacy.LocalCipherProvider;
  19. import org.lantern.proxy.DefaultProxyTracker;
  20. import org.lantern.proxy.ProxyTracker;
  21. import org.lantern.state.Model;
  22. import org.lantern.state.ModelIo;
  23. import org.lantern.state.ModelService;
  24. import org.lantern.state.ModelUtils;
  25. import org.lantern.state.Settings;
  26. import org.lantern.util.HttpClientFactory;
  27. import org.lastbamboo.common.portmapping.NatPmpService;
  28. import org.lastbamboo.common.portmapping.PortMapListener;
  29. import org.lastbamboo.common.portmapping.PortMappingProtocol;
  30. import org.lastbamboo.common.portmapping.UpnpService;
  31. import org.littleshoot.commom.xmpp.XmppUtils;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import com.google.inject.Binder;
  35. import com.google.inject.Guice;
  36. import com.google.inject.Inject;
  37. import com.google.inject.Injector;
  38. import com.google.inject.Module;
  39. public class TestUtils {
  40. private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
  41. private static final File privatePropsFile;
  42. private static final Properties privateProps = new Properties();
  43. private static DefaultXmppHandler xmppHandler;
  44. private static LanternSocketsUtil socketsUtil;
  45. private static LanternKeyStoreManager ksm;
  46. private static LanternXmppUtil lanternXmppUtil;
  47. private static Model model;
  48. private static LocalCipherProvider localCipherProvider;
  49. private static EncryptedFileService encryptedFileService;
  50. private static JettyLauncher jettyLauncher;
  51. private static MessageService messageService;
  52. private static Roster roster;
  53. private static ModelService modelService;
  54. private static Proxifier proxifier;
  55. private static ModelIo modelIo;
  56. private static ModelUtils modelUtils;
  57. private static boolean loaded;
  58. private static DefaultProxyTracker proxyTracker;
  59. private static LanternTrustStore trustStore;
  60. private static Injector injector;
  61. private static boolean started = false;
  62. private static HttpClientFactory httpClientFactory;
  63. private static CountryService countryService;
  64. private static GeoIpLookupService geoIpLookupService;
  65. static {
  66. if (LanternClientConstants.TEST_PROPS.isFile()) {
  67. privatePropsFile = LanternClientConstants.TEST_PROPS;
  68. } else {
  69. privatePropsFile = LanternClientConstants.TEST_PROPS2;
  70. }
  71. if (privatePropsFile.isFile()) {
  72. InputStream is = null;
  73. try {
  74. is = new FileInputStream(privatePropsFile);
  75. privateProps.load(is);
  76. } catch (final IOException e) {
  77. System.err.println("NO PRIVATE PROPS FILE AT "+
  78. privatePropsFile.getAbsolutePath());
  79. e.printStackTrace();
  80. } finally {
  81. IOUtils.closeQuietly(is);
  82. }
  83. /*
  84. if (StringUtils.isBlank(getRefreshToken()) ||
  85. StringUtils.isBlank(getAccessToken())) {
  86. System.err.println("NO REFRESH OR ACCESS TOKENS!!");
  87. //throw new Error("Tokens not in "+privatePropsFile);
  88. }
  89. */
  90. }
  91. }
  92. public static void load() {
  93. try {
  94. load(false);
  95. } catch (Exception e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. }
  99. }
  100. public static void load(final boolean start) throws Exception {
  101. if (loaded) {
  102. LOG.warn("ALREADY LOADED. HOW THE HECK DOES SUREFIRE CLASSLOADING WORK?");
  103. if (!started) {
  104. start(start);
  105. }
  106. return;
  107. }
  108. loaded = true;
  109. final Module lm = newTestLanternModule();
  110. injector = Guice.createInjector(lm);
  111. xmppHandler = instance(DefaultXmppHandler.class);
  112. socketsUtil = instance(LanternSocketsUtil.class);
  113. ksm = instance(LanternKeyStoreManager.class);
  114. lanternXmppUtil = instance(LanternXmppUtil.class);
  115. localCipherProvider = instance(LocalCipherProvider.class);
  116. encryptedFileService = instance(EncryptedFileService.class);
  117. model = instance(Model.class);
  118. jettyLauncher = instance(JettyLauncher.class);
  119. messageService = instance(MessageService.class);
  120. roster = instance(Roster.class);
  121. modelService = instance(ModelService.class);
  122. proxifier = instance(Proxifier.class);
  123. modelUtils = instance(ModelUtils.class);
  124. modelIo = instance(ModelIo.class);
  125. proxyTracker = instance(DefaultProxyTracker.class);
  126. trustStore = instance(LanternTrustStore.class);
  127. httpClientFactory = instance(HttpClientFactory.class);
  128. geoIpLookupService = instance(GeoIpLookupService.class);
  129. countryService = instance(CountryService.class);
  130. final Settings set = model.getSettings();
  131. LOG.debug("setting oauth token values...");
  132. LOG.debug("secure env vars available? {}", System.getenv("TRAVIS_SECURE_ENV_VARS"));
  133. //set.setAccessToken(getAccessToken());
  134. set.setRefreshToken(getRefreshToken());
  135. set.setUseGoogleOAuth2(true);
  136. start(start);
  137. }
  138. static class TestModelIo extends ModelIo {
  139. private static File file = new File(LanternClientConstants.DEFAULT_MODEL_FILE + ".test");
  140. @Inject
  141. public TestModelIo(EncryptedFileService encryptedFileService,
  142. CountryService countryService) throws Exception {
  143. super(file, encryptedFileService, countryService,
  144. TestingUtils.newCommandLine(), mock(LocalCipherProvider.class));
  145. }
  146. @Override
  147. public void write() {
  148. // do not write anything in test mode
  149. }
  150. }
  151. public static LanternModule newTestLanternModule() throws Exception {
  152. final LanternModule lm = new LanternModule(new String[]{});
  153. lm.setGeoIpLookupService(new GeoIpLookupService());
  154. lm.setUpnpService(new UpnpService() {
  155. @Override
  156. public void shutdown() {}
  157. @Override
  158. public void removeUpnpMapping(int mappingIndex) {}
  159. @Override
  160. public int addUpnpMapping(PortMappingProtocol protocol, int localPort,
  161. int externalPortRequested, PortMapListener portMapListener) {
  162. return 0;
  163. }
  164. });
  165. lm.setNatPmpService(new NatPmpService() {
  166. @Override
  167. public void shutdown() {}
  168. @Override
  169. public void removeNatPmpMapping(int mappingIndex) {}
  170. @Override
  171. public int addNatPmpMapping(PortMappingProtocol protocol, int localPort,
  172. int externalPortRequested, PortMapListener portMapListener) {
  173. return 0;
  174. }
  175. });
  176. //return Modules.override(lm).with(new TestModule());
  177. return lm;
  178. }
  179. protected static class TestModule implements Module {
  180. @Override
  181. public void configure(Binder binder) {
  182. binder.bind(ModelIo.class).to(TestModelIo.class);
  183. }
  184. }
  185. private static void start(final boolean start) {
  186. if (start) {
  187. started = true;
  188. //xmppHandler.start();
  189. }
  190. }
  191. private static <T> T instance(final Class<T> clazz) {
  192. final T inst = injector.getInstance(clazz);
  193. if (Shutdownable.class.isAssignableFrom(clazz)) {
  194. addCloseHook((Shutdownable) inst);
  195. }
  196. if (inst == null) {
  197. throw new NullPointerException("Could not load instance of "+clazz);
  198. }
  199. return inst;
  200. }
  201. private static final Collection<Shutdownable> shutdownables =
  202. new ArrayList<Shutdownable>();
  203. private static void addCloseHook(final Shutdownable inst) {
  204. shutdownables.add(inst);
  205. }
  206. /*
  207. public static void close() {
  208. for (final Shutdownable shutdown : shutdownables) {
  209. shutdown.stop();
  210. }
  211. }
  212. */
  213. public static XMPPConnection xmppConnection() throws CredentialException,
  214. IOException {
  215. final LanternGoogleOAuth2Credentials creds = getGoogleOauthCreds();
  216. final int attempts = 2;
  217. final XMPPConnection conn =
  218. XmppUtils.persistentXmppConnection(creds, attempts,
  219. "talk.google.com", 5222, "gmail.com", null);
  220. return conn;
  221. }
  222. public static LanternGoogleOAuth2Credentials getGoogleOauthCreds() {
  223. return new LanternGoogleOAuth2Credentials("anon@getlantern.org",
  224. getRefreshToken(), "gmail.");
  225. }
  226. public static String getRefreshToken() {
  227. return privateProps.getProperty("refresh_token");
  228. /*
  229. //final String oauth = System.getenv("LANTERN_OAUTH_REFTOKEN");
  230. if (StringUtils.isBlank(oauth)) {
  231. return privateProps.getProperty("refresh_token");
  232. }
  233. return oauth;
  234. */
  235. }
  236. /*
  237. public static String getAccessToken() {
  238. final String oauth = System.getenv("LANTERN_OAUTH_ACCTOKEN");
  239. if (StringUtils.isBlank(oauth)) {
  240. return privateProps.getProperty("access_token");
  241. }
  242. return oauth;
  243. }
  244. */
  245. public static String getUserName() {
  246. final String oauth = System.getenv("LANTERN_OAUTH_USERNAME");
  247. if (StringUtils.isBlank(oauth)) {
  248. return privateProps.getProperty("username");
  249. }
  250. return oauth;
  251. }
  252. public static JettyLauncher getJettyLauncher() {
  253. if (!loaded) load();
  254. return jettyLauncher;
  255. }
  256. public static DefaultXmppHandler getXmppHandler() {
  257. if (!loaded) load();
  258. return xmppHandler;
  259. }
  260. public static LanternSocketsUtil getSocketsUtil() {
  261. if (!loaded) load();
  262. return socketsUtil;
  263. }
  264. public static LanternKeyStoreManager getKsm() {
  265. if (!loaded) load();
  266. return ksm;
  267. }
  268. public static LanternXmppUtil getLanternXmppUtil() {
  269. if (!loaded) load();
  270. return lanternXmppUtil;
  271. }
  272. public static Model getModel() {
  273. if (!loaded) load();
  274. return model;
  275. }
  276. public static LocalCipherProvider getLocalCipherProvider() {
  277. if (!loaded) load();
  278. return localCipherProvider;
  279. }
  280. public static EncryptedFileService getEncryptedFileService() {
  281. if (!loaded) load();
  282. return encryptedFileService;
  283. }
  284. public static MessageService getMessageService() {
  285. if (!loaded) load();
  286. return messageService;
  287. }
  288. public static Roster getRoster() {
  289. if (!loaded) load();
  290. return roster;
  291. }
  292. public static ModelService getModelService() {
  293. if (!loaded) load();
  294. return modelService;
  295. }
  296. public static Proxifier getProxifier() {
  297. if (!loaded) load();
  298. return proxifier;
  299. }
  300. public static ModelIo getModelIo() {
  301. if (!loaded) load();
  302. return modelIo;
  303. }
  304. public static ModelUtils getModelUtils() {
  305. if (!loaded) load();
  306. return modelUtils;
  307. }
  308. public static ProxyTracker getProxyTracker() {
  309. if (!loaded) load();
  310. return proxyTracker;
  311. }
  312. public static LanternTrustStore getTrustStore() {
  313. if (!loaded) load();
  314. return trustStore;
  315. }
  316. public static LanternTrustStore buildTrustStore() {
  317. return new LanternTrustStore(new LanternKeyStoreManager());
  318. }
  319. public static HttpClientFactory getHttpClientFactory() {
  320. if (!loaded) load();
  321. return httpClientFactory;
  322. }
  323. public static CountryService getCountryService() {
  324. if (!loaded) load();
  325. return countryService;
  326. }
  327. public static GeoIpLookupService getGeoIpLookupService() {
  328. if (!loaded) load();
  329. return geoIpLookupService;
  330. }
  331. }