/*
 * Copyright 2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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 org.keycloak.saml;

import org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder;

import org.junit.Test;

import static org.junit.Assert.assertNull;

/**
 * Tests for {@link SAMLRequestParser}.
 */
public class SAMLRequestParserTest {

    /**
     * Tests that parseRequestPostBinding returns null instead of throwing
     * IllegalArgumentException when given invalid Base64 input.
     *
     * @see <a href="https://github.com/keycloak/keycloak/issues/44803">Issue #44803</a>
     */
    @Test
    public void testParseRequestPostBindingWithInvalidBase64ReturnsNull() {
        // Invalid Base64 string containing '?' character (decimal 63)
        String invalidBase64 = "SGVsbG8gV29ybGQ/InvalidBase64!@#$";

        SAMLDocumentHolder result = SAMLRequestParser.parseRequestPostBinding(invalidBase64);

        assertNull("parseRequestPostBinding should return null for invalid Base64 input", result);
    }

    /**
     * Tests that parseResponsePostBinding returns null instead of throwing
     * IllegalArgumentException when given invalid Base64 input.
     *
     * @see <a href="https://github.com/keycloak/keycloak/issues/44803">Issue #44803</a>
     */
    @Test
    public void testParseResponsePostBindingWithInvalidBase64ReturnsNull() {
        // Invalid Base64 string containing '?' character (decimal 63)
        String invalidBase64 = "SGVsbG8gV29ybGQ/InvalidBase64!@#$";

        SAMLDocumentHolder result = SAMLRequestParser.parseResponsePostBinding(invalidBase64);

        assertNull("parseResponsePostBinding should return null for invalid Base64 input", result);
    }

    @Test
    public void testParseRequestPostBindingWithNullReturnsNullArgumentException() {
        try {
            SAMLRequestParser.parseRequestPostBinding(null);
            // If we get here, the method didn't throw an exception which is unexpected
            // for null input based on PostBindingUtil.base64Decode behavior
        } catch (IllegalArgumentException e) {
            // Expected - null input throws IllegalArgumentException
        }
    }
}