<commit_before>/***********************************************************************************************************************************
Test Tls Client
***********************************************************************************************************************************/
#include <fcntl.h>
#include <unistd.h>

#include "common/time.h"

#include "common/harnessTls.h"

/***********************************************************************************************************************************
Test server with subject alternate names
***********************************************************************************************************************************/
static void
testTlsServerAltName(void)
{
    FUNCTION_HARNESS_VOID();

    if (fork() == 0)
    {
        // Change log process id to aid in debugging
        hrnLogProcessIdSet(1);

        harnessTlsServerInit(
            harnessTlsTestPort(),
            strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-alt-name.crt", testRepoPath())),
            strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".key", testRepoPath())));

        // Certificate error on invalid ca path
        harnessTlsServerAccept();
        harnessTlsServerClose();

        if (testContainer())
        {
            // Success on valid ca file and match common name
            harnessTlsServerAccept();
            harnessTlsServerClose();

            // Success on valid ca file and match alt name
            harnessTlsServerAccept();
            harnessTlsServerClose();

            // Unable to find matching hostname in certificate
            harnessTlsServerAccept();
            harnessTlsServerClose();
        }

        // Certificate error
        harnessTlsServerAccept();
        harnessTlsServerClose();

        // Certificate ignored
        harnessTlsServerAccept();
        harnessTlsServerClose();

        exit(0);
    }

    FUNCTION_HARNESS_RESULT_VOID();
}

/***********************************************************************************************************************************
Test server
***********************************************************************************************************************************/
static void
testTlsServer(void)
{
    FUNCTION_HARNESS_VOID();

    if (fork() == 0)
    {
        // Change log process id to aid in debugging
        hrnLogProcessIdSet(1);

        harnessTlsServerInitDefault();

        // First protocol exchange
        harnessTlsServerAccept();

        harnessTlsServerExpect("some protocol info");
        harnessTlsServerReply("something:0\n");

        sleepMSec(100);
        harnessTlsServerReply("some ");

        sleepMSec(100);
        harnessTlsServerReply("contentAND MORE");

        // This will cause the client to disconnect
        sleepMSec(500);

        // Second protocol exchange
        harnessTlsServerExpect("more protocol info");
        harnessTlsServerReply("0123456789AB");
        harnessTlsServerClose();

        // Need data in read buffer to test tlsWriteContinue()
        harnessTlsServerAccept();
        harnessTlsServerReply("0123456789AB");
        harnessTlsServerClose();

        exit(0);
    }

    FUNCTION_HARNESS_RESULT_VOID();
}

/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
void
testRun(void)
{
    FUNCTION_HARNESS_VOID();

    // *****************************************************************************************************************************
    if (testBegin("Socket Common"))
    {
        // Save socket settings
        struct SocketLocal socketLocalSave = socketLocal;

        struct addrinfo hints = (struct addrinfo)
        {
            .ai_family = AF_UNSPEC,
            .ai_socktype = SOCK_STREAM,
            .ai_protocol = IPPROTO_TCP,
        };

        struct addrinfo *hostAddress;
        int result;
        const char *host = "127.0.0.1";
        const char *port = "7777";

        if ((result = getaddrinfo(host, port, &hints, &hostAddress)) != 0)
            THROW_FMT(HostConnectError, "unable to get address for '%s': [%d] %s", host, result, gai_strerror(result));

        TRY_BEGIN()
        {
            int fd = socket(hostAddress->ai_family, hostAddress->ai_socktype, hostAddress->ai_protocol);
            THROW_ON_SYS_ERROR(fd == -1, HostConnectError, "unable to create socket");

            // ---------------------------------------------------------------------------------------------------------------------
            TEST_TITLE("enable options");

            sckInit(true, 32, 3113, 818);
            sckOptionSet(fd);

            TEST_RESULT_INT(fcntl(fd, F_GETFD), FD_CLOEXEC, "check FD_CLOEXEC");

            socklen_t socketValueSize = sizeof(int);
            int noDelayValue = 0;

            THROW_ON_SYS_ERROR(
                getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &noDelayValue, &socketValueSize) == -1, ProtocolError,
                "unable get TCP_NO_DELAY");

            TEST_RESULT_INT(noDelayValue, 1, "check TCP_NODELAY");

            int keepAliveValue = 0;<commit_msg>Suppress uncoverable error in common/io-tls test module.<commit_after>/***********************************************************************************************************************************
Test Tls Client
***********************************************************************************************************************************/
#include <fcntl.h>
#include <unistd.h>

#include "common/time.h"

#include "common/harnessTls.h"

/***********************************************************************************************************************************
Test server with subject alternate names
***********************************************************************************************************************************/
static void
testTlsServerAltName(void)
{
    FUNCTION_HARNESS_VOID();

    if (fork() == 0)
    {
        // Change log process id to aid in debugging
        hrnLogProcessIdSet(1);

        harnessTlsServerInit(
            harnessTlsTestPort(),
            strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-alt-name.crt", testRepoPath())),
            strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".key", testRepoPath())));

        // Certificate error on invalid ca path
        harnessTlsServerAccept();
        harnessTlsServerClose();

        if (testContainer())
        {
            // Success on valid ca file and match common name
            harnessTlsServerAccept();
            harnessTlsServerClose();

            // Success on valid ca file and match alt name
            harnessTlsServerAccept();
            harnessTlsServerClose();

            // Unable to find matching hostname in certificate
            harnessTlsServerAccept();
            harnessTlsServerClose();
        }

        // Certificate error
        harnessTlsServerAccept();
        harnessTlsServerClose();

        // Certificate ignored
        harnessTlsServerAccept();
        harnessTlsServerClose();

        exit(0);
    }

    FUNCTION_HARNESS_RESULT_VOID();
}

/***********************************************************************************************************************************
Test server
***********************************************************************************************************************************/
static void
testTlsServer(void)
{
    FUNCTION_HARNESS_VOID();

    if (fork() == 0)
    {
        // Change log process id to aid in debugging
        hrnLogProcessIdSet(1);

        harnessTlsServerInitDefault();

        // First protocol exchange
        harnessTlsServerAccept();

        harnessTlsServerExpect("some protocol info");
        harnessTlsServerReply("something:0\n");

        sleepMSec(100);
        harnessTlsServerReply("some ");

        sleepMSec(100);
        harnessTlsServerReply("contentAND MORE");

        // This will cause the client to disconnect
        sleepMSec(500);

        // Second protocol exchange
        harnessTlsServerExpect("more protocol info");
        harnessTlsServerReply("0123456789AB");
        harnessTlsServerClose();

        // Need data in read buffer to test tlsWriteContinue()
        harnessTlsServerAccept();
        harnessTlsServerReply("0123456789AB");
        harnessTlsServerClose();

        exit(0);
    }

    FUNCTION_HARNESS_RESULT_VOID();
}

/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
void
testRun(void)
{
    FUNCTION_HARNESS_VOID();

    // *****************************************************************************************************************************
    if (testBegin("Socket Common"))
    {
        // Save socket settings
        struct SocketLocal socketLocalSave = socketLocal;

        struct addrinfo hints = (struct addrinfo)
        {
            .ai_family = AF_UNSPEC,
            .ai_socktype = SOCK_STREAM,
            .ai_protocol = IPPROTO_TCP,
        };

        struct addrinfo *hostAddress;
        int result;
        const char *host = "127.0.0.1";
        const char *port = "7777";

        if ((result = getaddrinfo(host, port, &hints, &hostAddress)) != 0)
        {
            THROW_FMT(                                              // {uncoverable - lookup on IP should never fail}
                HostConnectError, "unable to get address for '%s': [%d] %s", host, result, gai_strerror(result));
        }

        TRY_BEGIN()
        {
            int fd = socket(hostAddress->ai_family, hostAddress->ai_socktype, hostAddress->ai_protocol);
            THROW_ON_SYS_ERROR(fd == -1, HostConnectError, "unable to create socket");

            // ---------------------------------------------------------------------------------------------------------------------
            TEST_TITLE("enable options");

            sckInit(true, 32, 3113, 818);
            sckOptionSet(fd);

            TEST_RESULT_INT(fcntl(fd, F_GETFD), FD_CLOEXEC, "check FD_CLOEXEC");

            socklen_t socketValueSize = sizeof(int);
            int noDelayValue = 0;

            THROW_ON_SYS_ERROR(
                getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &noDelayValue, &socketValueSize) == -1, ProtocolError,
                "unable get TCP_NO_DELAY");

            TEST_RESULT_INT(noDelayValue, 1, "check TCP_NODELAY");

            int keepAliveValue = 0;
{'fraction_non_alphanumeric': 0.3154194801590972, 'fraction_numerical': 0.01128480251595597, 'mean_word_length': 2.8313253012048194, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4919656', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2446, 'n_words': 539}