forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionStringUnitTest.java
More file actions
143 lines (132 loc) · 7.01 KB
/
ConnectionStringUnitTest.java
File metadata and controls
143 lines (132 loc) · 7.01 KB
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
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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 com.mongodb;
import com.mongodb.assertions.Assertions;
import com.mongodb.connection.ServerMonitoringMode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
final class ConnectionStringUnitTest {
private static final String DEFAULT_OPTIONS = "mongodb://localhost/?";
@Test
void defaults() {
ConnectionString connectionStringDefault = new ConnectionString(DEFAULT_OPTIONS);
assertAll(
() -> assertNull(connectionStringDefault.getServerMonitoringMode()),
() -> assertNull(connectionStringDefault.getMaxAdaptiveRetries())
);
}
@ParameterizedTest
@ValueSource(strings = {
"serverMonitoringMode=stream",
"maxAdaptiveRetries=42",
"enableOverloadRetargeting=true"
})
void equalAndHashCode(final String connectionStringOptions) {
ConnectionString default1 = new ConnectionString(DEFAULT_OPTIONS);
ConnectionString default2 = new ConnectionString(DEFAULT_OPTIONS);
String connectionString = DEFAULT_OPTIONS + connectionStringOptions;
ConnectionString actual1 = new ConnectionString(connectionString);
ConnectionString actual2 = new ConnectionString(connectionString);
assertAll(
() -> assertEquals(default1, default2),
() -> assertEquals(default1.hashCode(), default2.hashCode()),
() -> assertEquals(actual1, actual2),
() -> assertEquals(actual1.hashCode(), actual2.hashCode()),
() -> assertNotEquals(default1, actual1)
);
}
@Test
public void mustDecodeNonOidcAsWhole() {
// this string allows us to check if there is no double decoding
String rawValue = encode("ot her");
assertAll(() -> {
// even though only one part has been encoded by the user, the whole option value (pre-split) must be decoded
ConnectionString cs = new ConnectionString(
"mongodb://foo:[email protected]/?authMechanism=GSSAPI&authMechanismProperties="
+ "SERVICE_NAME:" + encode(rawValue) + ",CANONICALIZE_HOST_NAME:true&authSource=$external");
MongoCredential credential = Assertions.assertNotNull(cs.getCredential());
assertEquals(rawValue, credential.getMechanismProperty("SERVICE_NAME", null));
}, () -> {
ConnectionString cs = new ConnectionString(
"mongodb://foo:[email protected]/?authMechanism=GSSAPI&authMechanismProperties="
+ encode("SERVICE_NAME:" + rawValue + ",CANONICALIZE_HOST_NAME:true&authSource=$external"));
MongoCredential credential = Assertions.assertNotNull(cs.getCredential());
assertEquals(rawValue, credential.getMechanismProperty("SERVICE_NAME", null));
});
}
private static String encode(final String string) {
try {
return URLEncoder.encode(string, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Test
void serverMonitoringMode() {
assertAll(
() -> assertEquals(ServerMonitoringMode.POLL,
new ConnectionString(DEFAULT_OPTIONS + "serverMonitoringMode=poll").getServerMonitoringMode()),
() -> assertThrows(IllegalArgumentException.class,
() -> new ConnectionString(DEFAULT_OPTIONS + "serverMonitoringMode=invalid"))
);
}
@ParameterizedTest
@ValueSource(strings = {"mongodb://foo:bar/@hostname/java?", "mongodb://foo:bar?@hostname/java/",
"mongodb+srv://foo:bar/@hostname/java?", "mongodb+srv://foo:bar?@hostname/java/",
"mongodb://foo:bar/@[::1]:27018", "mongodb://foo:bar?@[::1]:27018",
"mongodb://foo:12345678/@hostname", "mongodb+srv://foo:12345678/@hostname",
"mongodb://foo:12345678/@hostname", "mongodb+srv://foo:12345678/@hostname",
"mongodb://foo:12345678%40hostname", "mongodb+srv://foo:12345678%40hostname",
"mongodb://foo:12345678@bar@hostname", "mongodb+srv://foo:12345678@bar@hostname"
})
void unescapedPasswordsShouldNotBeLeakedInExceptionMessages(final String input) {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new ConnectionString(input));
assertFalse(exception.getMessage().contains("bar"));
assertFalse(exception.getMessage().contains("12345678"));
}
@Test
void maxAdaptiveRetries() {
assertAll(
() -> assertEquals(42,
new ConnectionString(DEFAULT_OPTIONS + "maxAdaptiveRetries=42").getMaxAdaptiveRetries()),
() -> assertEquals(0,
new ConnectionString(DEFAULT_OPTIONS + "maxAdaptiveRetries=0").getMaxAdaptiveRetries()),
() -> assertThrows(IllegalArgumentException.class,
() -> new ConnectionString(DEFAULT_OPTIONS + "maxAdaptiveRetries=-1")),
() -> assertThrows(IllegalArgumentException.class,
() -> new ConnectionString(DEFAULT_OPTIONS + "maxAdaptiveRetries=invalid"))
);
}
@Test
void enableOverloadRetargeting() {
assertAll(
() -> assertNull(new ConnectionString("mongodb://localhost/").getEnableOverloadRetargeting()),
() -> assertEquals(false, new ConnectionString(DEFAULT_OPTIONS + "enableOverloadRetargeting=false").getEnableOverloadRetargeting()),
() -> assertEquals(true, new ConnectionString(DEFAULT_OPTIONS + "enableOverloadRetargeting=true").getEnableOverloadRetargeting()),
() -> assertNull(new ConnectionString(DEFAULT_OPTIONS + "enableOverloadRetargeting=foos").getEnableOverloadRetargeting())
);
}
}