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 int _athenactl_ListFIB(PARCIdentity *identity, int argc, char **argv) { CCNxName *name = ccnxName_CreateFromURI(CCNxNameAthenaCommand_FIBList); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); const char *result = _athenactl_SendInterestControl(identity, interest); if (result) { PARCJSON *jsonContent = parcJSON_ParseString(result); if (jsonContent != NULL) { PARCJSONValue *resultValue = parcJSON_GetValueByName(jsonContent, JSON_KEY_RESULT); PARCJSONArray *fibEntryList = parcJSONValue_GetArray(resultValue); size_t fibEntryListLength = parcJSONArray_GetLength(fibEntryList); printf("Routes (%d):\n", (int) fibEntryListLength); if (fibEntryListLength == 0) { printf(" No Entries\n"); } for (size_t i = 0; i < fibEntryListLength; ++i) { PARCJSONValue *elementValue = parcJSONArray_GetValue(fibEntryList, i); PARCJSON *valueObj = parcJSONValue_GetJSON(elementValue); PARCJSONValue *value = parcJSON_GetValueByName(valueObj, JSON_KEY_NAME); char *prefixString = parcBuffer_ToString(parcJSONValue_GetString(value)); value = parcJSON_GetValueByName(valueObj, JSON_KEY_LINK); char *linkString = parcBuffer_ToString(parcJSONValue_GetString(value)); printf(" %s -> %s\n", prefixString, linkString); parcMemory_Deallocate(&prefixString); parcMemory_Deallocate(&linkString); } parcJSON_Release(&jsonContent); } else { printf("Returned value is not JSON: %s\n", result); } parcMemory_Deallocate(&result); } else { printf("NULL result recieved from route List request\n"); } ccnxMetaMessage_Release(&interest); return 0; }
CCNxPortalAnchor * ccnxPortalAnchor_CreateFromJSON(const PARCJSON *json) { CCNxPortalAnchor *result = parcObject_CreateInstance(CCNxPortalAnchor); if (result != NULL) { result->prefix = ccnxName_CreateFromURI(parcBuffer_Overlay(parcJSONValue_GetString(parcJSON_GetByPath(json, "/namePrefix")), 0)); result->expireTime = parcJSONValue_GetInteger(parcJSON_GetByPath(json, "/expireTime")); ccnxPortalAnchor_OptionalAssertValid(result); } return result; }
/** * If successful, return true and fill in the parameters in the output argument */ bool publicKeySignerPkcs12Store_GetConnectionParams(PARCJSON *connectionJson, struct publickeysigner_params *output) { assertNotNull(connectionJson, "Parameter connectionJson must be non-null"); assertNotNull(output, "Parameter output must be non-null"); PARCJSONValue *value = parcJSON_GetValueByName(connectionJson, publicKeySignerPkcs12Store_GetName()); assertNotNull(value, "JSON key %s missing", publicKeySignerPkcs12Store_GetName()); PARCJSON *keystoreJson = parcJSONValue_GetJSON(value); value = parcJSON_GetValueByName(keystoreJson, param_KEYSTORE_NAME); assertNotNull(value, "JSON key %s missing inside %s", param_KEYSTORE_NAME, publicKeySignerPkcs12Store_GetName()); PARCBuffer *sBuf = parcJSONValue_GetString(value); output->filename = parcBuffer_Overlay(sBuf, 0); value = parcJSON_GetValueByName(keystoreJson, param_KEYSTORE_PASSWD); assertNotNull(value, "JSON key %s missing inside %s", param_KEYSTORE_PASSWD, publicKeySignerPkcs12Store_GetName()); sBuf = parcJSONValue_GetString(value); output->password = parcBuffer_Overlay(sBuf, 0); return true; }
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_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); }
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; }
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; }