Skip to content

Commit 5049b73

Browse files
committed
Update Junits
1 parent 90fc833 commit 5049b73

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache5;
17+
18+
import static org.assertj.core.api.Assertions.assertThatNoException;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
21+
import java.security.Permission;
22+
import java.util.Arrays;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
import java.util.stream.Stream;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.condition.EnabledForJreRange;
29+
import org.junit.jupiter.api.condition.JRE;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.Arguments;
32+
import org.junit.jupiter.params.provider.MethodSource;
33+
34+
/**
35+
* Tests that Apache5HttpClient fails fast at construction time when a SecurityManager
36+
* denies jdk.net.NetworkPermission for TCP keepalive extended options.
37+
*/
38+
@EnabledForJreRange(max = JRE.JAVA_17)
39+
class Apache5HttpClientSecurityManagerTest {
40+
41+
@AfterEach
42+
void tearDown() {
43+
System.setSecurityManager(null);
44+
System.clearProperty("java.security.policy");
45+
java.security.Policy.getPolicy().refresh();
46+
}
47+
48+
@Test
49+
void buildWithDefaults_whenStandardPermissionsGrantedButNetworkPermissionMissing_shouldThrowIllegalStateException() {
50+
System.setProperty("java.security.policy", "=" + getPolicyUrl());
51+
java.security.Policy.getPolicy().refresh();
52+
System.setSecurityManager(new SecurityManager());
53+
54+
assertThatThrownBy(() -> Apache5HttpClient.builder().build())
55+
.isInstanceOf(IllegalStateException.class)
56+
.hasMessageContaining("jdk.net.NetworkPermission");
57+
}
58+
59+
private String getPolicyUrl() {
60+
return getClass().getResource("security-manager-test.policy").toExternalForm();
61+
}
62+
63+
@ParameterizedTest
64+
@MethodSource("partiallyGrantedPermissions")
65+
void buildWithDefaults_whenNotAllPermissionsGranted_shouldThrowIllegalStateException(Set<String> grantedPermissions) {
66+
System.setSecurityManager(new GrantOnlyNetworkPermissionSecurityManager(grantedPermissions));
67+
68+
assertThatThrownBy(() -> Apache5HttpClient.builder().build())
69+
.isInstanceOf(IllegalStateException.class)
70+
.hasMessageContaining("jdk.net.NetworkPermission");
71+
}
72+
73+
@Test
74+
void buildWithDefaults_whenAllPermissionsGranted_shouldSucceed() {
75+
Set<String> allGranted = new HashSet<>(Arrays.asList(
76+
"setOption.TCP_KEEPIDLE", "setOption.TCP_KEEPINTERVAL", "setOption.TCP_KEEPCOUNT"));
77+
System.setSecurityManager(new GrantOnlyNetworkPermissionSecurityManager(allGranted));
78+
assertThatNoException().isThrownBy(() -> {
79+
Apache5HttpClient.builder().build().close();
80+
});
81+
}
82+
83+
@Test
84+
void buildWithDefaults_whenNoSecurityManager_shouldSucceed() {
85+
assertThatNoException().isThrownBy(() -> {
86+
Apache5HttpClient.builder().build().close();
87+
});
88+
}
89+
90+
static Stream<Arguments> partiallyGrantedPermissions() {
91+
return Stream.of(
92+
// 0 out of 3 granted
93+
Arguments.of(new HashSet<>()),
94+
// 1 out of 3 granted
95+
Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPIDLE"))),
96+
Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPINTERVAL"))),
97+
Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPCOUNT"))),
98+
// 2 out of 3 granted
99+
Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPIDLE", "setOption.TCP_KEEPINTERVAL"))),
100+
Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPIDLE", "setOption.TCP_KEEPCOUNT"))),
101+
Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPINTERVAL", "setOption.TCP_KEEPCOUNT")))
102+
);
103+
}
104+
105+
/**
106+
* SecurityManager that only grants specific jdk.net.NetworkPermission entries and denies the rest.
107+
*/
108+
private static class GrantOnlyNetworkPermissionSecurityManager extends SecurityManager {
109+
private final Set<String> grantedPermissions;
110+
111+
GrantOnlyNetworkPermissionSecurityManager(Set<String> grantedPermissions) {
112+
this.grantedPermissions = grantedPermissions;
113+
}
114+
115+
@Override
116+
public void checkPermission(Permission perm) {
117+
if ("jdk.net.NetworkPermission".equals(perm.getClass().getName())
118+
&& !grantedPermissions.contains(perm.getName())) {
119+
throw new SecurityException("Denied: " + perm.getName());
120+
}
121+
}
122+
}
123+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
grant {
2+
permission java.util.PropertyPermission "*", "read,write";
3+
permission java.io.FilePermission "<<ALL FILES>>", "read,write";
4+
permission java.lang.RuntimePermission "getenv.*";
5+
permission "java.lang.RuntimePermission" "accessDeclaredMembers";
6+
permission "javax.net.ssl.SSLPermission" "setDefaultSSLContext";
7+
permission "java.net.SocketPermission" "*", "connect,resolve";
8+
9+
// Needed for test to remove the security manager
10+
permission java.lang.RuntimePermission "setSecurityManager";
11+
12+
// jdk.net.NetworkPermission for setOption.TCP_KEEPIDLE, setOption.TCP_KEEPINTERVAL,
13+
// setOption.TCP_KEEPCOUNT is explicitly NOT granted to test that Apache5HttpClient
14+
// fails fast when these permissions are missing.
15+
};

0 commit comments

Comments
 (0)