/*
 * Copyright 2004-present the original author or authors.
 *
 * 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
 *
 *      https://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 org.springframework.security.crypto.password;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.security.crypto.keygen.KeyGenerators;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
 * Tests {@link LdapShaPasswordEncoder}.
 *
 * @author Luke Taylor
 */
@SuppressWarnings("deprecation")
public class LdapShaPasswordEncoderTests extends AbstractPasswordEncoderValidationTests {

	@BeforeEach
	void setup() {
		setEncoder(new LdapShaPasswordEncoder());
	}

	@Test
	public void invalidPasswordFails() {
		assertThat(getEncoder().matches("wrongpassword", "{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=")).isFalse();
	}

	@Test
	public void invalidSaltedPasswordFails() {
		assertThat(getEncoder().matches("wrongpassword", "{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX")).isFalse();
		assertThat(getEncoder().matches("wrongpassword", "{SSHA}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd")).isFalse();
	}

	/**
	 * Test values generated by 'slappasswd -h {SHA} -s boabspasswurd'
	 */
	@Test
	public void validPasswordSucceeds() {
		LdapShaPasswordEncoder ldap = getEncoder();
		ldap.setForceLowerCasePrefix(false);
		assertThat(getEncoder().matches("boabspasswurd", "{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=")).isTrue();
		assertThat(getEncoder().matches("boabspasswurd", "{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=")).isTrue();
		ldap.setForceLowerCasePrefix(true);
		assertThat(getEncoder().matches("boabspasswurd", "{SHA}ddSFGmjXYPbZC+NXR2kCzBRjqiE=")).isTrue();
		assertThat(getEncoder().matches("boabspasswurd", "{sha}ddSFGmjXYPbZC+NXR2kCzBRjqiE=")).isTrue();
	}

	/**
	 * Test values generated by 'slappasswd -s boabspasswurd'
	 */
	@Test
	public void validSaltedPasswordSucceeds() {
		LdapShaPasswordEncoder ldap = getEncoder();
		ldap.setForceLowerCasePrefix(false);
		assertThat(getEncoder().matches("boabspasswurd", "{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX")).isTrue();
		assertThat(getEncoder().matches("boabspasswurd", "{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd")).isTrue();
		ldap.setForceLowerCasePrefix(true);
		assertThat(getEncoder().matches("boabspasswurd", "{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX")).isTrue();
		assertThat(getEncoder().matches("boabspasswurd", "{ssha}PQy2j+6n5ytA+YlAKkM8Fh4p6u2JxfVd")).isTrue();
	}

	@Test
	// SEC-1031
	public void fullLengthOfHashIsUsedInComparison() {
		assertThat(getEncoder().matches("boabspasswurd", "{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgX")).isTrue();
		// Change the first hash character from '2' to '3'
		assertThat(getEncoder().matches("boabspasswurd", "{SSHA}35ro4PKC8jhQZ26jVsozhX/xaP0suHgX")).isFalse();
		// Change the last hash character from 'X' to 'Y'
		assertThat(getEncoder().matches("boabspasswurd", "{SSHA}25ro4PKC8jhQZ26jVsozhX/xaP0suHgY")).isFalse();
	}

	@Test
	public void correctPrefixCaseIsUsed() {
		LdapShaPasswordEncoder ldap = getEncoder();
		ldap.setForceLowerCasePrefix(false);
		assertThat(ldap.encode("somepassword").startsWith("{SSHA}"));
		ldap.setForceLowerCasePrefix(true);
		assertThat(ldap.encode("somepassword").startsWith("{ssha}"));
		setEncoder(new LdapShaPasswordEncoder(KeyGenerators.shared(0)));
		ldap.setForceLowerCasePrefix(false);
		assertThat(getEncoder().encode("somepassword").startsWith("{SHA}"));
		ldap.setForceLowerCasePrefix(true);
		assertThat(getEncoder().encode("somepassword").startsWith("{SSHA}"));
	}

	@Test
	public void invalidPrefixIsRejected() {
		assertThatIllegalArgumentException().isThrownBy(() -> getEncoder().matches("somepassword", "{MD9}xxxxxxxxxx"));
	}

	@Test
	public void malformedPrefixIsRejected() {
		// No right brace
		assertThatIllegalArgumentException()
			.isThrownBy(() -> getEncoder().matches("somepassword", "{SSHA25ro4PKC8jhQZ26jVsozhX/xaP0suHgX"));
	}

}
