<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.tomcat.util.net.openssl;

import static org.junit.Assert.assertEquals;

import org.junit.Assume;
import org.junit.Test;

import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.jni.SSLContext;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.TesterSupport;

public class TestOpenSSLConf extends TomcatBaseTest {

    private static final String CIPHER = "AES256-SHA256";
    private static final String PROTOCOL = "-SSLv3,-TLSv1,TLSv1.1,-TLSv1.2";
    private static final String EXPECTED_PROTOCOLS = "SSLv2Hello,TLSv1.1";

    public SSLHostConfig initOpenSSLConfCmdCipher(String name, String value) throws Exception {
        Tomcat tomcat = getTomcatInstance();

        TesterSupport.initSsl(tomcat);

        String protocol = tomcat.getConnector().getProtocolHandlerClassName();
        // The tests are only supported for APR and OpenSSL
        if (!protocol.contains("Apr")) {
            String sslImplementation = String.valueOf(
                    tomcat.getConnector().getProperty("sslImplementationName"));
            Assume.assumeTrue("This test is only for OpenSSL based SSL connectors",
                sslImplementation.contains("openssl"));
        }

        OpenSSLConfCmd cmd = new OpenSSLConfCmd();
        cmd.setName(name);
        cmd.setValue(value);
        OpenSSLConf conf = new OpenSSLConf();
        conf.addCmd(cmd);
        SSLHostConfig[] sslHostConfigs = tomcat.getConnector().getProtocolHandler().findSslHostConfigs();
        assertEquals("Checking SSLHostConfigCount", 1, sslHostConfigs.length);
        sslHostConfigs[0].setOpenSslConf(conf);

        tomcat.start();

        sslHostConfigs = tomcat.getConnector().getProtocolHandler().findSslHostConfigs();
        assertEquals("Checking SSLHostConfigCount", 1, sslHostConfigs.length);
        return sslHostConfigs[0];
    }

    @Test
    public void testOpenSSLConfCmdCipher() throws Exception {
        SSLHostConfig sslHostConfig = initOpenSSLConfCmdCipher("CipherString", CIPHER);
        String[] ciphers = sslHostConfig.getEnabledCiphers();
        String cipherList = String.join(",", ciphers);
        assertEquals("Checking enabled cipher count (ciphers " +
                     cipherList + ")", 1, ciphers.length);
        assertEquals("Checking enabled cipher", CIPHER, ciphers[0]);
        ciphers = SSLContext.getCiphers(sslHostConfig.getOpenSslContext().longValue());
        cipherList = String.join(",", ciphers);
        assertEquals("Checking context cipher count (ciphers " +
                     cipherList + ")", 1, ciphers.length);
        assertEquals("Checking context cipher", CIPHER, ciphers[0]);
    }

    @Test
    public void testOpenSSLConfCmdProtocol() throws Exception {
        SSLHostConfig sslHostConfig = initOpenSSLConfCmdCipher("Protocol", PROTOCOL);
        String[] protocols = sslHostConfig.getEnabledProtocols();
        String protocolList = String.join(",", protocols);
        assertEquals("Checking enabled protocol count (protocols "
                     + protocolList + ")", 2, protocols.length);
        assertEquals("Checking enabled protocols", EXPECTED_PROTOCOLS,
                     protocolList);
    }
}
<commit_msg>Modernice array checks in unit test.<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.tomcat.util.net.openssl;

import org.hamcrest.CoreMatchers;

import static org.junit.Assert.assertEquals;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;

import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.jni.SSLContext;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.TesterSupport;

public class TestOpenSSLConf extends TomcatBaseTest {

    private static final String CIPHER = "AES256-SHA256";
    private static final String[] EXPECTED_CIPHERS = {"AES256-SHA256"};
    private static final String PROTOCOL = "-SSLv3,-TLSv1,TLSv1.1,-TLSv1.2";
    private static final String[] EXPECTED_PROTOCOLS = {"SSLv2Hello", "TLSv1.1"};

    public SSLHostConfig initOpenSSLConfCmdCipher(String name, String value) throws Exception {
        Tomcat tomcat = getTomcatInstance();

        TesterSupport.initSsl(tomcat);

        String protocol = tomcat.getConnector().getProtocolHandlerClassName();
        // The tests are only supported for APR and OpenSSL
        if (!protocol.contains("Apr")) {
            String sslImplementation = String.valueOf(
                    tomcat.getConnector().getProperty("sslImplementationName"));
            Assume.assumeTrue("This test is only for OpenSSL based SSL connectors",
                sslImplementation.contains("openssl"));
        }

        OpenSSLConfCmd cmd = new OpenSSLConfCmd();
        cmd.setName(name);
        cmd.setValue(value);
        OpenSSLConf conf = new OpenSSLConf();
        conf.addCmd(cmd);
        SSLHostConfig[] sslHostConfigs = tomcat.getConnector().getProtocolHandler().findSslHostConfigs();
        assertEquals("Checking SSLHostConfigCount", 1, sslHostConfigs.length);
        sslHostConfigs[0].setOpenSslConf(conf);

        tomcat.start();

        sslHostConfigs = tomcat.getConnector().getProtocolHandler().findSslHostConfigs();
        assertEquals("Checking SSLHostConfigCount", 1, sslHostConfigs.length);
        return sslHostConfigs[0];
    }

    @Test
    public void testOpenSSLConfCmdCipher() throws Exception {
        SSLHostConfig sslHostConfig = initOpenSSLConfCmdCipher("CipherString", CIPHER);
        String[] ciphers = sslHostConfig.getEnabledCiphers();
        Assert.assertThat("Checking HostConfig ciphers", ciphers,
                          CoreMatchers.is(EXPECTED_CIPHERS));
        ciphers = SSLContext.getCiphers(sslHostConfig.getOpenSslContext().longValue());
        Assert.assertThat("Checking native SSL context ciphers", ciphers,
                          CoreMatchers.is(EXPECTED_CIPHERS));
    }

    @Test
    public void testOpenSSLConfCmdProtocol() throws Exception {
        SSLHostConfig sslHostConfig = initOpenSSLConfCmdCipher("Protocol", PROTOCOL);
        String[] protocols = sslHostConfig.getEnabledProtocols();
        Assert.assertThat("Checking enabled HostConfig protocols", protocols,
                          CoreMatchers.is(EXPECTED_PROTOCOLS));
    }
}

{'fraction_non_alphanumeric': 0.09272432297516536, 'fraction_numerical': 0.00711344065892924, 'mean_word_length': 3.4374307862679956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1370682', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2174, 'n_words': 586}