CCNxControl * cpi_CreateResponse(CCNxControl *request, PARCJSON *operation) { PARCJSON *requestJson = ccnxControl_GetJson(request); // use the same key as the request uint64_t seqnum = controlPlaneInterface_GetSequenceNumber(requestJson); PARCJSONValue *value = parcJSON_GetValueByName(requestJson, cpiRequest); assertNotNull(value, "Could not get request or response"); assertTrue(parcJSONValue_IsJSON(value), "cpiRequest should be a JSON object"); PARCJSON *operationJson = parcJSONValue_GetJSON(value); PARCJSONPair *pair = parcJSON_GetPairByIndex(operationJson, 1); const PARCBuffer *opKeyBuf = parcJSONPair_GetName(pair); const char *opKey = parcBuffer_ToString(opKeyBuf); PARCJSON *response = parcJSON_Create(); parcJSON_AddInteger(response, cpiSeqnum, (int) seqnum); parcJSON_AddObject(response, opKey, operation); parcMemory_Deallocate(&opKey); PARCJSON *responseJson = parcJSON_Create(); parcJSON_AddObject(responseJson, cpiResponse, response); parcJSON_Release(&response); CCNxControl *result = ccnxControl_CreateCPIRequest(responseJson); parcJSON_Release(&responseJson); return result; }
LONGBOW_TEST_CASE(Global, cpiAck_CreateNack) { CCNxName *name = ccnxName_CreateFromCString("lci:/foo/bar"); CPIRouteEntry *route = cpiRouteEntry_CreateRouteToSelf(name); PARCJSON *request = cpiForwarding_CreateAddRouteRequest(route); PARCJSON *actual = cpiAcks_CreateNack(request); assertFalse(cpiAcks_IsAck(actual), "Expected cpiAcks_IsAck to return false."); parcJSON_Release(&actual); parcJSON_Release(&request); cpiRouteEntry_Destroy(&route); ccnxName_Release(&name); }
PARCJSON * ccnxManifest_ToJSON(const CCNxManifest *manifest) { PARCJSON *root = parcJSON_Create(); char *nameString = ccnxName_ToString(ccnxManifest_GetName(manifest)); parcJSON_AddString(root, "locator", nameString); parcMemory_Deallocate(&nameString); PARCJSONArray *array = parcJSONArray_Create(); for (size_t i = 0; i < ccnxManifest_GetNumberOfHashGroups(manifest); i++) { CCNxManifestHashGroup *group = ccnxManifest_GetHashGroupByIndex(manifest, i); PARCJSON *groupJson = ccnxManifestHashGroup_ToJson(group); PARCJSONValue *jsonValue = parcJSONValue_CreateFromJSON(groupJson); parcJSONArray_AddValue(array, jsonValue); parcJSONValue_Release(&jsonValue); parcJSON_Release(&groupJson); ccnxManifestHashGroup_Release(&group); } parcJSON_AddArray(root, "HashGroups", array); parcJSONArray_Release(&array); return root; }
LONGBOW_TEST_CASE(JSON, parcJSON_AddValue) { PARCJSON *json = parcJSON_Create(); char *expectedName = "value"; PARCJSONValue *value = parcJSONValue_CreateFromCString("Some Pig"); parcJSON_AddValue(json, expectedName, value); const PARCJSONPair *pair = parcJSON_GetPairByName(json, expectedName); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, (char *) parcBuffer_ToString(actualName)); assertTrue(parcJSONValue_IsString(actualValue), "Expect value to be type PARCJSONArray"); assertTrue(parcBuffer_Equals(parcJSONValue_GetString(actualValue), parcJSONValue_GetString(value)), "Expected %s actual %s", parcJSONValue_ToString(value), parcJSONValue_ToString(actualValue)); parcJSONValue_Release(&value); parcJSON_Release(&json); }
static CCNxControl * metisConfiguration_ProcessConnectionList(MetisConfiguration *config, CCNxControl *request, unsigned ingressId) { CPIConnectionList *tunnelList = cpiConnectionList_Create(); MetisConnectionTable *table = metisForwarder_GetConnectionTable(config->metis); MetisConnectionList *connList = metisConnectionTable_GetEntries(table); for (size_t i = 0; i < metisConnectionList_Length(connList); i++) { // Don't release original, we're not storing it MetisConnection *original = metisConnectionList_Get(connList, i); const MetisAddressPair *addressPair = metisConnection_GetAddressPair(original); CPIAddress *localAddress = cpiAddress_Copy(metisAddressPair_GetLocal(addressPair)); CPIAddress *remoteAddress = cpiAddress_Copy(metisAddressPair_GetRemote(addressPair)); CPIConnectionType type = metisIoOperations_GetConnectionType(metisConnection_GetIoOperations(original)); CPIConnection *cpiConn = cpiConnection_Create(metisConnection_GetConnectionId(original), localAddress, remoteAddress, type); cpiConnection_SetState(cpiConn, metisConnection_IsUp(original) ? CPI_IFACE_UP : CPI_IFACE_DOWN); cpiConnectionList_Append(tunnelList, cpiConn); } PARCJSON *connectListJson = cpiConnectionList_ToJson(tunnelList); CCNxControl *response = cpi_CreateResponse(request, connectListJson); parcJSON_Release(&connectListJson); cpiConnectionList_Destroy(&tunnelList); metisConnectionList_Destroy(&connList); return response; }
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(©); cpiAddress_Destroy(&fromjson); }
LONGBOW_TEST_CASE(JSON, parcJSON_CreateRelease) { PARCJSON *json = parcJSON_Create(); parcJSON_Release(&json); assertNull(json, "Expected the NULL pointer side-effect of Release."); }
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(©); cpiAddress_Destroy(&fromjson); }
PARCJSON * parcHashMap_ToJSON(const PARCHashMap *hashMap) { parcHashMap_OptionalAssertValid(hashMap); PARCJSON *result = parcJSON_Create(); PARCIterator *iterator = parcHashMap_CreateKeyIterator((PARCHashMap *) hashMap); while (parcIterator_HasNext(iterator)) { PARCObject *keyObject = parcIterator_Next(iterator); const PARCObject *valueObject = parcHashMap_Get(hashMap, keyObject); char *key = parcObject_ToString(keyObject); PARCJSON *value = parcObject_ToJSON(valueObject); parcJSON_AddObject(result, key, value); parcMemory_Deallocate(&key); parcJSON_Release(&value); } parcIterator_Release(&iterator); return result; }
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(©); cpiAddress_Destroy(&fromjson); parcBuffer_Release(&macbuffer); }
static CCNxControl * metisConfiguration_ProcessRegistrationList(MetisConfiguration *config, CCNxControl *request, unsigned ingressId) { MetisFibEntryList *fibList = metisForwarder_GetFibEntries(config->metis); CPIRouteEntryList *routeEntryList = cpiRouteEntryList_Create(); for (size_t i = 0; i < metisFibEntryList_Length(fibList); i++) { const MetisFibEntry *fibEntry = metisFibEntryList_Get(fibList, i); MetisTlvName *prefix = metisFibEntry_GetPrefix(fibEntry); const MetisNumberSet *nexthops = metisFibEntry_GetNexthops(fibEntry); for (int j = 0; j < metisNumberSet_Length(nexthops); j++) { CPIRouteEntry *route = cpiRouteEntry_Create(metisTlvName_ToCCNxName(prefix), metisNumberSet_GetItem(nexthops, j), NULL, cpiNameRouteProtocolType_STATIC, cpiNameRouteType_LONGEST_MATCH, NULL, // lifetime 1); // cost cpiRouteEntryList_Append(routeEntryList, route); } metisTlvName_Release(&prefix); } PARCJSON *entryListJson = cpiRouteEntryList_ToJson(routeEntryList); CCNxControl *response = cpi_CreateResponse(request, entryListJson); parcJSON_Release(&entryListJson); cpiRouteEntryList_Destroy(&routeEntryList); metisFibEntryList_Destroy(&fibList); return response; }
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(©); cpiAddress_Destroy(&fromjson); }
PARCJSONValue * parcJSONValue_ObjectParser(PARCJSONParser *parser) { PARCJSONValue *result = NULL; // absorb the (required) '{' character. if (parcJSONParser_NextChar(parser) == '{') { PARCJSON *json = parcJSON_Create(); while (parcJSONParser_Remaining(parser)) { char c = parcJSONParser_PeekNextChar(parser); if (c == '}') { // Absorb the '}' and terminate. parcJSONParser_NextChar(parser); result = parcJSONValue_CreateFromJSON(json); break; } else if (c == ',') { // absorb the ',' character and continue parcJSONParser_NextChar(parser); } else if (c == '"') { PARCJSONPair *pair = parcJSONPair_Parser(parser); if (pair == NULL) { break; } parcJSON_AddPair(json, pair); parcJSONPair_Release(&pair); } else { break; } } parcJSON_Release(&json); } return result; }
PARCJSON * ccnxManifestHashGroup_ToJson(const CCNxManifestHashGroup *group) { PARCJSON *root = parcJSON_Create(); PARCJSONArray *ptrList = parcJSONArray_Create(); for (size_t i = 0; i < parcLinkedList_Size(group->pointers); i++) { CCNxManifestHashGroupPointer *ptr = (CCNxManifestHashGroupPointer *) parcLinkedList_GetAtIndex(group->pointers, i); PARCJSON *ptrJson = parcJSON_Create(); // Type. parcJSON_AddInteger(ptrJson, "type", ccnxManifestHashGroupPointer_GetType(ptr)); // Digest. char *digestString = parcBuffer_ToHexString(ptr->digest); parcJSON_AddString(ptrJson, "digest", digestString); parcMemory_Deallocate(&digestString); // Add the tuple to the list. PARCJSONValue *val = parcJSONValue_CreateFromJSON(ptrJson); parcJSONArray_AddValue(ptrList, val); // Cleanup parcJSONValue_Release(&val); parcJSON_Release(&ptrJson); } root = parcJSON_AddArray(root, "HashGroup", ptrList); parcJSONArray_Release(&ptrList); if (group->overallDataDigest != NULL) { char *digestString = parcBuffer_ToHexString(group->overallDataDigest); root = parcJSON_AddString(root, "overallDataDigest", digestString); parcMemory_Deallocate((void **) &digestString); } if (group->locator != NULL) { char *locatorString = ccnxName_ToString(group->locator); root = parcJSON_AddString(root, "locator", locatorString); parcMemory_Deallocate((void **) &locatorString); } if (group->entrySize > 0) { root = parcJSON_AddInteger(root, "entrySize", group->entrySize); } if (group->dataSize > 0) { root = parcJSON_AddInteger(root, "dataSize", group->dataSize); } if (group->blockSize > 0) { root = parcJSON_AddInteger(root, "blockSize", group->blockSize); } if (group->treeHeight > 0) { root = parcJSON_AddInteger(root, "treeHeight", group->treeHeight); } return root; }
char * ccnxManifestHashGroup_ToString(const CCNxManifestHashGroup *section) { PARCJSON *json = ccnxManifestHashGroup_ToJson(section); char *stringRep = parcJSON_ToString(json); parcJSON_Release(&json); return stringRep; }
CCNxControl * ccnxControl_CreateRemoveRouteRequest(const CPIRouteEntry *route) { PARCJSON *cpiRequest = cpiForwarding_CreateRemoveRouteRequest(route); CCNxControl *result = ccnxControl_CreateCPIRequest(cpiRequest); parcJSON_Release(&cpiRequest); return result; }
CCNxControl * ccnxControl_CreateInterfaceListRequest() { PARCJSON *cpiRequest = cpiLinks_CreateInterfaceListRequest(); CCNxControl *result = ccnxControl_CreateCPIRequest(cpiRequest); parcJSON_Release(&cpiRequest); return result; }
CCNxControl * ccnxControl_CreateCancelFlowRequest(const CCNxName *name) { PARCJSON *request = cpiCancelFlow_CreateRequest(name); CCNxControl *result = ccnxControl_CreateCPIRequest(request); parcJSON_Release(&request); return result; }
CCNxControl * ccnxControl_CreateIPTunnelRequest(const CPIInterfaceIPTunnel *tunnel) { PARCJSON *request = cpiLinks_CreateIPTunnel(tunnel); CCNxControl *result = ccnxControl_CreateCPIRequest(request); parcJSON_Release(&request); return result; }
CCNxControl * ccnxControl_CreateFlushRequest(void) { PARCJSON *cpiRequest = cpi_CreateFlushRequest(); CCNxControl *result = ccnxControl_CreateCPIRequest(cpiRequest); parcJSON_Release(&cpiRequest); return result; }
CCNxControl * ccnxControl_CreatePauseInputRequest() { PARCJSON *cpiRequest = cpi_CreatePauseInputRequest(); CCNxControl *result = ccnxControl_CreateCPIRequest(cpiRequest); parcJSON_Release(&cpiRequest); return result; }
CCNxControl * ccnxControl_CreateRouteListRequest() { PARCJSON *cpiRequest = cpiForwarding_CreateRouteListRequest(); CCNxControl *result = ccnxControl_CreateCPIRequest(cpiRequest); parcJSON_Release(&cpiRequest); return result; }
static void _parcObjectTesting_AssertToJSON(const PARCObject *instance) { PARCJSON *json = parcObject_ToJSON(instance); char *result = parcJSON_ToString(json); assertNotNull(result, "Something should be returned"); parcMemory_Deallocate(&result); parcJSON_Release(&json); }
static void _cmd_Version(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params) { PARCJSON *versionJson = metisConfiguration_GetVersion(metisForwarder_GetConfiguration(session->parentCli->metis)); char *str = parcJSON_ToString(versionJson); parcEventQueue_Printf(session->streamBuffer, "%s", str); parcMemory_Deallocate((void **) &str); parcJSON_Release(&versionJson); }
CCNxPortalAnchor * ccnxPortalAnchor_Deserialize(PARCBuffer *buffer) { PARCJSON *json = parcJSON_ParseBuffer(buffer); CCNxPortalAnchor *result = ccnxPortalAnchor_CreateFromJSON(json); parcJSON_Release(&json); return result; }
PARCJSON * cpi_CreateFlushRequest(void) { PARCJSON *operation = parcJSON_Create(); PARCJSON *result = cpi_CreateRequest(cpiFlush, operation); parcJSON_Release(&operation); return result; }
PARCJSON * cpi_CreatePauseInputRequest(void) { PARCJSON *operation = parcJSON_Create(); PARCJSON *result = cpi_CreateRequest(cpiPause, operation); parcJSON_Release(&operation); return result; }
LONGBOW_TEST_CASE(JSON, parcJSON_Equals) { PARCJSON *x = parcJSON_ParseString("{ \"string\" : \"xyzzy\" }"); PARCJSON *y = parcJSON_ParseString("{ \"string\" : \"xyzzy\" }"); PARCJSON *z = parcJSON_ParseString("{ \"string\" : \"xyzzy\" }"); PARCJSON *notEqual1 = parcJSON_ParseString("{ \"string\" : \"string\" }"); PARCJSON *notEqual2 = parcJSON_ParseString("{ \"string\" : \"xyzzy\", \"integer\" : 1 }"); parcObjectTesting_AssertEqualsFunction(parcJSON_Equals, x, y, z, notEqual1, notEqual2); parcJSON_Release(&x); parcJSON_Release(&y); parcJSON_Release(&z); parcJSON_Release(¬Equal1); parcJSON_Release(¬Equal2); }
LONGBOW_TEST_CASE(Object, parcStopwatch_ToJSON) { PARCStopwatch *instance = parcStopwatch_Create(); PARCJSON *json = parcStopwatch_ToJSON(instance); parcJSON_Release(&json); parcStopwatch_Release(&instance); }
LONGBOW_TEST_CASE(JSON, parcJSON_Copy) { TestData *data = longBowTestCase_GetClipBoardData(testCase); PARCJSON *copy = parcJSON_Copy(data->json); assertTrue(parcJSON_Equals(data->json, copy), "Expect copy to equal original"); parcJSON_Release(©); }