char * parcURISegment_ToString(const PARCURISegment *segment) { PARCBufferComposer *composer = parcBufferComposer_Create(); char *result = NULL; if (parcURISegment_BuildString(segment, composer)) { PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); result = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); } parcBufferComposer_Release(&composer); return result; }
char * ccnxNameSegment_ToString(const CCNxNameSegment *segment) { char *result = NULL; PARCBufferComposer *composer = parcBufferComposer_Create(); if (composer != NULL) { ccnxNameSegment_BuildString(segment, composer); PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); result = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); parcBufferComposer_Release(&composer); } return result; }
char * parcHashMap_ToString(const PARCHashMap *hashMap) { parcHashMap_OptionalAssertValid(hashMap); char *result = NULL; PARCBufferComposer *composer = parcBufferComposer_Create(); if (composer != NULL) { parcHashMap_BuildString(hashMap, composer); PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); result = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); parcBufferComposer_Release(&composer); } return result; }
char * parcURIPath_ToString(const PARCURIPath *path) { char *result = NULL; PARCBufferComposer *composer = parcBufferComposer_Create(); if (composer != NULL) { if (parcURIPath_BuildString(path, composer) != NULL) { PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); result = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); } parcBufferComposer_Release(&composer); } return result; }
int consumer(void) { parcSecurity_Init(); CCNxPortalFactory *factory = setupConsumerFactory(); CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message); assertNotNull(portal, "Expected a non-null CCNxPortal pointer."); CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World"); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest); if (ccnxPortal_Send(portal, message,CCNxStackTimeout_Never)) { while (ccnxPortal_IsError(portal) == false) { CCNxMetaMessage *response = ccnxPortal_Receive(portal,CCNxStackTimeout_Never); if (response != NULL) { if (ccnxMetaMessage_IsContentObject(response)) { CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response); PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject); char *string = parcBuffer_ToString(payload); printf("%s\n", string); parcMemory_Deallocate((void **)&string); break; } } ccnxMetaMessage_Release(&response); } } ccnxPortal_Release(&portal); ccnxPortalFactory_Release(&factory); parcSecurity_Fini(); return 0; }
LONGBOW_TEST_CASE(Global, parcNetwork_LinkAddress_BuildString_colons) { char *expected = "link://01-23-45-67-89-ab"; PARCBuffer *address = parcNetwork_ParseLinkAddress("link://01:23:45:67:89:ab"); PARCBufferComposer *composer = parcBufferComposer_Create(); parcNetwork_LinkAddress_BuildString((unsigned char *) parcBuffer_Overlay(address, 0), parcBuffer_Remaining(address), composer); PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); char *actual = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); assertTrue(strcmp(expected, actual) == 0, "Expected '%s', actual '%s'", expected, actual); parcMemory_Deallocate((void **) &actual); parcBufferComposer_Release(&composer); parcBuffer_Release(&address); }
static char * _toString(const PARCLogEntry *entry) { PARCBufferComposer *composer = parcBufferComposer_Create(); parcBufferComposer_Format(composer, "%ld.%06d %d ", (long) entry->timeStamp.tv_sec, (int) entry->timeStamp.tv_usec, entry->level); size_t position = parcBuffer_Position(entry->payload); parcBufferComposer_PutBuffer(composer, entry->payload); parcBuffer_SetPosition(entry->payload, position); PARCBuffer *buffer = parcBufferComposer_GetBuffer(composer); parcBuffer_Rewind(buffer); char *result = parcBuffer_ToString(buffer); parcBufferComposer_Release(&composer); return result; }
LONGBOW_TEST_CASE(Local, _LinkToString) { uint8_t addr[6] = { 0x01, 0x02, 0x03, 0xF4, 0xF5, 0xF6 }; char *expected = "link://01-02-03-f4-f5-f6"; CPIAddress *cpiaddr = cpiAddress_CreateFromLink(addr, sizeof(addr)); PARCBufferComposer *composer = parcBufferComposer_Create(); _Link_BuildString(cpiaddr, composer); PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); char *actual = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); parcBufferComposer_Release(&composer); assertTrue(strcmp(expected, actual) == 0, "Expected '%s' actual '%s'", expected, actual); parcMemory_Deallocate((void **) &actual); cpiAddress_Destroy(&cpiaddr); }
static const char * _moduleNameToLibrary(const char *moduleName) { char *result = NULL; assertNotNull(moduleName, "module name must not be null"); const char *module = _strtoupper(moduleName); PARCBufferComposer *composer = parcBufferComposer_Create(); if (composer != NULL) { parcBufferComposer_Format(composer, "%s%s%s", LIBRARY_MODULE_PREFIX, module, LIBRARY_MODULE_SUFFIX); PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); parcBufferComposer_Release(&composer); result = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); } parcMemory_Deallocate(&module); return result; }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_ToJSON) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); PARCJSON *json = parcRandomAccessFile_ToJSON(instance); PARCJSONPair *pair = parcJSON_GetPairByName(json, "fname"); PARCJSONValue *value = parcJSONPair_GetValue(pair); PARCBuffer *buffer = parcJSONValue_GetString(value); char *string = parcBuffer_ToString(buffer); assertTrue(strcmp("/tmp/tmpfile", string) == 0, "The file was stored correctly"); parcMemory_Deallocate(&string); parcJSON_Release(&json); parcRandomAccessFile_Release(&instance); }
LONGBOW_TEST_CASE(JSON, parcJSON_GetPairByName) { TestData *data = longBowTestCase_GetClipBoardData(testCase); int expected = 31415; const PARCJSONPair *pair = parcJSON_GetPairByName(data->json, "integer"); PARCBuffer *name = parcJSONPair_GetName(pair); PARCJSONValue *value = parcJSONPair_GetValue(pair); int64_t actual = parcJSONValue_GetInteger(value); PARCBuffer *expectedName = parcBuffer_WrapCString("integer"); assertTrue(parcBuffer_Equals(expectedName, name), "Expected 'integer', actual '%s'", (char *) parcBuffer_ToString(name)); assertTrue(expected == actual, "Expected %d, actual %" PRIi64 "", expected, actual); parcBuffer_Release(&expectedName); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddBoolean) { PARCJSON *json = parcJSON_Create(); char *expectedName = "boolean"; bool expectedValue = true; parcJSON_AddBoolean(json, expectedName, expectedValue); 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(expectedValue == parcJSONValue_GetBoolean(actualValue), "Expected %d actual %d", expectedValue, parcJSONValue_GetBoolean(actualValue)); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddInteger) { PARCJSON *json = parcJSON_Create(); char *expectedName = "integer"; uint64_t expectedValue = 12345; parcJSON_AddInteger(json, expectedName, expectedValue); 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(expectedValue == parcJSONValue_GetInteger(actualValue), "Expected %" PRIi64 "d actual %" PRIi64 "d", expectedValue, parcJSONValue_GetInteger(actualValue)); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(Global, Hello) { TestData *data = longBowTestCase_GetClipBoardData(testCase); CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK); CCNxPortal *portalIn = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK); assertNotNull(portal, "Expected a non-null CCNxPortal pointer."); CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World"); CCNxInterest *interest = ccnxInterest_CreateSimple(name); CCNxMetaMessage *interestMessage = ccnxMetaMessage_CreateFromInterest(interest); if (ccnxPortal_Send(portal, interestMessage, CCNxStackTimeout_Never)) { for (int responses = 0; responses == 0; ) { CCNxMetaMessage *message = ccnxPortal_Receive(portalIn, CCNxStackTimeout_Never); if (message != NULL) { if (ccnxMetaMessage_IsContentObject(message)) { CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(message); PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject); if (parcBuffer_HasRemaining(payload) == false) { fprintf(stderr, "**************** Content object has arrived WITH EMPTY CONTENT\n"); } else { char *string = parcBuffer_ToString(payload); fprintf(stderr, "**************** Content object has arrived: %s\n", string); parcMemory_Deallocate((void **)&string); } responses++; } ccnxMetaMessage_Release(&message); } } } ccnxMetaMessage_Release(&interestMessage); ccnxPortal_Release(&portal); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddObject) { PARCJSON *json = parcJSON_Create(); PARCJSON *expectedValue = parcJSON_ParseString("{ \"string\" : \"xyzzy\" }"); parcJSON_AddObject(json, "object", expectedValue); char *expectedName = "object"; 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(parcJSON_Equals(expectedValue, parcJSONValue_GetJSON(actualValue)), "Expected value did not match the actual value."); parcJSON_Release(&expectedValue); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(Global, parcSigner_SignDigest) { _MockSigner *mock = _createSigner(); PARCSigner *signer = parcSigner_Create(mock, _MockSignerInterface); _mockSigner_Release(&mock); PARCBuffer *buffer = parcBuffer_Allocate(10); PARCCryptoHash *hash = parcCryptoHash_Create(PARCCryptoHashType_SHA256, buffer); PARCSignature *signature = parcSigner_SignDigest(signer, hash); assertNotNull(signature, "Expected non-NULL PARCSignature"); PARCBuffer *bits = parcSignature_GetSignature(signature); char *bitstring = parcBuffer_ToString(bits); char *expectedString = FAKE_SIGNATURE; assertTrue(strcmp(bitstring, expectedString) == 0, "Expected the forced signature as output %s, got %s", expectedString, bitstring); parcMemory_Deallocate(&bitstring); parcCryptoHash_Release(&hash); parcBuffer_Release(&buffer); parcSignature_Release(&signature); parcSigner_Release(&signer); }
LONGBOW_TEST_CASE(Global, parcNetwork_SockInet6Address_BuildString) { struct sockaddr_in6 *address = parcNetwork_SockInet6Address("2001:720:1500:1::a100", 1234, 0, 1); #if defined(SIN6_LEN) assertTrue(address->sin6_len == sizeof(struct sockaddr_in6), "Expecting sockaddr.sin6_len to be %zu not %hhu\n", sizeof(struct sockaddr_in6), address->sin6_len); #endif PARCBufferComposer *composer = parcBufferComposer_Create(); parcNetwork_SockInet6Address_BuildString(address, composer); char *expected = "inet6://[2001:720:1500:1::a100%1]:1234"; PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); char *actual = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); assertTrue(strcmp(expected, actual) == 0, "Expected '%s', actual '%s'", expected, actual); parcMemory_Deallocate((void **) &actual); parcMemory_Deallocate((void **) &address); parcBufferComposer_Release(&composer); }
/** * Given a sequential chunk of a 'list' response, append it to the in-memory buffer * that holds the listing. When the directory listing is complete, return it as a * string. The string must be freed by the caller. * * @param [in] payload A PARCBuffer containing the chunk of the directory listing to be appended. * @param [in] chunkNumber The number of the chunk that this payload belongs to. * @param [in] finalChunkNumber The number of the final chunk in this list response. * * @return A string containing the complete directory listing, or NULL if the complete directory * listing hasn't yet been received. */ static char * _assembleDirectoryListing(PARCBuffer *payload, uint64_t chunkNumber, uint64_t finalChunkNumber) { char *result = NULL; static PARCBufferComposer *directoryList = NULL; if (directoryList == NULL) { directoryList = parcBufferComposer_Create(); } parcBufferComposer_PutBuffer(directoryList, payload); if (chunkNumber == finalChunkNumber) { PARCBuffer *buffer = parcBufferComposer_ProduceBuffer(directoryList); // Since this was the last chunk, return the completed directory listing. result = parcBuffer_ToString(buffer); parcBuffer_Release(&buffer); parcBufferComposer_Release(&directoryList); } return result; }
LONGBOW_TEST_CASE(Local, _Inet_BuildString) { struct sockaddr_in addr_in; addr_in.sin_addr.s_addr = 0x04030201; addr_in.sin_port = htons(12345); char *expected = "inet4://1.2.3.4:12345"; CPIAddress *cpiaddr = cpiAddress_CreateFromInet(&addr_in); PARCBufferComposer *composer = parcBufferComposer_Create(); _Inet_BuildString(cpiaddr, composer); PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); char *actual = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); parcBufferComposer_Release(&composer); assertTrue(strcmp(expected, actual) == 0, "Expected '%s' actual '%s'", expected, actual); parcMemory_Deallocate((void **) &actual); cpiAddress_Destroy(&cpiaddr); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddString) { PARCJSON *json = parcJSON_Create(); char *expectedName = "string"; char *expectedValue = "value"; parcJSON_AddString(json, expectedName, expectedValue); const PARCJSONPair *pair = parcJSON_GetPairByName(json, "string"); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, parcBuffer_ToString(actualName)); assertTrue(strcmp(expectedValue, parcBuffer_Overlay(parcJSONValue_GetString(actualValue), 0)) == 0, "Expected value %s, actual %s", expectedValue, parcJSONValue_ToString(actualValue)); parcJSON_Release(&json); }
static const char * _athenactl_SendInterestControl(PARCIdentity *identity, CCNxMetaMessage *message) { const char *result = NULL; CCNxPortalFactory *factory = ccnxPortalFactory_Create(identity); CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message); assertNotNull(portal, "Expected a non-null CCNxPortal pointer."); athenactl_EncodeMessage(message); if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never)) { while (ccnxPortal_IsError(portal) == false) { CCNxMetaMessage *response = ccnxPortal_Receive(portal, CCNxStackTimeout_Never); if (response != NULL) { if (ccnxMetaMessage_IsContentObject(response)) { CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response); PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject); if (payload) { result = parcBuffer_ToString(payload); } } ccnxMetaMessage_Release(&response); break; } } } ccnxPortal_Release(&portal); ccnxPortalFactory_Release(&factory); return result; }
static int _athenactl_ListLinks(PARCIdentity *identity, int argc, char **argv) { CCNxName *name = ccnxName_CreateFromURI(CCNxNameAthenaCommand_LinkList); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); const char *result = _athenactl_SendInterestControl(identity, interest); printf("Link: Interface list"); if (result) { PARCBuffer *buffer = parcBuffer_WrapCString((char *) result); PARCJSONParser *parser = parcJSONParser_Create(buffer); PARCJSONValue *value = parcJSONValue_Parser(parser); if (value == NULL) { printf("\n\tCould not parse forwarder list response"); } else { PARCJSONArray *array = parcJSONValue_GetArray(value); for (int i = 0; i < parcJSONArray_GetLength(array); i++) { PARCJSONValue *getvalue = parcJSONArray_GetValue(array, i); PARCJSON *json = parcJSONValue_GetJSON(getvalue); PARCJSONValue *pairValue = parcJSON_GetValueByName(json, "linkName"); PARCBuffer *bufferString = parcJSONValue_GetString(pairValue); char *linkName = parcBuffer_ToString(bufferString); pairValue = parcJSON_GetValueByName(json, "index"); int64_t index = parcJSONValue_GetInteger(pairValue); pairValue = parcJSON_GetValueByName(json, "notLocal"); bool notLocal = parcJSONValue_GetBoolean(pairValue); pairValue = parcJSON_GetValueByName(json, "localForced"); bool localForced = parcJSONValue_GetBoolean(pairValue); if (index < 0) { if (notLocal) { printf("\n Link listener%s: %s", localForced ? " (forced remote)" : "", linkName); } else { printf("\n Link listener%s: %s", localForced ? " (forced local)" : "", linkName); } } else { if (notLocal) { printf("\n Link instance [%" PRId64 "] %s: %s", index, localForced ? "(forced remote)" : "(remote)", linkName); } else { printf("\n Link instance [%" PRId64 "] %s: %s", index, localForced ? "(forced local)" : "(local)", linkName); } } parcMemory_Deallocate(&linkName); } parcJSONValue_Release(&value); } parcJSONParser_Release(&parser); parcBuffer_Release(&buffer); parcMemory_Deallocate(&result); } printf("\nDone.\n"); ccnxMetaMessage_Release(&interest); return 0; }
CCNxManifestHashGroup * ccnxManifestHashGroup_CreateFromJson(const PARCJSON *json) { CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create(); PARCJSONValue *ptrListValue = parcJSON_GetValueByName(json, "HashGroup"); PARCJSONArray *ptrList = parcJSONValue_GetArray(ptrListValue); size_t numberOfPointers = parcJSONArray_GetLength(ptrList); for (size_t i = 0; i < numberOfPointers; i++) { PARCJSONValue *pointerValue = parcJSONArray_GetValue(ptrList, i); PARCJSON *typeJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *typeValue = parcJSON_GetValueByName(typeJson, "type"); CCNxManifestHashGroupPointerType type; if (parcJSONValue_GetInteger(typeValue) == 0) { type = CCNxManifestHashGroupPointerType_Data; } else { type = CCNxManifestHashGroupPointerType_Manifest; } PARCJSON *digestJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *digestValue = parcJSON_GetValueByName(digestJson, "digest"); PARCBuffer *digestHex = parcJSONValue_GetString(digestValue); char *hexString = parcBuffer_ToString(digestHex); PARCBuffer *digest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); ccnxManifestHashGroup_AppendPointer(group, type, digest); parcBuffer_Release(&digest); } if (parcJSON_GetPairByName(json, "overallDataDigest") != NULL) { PARCJSONValue *overallDataDigestValue = parcJSON_GetValueByName(json, "overallDataDigest"); PARCBuffer *digestHex = parcJSONValue_GetString(overallDataDigestValue); char *hexString = parcBuffer_ToString(digestHex); group->overallDataDigest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); } if (parcJSON_GetPairByName(json, "locator") != NULL) { PARCJSONValue *locatorValue = parcJSON_GetValueByName(json, "locator"); PARCBuffer *buffer = parcJSONValue_GetString(locatorValue); char *locator = parcBuffer_ToString(buffer); group->locator = ccnxName_CreateFromCString(locator); parcMemory_Deallocate(&locator); } if (parcJSON_GetPairByName(json, "entrySize") != NULL) { PARCJSONValue *childBlockNodeSizeValue = parcJSON_GetValueByName(json, "entrySize"); group->entrySize = parcJSONValue_GetInteger(childBlockNodeSizeValue); } if (parcJSON_GetPairByName(json, "dataSize") != NULL) { PARCJSONValue *totalSizeValue = parcJSON_GetValueByName(json, "dataSize"); group->dataSize = parcJSONValue_GetInteger(totalSizeValue); } if (parcJSON_GetPairByName(json, "blockSize") != NULL) { PARCJSONValue *blockSizeValue = parcJSON_GetValueByName(json, "blockSize"); group->blockSize = parcJSONValue_GetInteger(blockSizeValue); } if (parcJSON_GetPairByName(json, "treeHeight") != NULL) { PARCJSONValue *treeHeightValue = parcJSON_GetValueByName(json, "treeHeight"); group->treeHeight = parcJSONValue_GetInteger(treeHeightValue); } return group; }