<commit_before>/* vi: set ts=4 expandtab shiftwidth=4: */
/**
 * @file
 *
 * Copyright 2015-2016 Digital Aggregates Corporation, Colorado, USA<BR>
 * Licensed under the terms in README.h<BR>
 * <NAME> <<EMAIL>><BR>
 * http://www.diag.com/navigation/downloads/Diminuto.html<BR>
 *
 * NOTE: Creating a raw socket such as used for ICMP is a privileged
 *       operation. Hence this unit test has to be run with root privileges.
 */

#include <unistd.h>
#include <string.h>
#include "com/diag/diminuto/diminuto_unittest.h"
#include "com/diag/diminuto/diminuto_log.h"
#include "com/diag/diminuto/diminuto_ping6.h"
#include "com/diag/diminuto/diminuto_delay.h"
#include "com/diag/diminuto/diminuto_frequency.h"

int main(int argc, char * argv[])
{
    int sock;
    char buffer[sizeof("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX")];
    diminuto_ipv6_t to;
    diminuto_ipv6_t from;
    ssize_t size;
    uint8_t type;
    uint16_t id;
    uint16_t seq;
    diminuto_port_t port;
    diminuto_ticks_t elapsed;
    const char * Address = 0;
    const char * Interface = 0;
    const char * Port = 0;
    extern char * optarg;
    extern int optind;
    extern int opterr;
    extern int optopt;
    int opt;

    SETLOGMASK();

    while ((opt = getopt(argc, argv, "a:i:p:")) >= 0) {
        switch (opt) {
        case 'a':
            /* e.g. "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b" */
            Address = optarg;
            break;
        case 'i':
            /* e.g. "eth0" */
            Interface = optarg;
            break;
        case 'p':
            /* e.g. "5555" */
            Port = optarg;
            break;
        default:
            break;
        }
    }

    {
        TEST();

        ASSERT((sock = diminuto_ping6_datagram_peer()) >= 0);

        STATUS();
    }

    {
        TEST();

        if (Interface != (const char *)0) {
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "interface=\"%s\"\n", Interface);
            ASSERT(diminuto_ping6_interface(sock, Interface) >= 0);
        }

        STATUS();
    }

    {
        TEST();

        memset(&from, 0, sizeof(from));
        if (Address != (const char *)0) {
            from = diminuto_ipc6_address(argv[2]);
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "address=\"%s\"=%x:%x:%x:%x:%x:%x:%x:%x=\"%s\"\n", Address, from.u16[0], from.u16[1], from.u16[2], from.u16[3], from.u16[4], from.u16[5], from.u16[6], from.u16[7], diminuto_ipc6_address2string(from, buffer, sizeof(buffer)));
        }

        port = 0;
        if (Port != (const char *)0) {
            port = strtoul(Port, (char **)0, 0);
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "port=\"%s\"=%u\n", Port, port);
        }

        if ((Address != (const char *)0) || (Port != (const char * )0)) {
            ASSERT(diminuto_ping6_source(sock, from, port) >= 0);
        }

        STATUS();
    }

    {
        static const uint16_t ID = 0xcafe;
        uint16_t ss;
        diminuto_ticks_t delay;

        TEST();

        delay = diminuto_frequency();

        to = diminuto_ipc6_address("google.com");
        DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "to=\"%s\"\n", diminuto_ipc6_address2string(to, buffer, sizeof(buffer)));
        ASSERT(memcmp(&to, &DIMINUTO_IPC6_UNSPECIFIED, sizeof(to)) != 0);

        for (ss = 0; ss < 10; ++ss) {
            ASSERT(diminuto_ping6_datagram_send(sock, to, ID, ss) > 0);
            memcpy(&from, &DIMINUTO_IPC6_UNSPECIFIED, sizeof(from));
            type = ~0;
            id = 0;
            seq = ~0;
            elapsed = 0;
            ASSERT((size = diminuto_ping6_datagram_recv(sock, &from, (uint8_t *)0, (uint16_t *)0, (uint16_t *)0, (diminuto_ticks_t *)0)) == 0);
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "from=\"%s\" size=%zu type=0x%x id=0x%x seq=%u elapsed=%lluticks\n", diminuto_ipc6_address2string(from, buffer, sizeof(buffer)), size, type, id, seq, elapsed);
            ASSERT(memcmp(&from, &DIMINUTO_IPC6_UNSPECIFIED, sizeof(from)) != 0);
            ASSERT(type != ~0);
            ASSERT(id == ID);
            ASSERT(seq == ss);
            ASSERT(elapsed > 0);
            diminuto_delay(delay, 0);
        }

        STATUS();
    }

    {
        TEST();

        ASSERT(diminuto_ping6_close(sock) >= 0);

        STATUS();
    }

    EXIT();
}
<commit_msg>[1] Successful test of ping6.<commit_after>/* vi: set ts=4 expandtab shiftwidth=4: */
/**
 * @file
 *
 * Copyright 2015-2016 Digital Aggregates Corporation, Colorado, USA<BR>
 * Licensed under the terms in README.h<BR>
 * <NAME> <<EMAIL>><BR>
 * http://www.diag.com/navigation/downloads/Diminuto.html<BR>
 *
 * NOTE: Creating a raw socket such as used for ICMP is a privileged
 *       operation. Hence this unit test has to be run with root privileges.
 */

#include <unistd.h>
#include <string.h>
#include "com/diag/diminuto/diminuto_unittest.h"
#include "com/diag/diminuto/diminuto_log.h"
#include "com/diag/diminuto/diminuto_ping6.h"
#include "com/diag/diminuto/diminuto_delay.h"
#include "com/diag/diminuto/diminuto_frequency.h"

int main(int argc, char * argv[])
{
    int sock;
    char buffer[sizeof("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX")];
    diminuto_ipv6_t to;
    diminuto_ipv6_t from;
    ssize_t size;
    uint8_t type;
    uint16_t id;
    uint16_t seq;
    diminuto_port_t port;
    diminuto_ticks_t elapsed;
    const char * Address = 0;
    const char * Interface = 0;
    const char * Port = 0;
    extern char * optarg;
    extern int optind;
    extern int opterr;
    extern int optopt;
    int opt;

    SETLOGMASK();

    while ((opt = getopt(argc, argv, "a:i:p:")) >= 0) {
        switch (opt) {
        case 'a':
            /* e.g. "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b" */
            Address = optarg;
            break;
        case 'i':
            /* e.g. "eth0" */
            Interface = optarg;
            break;
        case 'p':
            /* e.g. "5555" */
            Port = optarg;
            break;
        default:
            break;
        }
    }

    {
        TEST();

        ASSERT((sock = diminuto_ping6_datagram_peer()) >= 0);

        STATUS();
    }

    {
        TEST();

        if (Interface != (const char *)0) {
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "interface=\"%s\"\n", Interface);
            ASSERT(diminuto_ping6_interface(sock, Interface) >= 0);
        }

        STATUS();
    }

    {
        TEST();

        memset(&from, 0, sizeof(from));
        if (Address != (const char *)0) {
            from = diminuto_ipc6_address(argv[2]);
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "address=\"%s\"=%x:%x:%x:%x:%x:%x:%x:%x=\"%s\"\n", Address, from.u16[0], from.u16[1], from.u16[2], from.u16[3], from.u16[4], from.u16[5], from.u16[6], from.u16[7], diminuto_ipc6_address2string(from, buffer, sizeof(buffer)));
        }

        port = 0;
        if (Port != (const char *)0) {
            port = strtoul(Port, (char **)0, 0);
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "port=\"%s\"=%u\n", Port, port);
        }

        if ((Address != (const char *)0) || (Port != (const char * )0)) {
            ASSERT(diminuto_ping6_source(sock, from, port) >= 0);
        }

        STATUS();
    }

    {
        static const uint16_t ID = 0xcafe;
        uint16_t ss;
        diminuto_ticks_t delay;

        TEST();

        delay = diminuto_frequency();

        to = diminuto_ipc6_address("google.com");
        DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "to=\"%s\"\n", diminuto_ipc6_address2string(to, buffer, sizeof(buffer)));
        ASSERT(memcmp(&to, &DIMINUTO_IPC6_UNSPECIFIED, sizeof(to)) != 0);

        for (ss = 0; ss < 10; ++ss) {
            ASSERT(diminuto_ping6_datagram_send(sock, to, ID, ss) > 0);
            memcpy(&from, &DIMINUTO_IPC6_UNSPECIFIED, sizeof(from));
            type = ~0;
            id = 0;
            seq = ~0;
            elapsed = 0;
            ASSERT((size = diminuto_ping6_datagram_recv(sock, &from, &type, &id, &seq, &elapsed)) > 0);
            DIMINUTO_LOG_DEBUG(DIMINUTO_LOG_HERE "from=\"%s\" size=%zu type=0x%x id=0x%x seq=%u elapsed=%lluticks\n", diminuto_ipc6_address2string(from, buffer, sizeof(buffer)), size, type, id, seq, elapsed);
            ASSERT(memcmp(&from, &DIMINUTO_IPC6_UNSPECIFIED, sizeof(from)) != 0);
            ASSERT(type != ~0);
            ASSERT(id == ID);
            ASSERT(seq == ss);
            ASSERT(elapsed > 0);
            diminuto_delay(delay, 0);
        }

        STATUS();
    }

    {
        TEST();

        ASSERT(diminuto_ping6_close(sock) >= 0);

        STATUS();
    }

    EXIT();
}

{'fraction_non_alphanumeric': 0.15318054256314312, 'fraction_numerical': 0.02981758652946679, 'mean_word_length': 2.2545662100456623, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '2241251', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3102, 'n_words': 620}