コード例 #1
0
ファイル: test_parc_JSON.c プロジェクト: PARC/Libparc
LONGBOW_TEST_CASE(JSON, parcJSON_BuildString)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    PARCBufferComposer *composer = parcBufferComposer_Create();
    parcJSON_BuildString(data->json, composer, false);

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    char *actual = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);
    parcBufferComposer_Release(&composer);

    assertTrue(strcmp(data->expected, actual) == 0, "Expected %s, actual %s", data->expected, actual);
    parcMemory_Deallocate((void **) &actual);

    composer = parcBufferComposer_Create();
    parcJSON_BuildString(data->json, composer, true);

    tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    actual = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);
    parcBufferComposer_Release(&composer);

    assertTrue(strcmp(data->compactExpected, actual) == 0, "Expected %s, actual %s", data->compactExpected, actual);
    parcMemory_Deallocate((void **) &actual);
}
コード例 #2
0
ファイル: test_parc_JSONArray.c プロジェクト: isolis/Libparc
LONGBOW_TEST_CASE(parc_JSONArray, parcJSONArray_BuildString)
{
    PARCJSONArray *array = parcJSONArray_Create();
    PARCJSONValue *expected = parcJSONValue_CreateFromInteger(10);
    parcJSONArray_AddValue(array, expected);

    PARCBufferComposer *composer = parcBufferComposer_Create();
    parcJSONArray_BuildString(array, composer, false);

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    parcBufferComposer_Release(&composer);
    char *result = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);

    assertTrue(strlen(result) > 0, "Expected non-empty string result");

    parcMemory_Deallocate((void **) &result);

    composer = parcBufferComposer_Create();
    parcJSONArray_BuildString(array, composer, true);
    tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    parcBufferComposer_Release(&composer);
    result = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);

    assertTrue(strlen(result) > 0, "Expected non-empty string result");

    parcMemory_Deallocate((void **) &result);

    parcJSONValue_Release(&expected);
    parcJSONArray_Release(&array);
}
コード例 #3
0
PARCBuffer *
tutorialFileIO_CreateDirectoryListing(const char *directoryName)
{
    DIR *directory = opendir(directoryName);

    assertNotNull(directory, "Couldn't open directory '%s' for reading.", directoryName);

    PARCBufferComposer *directoryListing = parcBufferComposer_Create();

    struct dirent *entry;
    while ((entry = readdir(directory)) != NULL) {
        switch (entry->d_type) {
            case DT_REG: {
                // a regular file

                // We need the full file path to check its size.
                PARCBufferComposer *fullFilePath = parcBufferComposer_Create();
                parcBufferComposer_Format(fullFilePath, "%s/%s", directoryName, entry->d_name);

                PARCBuffer *fileNameBuffer = parcBufferComposer_ProduceBuffer(fullFilePath);
                char *fullFilePathString = parcBuffer_ToString(fileNameBuffer);
                parcBuffer_Release(&fileNameBuffer);

                if (tutorialFileIO_IsFileAvailable(fullFilePathString)) {
                    parcBufferComposer_Format(directoryListing, "  %s  (%zu bytes)\n",
                                              entry->d_name, tutorialFileIO_GetFileSize(fullFilePathString));
                }

                parcBufferComposer_Release(&fullFilePath);
                parcMemory_Deallocate((void **) &fullFilePathString);

                break;
            }

            case DT_LNK:
            case DT_DIR:
            default:
                // ignore everything but regular files
                break;
        }
    }

    closedir(directory);

    PARCBuffer *result = parcBufferComposer_ProduceBuffer(directoryListing);
    parcBufferComposer_Release(&directoryListing);

    return result;
}
コード例 #4
0
PARCBuffer *
makePayload(const CCNxName *interestName, const char *commandString)
{
    char *commandToExecute;

    char *nameAsString = ccnxName_ToString(interestName);
    int failure = asprintf(&commandToExecute, commandString, nameAsString);
    assertTrue(failure > -1, "Error asprintf");
    parcMemory_Deallocate((void **) &nameAsString);

    PARCBufferComposer *accumulator = parcBufferComposer_Create();

    FILE *fp = popen(commandToExecute, "r");
    if (fp != NULL) {
        unsigned char buffer[1024];

        while (feof(fp) == 0) {
            size_t length = fread(buffer, sizeof(char), sizeof(buffer), fp);
            parcBufferComposer_PutArray(accumulator, buffer, length);
        }
        pclose(fp);
    } else {
        parcBufferComposer_PutString(accumulator, "Cannot execute: ");
        parcBufferComposer_PutString(accumulator, commandString);
    }

    PARCBuffer *payload = parcBufferComposer_ProduceBuffer(accumulator);
    parcBufferComposer_Release(&accumulator);

    return payload;
}
コード例 #5
0
LONGBOW_TEST_CASE(Local, _Inet6_BuildString)
{
    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 = htons(43215);

    char *expected = "inet6://[2001:720:1500:1::a100%0]:43215";

    CPIAddress *cpiaddr = cpiAddress_CreateFromInet6(&addr_in6);

    PARCBufferComposer *composer = parcBufferComposer_Create();
    _Inet6_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);
}
コード例 #6
0
LONGBOW_TEST_CASE(Global, parcBufferComposer_AssertValid)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();

    assertNotNull(composer, "PARCBufferComposer instance should be non-NULL.");
    parcBufferComposer_AssertValid(composer);

    parcBufferComposer_Release(&composer);
}
コード例 #7
0
static TestData*
commonSetup()
{
    uint32_t outstanding = parcSafeMemory_Outstanding();
    TestData *data = parcMemory_AllocateAndClear(sizeof(TestData));
    assertNotNull(data, "parcMemory_AllocateAndClear of %zu bytes returned NULL.", sizeof(TestData));
    data->setupAllocations = outstanding;
    data->composer = parcBufferComposer_Create();
    return data;
}
コード例 #8
0
LONGBOW_TEST_CASE(Global, cpiAddress_BuildString)
{
    CPIAddress *address = cpiAddress_CreateFromInterface(1);
    uint32_t beforeBalance = parcMemory_Outstanding();
    PARCBufferComposer *composer = cpiAddress_BuildString(address, parcBufferComposer_Create());
    parcBufferComposer_Release(&composer);
    uint32_t afterBalance = parcMemory_Outstanding();

    cpiAddress_Destroy(&address);
    assertTrue(beforeBalance == afterBalance, "Memory leak off by %d allocations", (int) (afterBalance - beforeBalance));
}
コード例 #9
0
LONGBOW_TEST_CASE(Global, parcBufferComposer_Acquire)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();

    PARCBufferComposer *handle = parcBufferComposer_Acquire(composer);

    assertNotNull(handle, "Acquired PARCBufferComposer handle should be non-NULL.");
    assertTrue(parcBufferComposer_Equals(composer, handle), "PARCBufferComposer instances should be equal");

    parcBufferComposer_Release(&handle);
    parcBufferComposer_Release(&composer);
}
コード例 #10
0
char *
ccnxPortalAnchor_ToString(const CCNxPortalAnchor *anchor)
{
    ccnxPortalAnchor_OptionalAssertValid(anchor);

    PARCBufferComposer *composer = parcBufferComposer_Create();
    ccnxPortalAnchor_BuildString(anchor, composer);
    char *result = parcBufferComposer_ToString(composer);
    parcBufferComposer_Release(&composer);

    return result;
}
コード例 #11
0
ファイル: parc_JSONValue.c プロジェクト: PARC/Libparc
static char *
_parcJSONValue_ToString(const PARCJSONValue *value, bool compact)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();
    parcJSONValue_BuildString(value, composer, compact);

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    char *result = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);

    parcBufferComposer_Release(&composer);

    return result;
}
コード例 #12
0
ファイル: parc_Lock.c プロジェクト: rayyagar/Libparc
char *
parcLock_ToString(const PARCLock *lock)
{
    char *result = NULL;

    PARCBufferComposer *composer = parcBufferComposer_Create();
    if (composer != NULL) {
        parcLock_BuildString(lock, composer);
        result = parcBufferComposer_ToString(composer);
        parcBufferComposer_Release(&composer);
    }

    return result;
}
コード例 #13
0
ファイル: parc_File.c プロジェクト: PARC/Libparc
char *
parcFile_ToString(const PARCFile *file)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();
    parcFile_BuildString(file, composer);

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    char *result = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);

    parcBufferComposer_Release(&composer);

    return result;
}
コード例 #14
0
ファイル: parc_JSONParser.c プロジェクト: isolis/Libparc
PARCBuffer *
parcJSONParser_ParseString(PARCJSONParser *parser)
{
    PARCBuffer *result = NULL;

    PARCBuffer *buffer = _getBuffer(parser);
    if (parcBuffer_GetUint8(buffer) == '"') { // skip the initial '"' character starting the string.
        PARCBufferComposer *composer = parcBufferComposer_Create();

        while (parcBuffer_Remaining(buffer)) {
            uint8_t c = parcBuffer_GetUint8(buffer);
            if (c == '"') {
                // This is the only successful way to exit this while loop.
                result = parcBufferComposer_ProduceBuffer(composer);
                break;
            } else if (c == '\\') {
                c = parcBuffer_GetUint8(buffer);
                if (c == '"') {
                    // this special character passes directly into the composed string.
                } else if (c == '\\') {
                    // this special character passes directly into the composed string.
                } else if (c == '/') {
                    // this special character passes directly into the composed string.
                } else if (c == 'b') {
                    c = '\b';
                } else if (c == 'f') {
                    c = '\f';
                } else if (c == 'n') {
                    c = '\n';
                } else if (c == 'r') {
                    c = '\r';
                } else if (c == 't') {
                    c = '\t';
                } else if (c == 'u') {
                    // Not supporting unicode at this point.
                    trapNotImplemented("Unicode is not supported.");
                }
            } else if (iscntrl(c)) {
                // !! Syntax Error.
                break;
            }
            parcBufferComposer_PutChar(composer, c);
        }

        parcBufferComposer_Release(&composer);
    }
    return result;
}
コード例 #15
0
PARCHashCode
ccnxSimpleFileTransferChunkList_HashCode(const CCNxSimpleFileTransferChunkList *chunkList)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();
    parcBufferComposer_PutUint64(composer, chunkList->chunkSize);
    parcBufferComposer_PutBuffer(composer, chunkList->fileName);

    PARCBuffer *buf = parcBufferComposer_ProduceBuffer(composer);

    PARCHashCode result = parcBuffer_HashCode(buf);

    parcBuffer_Release(&buf);
    parcBufferComposer_Release(&composer);

    return result;
}
コード例 #16
0
ファイル: parc_URISegment.c プロジェクト: isolis/Libparc
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;
}
コード例 #17
0
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;
}
コード例 #18
0
ファイル: parc_HashMap.c プロジェクト: rayyagar/Libparc
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;
}
コード例 #19
0
ファイル: parc_URIPath.c プロジェクト: isolis/Libparc
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;
}
コード例 #20
0
ファイル: test_parc_Network.c プロジェクト: PARC/Libparc
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);
}
コード例 #21
0
ファイル: parc_LogEntry.c プロジェクト: PARC/Libparc
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;
}
コード例 #22
0
ファイル: athena_LRUContentStore.c プロジェクト: PARC/Athena
static PARCObject *
_createHashableKey(const CCNxName *name, const PARCBuffer *keyId, const PARCBuffer *contentObjectHash)
{
    PARCBufferComposer *keyComposer = parcBufferComposer_Create();

    keyComposer = ccnxName_BuildString(name, keyComposer);

    if (keyId != NULL) {
        parcBufferComposer_PutBuffer(keyComposer, keyId);
    }

    if (contentObjectHash != NULL) {
        parcBufferComposer_PutBuffer(keyComposer, contentObjectHash);
    }

    PARCBuffer *result = parcBufferComposer_ProduceBuffer(keyComposer);
    parcBufferComposer_Release(&keyComposer);

    return result;
}
コード例 #23
0
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;
}
コード例 #24
0
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);
}
コード例 #25
0
/**
 * 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;
}
コード例 #26
0
ファイル: test_parc_Network.c プロジェクト: PARC/Libparc
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);
}
コード例 #27
0
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);
}