END_TEST START_TEST (test_blob) { char *str = "this is a string subtly disguised as a blob"; OmlValue v, v2; OmlValueU vu; size_t len = strlen(str); void *test = (void*)str; size_t bcount; size_t alloc_diff; oml_value_init(&v); oml_value_init(&v2); omlc_zero(vu); /* Prepare the OmlValueU to be duplicated in the OmlValue */ omlc_set_blob(vu, test, len); bcount = xmembytes(); oml_value_set(&v, &vu, OML_BLOB_VALUE); alloc_diff = xmembytes() - bcount - sizeof(size_t); bcount = xmembytes(); fail_if(alloc_diff < len, "OmlValue blob allocated memory not big enough (%d instead of at least %d)", alloc_diff, len); oml_value_duplicate(&v2, &v); fail_if(omlc_get_blob_ptr(*oml_value_get_value(&v2)) == omlc_get_blob_ptr(*oml_value_get_value(&v)), "Copied OmlValue blob pointer not allocated properly"); fail_if(strncmp(omlc_get_blob_ptr(*oml_value_get_value(&v2)), omlc_get_blob_ptr(*oml_value_get_value(&v)), len), "Copied OmlValue blob mismatch"); fail_unless(omlc_get_blob_length(*oml_value_get_value(&v2)) == omlc_get_blob_length(*oml_value_get_value(&v)), "Copied OmlValue blob length not set properly (%d instead of %d)", omlc_get_blob_length(*oml_value_get_value(&v2)), omlc_get_blob_length(*oml_value_get_value(&v2))); alloc_diff = xmembytes() - bcount - sizeof(size_t); /* oml_malloc() always allocates at least sizeof(size_t) more for bookkeeping */ bcount = xmembytes(); fail_if(alloc_diff < len, "Copied OmlValue blob allocated memory not big enough (%d instead of at least %d)", alloc_diff, len); fail_unless(omlc_get_blob_size(*oml_value_get_value(&v2)) == oml_malloc_usable_size(omlc_get_blob_ptr(*oml_value_get_value(&v2))), "Copied OmlValue blob allocated size not set properly (%d instead of %d)", omlc_get_blob_size(*oml_value_get_value(&v2)), oml_malloc_usable_size(omlc_get_blob_ptr(*oml_value_get_value(&v2)))); oml_value_set_type(&v, OML_UINT64_VALUE); fail_unless(xmembytes() < bcount, "OmlValue blob was not freed after oml_value_set_type() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v2); fail_unless(xmembytes() < bcount, "OmlValue blob was not freed after oml_value_reset() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v); omlc_reset_blob(vu); }
END_TEST START_TEST (test_string) { char *test = "test"; OmlValue v, v2; OmlValueU vu; size_t bcount = xmembytes(); size_t alloc_diff; oml_value_init(&v); oml_value_init(&v2); omlc_zero(vu); /* Prepare the OmlValueU to be duplicated in the OmlValue */ omlc_set_const_string(vu, test); oml_value_set(&v, &vu, OML_STRING_VALUE); alloc_diff = xmembytes() - bcount - sizeof(size_t); bcount = xmembytes(); fail_if(alloc_diff < strlen(test) + 1, "OmlValue string allocated memory not big enough (%d instead of at least %d)", alloc_diff, strlen(test) + 1); oml_value_duplicate(&v2, &v); fail_if(omlc_get_string_ptr(*oml_value_get_value(&v2)) == omlc_get_string_ptr(*oml_value_get_value(&v)), "Copied OmlValue string pointer not allocated properly"); fail_if(strcmp(omlc_get_string_ptr(*oml_value_get_value(&v2)), omlc_get_string_ptr(*oml_value_get_value(&v))), "Copied OmlValue string mismatch ('%s' instead of '%s')", omlc_get_string_ptr(*oml_value_get_value(&v2)), omlc_get_string_ptr(*oml_value_get_value(&v))); fail_unless(omlc_get_string_length(*oml_value_get_value(&v2)) == strlen(omlc_get_string_ptr(*oml_value_get_value(&v2))), "Copied OmlValue string length not set properly (%d instead of %d)", omlc_get_string_length(*oml_value_get_value(&v2)), strlen(omlc_get_string_ptr(*oml_value_get_value(&v2))) + 1); alloc_diff = xmembytes() - bcount - sizeof(size_t); /* oml_malloc() always allocates at least sizeof(size_t) more for bookkeeping */ bcount = xmembytes(); fail_if(alloc_diff < strlen(omlc_get_string_ptr(*oml_value_get_value(&v))) + 1, "Copied OmlValue string allocated memory not big enough (%d instead of at least %d)", alloc_diff, strlen(omlc_get_string_ptr(*oml_value_get_value(&v))) + 1); fail_unless(omlc_get_string_size(*oml_value_get_value(&v2)) == oml_malloc_usable_size(omlc_get_string_ptr(*oml_value_get_value(&v2))), "Copied OmlValue string allocated size not set properly (%d instead of %d)", omlc_get_string_size(*oml_value_get_value(&v2)), oml_malloc_usable_size(omlc_get_string_ptr(*oml_value_get_value(&v2)))); fail_unless(omlc_get_string_is_const(*oml_value_get_value(&v2)) == 0, "Copied OmlValue string should not be constant"); oml_value_set_type(&v, OML_UINT64_VALUE); fail_unless(xmembytes() < bcount, "OmlValue string was not freed after oml_value_set_type() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v2); fail_unless(xmembytes() < bcount, "OmlValue string was not freed after oml_value_reset() (%d allocated, which is not less than %d)", xmembytes(), bcount); bcount = xmembytes(); oml_value_reset(&v); omlc_reset_string(vu); }
int text_read_msg_start (struct oml_message *msg, MBuffer *mbuf) { int len = mbuf_find (mbuf, '\n'); // Find length of line OmlValue value; int bytes = 0; oml_value_init(&value); if (len == -1) return 0; // Haven't got a full line msg->length = (uint32_t)len + 1; /* Read the timestamp first */ oml_value_set_type(&value, OML_DOUBLE_VALUE); bytes = text_read_value (mbuf, &value, len); if (bytes == -1) return -1; else len -= bytes; msg->timestamp = omlc_get_double(*oml_value_get_value(&value)); /* Read the stream index */ oml_value_set_type(&value, OML_UINT32_VALUE); bytes = text_read_value (mbuf, &value, len); if (bytes == -1) return -1; else len -= bytes; msg->stream = omlc_get_uint32(*oml_value_get_value(&value)); /* Read the sequence number */ oml_value_set_type(&value, OML_UINT32_VALUE); bytes = text_read_value (mbuf, &value, len); if (bytes == -1) return -1; else len -= bytes; msg->seqno = omlc_get_uint32(*oml_value_get_value(&value)); oml_value_reset(&value); return msg->length; }
/** Read the marshalling header information contained in an MBuffer. * * \param mbuf MBuffer to read from * \param header pointer to an OmlBinaryHeader into which the data from the * mbuf should be unmarshalled * \return 1 on success, the size of the missing section as a negative number * if the buffer is too short, or 0 if something failed */ int unmarshal_init(MBuffer* mbuf, OmlBinaryHeader* header) { uint8_t header_str[PACKET_HEADER_SIZE + 2]; uint8_t stream_header_str[STREAM_HEADER_SIZE]; int result, n; OmlValue seqno; OmlValue timestamp; oml_value_init(&seqno); oml_value_init(×tamp); result = mbuf_begin_read (mbuf); if (result == -1) { logerror("Couldn't start unmarshalling packet (mbuf_begin_read())\n"); return 0; } result = mbuf_read (mbuf, header_str, 3); if (result == -1) { return mbuf_rd_remaining (mbuf) - 3; } if (! (header_str[0] == SYNC_BYTE && header_str[1] == SYNC_BYTE)) { logdebug("Cannot find sync bytes in binary stream, out of sync; first 3 bytes: %#0x %#0x %#0x\n", header_str[0], header_str[1], header_str[2]); return 0; } header->type = (OmlBinMsgType)header_str[2]; if (header->type == OMB_DATA_P) { // Read 2 more bytes of the length field uint16_t nv16 = 0; result = mbuf_read (mbuf, (uint8_t*)&nv16, sizeof (uint16_t)); if (result == -1) { n = mbuf_rd_remaining (mbuf) - 2; mbuf_reset_read (mbuf); return n; } header->length = (int)ntohs (nv16); } else if (header->type == OMB_LDATA_P) { // Read 4 more bytes of the length field uint32_t nv32 = 0; result = mbuf_read (mbuf, (uint8_t*)&nv32, sizeof (uint32_t)); if (result == -1) { n = mbuf_rd_remaining (mbuf) - 4; mbuf_reset_read (mbuf); return n; } header->length = (int)ntohl (nv32); } else { logwarn ("Unknown packet type %d\n", (int)header->type); return 0; } int extra = mbuf_rd_remaining (mbuf) - header->length; if (extra < 0) { mbuf_reset_read (mbuf); return extra; } result = mbuf_read (mbuf, stream_header_str, LENGTH (stream_header_str)); if (result == -1) { n = mbuf_rd_remaining (mbuf) - LENGTH (stream_header_str); mbuf_reset_read (mbuf); return n; } header->values = (int)stream_header_str[0]; header->stream = (int)stream_header_str[1]; if (unmarshal_typed_value (mbuf, "seq-no", OML_INT32_VALUE, &seqno) == -1) return 0; if (unmarshal_typed_value (mbuf, "timestamp", OML_DOUBLE_VALUE, ×tamp) == -1) return 0; header->seqno = omlc_get_int32(*oml_value_get_value(&seqno)); header->timestamp = omlc_get_double(*oml_value_get_value(×tamp)); oml_value_reset(&seqno); oml_value_reset(×tamp); return 1; }