<commit_before>#include "minunit.h"
#include <connection.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <zmq.h>
#include <task/task.h>
#include <dir.h>

FILE *LOG_FILE = NULL;

Server *SRV = NULL;

char *test_Connection_create_destroy() 
{
    const char remote[IPADDR_SIZE];

    Connection *conn = Connection_create(NULL, 0, 0, remote);
    Connection_destroy(conn);

    return NULL;
}

char *test_Connection_deliver()
{
    bstring t1;

    int rc = Connection_deliver(1, t1 = bfromcstr("TEST"));
    // depending on the platform this will fail or not if send is allowed on files
    mu_assert(rc == -1, "Should NOT be able to write.");

    bdestroy(t1);

    return NULL;
}

int test_task_with_sample(const char *sample_file)
{
    check(SRV, "Server isn't configured.");

    Connection *conn = Connection_create(SRV, 12, 1400, "127.0.0.1");
    check(conn, "Failed to create the conn.");

    conn->fd = open(sample_file, O_RDONLY);
    check(!conn->fd >= 0, "Failed to open the sample file: %s.", sample_file);

    Connection_task(conn);

    return 1;
error:
    return 0;
}

char *test_Connection_task() 
{
    debug(">>>>> XML <<<<<");
    mu_assert(test_task_with_sample("tests/sample.xml"), "xml failed.");
    debug(">>>>> JSON <<<<<");
    mu_assert(test_task_with_sample("tests/sample.json"), "json failed.");
    debug(">>>>> GARBAGE <<<<<");
    mu_assert(test_task_with_sample("tests/sample.garbage"), "garbage failed");
    debug(">>>>>> HTTP <<<<<<");

    mu_assert(test_task_with_sample("tests/sample.http"), "http failed");

    return NULL;
}

char * all_tests() {
    mu_suite_start();

    Server_init();
    Server *SRV = Server_create("uuid", "1999", "chroot", "access_log", "error_log", "pid_file");
    Host *zedshaw_com = Host_create("zedshaw.com");

    Host_add_backend(zedshaw_com, "@chat", strlen("@chat"), BACKEND_HANDLER, NULL);

    Dir *tests = Dir_create("tests/", "/tests/", "index.html", "text/plain");

    Host_add_backend(zedshaw_com, "/tests", strlen("/tests"), BACKEND_DIR, tests);

    Server_set_default_host(SRV, zedshaw_com);

    mu_run_test(test_Connection_create_destroy);
    mu_run_test(test_Connection_deliver);
    mu_run_test(test_Connection_task);

    Server_destroy(SRV);
    // TODO: the above will eventually do this<commit_msg>Connection tests won't work now that we use fdsend.<commit_after>#include "minunit.h"
#include <connection.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <zmq.h>
#include <task/task.h>
#include <dir.h>

FILE *LOG_FILE = NULL;

Server *SRV = NULL;

char *test_Connection_create_destroy() 
{
    const char remote[IPADDR_SIZE];

    Connection *conn = Connection_create(NULL, 0, 0, remote);
    Connection_destroy(conn);

    return NULL;
}

char *test_Connection_deliver()
{
    bstring t1;

    int rc = Connection_deliver(1, t1 = bfromcstr("TEST"));
    // depending on the platform this will fail or not if send is allowed on files
    mu_assert(rc == -1, "Should NOT be able to write.");

    bdestroy(t1);

    return NULL;
}

int test_task_with_sample(const char *sample_file)
{
    check(SRV, "Server isn't configured.");

    Connection *conn = Connection_create(SRV, 12, 1400, "127.0.0.1");
    check(conn, "Failed to create the conn.");

    conn->fd = open(sample_file, O_RDONLY);
    check(!conn->fd >= 0, "Failed to open the sample file: %s.", sample_file);

    Connection_task(conn);

    return 1;
error:
    return 0;
}

char *test_Connection_task() 
{
    // TODO: bring these back with a different mechanism
    // debug(">>>>> XML <<<<<");
    // mu_assert(test_task_with_sample("tests/sample.xml"), "xml failed.");
    // debug(">>>>> JSON <<<<<");
    // mu_assert(test_task_with_sample("tests/sample.json"), "json failed.");
    // debug(">>>>> GARBAGE <<<<<");
    // mu_assert(test_task_with_sample("tests/sample.garbage"), "garbage failed");
    // debug(">>>>>> HTTP <<<<<<");
    // mu_assert(test_task_with_sample("tests/sample.http"), "http failed");

    return NULL;
}

char * all_tests() {
    mu_suite_start();

    Server_init();
    Server *SRV = Server_create("uuid", "1999", "chroot", "access_log", "error_log", "pid_file");
    Host *zedshaw_com = Host_create("zedshaw.com");

    Host_add_backend(zedshaw_com, "@chat", strlen("@chat"), BACKEND_HANDLER, NULL);

    Dir *tests = Dir_create("tests/", "/tests/", "index.html", "text/plain");

    Host_add_backend(zedshaw_com, "/tests", strlen("/tests"), BACKEND_DIR, tests);

    Server_set_default_host(SRV, zedshaw_com);

    mu_run_test(test_Connection_create_destroy);
    mu_run_test(test_Connection_deliver);
    mu_run_test(test_Connection_task);

    Server_destroy(SRV);
    // TODO: the above will eventually do this
{'fraction_non_alphanumeric': 0.16568668626627467, 'fraction_numerical': 0.010919781604367913, 'mean_word_length': 4.199781659388647, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, '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': '482861', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1631, 'n_words': 371}