LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromInterface)
{
    uint32_t ifidx = 0x01020304;
    uint32_t test;

    CPIAddress *address = cpiAddress_CreateFromInterface(ifidx);

    bool success = cpiAddress_GetInterfaceIndex(address, &test);
    assertTrue(success, "Got false converting back address");

    assertTrue(ifidx == test, "Got mismatch addressed");

    assertTrue(cpiAddress_GetType(address) == cpiAddressType_IFACE,
               "Got wrong address type, expected %d, got %d", cpiAddressType_IFACE, cpiAddress_GetType(address));

    PARCJSON *json = cpiAddress_ToJson(address);
    CPIAddress *fromjson = cpiAddress_CreateFromJson(json);

    assertTrue(parcBuffer_Equals(address->blob, fromjson->blob), "fromjson blob does not equal known address");
    assertTrue(cpiAddress_Equals(address, fromjson), "cpiAddress_Equals broken for IFACE type");

    CPIAddress *copy = cpiAddress_Copy(address);
    assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for IFACE");

    parcJSON_Release(&json);
    cpiAddress_Destroy(&address);
    cpiAddress_Destroy(&copy);
    cpiAddress_Destroy(&fromjson);
}
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromLink)
{
    uint8_t mac[] = { 0x01, 0x02, 0x03, 0x04, 0xFF, 0x8F };
    PARCBuffer *macbuffer = parcBuffer_Flip(parcBuffer_CreateFromArray(mac, sizeof(mac)));

    CPIAddress *address = cpiAddress_CreateFromLink(mac, sizeof(mac));

    // Do not release test, it is the same reference as address->blob
    PARCBuffer *test = cpiAddress_GetLinkAddress(address);
    assertNotNull(test, "Got null link address buffer");
    assertTrue(parcBuffer_Equals(test, address->blob), "Returned buffer from cpiAddress_GetLinkAddress not equal to address");

    assertTrue(cpiAddress_GetType(address) == cpiAddressType_LINK,
               "Got wrong address type, expected %d, got %d", cpiAddressType_LINK, cpiAddress_GetType(address));

    PARCJSON *json = cpiAddress_ToJson(address);
    CPIAddress *fromjson = cpiAddress_CreateFromJson(json);

    assertTrue(cpiAddress_GetType(address) == cpiAddress_GetType(fromjson),
               "fromjson type does not equal known");
    assertTrue(parcBuffer_Equals(address->blob, fromjson->blob),
               "fromjson blob does not equal known address");
    assertTrue(cpiAddress_Equals(address, fromjson),
               "cpiAddress_Equals broken for LINK type");

    CPIAddress *copy = cpiAddress_Copy(address);
    assertTrue(cpiAddress_Equals(copy, address),
               "Copy and address not equal for LINK");

    parcJSON_Release(&json);
    cpiAddress_Destroy(&address);
    cpiAddress_Destroy(&copy);
    cpiAddress_Destroy(&fromjson);
    parcBuffer_Release(&macbuffer);
}
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromInet6)
{
    struct sockaddr_in6 addr_in6;
    memset(&addr_in6, 0, sizeof(struct sockaddr_in6));

    inet_pton(AF_INET6, "2001:720:1500:1::a100", &(addr_in6.sin6_addr));
    addr_in6.sin6_family = AF_INET6;
    addr_in6.sin6_port = 0x0A0B;
    addr_in6.sin6_flowinfo = 0x01020304;

    CPIAddress *address = cpiAddress_CreateFromInet6(&addr_in6);

    struct sockaddr_in6 addr_test;
    bool success = cpiAddress_GetInet6(address, &addr_test);
    assertTrue(success, "Got false converting back address");

    assertTrue(memcmp(&addr_in6, &addr_test, sizeof(struct sockaddr_in6)) == 0, "Got mismatch addressed");

    assertTrue(cpiAddress_GetType(address) == cpiAddressType_INET6,
               "Got wrong address type, expected %d, got %d", cpiAddressType_INET6, cpiAddress_GetType(address));

    PARCJSON *json = cpiAddress_ToJson(address);
    CPIAddress *fromjson = cpiAddress_CreateFromJson(json);

    assertTrue(parcBuffer_Equals(address->blob, fromjson->blob), "fromjson blob does not equal known address");
    assertTrue(cpiAddress_Equals(address, fromjson), "cpiAddress_Equals broken for INET6 type");

    CPIAddress *copy = cpiAddress_Copy(address);
    assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for INET6");

    parcJSON_Release(&json);
    cpiAddress_Destroy(&address);
    cpiAddress_Destroy(&copy);
    cpiAddress_Destroy(&fromjson);
}
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromUnix)
{
    struct sockaddr_un addr_un;
    struct sockaddr_un addr_test;
    memset(&addr_un, 0, sizeof(struct sockaddr_un));
    char path[] = "/Hello/Cruel/World";
    strcpy(addr_un.sun_path, path);
    addr_un.sun_family = AF_UNIX;

    CPIAddress *address = cpiAddress_CreateFromUnix(&addr_un);

    bool success = cpiAddress_GetUnix(address, &addr_test);
    assertTrue(success, "Got false converting back address");

    assertTrue(memcmp(&addr_un, &addr_test, sizeof(struct sockaddr_un)) == 0, "Got mismatch addressed");

    assertTrue(cpiAddress_GetType(address) == cpiAddressType_UNIX,
               "Got wrong address type, expected %d, got %d", cpiAddressType_UNIX, cpiAddress_GetType(address));

    PARCJSON *json = cpiAddress_ToJson(address);
    CPIAddress *fromjson = cpiAddress_CreateFromJson(json);

    assertTrue(parcBuffer_Equals(address->blob, fromjson->blob), "fromjson blob does not equal known address");
    assertTrue(cpiAddress_Equals(address, fromjson), "cpiAddress_Equals broken for UNIX type");

    CPIAddress *copy = cpiAddress_Copy(address);
    assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for UNIX");

    parcJSON_Release(&json);
    cpiAddress_Destroy(&address);
    cpiAddress_Destroy(&copy);
    cpiAddress_Destroy(&fromjson);
}
Example #5
0
/**
 * @function metisListenerSet_Add
 * @abstract Adds the listener to the set
 * @discussion
 *     Unique set based on pair (MetisEncapType, localAddress)
 *
 * @param <#param1#>
 * @return <#return#>
 */
bool
metisListenerSet_Add(MetisListenerSet *set, MetisListenerOps *ops)
{
    assertNotNull(set, "Parameter set must be non-null");
    assertNotNull(ops, "Parameter ops must be non-null");

    int opsEncap = ops->getEncapType(ops);
    const CPIAddress *opsAddress = ops->getListenAddress(ops);

    // make sure its not in the set
    size_t length = parcArrayList_Size(set->listOfListeners);
    for (size_t i = 0; i < length; i++) {
        MetisListenerOps *entry = parcArrayList_Get(set->listOfListeners, i);

        int entryEncap = entry->getEncapType(entry);
        const CPIAddress *entryAddress = entry->getListenAddress(entry);

        if (opsEncap == entryEncap && cpiAddress_Equals(opsAddress, entryAddress)) {
            // duplicate
            return false;
        }
    }

    parcArrayList_Add(set->listOfListeners, ops);
    return true;
}
LONGBOW_TEST_CASE(Global, cpiAddress_Copy)
{
    CPIAddress *a = cpiAddress_CreateFromInterface(1);
    CPIAddress *b = cpiAddress_Copy(a);

    assertTrue(cpiAddress_Equals(a, b), "Copy did not compare as equal: %s and %s", cpiAddress_ToString(a), cpiAddress_ToString(b));

    cpiAddress_Destroy(&a);
    cpiAddress_Destroy(&b);
}
LONGBOW_TEST_CASE(Global, cpiAddress_CreateFromInet)
{
    struct sockaddr_in addr_in;
    struct sockaddr_in addr_test;
    memset(&addr_in, 0, sizeof(struct sockaddr_in));

    addr_in.sin_addr.s_addr = 0x01020304;
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = 0x0A0B;

    CPIAddress *address = cpiAddress_CreateFromInet(&addr_in);

    bool success = cpiAddress_GetInet(address, &addr_test);
    assertTrue(success, "Got false converting back address");

    assertTrue(memcmp(&addr_in, &addr_test, sizeof(struct sockaddr_in)) == 0, "Got mismatch addressed");

    assertTrue(cpiAddress_GetType(address) == cpiAddressType_INET,
               "Got wrong address type, expected %d, got %d", cpiAddressType_INET, cpiAddress_GetType(address));

    PARCJSON *json = cpiAddress_ToJson(address);
    CPIAddress *fromjson = cpiAddress_CreateFromJson(json);

    assertTrue(cpiAddress_GetType(address) == cpiAddress_GetType(fromjson), "fromjson type does not equal known");
    assertTrue(parcBuffer_Equals(address->blob, fromjson->blob), "fromjson blob does not equal known address");
    assertTrue(cpiAddress_Equals(address, fromjson), "cpiAddress_Equals broken for INET type");

    // This test does too much.  Case 1032
    CPIAddress *copy = cpiAddress_Copy(address);
    assertTrue(cpiAddress_Equals(copy, address), "Copy and address not equal for INET");

    cpiAddress_Destroy(&copy);
    cpiAddress_Destroy(&fromjson);

    parcJSON_Release(&json);

    cpiAddress_Destroy(&address);
    return;
}
LONGBOW_TEST_CASE(Global, cpiAddress_Equals_SamePointer)
{
    struct sockaddr_in addr_in;
    memset(&addr_in, 0, sizeof(struct sockaddr_in));

    addr_in.sin_addr.s_addr = 0x01020304;
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = 0x0A0B;

    CPIAddress *a = cpiAddress_CreateFromInet(&addr_in);

    assertTrue(cpiAddress_Equals(a, a), "Equals did not compare two equal addresses: %s and %s", cpiAddress_ToString(a), cpiAddress_ToString(a));

    cpiAddress_Destroy(&a);
}
LONGBOW_TEST_CASE(Global, cpiAddress_Equals_NotEqual)
{
    struct sockaddr_in addr_in;
    memset(&addr_in, 0, sizeof(struct sockaddr_in));

    addr_in.sin_addr.s_addr = 0x01020304;
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = 0x0A0B;

    CPIAddress *a = cpiAddress_CreateFromInet(&addr_in);
    CPIAddress *b = cpiAddress_CreateFromInterface(1);

    assertFalse(cpiAddress_Equals(a, b), "Equals failed on different addresses: %s and %s", cpiAddress_ToString(a), cpiAddress_ToString(b));

    cpiAddress_Destroy(&a);
    cpiAddress_Destroy(&b);
}
Example #10
0
MetisListenerOps *
metisListenerSet_Find(const MetisListenerSet *set, MetisEncapType encapType, const CPIAddress *localAddress)
{
    assertNotNull(set, "Parameter set must be non-null");
    assertNotNull(localAddress, "Parameter localAddress must be non-null");

    MetisListenerOps *match = NULL;

    for (size_t i = 0; i < parcArrayList_Size(set->listOfListeners) && !match; i++) {
        MetisListenerOps *ops = parcArrayList_Get(set->listOfListeners, i);
        assertNotNull(ops, "Got null listener ops at index %zu", i);

        if (ops->getEncapType(ops) == encapType) {
            if (cpiAddress_Equals(localAddress, ops->getListenAddress(ops))) {
                match = ops;
            }
        }
    }

    return match;
}
LONGBOW_TEST_CASE(Global, cpiAddressList_GetItem)
{
    CPIAddressList *list = cpiAddressList_Create();
    unsigned loops = 10;

    for (unsigned i = 0; i < loops; i++) {
        cpiAddressList_Append(list, cpiAddress_CreateFromInterface(i));
    }

    assertTrue(cpiAddressList_Length(list) == loops,
               "Got wrong length, expected %u got %zu",
               loops,
               cpiAddressList_Length(list));

    CPIAddress *truth = cpiAddress_CreateFromInterface(5);
    const CPIAddress *test = cpiAddressList_GetItem(list, 5);
    assertTrue(cpiAddress_Equals(truth, test), "Item 5 did not match!");

    cpiAddressList_Destroy(&list);
    cpiAddress_Destroy(&truth);
}
LONGBOW_TEST_CASE(Global, cpiAddressList_Copy)
{
    CPIAddressList *list = cpiAddressList_Create();
    unsigned loops = 10;

    for (unsigned i = 0; i < loops; i++) {
        cpiAddressList_Append(list, cpiAddress_CreateFromInterface(i));
    }

    CPIAddressList *copy = cpiAddressList_Copy(list);
    assertTrue(cpiAddressList_Length(copy) == cpiAddressList_Length(list),
               "Copy wrong size, got %zu expected %zu",
               cpiAddressList_Length(copy),
               cpiAddressList_Length(list));

    for (unsigned i = 0; i < cpiAddressList_Length(copy); i++) {
        const CPIAddress *truth = cpiAddressList_GetItem(list, i);
        const CPIAddress *test = cpiAddressList_GetItem(copy, i);
        assertTrue(cpiAddress_Equals(truth, test), "Lists do not match at element %u", i);
    }

    cpiAddressList_Destroy(&list);
    cpiAddressList_Destroy(&copy);
}
    CPIAddress *dst = cpiAddress_CreateFromInet(&((struct sockaddr_in) { .sin_addr.s_addr = 0x05060708 }));
    CPIConnection *iptun = cpiConnection_Create(1, src, dst, cpiConnection_TCP);

    CPIConnection *copy = cpiConnection_Copy(iptun);

    assertTrue(cpiConnection_GetIndex(copy) == cpiConnection_GetIndex(iptun),
               "ifidx did not match, expected %u got %u",
               cpiConnection_GetIndex(iptun),
               cpiConnection_GetIndex(copy));

    assertTrue(cpiConnection_GetState(copy) == cpiConnection_GetState(iptun),
               "states did not match, expected %d got %d",
               cpiConnection_GetState(iptun),
               cpiConnection_GetState(copy));

    assertTrue(cpiAddress_Equals(cpiConnection_GetSourceAddress(copy), cpiConnection_GetSourceAddress(iptun)),
               "did not get same source address");
    assertTrue(cpiAddress_Equals(cpiConnection_GetDestinationAddress(copy), cpiConnection_GetDestinationAddress(iptun)),
               "did not get same destination address");

    assertTrue(cpiConnection_GetConnectionType(copy) == cpiConnection_GetConnectionType(iptun),
               "did not get same connection types!");

    cpiConnection_Release(&copy);
    cpiConnection_Release(&iptun);
}

LONGBOW_TEST_CASE(Global, cpiConnection_Create_Destroy)
{
    CPIAddress *src = cpiAddress_CreateFromInet(&((struct sockaddr_in) { .sin_addr.s_addr = 0x01020304 }));
    CPIAddress *dst = cpiAddress_CreateFromInet(&((struct sockaddr_in) { .sin_addr.s_addr = 0x05060708 }));