Esempio n. 1
0
static void cast_string(void)
{
        idmef_data_t *data;
        idmef_value_t *value;
        requiem_string_t *str;

        assert(requiem_string_new_ref(&str, "abcdefgh") == 0);
        assert(idmef_value_new_string(&value, str) == 0);
        idmef_value_dont_have_own_data(value);

        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT8, -1) < 0);
//        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT32, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_ENUM, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_LIST, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_CLASS, -1) < 0);

        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DATA, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DATA);
        assert(data = idmef_value_get_data(value));
        assert(idmef_data_get_len(data) == (requiem_string_get_len(str) + 1));
        assert(memcmp(requiem_string_get_string(str), idmef_data_get_data(data), idmef_data_get_len(data)) == 0);
        requiem_string_destroy(str);

        cast_data(value);
        idmef_value_destroy(value);

        assert(requiem_string_new_ref(&str, "2008-01-01 20:42:31") == 0);
        assert(idmef_value_new_string(&value, str) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) == 0);
        idmef_value_destroy(value);
}
/**
 * idmef_message_set_string:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to be set within @message.
 * @value: Value to associate @message[@path] with.
 *
 * This function will set the @path member within @message to the
 * provided @value, which will be converted to the corresponding
 * @path value type.
 *
 * Example:
 * idmef_message_set_string(message, "alert.classification.text", "MyText");
 * idmef_message_set_string(message, "alert.source(0).service.port", "1024");
 *
 * Returns: 0 on success, or a negative value if an error occured.
 */
int idmef_message_set_string(idmef_message_t *message, const char *path, const char *value)
{
        int ret;
        idmef_value_t *iv;
        prelude_string_t *str;

        ret = prelude_string_new_dup(&str, value);
        if ( ret < 0 )
                return ret;

        ret = idmef_value_new_string(&iv, str);
        if ( ret < 0 ) {
                prelude_string_destroy(str);
                return ret;
        }

        ret = idmef_message_set_value(message, path, iv);
        idmef_value_destroy(iv);

        return ret;
}