|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. |
| 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 | + |
| 17 | +package com.google.cloud.tools.jib.http; |
| 18 | + |
| 19 | +import com.google.api.client.http.apache.ApacheHttpTransport; |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import org.apache.http.auth.AuthScope; |
| 25 | +import org.apache.http.auth.Credentials; |
| 26 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +/** Tests for {@link Connection} with setting proxy credentials. */ |
| 33 | +public class ConnectionWithProxyCredentialsTest { |
| 34 | + |
| 35 | + private static final ImmutableList<String> proxyProperties = |
| 36 | + ImmutableList.of( |
| 37 | + "http.proxyHost", |
| 38 | + "http.proxyPort", |
| 39 | + "http.proxyUser", |
| 40 | + "http.proxyPassword", |
| 41 | + "https.proxyHost", |
| 42 | + "https.proxyPort", |
| 43 | + "https.proxyUser", |
| 44 | + "https.proxyPassword"); |
| 45 | + |
| 46 | + // HashMap to allow saving null values. |
| 47 | + private final HashMap<String, String> savedProperties = new HashMap<>(); |
| 48 | + |
| 49 | + private final ApacheHttpTransport transport = new ApacheHttpTransport(); |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() { |
| 53 | + proxyProperties.stream().forEach(key -> savedProperties.put(key, System.getProperty(key))); |
| 54 | + proxyProperties.stream().forEach(key -> System.clearProperty(key)); |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void tearDown() { |
| 59 | + Consumer<Map.Entry<String, String>> restoreProperty = |
| 60 | + entry -> { |
| 61 | + if (entry.getValue() == null) { |
| 62 | + System.clearProperty(entry.getKey()); |
| 63 | + } else { |
| 64 | + System.setProperty(entry.getKey(), entry.getValue()); |
| 65 | + } |
| 66 | + }; |
| 67 | + savedProperties.entrySet().stream().forEach(restoreProperty); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testAddProxyCredentials_undefined() { |
| 72 | + Connection.addProxyCredentials(transport); |
| 73 | + DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); |
| 74 | + Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY); |
| 75 | + Assert.assertNull(credentials); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testAddProxyCredentials() { |
| 80 | + System.setProperty("http.proxyHost", "http://localhost"); |
| 81 | + System.setProperty("http.proxyPort", "1080"); |
| 82 | + System.setProperty("http.proxyUser", "user"); |
| 83 | + System.setProperty("http.proxyPassword", "pass"); |
| 84 | + |
| 85 | + System.setProperty("https.proxyHost", "https://host.com"); |
| 86 | + System.setProperty("https.proxyPort", "1443"); |
| 87 | + System.setProperty("https.proxyUser", "s-user"); |
| 88 | + System.setProperty("https.proxyPassword", "s-pass"); |
| 89 | + |
| 90 | + Connection.addProxyCredentials(transport); |
| 91 | + DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); |
| 92 | + Credentials httpCredentials = |
| 93 | + httpClient.getCredentialsProvider().getCredentials(new AuthScope("http://localhost", 1080)); |
| 94 | + Assert.assertEquals("user", httpCredentials.getUserPrincipal().getName()); |
| 95 | + Assert.assertEquals("pass", httpCredentials.getPassword()); |
| 96 | + |
| 97 | + Credentials httpsCredentials = |
| 98 | + httpClient.getCredentialsProvider().getCredentials(new AuthScope("https://host.com", 1443)); |
| 99 | + Assert.assertEquals("s-user", httpsCredentials.getUserPrincipal().getName()); |
| 100 | + Assert.assertEquals("s-pass", httpsCredentials.getPassword()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testAddProxyCredentials_defaultPorts() { |
| 105 | + System.setProperty("http.proxyHost", "http://localhost"); |
| 106 | + System.setProperty("http.proxyUser", "user"); |
| 107 | + System.setProperty("http.proxyPassword", "pass"); |
| 108 | + |
| 109 | + System.setProperty("https.proxyHost", "https://host.com"); |
| 110 | + System.setProperty("https.proxyUser", "s-user"); |
| 111 | + System.setProperty("https.proxyPassword", "s-pass"); |
| 112 | + |
| 113 | + Connection.addProxyCredentials(transport); |
| 114 | + DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); |
| 115 | + Credentials httpCredentials = |
| 116 | + httpClient.getCredentialsProvider().getCredentials(new AuthScope("http://localhost", 80)); |
| 117 | + Assert.assertEquals("user", httpCredentials.getUserPrincipal().getName()); |
| 118 | + Assert.assertEquals("pass", httpCredentials.getPassword()); |
| 119 | + |
| 120 | + Credentials httpsCredentials = |
| 121 | + httpClient.getCredentialsProvider().getCredentials(new AuthScope("https://host.com", 443)); |
| 122 | + Assert.assertEquals("s-user", httpsCredentials.getUserPrincipal().getName()); |
| 123 | + Assert.assertEquals("s-pass", httpsCredentials.getPassword()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testAddProxyCredentials_hostUndefined() { |
| 128 | + System.setProperty("http.proxyUser", "user"); |
| 129 | + System.setProperty("http.proxyPassword", "pass"); |
| 130 | + |
| 131 | + System.setProperty("https.proxyUser", "s-user"); |
| 132 | + System.setProperty("https.proxyPassword", "s-pass"); |
| 133 | + |
| 134 | + Connection.addProxyCredentials(transport); |
| 135 | + DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); |
| 136 | + Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY); |
| 137 | + Assert.assertNull(credentials); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testAddProxyCredentials_userUndefined() { |
| 142 | + System.setProperty("http.proxyHost", "http://localhost"); |
| 143 | + System.setProperty("http.proxyPassword", "pass"); |
| 144 | + |
| 145 | + System.setProperty("https.proxyHost", "https://host.com"); |
| 146 | + System.setProperty("https.proxyPassword", "s-pass"); |
| 147 | + |
| 148 | + Connection.addProxyCredentials(transport); |
| 149 | + DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); |
| 150 | + Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY); |
| 151 | + Assert.assertNull(credentials); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testAddProxyCredentials_passwordUndefined() { |
| 156 | + System.setProperty("http.proxyHost", "http://localhost"); |
| 157 | + System.setProperty("http.proxyUser", "user"); |
| 158 | + |
| 159 | + System.setProperty("https.proxyHost", "https://host.com"); |
| 160 | + System.setProperty("https.proxyUser", "s-user"); |
| 161 | + |
| 162 | + Connection.addProxyCredentials(transport); |
| 163 | + DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); |
| 164 | + Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY); |
| 165 | + Assert.assertNull(credentials); |
| 166 | + } |
| 167 | +} |
0 commit comments