summaryrefslogtreecommitdiff
path: root/testing/hstox/test_main.c
blob: 7ac18da0f3508becb9e7af6bfc48f365ac937f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "driver.h"
#include "errors.h"

#include <stdio.h>
#include <stdlib.h>

// The msgpack-rpc test port expected by the test runner.
#define PORT 1234

// Timeout in seconds after which the driver shuts down. Currently, a complete
// test run takes about 7 seconds. The timeout of 2 minutes is a guess so it
// keeps working for a while, even on a very slow computer.
#define TIMEOUT 120

static char const *error_desc(int code)
{
    switch (code) {
        case E_OK:
            return "Success";

        case E_NOMEM:
            return "Error: Out of memory";

        case E_SOCKET:
            return "Error: socket creation failed";

        case E_BIND:
            return "Error: bind failed";

        case E_LISTEN:
            return "Error: listen failed";

        case E_ACCEPT:
            return "Error: accept failed";

        case E_PARSE:
            return "Error: unable to parse msgpack input";

        case E_OPEN:
            return "Error: open failed";

        case E_READ:
            return "Error: read failed";

        case E_WRITE:
            return "Error: write failed";

        case E_SODIUM:
            return "Error: libsodium initialisation failed";
    }

    return "Unknown error code";
}

#ifdef __GLASGOW_HASKELL__
#define main test_main
#endif
int main(void)
{
    struct settings cfg    = {true, true};
    uint32_t        result = network_main(cfg, PORT, TIMEOUT);
    int             line   = result >> 16;
    int             error  = (result >> 8) & 0xff;
    int             code   = result & 0xff;

    if (code != E_OK) {
        printf("%s, errno=%d, line=%d\n", error_desc(code), error, line);
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}