Example #1
0
int IsIPV6Address(char *name)
{
    if (!name)
    {
        return false;
    }
    Buffer *buffer = BufferNewFrom(name, strlen(name));
    if (!buffer)
    {
        return false;
    }
    IPAddress *ip_address = NULL;
    bool is_ip = false;
    is_ip = IPAddressIsIPAddress(buffer, &ip_address);
    if (!is_ip)
    {
        BufferDestroy(buffer);
        return false;
    }
    if (IPAddressType(ip_address) != IP_ADDRESS_TYPE_IPV6)
    {
        BufferDestroy(buffer);
        IPAddressDestroy(&ip_address);
        return false;
    }
    BufferDestroy(buffer);
    IPAddressDestroy(&ip_address);
    return true;
}
Example #2
0
static void test_generic_interface(void)
{
    /*
     * This test might seem short, but it is intentional.
     * All the parsing tests should be implemented directly
     * on the corresponding parser test. Keep this test as
     * lean as possible.
     */
    IPAddress *address = NULL;
    Buffer *buffer = NULL;

    buffer = BufferNew();
    assert_true(buffer != NULL);
    BufferSet(buffer, "127.0.0.1", strlen("127.0.0.1"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV4, IPAddressType(address));
    assert_string_equal("127.0.0.1", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(0, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferSet(buffer, "127.0.0.1:8080", strlen("127.0.0.1:8080"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV4, IPAddressType(address));
    assert_string_equal("127.0.0.1", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(8080, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferSet(buffer, "0:1:2:3:4:5:6:7", strlen("0:1:2:3:4:5:6:7"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV6, IPAddressType(address));
    assert_string_equal("0:1:2:3:4:5:6:7", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(0, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferSet(buffer, "[0:1:2:3:4:5:6:7]:9090", strlen("[0:1:2:3:4:5:6:7]:9090"));
    address = IPAddressNew(buffer);
    assert_true(address != NULL);
    assert_int_equal(IP_ADDRESS_TYPE_IPV6, IPAddressType(address));
    assert_string_equal("0:1:2:3:4:5:6:7", BufferData(IPAddressGetAddress(address)));
    assert_int_equal(9090, IPAddressGetPort(address));
    assert_int_equal(0, IPAddressDestroy(&address));

    BufferDestroy(buffer);
}
Example #3
0
File: sort.c Project: cduclos/core
static bool RlistItemIPLess(void *lhs, void *rhs, ARG_UNUSED void *ctx)
{
    const char *left_item = RlistScalarValue((Rlist*)lhs);
    const char *right_item = RlistScalarValue((Rlist*)rhs);

    Buffer *left_buffer = BufferNewFrom(left_item, strlen(left_item));
    Buffer *right_buffer = BufferNewFrom(right_item, strlen(right_item));

    IPAddress *left = IPAddressNew(left_buffer);
    IPAddress *right = IPAddressNew(right_buffer);

    bool matched_left = left != NULL;
    bool matched_right = right != NULL;

    BufferDestroy(&left_buffer);
    BufferDestroy(&right_buffer);

    if (matched_left && matched_right)
    {
        int less = IPAddressCompareLess(left, right);
        IPAddressDestroy(&left);
        IPAddressDestroy(&right);
        return less;
    }

    IPAddressDestroy(&left);
    IPAddressDestroy(&right);

    if (matched_left)
    {
        return false;
    }

    if (matched_right)
    {
        return true;
    }

    // neither item matched
    return RlistItemLess(lhs, rhs, ctx);
}
Example #4
0
static void test_isipaddress(void)
{
    /*
     * This test is just a summary of the other tests.
     * We just check that this interface works accordingly, most of the
     * functionality has already been tested.
     * 1.2.3.4         -> ok
     * 1.2..3          -> not
     * 1.a.2.3         -> not
     * 256.255.255.255 -> not
     * 255.255.255.255 -> ok
     * 1:0:0:0:0:0:0:1 -> ok
     * 1:1:1:1:0:1:1:1 -> ok
     * a:b:c:d:e:f:0:1 -> ok
     * a:b:c:d:e:f:g:h -> not
     * ffff:ffff:fffff:0:0:0:0:1 -> not
     */
    IPAddress *address = NULL;
    Buffer *bufferAddress = NULL;

    bufferAddress = BufferNew();
    assert_true (bufferAddress != NULL);

    BufferSet(bufferAddress, "1.2.3.4", strlen("1.2.3.4"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1.2..3", strlen("1.2..3"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "1.a.2.3", strlen("1.a.2.3"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "256.255.255.255", strlen("256.255.255.255"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "255.255.255.255", strlen("255.255.255.255"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1:1:1:1:0:1:1:1", strlen("1:1:1:1:0:1:1:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "a:b:c:d:e:f:0:1", strlen("a:b:c:d:e:f:0:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "a:b:c:d:e:f:g:h", strlen("a:b:c:d:e:f:g:h"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "ffff:ffff:fffff:0:0:0:0:1", strlen("ffff:ffff:fffff:0:0:0:0:1"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    BufferClear(bufferAddress);
}
Example #5
0
static void test_ipv6_address_comparison(void)
{
    /*
     * We test different IPV6 combinations:
     * 1:1:1:1:1:1:1:1 vs 1:1:1:1:1:1:1:1 -> equal
     * 1:2:3:4:5:6:7:8 vs 1:1:1:1:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:1:1:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:1:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:1:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:1:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:1:1 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:1 -> not equal
     * 2:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:8 -> not equal
     * 1:2:3:4:5:6:7:8 vs 1:2:3:4:5:6:7:8 -> equal
     * Exotic variants
     * 1:0:0:0:0:0:0:1 vs 1::1            -> equal
     * 1:1:0:0:0:0:0:1 vs 1::1            -> not equal
     * 1:1:0:0:0:0:0:1 vs 1:1::1          -> equal
     * 1:0:0:0:0:0:1:1 vs 1::1:1          -> equal
     * Error conditions
     * 1::1:1 vs NULL                     -> error
     * 1::1:1 vs 1.2.3.4                  -> error
     */

    IPAddress *a = NULL;
    IPAddress *b = NULL;
    Buffer *bufferA = NULL;
    Buffer *bufferB = NULL;

    bufferA = BufferNew();
    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:1:1:1:1:1:1:1", strlen("1:1:1:1:1:1:1:1"));
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    bufferB = BufferNew();
    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:1:1:1:1:1:1:1", strlen("1:1:1:1:1:1:1:1"));
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:2:3:4:5:6:7:8", strlen("1:1:1:1:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:1:1:1:1:1:1", strlen("1:2:1:1:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:1:1:1:1:1", strlen("1:2:3:1:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:1:1:1:1", strlen("1:2:3:4:1:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:5:1:1:1", strlen("1:2:3:4:5:1:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:5:6:1:1", strlen("1:2:3:4:5:6:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:2:3:4:5:6:7:1", strlen("1:2:3:4:5:6:7:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "2:2:3:4:5:6:7:8", strlen("2:2:3:4:5:6:7:8"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1::1", strlen("1::1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1:1:0:0:0:0:0:1", strlen("1:1:0:0:0:0:0:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_false(IPAddressIsEqual(a, b));

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:1::1", strlen("1:1::1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1::1:1", strlen("1::1:1"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1:0:0:0:0:0:1:1", strlen("1:0:0:0:0:0:1:1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_int_equal(IPAddressIsEqual(a, NULL), -1);

    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_int_equal(IPAddressIsEqual(a, b), -1);
}
Example #6
0
static void test_ipv4_address_comparison(void)
{
    /*
     * We test different IPV4 combinations:
     * 1.1.1.1 vs 1.1.1.1         -> equal
     * 1.2.3.4 vs 1.1.1.1         -> not equal
     * 1.2.3.4 vs 1.2.1.1         -> not equal
     * 1.2.3.4 vs 1.2.3.1         -> not equal
     * 2.2.3.4 vs 1.2.3.4         -> not equal
     * 1.2.3.4 vs 1.2.3.4         -> equal
     * 1.2.3.4 vs NULL            -> error
     * 1.2.3.4 vs 1:2:3:4:5:6:7:8 -> error
     */
    IPAddress *a = NULL;
    IPAddress *b = NULL;
    Buffer *bufferA = NULL;
    Buffer *bufferB = NULL;
            
    bufferA = BufferNew();
    assert_true(bufferA != NULL);
    BufferSet(bufferA, "1.1.1.1", strlen("1.1.1.1"));
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    bufferB = BufferNew();
    assert_true(bufferB != NULL);
    BufferSet(bufferB, "1.1.1.1", strlen("1.1.1.1"));
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);
    
    assert_true(IPAddressIsEqual(a, b));
    
    BufferSet(bufferA, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);
    
    assert_false(IPAddressIsEqual(a, b));       

    BufferSet(bufferB, "1.2.1.1", strlen("1.2.1.1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    BufferSet(bufferB, "1.2.3.1", strlen("1.2.3.1"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    BufferSet(bufferA, "2.2.3.4", strlen("2.2.3.4"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    BufferSet(bufferB, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&b), 0);
    b = IPAddressNew(bufferB);
    assert_true(b != NULL);

    assert_false(IPAddressIsEqual(a, b));

    BufferSet(bufferA, "1.2.3.4", strlen("1.2.3.4"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_true(IPAddressIsEqual(a, b));

    assert_int_equal(IPAddressIsEqual(a, NULL), -1);
    assert_int_equal(IPAddressIsEqual(NULL, a), -1);

    BufferSet(bufferA, "1:2:3:4:5:6:7:8", strlen("1:2:3:4:5:6:7:8"));
    assert_int_equal(IPAddressDestroy(&a), 0);
    a = IPAddressNew(bufferA);
    assert_true(a != NULL);

    assert_int_equal(IPAddressIsEqual(a, b), -1);
    assert_int_equal(IPAddressIsEqual(b, a), -1);
}