Skip to content

Commit c71deda

Browse files
review: fix tests for plain auth v7 - with mutable and junit5
1 parent 5f2641e commit c71deda

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

fe/fe-common/src/main/java/org/apache/doris/common/LdapConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,6 @@ public static String getConnectionURL(String hostPortInAccessibleFormat) {
179179
/**
180180
* Flag to enable login with empty pass.
181181
*/
182-
@ConfigBase.ConfField
182+
@ConfigBase.ConfField(mutable = true)
183183
public static boolean ldap_allow_empty_pass = true;
184184
}

fe/fe-core/src/test/java/org/apache/doris/mysql/authenticate/ldap/LdapAuthenticatorTest.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,56 @@ public void testGetPasswordResolver() {
148148
}
149149

150150
@Test
151-
public void testEmptyPassword() throws IOException {
151+
public void testEmptyPasswordWithAllowEmptyPassDefault() throws IOException {
152152
setCheckPassword(true);
153153
setGetUserInDoris(true);
154-
AuthenticateRequest request = new AuthenticateRequest(USER_NAME, new ClearPassword(""), IP);
155154
//running test with non-specified value - ldap_allow_empty_pass should be true
155+
//test with empty pass - success
156+
AuthenticateRequest request = new AuthenticateRequest("user1.1", new ClearPassword(""), IP);
157+
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
156158
AuthenticateResponse response = ldapAuthenticator.authenticate(request);
157159
Assert.assertTrue(response.isSuccess());
158-
//running test with specified value - true - ldap_allow_empty_pass is explicitly set to true
160+
//test with non empty pass - success
161+
request = new AuthenticateRequest("user1.2", new ClearPassword("pass"), IP);
162+
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
163+
response = ldapAuthenticator.authenticate(request);
164+
Assert.assertTrue(response.isSuccess());
165+
}
166+
167+
@Test
168+
public void testEmptyPasswordWithAllowEmptyPassTrue() throws IOException {
169+
setCheckPassword(true);
170+
setGetUserInDoris(true);
171+
//running test with specified value - ldap_allow_empty_pass is true
159172
LdapConfig.ldap_allow_empty_pass = true;
173+
//test with empty pass - success
174+
AuthenticateRequest request = new AuthenticateRequest("user2.1", new ClearPassword(""), IP);
175+
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
176+
AuthenticateResponse response = ldapAuthenticator.authenticate(request);
177+
Assert.assertTrue(response.isSuccess());
178+
//test with non empty pass - success
179+
request = new AuthenticateRequest("user2.2", new ClearPassword("pass"), IP);
180+
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
160181
response = ldapAuthenticator.authenticate(request);
161182
Assert.assertTrue(response.isSuccess());
162-
//running test with specified value - false - ldap_allow_empty_pass is explicitly set to false
183+
}
184+
185+
@Test
186+
public void testEmptyPasswordWithAllowEmptyPassFalse() throws IOException {
187+
setCheckPassword(true);
188+
setGetUserInDoris(true);
189+
//running test with specified value - ldap_allow_empty_pass is false
163190
LdapConfig.ldap_allow_empty_pass = false;
164-
response = ldapAuthenticator.authenticate(request);
191+
//test with empty pass - failure
192+
AuthenticateRequest request = new AuthenticateRequest("user3.1", new ClearPassword(""), IP);
193+
Assert.assertFalse(LdapConfig.ldap_allow_empty_pass);
194+
AuthenticateResponse response = ldapAuthenticator.authenticate(request);
165195
Assert.assertFalse(response.isSuccess());
196+
//test with non empty pass - success
197+
request = new AuthenticateRequest("user3.2", new ClearPassword("pass"), IP);
198+
Assert.assertFalse(LdapConfig.ldap_allow_empty_pass);
199+
response = ldapAuthenticator.authenticate(request);
200+
Assert.assertTrue(response.isSuccess());
166201
}
167202

168203
@After

fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/PlainAuthWithEmptyPasswordAndLdapTest.java

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525

2626
import mockit.Expectations;
2727
import mockit.Mocked;
28-
import org.junit.After;
29-
import org.junit.Assert;
30-
import org.junit.Before;
28+
import org.junit.jupiter.api.AfterEach;
3129
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.BeforeEach;
3231
import org.junit.jupiter.api.Test;
3332

3433
public class PlainAuthWithEmptyPasswordAndLdapTest extends TestWithFeService {
@@ -58,73 +57,63 @@ private void setCheckPassword(boolean res) {
5857
}
5958

6059
@Test
61-
public void testPlainPasswordAuthWithNonEmptyPassword() throws Exception {
62-
System.out.println("1.0 [" + LdapConfig.ldap_allow_empty_pass + "]");
60+
public void testPlainPasswordAuthWithAllowEmptyPassDefault() throws Exception {
6361
setLdapUserExist(true);
6462
setCheckPassword(true);
6563

66-
//test with true value and non-empty pass: login is allowed
67-
LdapConfig.ldap_allow_empty_pass = true;
68-
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
69-
System.out.println("1.1 [" + LdapConfig.ldap_allow_empty_pass + "]");
70-
Env.getCurrentEnv().getAuth().checkPlainPassword("user1.1", IP, "testPass", null);
71-
72-
//test with false value and non-empty pass: login is allowed
73-
LdapConfig.ldap_allow_empty_pass = false;
74-
Assert.assertFalse(LdapConfig.ldap_allow_empty_pass);
75-
System.out.println("1.2 [" + LdapConfig.ldap_allow_empty_pass + "]");
64+
//running test with non-specified value - ldap_allow_empty_pass should be true
65+
//empty pass - success
66+
Assertions.assertTrue(LdapConfig.ldap_allow_empty_pass);
67+
Env.getCurrentEnv().getAuth().checkPlainPassword("user1.1", IP, "", null);
68+
//non empty pass - success
69+
Assertions.assertTrue(LdapConfig.ldap_allow_empty_pass);
7670
Env.getCurrentEnv().getAuth().checkPlainPassword("user1.2", IP, "testPass", null);
7771
}
7872

7973

8074
@Test
81-
public void testPlainPasswordAuthWithEmptyPasswordAllowed() throws Exception {
82-
System.out.println("2.0 [" + LdapConfig.ldap_allow_empty_pass + "]");
75+
public void testPlainPasswordAuthWithAllowEmptyPassTrue() throws Exception {
8376
setLdapUserExist(true);
8477
setCheckPassword(true);
8578

86-
System.out.println("2.1 [" + LdapConfig.ldap_allow_empty_pass + "]");
87-
//test default (ldap_allow_empty_pass=true) and empty pass: login is allowed
88-
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
79+
//running test with specified value - ldap_allow_empty_pass is be true
80+
LdapConfig.ldap_allow_empty_pass = true;
81+
82+
//empty pass - success
83+
Assertions.assertTrue(LdapConfig.ldap_allow_empty_pass);
8984
Env.getCurrentEnv().getAuth().checkPlainPassword("user2.1", IP, "", null);
9085

91-
//test with true and empty pass: login is allowed
92-
LdapConfig.ldap_allow_empty_pass = true;
93-
System.out.println("2.2 [" + LdapConfig.ldap_allow_empty_pass + "]");
94-
Assert.assertTrue(LdapConfig.ldap_allow_empty_pass);
95-
Env.getCurrentEnv().getAuth().checkPlainPassword("user2.2", IP, "", null);
86+
//non empty pass - success
87+
Assertions.assertTrue(LdapConfig.ldap_allow_empty_pass);
88+
Env.getCurrentEnv().getAuth().checkPlainPassword("user2.2", IP, "testPass", null);
9689
}
9790

9891
@Test
99-
public void testPlainPasswordAuthWithEmptyPasswordProhibitted() throws Exception {
100-
System.out.println("3.0 [" + LdapConfig.ldap_allow_empty_pass + "]");
92+
public void testPlainPasswordAuthWithAllowEmptyPassFalse() throws Exception {
10193
setLdapUserExist(true);
10294
setCheckPassword(true);
10395

104-
//test with false and empty pass: login is not allowed
96+
//running test with specified value - ldap_allow_empty_pass is false
10597
LdapConfig.ldap_allow_empty_pass = false;
106-
System.out.println("3.1 [" + LdapConfig.ldap_allow_empty_pass + "]");
107-
Assert.assertFalse(LdapConfig.ldap_allow_empty_pass);
108-
Assertions.assertThrows(AuthenticationException.class, () -> {
109-
Env.getCurrentEnv().getAuth().checkPlainPassword("user3.1", IP, null, null);
110-
});
11198

112-
//test with false and null pass: login is not allowed
113-
LdapConfig.ldap_allow_empty_pass = false;
114-
System.out.println("3.2 [" + LdapConfig.ldap_allow_empty_pass + "]");
115-
Assert.assertFalse(LdapConfig.ldap_allow_empty_pass);
99+
//empty pass - failure
100+
Assertions.assertFalse(LdapConfig.ldap_allow_empty_pass);
116101
Assertions.assertThrows(AuthenticationException.class, () -> {
117-
Env.getCurrentEnv().getAuth().checkPlainPassword("user3.2", IP, "", null);
102+
Env.getCurrentEnv().getAuth().checkPlainPassword("user3.1", IP, "", null);
118103
});
104+
105+
//non empty pass - success
106+
Assertions.assertFalse(LdapConfig.ldap_allow_empty_pass);
107+
Env.getCurrentEnv().getAuth().checkPlainPassword("user3.2", IP, "testPass", null);
119108
}
120109

121-
@After
110+
@AfterEach
122111
public void tearDown() {
123112
System.out.println("4.0 [" + LdapConfig.ldap_allow_empty_pass + "]");
124113
LdapConfig.ldap_allow_empty_pass = true; // restoring default value for other tests
125114
}
126115

127-
@Before
116+
@BeforeEach
128117
public void setUp() {
129118
System.out.println("5.0 [" + LdapConfig.ldap_allow_empty_pass + "]");
130119
LdapConfig.ldap_allow_empty_pass = true; //restoring default value for other tests

0 commit comments

Comments
 (0)