|
| 1 | +/* |
| 2 | + * Copyright 1999-2025 Alibaba Group Holding Ltd. |
| 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 | + * You may obtain a copy of 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.nacos.plugin.auth.impl; |
| 18 | + |
| 19 | +import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants; |
| 20 | +import org.apache.commons.lang.StringUtils; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +/** |
| 27 | + * SafeBcryptPasswordEncoderTest. |
| 28 | + * |
| 29 | + * @author linuwmingshi |
| 30 | + */ |
| 31 | +public class SafeBcryptPasswordEncoderTest { |
| 32 | + |
| 33 | + /** |
| 34 | + * SafeBCryptPasswordEncoder. |
| 35 | + */ |
| 36 | + private static final SafeBcryptPasswordEncoder ENCODER = new SafeBcryptPasswordEncoder(); |
| 37 | + |
| 38 | + @Test |
| 39 | + void testValidPasswordLength() { |
| 40 | + String rawPassword = StringUtils.repeat("A", AuthConstants.MAX_PASSWORD_LENGTH); |
| 41 | + String encodedPassword = ENCODER.encode(rawPassword); |
| 42 | + |
| 43 | + assertTrue(ENCODER.matches(rawPassword, encodedPassword), "72-character rawPassword should match"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testExcessivePasswordLength() { |
| 48 | + String rawPassword = StringUtils.repeat("A", AuthConstants.MAX_PASSWORD_LENGTH + 1); |
| 49 | + String encodedPassword = ENCODER.encode(rawPassword.substring(0, AuthConstants.MAX_PASSWORD_LENGTH)); |
| 50 | + |
| 51 | + assertFalse(ENCODER.matches(rawPassword, encodedPassword), "73-character rawPassword should be rejected"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testEdgeCase() { |
| 56 | + String rawPassword72 = StringUtils.repeat("A", AuthConstants.MAX_PASSWORD_LENGTH); |
| 57 | + String rawPassword73 = rawPassword72 + "A"; |
| 58 | + String encodedPassword = ENCODER.encode(rawPassword72); |
| 59 | + |
| 60 | + assertTrue(ENCODER.matches(rawPassword72, encodedPassword), "72-character password must pass"); |
| 61 | + assertFalse(ENCODER.matches(rawPassword73, encodedPassword), "73-character password must fail"); |
| 62 | + } |
| 63 | +} |
0 commit comments