<commit_before>/*
Copyright 2017 Red Hat, Inc.

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.wildfly.extension.elytron;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CANCELLED;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
import static org.junit.Assert.assertTrue;

import org.apache.commons.lang3.SystemUtils;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.subsystem.test.AbstractSubsystemTest;
import org.jboss.as.subsystem.test.KernelServices;
import org.jboss.dmr.ModelNode;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests of syslog audit logging attributes
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class SyslogAuditLogTestCase extends AbstractSubsystemTest {

    private static final int UDP_PORT = 10838;
    private final int RECONNECT_NUMBER = 10;
    private final int BAD_RECONNECT_NUMBER = -2;
    private final int RECONNECT_TIMEOUT = 1;
    private final String HOST_NAME = "localhost";
    private final String SERVER_ADDRESS = "127.0.0.1";
    private final String BAD_SERVER_ADDRESS = "0.0.0.1";
    private final String UDP_TRANSPORT = "UDP";
    private final String BASE_SYSLOG_MESSAGE = "Elytron audit logging enabled with RFC format: ";
    private final String RFC3164_STRING = "RFC3164";
    private final String RFC3164_SYSLOG_MESSAGE = BASE_SYSLOG_MESSAGE + RFC3164_STRING;
    private final String RFC5424_STRING = "RFC5424";
    private final String RFC5424_SYSLOG_MESSAGE = BASE_SYSLOG_MESSAGE + RFC5424_STRING;
    private final String BAD_RFC_STRING = "RFC1";
    private String failString = "";

    private KernelServices services = null;
    private static SimpleSyslogServer udpServer = null;
    private ModelNode udpOperation = null;
    private ModelNode badHostUdpOperation = null;

    public SyslogAuditLogTestCase() {
        super(ElytronExtension.SUBSYSTEM_NAME, new ElytronExtension());
    }

    /**
     * Creates the kernel services and sets up the base operations before each test
     */
    @Before
    public void init() throws Exception {
        services = super.createKernelServicesBuilder(new TestEnvironment()).setSubsystemXmlResource("syslog-audit-logging.xml").build();
        if (!services.isSuccessfulBoot()) {
            Assert.fail(services.getBootError().toString());
        }
        udpOperation = createUdpSyslogOperation(SERVER_ADDRESS);
        badHostUdpOperation = createUdpSyslogOperation(BAD_SERVER_ADDRESS);
    }

    /**
     * Initializes the syslog server for all tests
     */
    @BeforeClass
    public static void initServer() throws Exception {
        udpServer = SimpleSyslogServer.createUdp(UDP_PORT);
    }

    /**
     * Resets the operations after each test
     */
    @After
    public void shutdownServer() {
        udpOperation = null;
        badHostUdpOperation = null;
    }

    /**
     * Closes the syslog server after all tests
     */
    @AfterClass
    public static void closeServer() {
        udpServer.close();
    }

    /**
     * Sets the syslog format to RFC3164 and verifies the Elytron AuditEndpoint is created with RFC3164
     * by verifying the connection message
     */
    @Test
    public void testRfc3164SyslogFormat() throws Exception {
        udpOperation.get(ElytronDescriptionConstants.SYSLOG_FORMAT).set(RFC3164_STRING);
        assertSuccess(services.executeOperation(udpOperation));
        verifyUdpMessage(RFC3164_SYSLOG_MESSAGE);
    }


    /**
     * Sets the syslog format to RFC5424 and verifies the Elytron AuditEndpoint is created with RFC5424
     * by verifying the connection message
     */
    @Test
    public void testRfc5424SyslogFormat() throws Exception {
        udpOperation.get(ElytronDescriptionConstants.SYSLOG_FORMAT).set(RFC5424_STRING);
        assertSuccess(services.executeOperation(udpOperation));
        verifyUdpMessage(RFC5424_SYSLOG_MESSAGE);
    }


    /**
     * Doesn't set a syslog and verifies the Elytron AuditEndpoint is created with the default value
     * of RFC5424 by verifying the connection message
     */
    @Test
    public void testDefaultSyslogFormat() throws Exception {
        assertSuccess(services.executeOperation(udpOperation));
        verifyUdpMessage(RFC5424_SYSLOG_MESSAGE);
    }

    /**
     * Tests that the syslog configuration operation fails if an invalid RFC format is used
     */
    @Test
    public void testWrongSyslogFormat() {
        udpOperation.get(ElytronDescriptionConstants.SYSLOG_FORMAT).set(BAD_RFC_STRING);
        ModelNode response = services.executeOperation(udpOperation);
        assertCorrectError(response, new String[] {"WFLYCTL0158", "RFC1"});
        assertFailed(response);
    }

    /**
     * Tests that the syslog configuration operation fails if an invalid reconnect-attempts number is used
     */
    @Test
    public void testBadIntegerReconnectAttempts() {
        udpOperation.get(ElytronDescriptionConstants.RECONNECT_ATTEMPTS).set(BAD_RECONNECT_NUMBER);
        ModelNode response = services.executeOperation(udpOperation);
        assertCorrectError(response, new String[] {"WFLYCTL0117", Integer.toString(BAD_RECONNECT_NUMBER)});
        assertFailed(response);
    }

    /**
     * Tests that the server does not attempt to resend the message
     */
    @Test
    public void testZeroReconnectAttemptsBadHost() {
        // Windows Server can have an issue echoing back an error, causing the test to fail by not seeing a failure sending
        Assume.assumeFalse("Test does not run on Windows Server", SystemUtils.OS_NAME.contains("Windows Server"));
        badHostUdpOperation.get(ElytronDescriptionConstants.RECONNECT_ATTEMPTS).set(0);
        ModelNode response = services.executeOperation(badHostUdpOperation);
        assertCorrectError(response, new String[] {"ELY12001", "0"});
        assertFailed(response);
    }

    /**
     * Tests that the server reattempts to send the message {@link SyslogAuditLogTestCase#RECONNECT_NUMBER} times
     */
    @Test
    public void testNumberedReconnectAttemptsBadHost() {
        // Windows Server can have an issue echoing back an error, causing the test to fail by not seeing a failure sending<commit_msg>[WFCORE-4631] Ignore the two reconnect tests for now as there will need to be changes based on ELY-1864.<commit_after>/*
Copyright 2017 Red Hat, Inc.

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.wildfly.extension.elytron;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CANCELLED;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
import static org.junit.Assert.assertTrue;

import org.apache.commons.lang3.SystemUtils;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.subsystem.test.AbstractSubsystemTest;
import org.jboss.as.subsystem.test.KernelServices;
import org.jboss.dmr.ModelNode;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

/**
 * Tests of syslog audit logging attributes
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class SyslogAuditLogTestCase extends AbstractSubsystemTest {

    private static final int UDP_PORT = 10838;
    private final int RECONNECT_NUMBER = 10;
    private final int BAD_RECONNECT_NUMBER = -2;
    private final int RECONNECT_TIMEOUT = 1;
    private final String HOST_NAME = "localhost";
    private final String SERVER_ADDRESS = "127.0.0.1";
    private final String BAD_SERVER_ADDRESS = "0.0.0.1";
    private final String UDP_TRANSPORT = "UDP";
    private final String BASE_SYSLOG_MESSAGE = "Elytron audit logging enabled with RFC format: ";
    private final String RFC3164_STRING = "RFC3164";
    private final String RFC3164_SYSLOG_MESSAGE = BASE_SYSLOG_MESSAGE + RFC3164_STRING;
    private final String RFC5424_STRING = "RFC5424";
    private final String RFC5424_SYSLOG_MESSAGE = BASE_SYSLOG_MESSAGE + RFC5424_STRING;
    private final String BAD_RFC_STRING = "RFC1";
    private String failString = "";

    private KernelServices services = null;
    private static SimpleSyslogServer udpServer = null;
    private ModelNode udpOperation = null;
    private ModelNode badHostUdpOperation = null;

    public SyslogAuditLogTestCase() {
        super(ElytronExtension.SUBSYSTEM_NAME, new ElytronExtension());
    }

    /**
     * Creates the kernel services and sets up the base operations before each test
     */
    @Before
    public void init() throws Exception {
        services = super.createKernelServicesBuilder(new TestEnvironment()).setSubsystemXmlResource("syslog-audit-logging.xml").build();
        if (!services.isSuccessfulBoot()) {
            Assert.fail(services.getBootError().toString());
        }
        udpOperation = createUdpSyslogOperation(SERVER_ADDRESS);
        badHostUdpOperation = createUdpSyslogOperation(BAD_SERVER_ADDRESS);
    }

    /**
     * Initializes the syslog server for all tests
     */
    @BeforeClass
    public static void initServer() throws Exception {
        udpServer = SimpleSyslogServer.createUdp(UDP_PORT);
    }

    /**
     * Resets the operations after each test
     */
    @After
    public void shutdownServer() {
        udpOperation = null;
        badHostUdpOperation = null;
    }

    /**
     * Closes the syslog server after all tests
     */
    @AfterClass
    public static void closeServer() {
        udpServer.close();
    }

    /**
     * Sets the syslog format to RFC3164 and verifies the Elytron AuditEndpoint is created with RFC3164
     * by verifying the connection message
     */
    @Test
    public void testRfc3164SyslogFormat() throws Exception {
        udpOperation.get(ElytronDescriptionConstants.SYSLOG_FORMAT).set(RFC3164_STRING);
        assertSuccess(services.executeOperation(udpOperation));
        verifyUdpMessage(RFC3164_SYSLOG_MESSAGE);
    }


    /**
     * Sets the syslog format to RFC5424 and verifies the Elytron AuditEndpoint is created with RFC5424
     * by verifying the connection message
     */
    @Test
    public void testRfc5424SyslogFormat() throws Exception {
        udpOperation.get(ElytronDescriptionConstants.SYSLOG_FORMAT).set(RFC5424_STRING);
        assertSuccess(services.executeOperation(udpOperation));
        verifyUdpMessage(RFC5424_SYSLOG_MESSAGE);
    }


    /**
     * Doesn't set a syslog and verifies the Elytron AuditEndpoint is created with the default value
     * of RFC5424 by verifying the connection message
     */
    @Test
    public void testDefaultSyslogFormat() throws Exception {
        assertSuccess(services.executeOperation(udpOperation));
        verifyUdpMessage(RFC5424_SYSLOG_MESSAGE);
    }

    /**
     * Tests that the syslog configuration operation fails if an invalid RFC format is used
     */
    @Test
    public void testWrongSyslogFormat() {
        udpOperation.get(ElytronDescriptionConstants.SYSLOG_FORMAT).set(BAD_RFC_STRING);
        ModelNode response = services.executeOperation(udpOperation);
        assertCorrectError(response, new String[] {"WFLYCTL0158", "RFC1"});
        assertFailed(response);
    }

    /**
     * Tests that the syslog configuration operation fails if an invalid reconnect-attempts number is used
     */
    @Test
    public void testBadIntegerReconnectAttempts() {
        udpOperation.get(ElytronDescriptionConstants.RECONNECT_ATTEMPTS).set(BAD_RECONNECT_NUMBER);
        ModelNode response = services.executeOperation(udpOperation);
        assertCorrectError(response, new String[] {"WFLYCTL0117", Integer.toString(BAD_RECONNECT_NUMBER)});
        assertFailed(response);
    }

    /**
     * Tests that the server does not attempt to resend the message
     */
    @Test
    @Ignore("WFCORE-4631 - need to possibly move this or invoke a SecurityDomain authentication attempt which will invoke a log message")
    public void testZeroReconnectAttemptsBadHost() {
        // Windows Server can have an issue echoing back an error, causing the test to fail by not seeing a failure sending
        Assume.assumeFalse("Test does not run on Windows Server", SystemUtils.OS_NAME.contains("Windows Server"));
        badHostUdpOperation.get(ElytronDescriptionConstants.RECONNECT_ATTEMPTS).set(0);
        ModelNode response = services.executeOperation(badHostUdpOperation);
        assertCorrectError(response, new String[] {"ELY12001", "0"});
        assertFailed(response);
    }

    /**
     * Tests that the server reattempts to send the message {@link SyslogAuditLogTestCase#RECONNECT_NUMBER} times
     */
    @Test
    @Ignore("WFCORE-4631 - need to possibly move this or invoke a SecurityDomain authentication attempt which will invoke a log message")
    public void testNumberedReconnectAttemptsBadHost() {
        // Windows Server can have an issue echoing back an error, causing the test to fail by not seeing a failure sending
{'fraction_non_alphanumeric': 0.0778655050367547, 'fraction_numerical': 0.01810509120609856, 'mean_word_length': 3.765812520272462, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 4, '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': '4233310', 'n_tokens_mistral': 4256, 'n_tokens_neox': 4003, 'n_words': 1162}