summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/testSource/org/jetbrains/idea/svn/ProxyTest.java
blob: 3b39563d3b09db3dd21ccfbb47ac9139c9a2557f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.svn;

//import org.junit.Test;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.internal.util.SVNSocketFactory;

import java.io.IOException;
import java.net.*;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Irina.Chernushina
 * Date: 1/20/13
 * Time: 10:51 PM
 */
public class ProxyTest {

  private ProxySelector myDefault;

  // to be run with java.net.useSystemProxies or without
  // result: hangs if used with -Djava.net.useSystemProxies=true
  @Test
  public void testSimpleSvnkitConnection() throws Exception {
    svnkitCall();
  }

  private void svnkitCall() throws IOException, SVNException {
    final String url = "msdc.labs.intellij.net";
    final InetAddress name = InetAddress.getByName(url);
    setAuthenticator();
    final Socket socket = SVNSocketFactory.createPlainSocket(name.getHostAddress(), 80, 120000, 120000, null);
    System.out.println("connected: " + socket.isConnected());
  }

  private void setAuthenticator() {
    Authenticator.setDefault(new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
        System.out.println("Authentication asked, requesting url: " + getRequestingHost());
        return new PasswordAuthentication("user1", "fg3W9".toCharArray());
      }
    });
  }

  // to be run with java.net.useSystemProxies or without
  // result: false anyway
  @Test
  public void testSimpleHttpConnection() throws Exception {
    System.out.println("java.net.useSystemProxies: " + System.getProperty("java.net.useSystemProxies"));
    httpCall();
  }

  private void httpCall() throws IOException {
    final String url = "http://msdc.labs.intellij.net";
    HttpURLConnection connection = null;
    try {
      URL url1 = new URL(url);
      connection = (HttpURLConnection)url1.openConnection();
      System.out.println("using proxy: " + connection.usingProxy());
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }
  }

  // result: properties does not affect
  @Test
  public void testSvnkitWithProxyInProperties() throws Exception {
    try {
      System.setProperty("proxySet", "true");
      System.setProperty("http.proxyHost", "proxy-auth-test.labs.intellij.net");
      System.setProperty("http.proxyPort", Integer.toString (3128));
      svnkitCall();
    } finally {
      System.clearProperty("proxySet");
      System.clearProperty("http.proxyHost");
      System.clearProperty("http.proxyPort");
    }
  }

  // result: properties does not affect
  @Test
  public void testHttpWithProxyInProperties() throws Exception {
    try {
      System.setProperty("proxySet", "true");
      System.setProperty("http.proxyHost", "proxy-auth-test.labs.intellij.net");
      System.setProperty("http.proxyPort", Integer.toString (3128));
      httpCall();
    } finally {
      System.clearProperty("proxySet");
      System.clearProperty("http.proxyHost");
      System.clearProperty("http.proxyPort");
    }
  }

  // result: exception!
  @Test
  public void testSvnkitWithProxySelector() throws Exception {
    try {
      defaultProxy();
      svnkitCall();
    } finally {
      ProxySelector.setDefault(myDefault);
    }
  }

  // result: does not affect
  @Test
  public void testHttpWithProxySelector() throws Exception {
    try {
      defaultProxy();
      httpCall();
    } finally {
      ProxySelector.setDefault(myDefault);
    }
  }

  private void defaultProxy() {
    myDefault = ProxySelector.getDefault();
    ProxySelector.setDefault(new ProxySelector() {
      @Override
      public List<Proxy> select(URI uri) {
        return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-auth-test.labs.intellij.net", 3128)));
      }

      @Override
      public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        System.out.println("failed for url: " + uri.getHost());
      }
    });
  }

  private @interface Test {}
}