You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiBrowserTestWithProxy.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.tb3;
  17. import java.io.File;
  18. import java.util.concurrent.atomic.AtomicInteger;
  19. import org.junit.After;
  20. import com.jcraft.jsch.JSch;
  21. import com.jcraft.jsch.JSchException;
  22. import com.jcraft.jsch.Session;
  23. import com.vaadin.testbench.parallel.TestCategory;
  24. @TestCategory("push")
  25. public abstract class MultiBrowserTestWithProxy extends MultiBrowserTest {
  26. private static AtomicInteger availablePort = new AtomicInteger(2000);
  27. private Session proxySession;
  28. private Integer proxyPort = null;
  29. private JSch jsch;
  30. private static String sshDir = System.getProperty("user.home") + "/.ssh/";
  31. private String[] publicKeys = new String[] {
  32. System.getProperty("sshkey.file"), sshDir + "id_rsa",
  33. sshDir + "id_dsa", sshDir + "id_rsa2" };
  34. @Override
  35. public void setup() throws Exception {
  36. super.setup();
  37. connectProxy();
  38. }
  39. @After
  40. public void teardownProxy() {
  41. disconnectProxy();
  42. }
  43. protected Integer getProxyPort() {
  44. if (proxyPort == null) {
  45. // Assumes we can use any port >= 2000
  46. proxyPort = availablePort.addAndGet(1);
  47. }
  48. return proxyPort;
  49. }
  50. /**
  51. * Disconnects the proxy if active
  52. */
  53. protected void disconnectProxy() {
  54. if (proxySession == null) {
  55. return;
  56. }
  57. proxySession.disconnect();
  58. proxySession = null;
  59. }
  60. /**
  61. * Ensure the proxy is active. Does nothing if the proxy is already active.
  62. */
  63. protected void connectProxy() throws JSchException {
  64. if (proxySession != null) {
  65. return;
  66. }
  67. for (int i = 0; i < 10; i++) {
  68. // Potential workaround for problem with establishing many ssh
  69. // connections at the same time
  70. try {
  71. createProxy(getProxyPort());
  72. break;
  73. } catch (JSchException e) {
  74. try {
  75. sleep(500);
  76. } catch (InterruptedException e1) {
  77. }
  78. if (i == 9) {
  79. throw new RuntimeException(
  80. "All 10 attempts to connect a proxy failed", e);
  81. }
  82. }
  83. }
  84. }
  85. private void createProxy(int proxyPort) throws JSchException {
  86. if (jsch == null) {
  87. jsch = new JSch();
  88. String keyFile = null;
  89. for (String publicKey : publicKeys) {
  90. if (publicKey != null) {
  91. if (new File(publicKey).exists()) {
  92. keyFile = publicKey;
  93. break;
  94. }
  95. }
  96. }
  97. jsch.addIdentity(keyFile);
  98. }
  99. proxySession = jsch.getSession("localhost");
  100. proxySession.setConfig("StrictHostKeyChecking", "no");
  101. proxySession.setPortForwardingL("0.0.0.0", proxyPort,
  102. super.getDeploymentHostname(), super.getDeploymentPort());
  103. proxySession.connect();
  104. }
  105. @Override
  106. protected String getBaseURL() {
  107. return "http://" + getDeploymentHostname() + ":" + getProxyPort();
  108. }
  109. }