Esempio n. 1
0
TEST_F(TestIBUtilField, AliasBytestr)
{
    const char s1[] = "hello";
    const char s2[] = "bye";
    ib_field_t *f;
    const ib_bytestr_t *obs;
    ib_status_t rc;
    ib_bytestr_t *bs;
    uint8_t *copy;

    copy = (uint8_t *)ib_mm_strdup(MM(), "x");
    rc = ib_field_create_bytestr_alias(&f, MM(),
                                       IB_S2SL("foo"), copy, 0);
    ASSERT_EQ(IB_OK, rc);

    rc = ib_bytestr_dup_nulstr(&bs, MM(), s1);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_setv(f, bs);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_value(f, ib_ftype_bytestr_out(&obs));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(strlen(s1), ib_bytestr_length(obs));
    ASSERT_EQ(0, memcmp(s1,
                        ib_bytestr_const_ptr(obs), ib_bytestr_length(obs)) );

    rc = ib_bytestr_dup_nulstr(&bs, MM(), s2);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_setv(f, bs);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_value(f, ib_ftype_bytestr_out(&obs));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(strlen(s2), ib_bytestr_length(obs));
    ASSERT_EQ(0, memcmp(s2,
                        ib_bytestr_const_ptr(obs), ib_bytestr_length(obs)) );
}
Esempio n. 2
0
ib_status_t ib_field_create(
    ib_field_t **pf,
    ib_mpool_t  *mp,
    const char  *name,
    size_t       nlen,
    ib_ftype_t   type,
    void        *in_pval
)
{
    ib_status_t rc;

    rc = ib_field_create_alias(pf, mp, name, nlen, type, NULL);
    if (rc != IB_OK) {
        goto failed;
    }

    /* Point to internal memory */
    (*pf)->val->pval = &((*pf)->val->u);

    rc = ib_field_setv((*pf), in_pval);
    if (rc != IB_OK) {
        goto failed;
    }

    ib_field_util_log_debug("FIELD_CREATE", (*pf));

    return IB_OK;

failed:
    /* Make sure everything is cleaned up on failure. */
    *pf = NULL;

    return rc;
}
Esempio n. 3
0
ib_status_t ib_cfgmap_set(ib_cfgmap_t *cm,
                          const char *name,
                          void *pval)
{
    IB_FTRACE_INIT(ib_cfgmap_set);
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_hash_get(cm->hash, name, (void *)&f);
    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    rc = ib_field_setv(f, pval);

    /// @todo Remove this extra debugging
    switch (f->type) {
        case IB_FTYPE_BYTESTR:
            ib_util_log_debug(4, "SET FIELD type=%d %" IB_BYTESTR_FMT "=\"%" IB_BYTESTR_FMT "\" (%p)", f->type, IB_BYTESTRSL_FMT_PARAM(f->name,f->nlen), IB_BYTESTR_FMT_PARAM(*(ib_bytestr_t **)pval), *(void **)pval);
            break;
        case IB_FTYPE_NULSTR:
            ib_util_log_debug(4, "SET FIELD type=%d %" IB_BYTESTR_FMT "=\"%s\" (%p)", f->type, IB_BYTESTRSL_FMT_PARAM(f->name,f->nlen), *(char **)pval, *(void **)pval);
            break;
        case IB_FTYPE_NUM:
            ib_util_log_debug(4, "SET FIELD type=%d %" IB_BYTESTR_FMT "=%d (%p)", f->type, IB_BYTESTRSL_FMT_PARAM(f->name,f->nlen), *(int *)pval, *(void **)pval);
            break;
        case IB_FTYPE_UNUM:
            ib_util_log_debug(4, "SET FIELD type=%d %" IB_BYTESTR_FMT "=%d (%p)", f->type, IB_BYTESTRSL_FMT_PARAM(f->name,f->nlen), *(unsigned int *)pval, *(void **)pval);
            break;
    }

    IB_FTRACE_RET_STATUS(rc);
}
Esempio n. 4
0
TEST_F(CoreOperatorsTest, NeTest)
{
    ib_status_t status;
    ib_num_t call_result;
    ib_operator_inst_t *op;
    ib_rule_t *rule = NULL; /* Not used by this operator. */

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "ne",
                                     "1",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_OK, status);

    // call contains
    ib_field_t *field;
    const ib_num_t matching = 2;
    const ib_num_t nonmatching = 1;
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NUM,
        ib_ftype_num_in(&matching)
    );

    ib_rule_exec_t rule_exec;
    memset(&rule_exec, 0, sizeof(rule_exec));
    rule_exec.ib = ib_engine;
    rule_exec.tx = ib_tx;
    rule_exec.rule = rule;

    ib_field_setv(field, ib_ftype_num_in(&matching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_num_in(&nonmatching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);
}
Esempio n. 5
0
TEST_F(TestIBUtilField, Alias)
{
    ib_num_t   num1;
    ib_num_t   num2;
    ib_float_t flt1;
    ib_float_t flt2;
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MM(), "foo", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(v, s);

    /*
     * Alias a numeric field
     */
    num1 = 1;
    rc = ib_field_create_alias(&f, MM(), "num", 3,
                               IB_FTYPE_NUM,
                               ib_ftype_num_in(&num1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);

    num1 = 3;
    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);


    /*
     * Alias a floating point field
     */
    flt1 = 1.1;
    rc = ib_field_create_alias(&f, MM(), "flt", 3,
                               IB_FTYPE_FLOAT,
                               ib_ftype_float_in(&flt1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);

    flt1 = 1.5;
    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);
}
Esempio n. 6
0
TEST_F(CoreOperatorsTest, ContainsTest)
{
    ib_status_t status;
    ib_num_t call_result;
    ib_operator_inst_t *op;
    ib_rule_t *rule = NULL; /* Not used by this operator. */

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "contains",
                                     "needle",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_OK, status);

    // call contains
    ib_field_t *field;
    const char *matching = "data with needle in it";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_rule_exec_t rule_exec;
    memset(&rule_exec, 0, sizeof(rule_exec));
    rule_exec.ib = ib_engine;
    rule_exec.rule = rule;

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);
}
Esempio n. 7
0
TEST_F(CoreOperatorsTest, ContainsTest)
{
    ib_status_t status;
    ib_num_t call_result;
    const ib_operator_t *op;
    void *instance_data;

    status = ib_operator_lookup(ib_engine, "contains", &op);

    ASSERT_EQ(IB_OK, status);


    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "needle",
                                     &instance_data);
    ASSERT_EQ(IB_OK, status);

    // call contains
    ib_field_t *field;
    const char *matching = "data with needle in it";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);
}
Esempio n. 8
0
TEST_F(CoreOperatorsTest, NeTest)
{
    ib_status_t status;
    ib_num_t call_result;
    const ib_operator_t *op;
    void *instance_data;

    status = ib_operator_lookup(ib_engine, "ne", &op);

    ASSERT_EQ(IB_OK, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "1",
                                     &instance_data);
    ASSERT_EQ(IB_OK, status);

    // call contains
    ib_field_t *field;
    const ib_num_t matching = 2;
    const ib_num_t nonmatching = 1;
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NUM,
        ib_ftype_num_in(&matching)
    );

    ib_field_setv(field, ib_ftype_num_in(&matching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_num_in(&nonmatching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);
}
Esempio n. 9
0
ib_status_t ib_field_setv_no_copy(
    ib_field_t *f,
    void *mutable_in_pval
)
{
    if (f->type == IB_FTYPE_NUM || ib_field_is_dynamic(f)) {
        return ib_field_setv(f, mutable_in_pval);
    }

    *(void **)(f->val->pval) = mutable_in_pval;


    return IB_OK;
}
Esempio n. 10
0
TEST_F(TestIBUtilField, Alias)
{
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MemPool(), "foo", 3, IB_FTYPE_NULSTR,
        ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(std::string(v), std::string(s));
}
Esempio n. 11
0
ib_status_t ib_cfgmap_set(ib_cfgmap_t *cm,
                          const char *name,
                          void *in_val)
{
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_hash_get(cm->hash, &f, name);
    if (rc != IB_OK) {
        return rc;
    }

    rc = ib_field_setv(f, in_val);

    return rc;
}
Esempio n. 12
0
// Cached version of the above dyn_get function.
static ib_status_t dyn_get_cached(
    const ib_field_t *f,
    void *out_value,
    const void *arg,
    size_t alen,
    void *data
)
{
    /* Call the get function */
    const char* cval;
    dyn_get(f, &cval, arg, alen, data);

    /* Cache the value */
    /* Caching does not semantically change value, so we can safely ignore
     * the constness of f. */
    ib_field_make_static((ib_field_t *)f);
    ib_field_setv((ib_field_t *)f, ib_ftype_nulstr_in(cval));

    *reinterpret_cast<const char**>(out_value) = cval;

    return IB_OK;
}
Esempio n. 13
0
TEST_F(OperatorTest, OperatorCallTest)
{
    ib_status_t status;
    ib_num_t call_result;
    void *instance_data;
    ib_operator_t *op;
    const ib_operator_t *cop;

    status = ib_operator_create_and_register(
        &op,
        ib_engine,
        "test_op",
        IB_OP_CAPABILITY_NON_STREAM,
        test_create_fn, NULL,
        NULL, NULL,
        test_execute_fn, NULL
    );
    ASSERT_EQ(IB_OK, status);

    status = ib_operator_lookup(ib_engine, "test_op", &cop);

    ASSERT_EQ(IB_OK, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "INVALID",
                                     &instance_data);
    ASSERT_EQ(IB_EINVAL, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "data",
                                     &instance_data);
    ASSERT_EQ(IB_OK, status);


    ib_field_t *field;
    const char *matching = "data matching string";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);

    status = ib_operator_inst_destroy(op, instance_data);
    ASSERT_EQ(IB_OK, status);
}
Esempio n. 14
0
File: tfn.c Progetto: niq/ironbee
ib_status_t ib_tfn_transform_field(ib_tfn_t *tfn,
                                   ib_field_t *f,
                                   ib_flags_t *pflags)
{
    IB_FTRACE_INIT(ib_tfn_transform);
    ib_bytestr_t *bs;
    char *str;
    uint8_t *data_out;
    size_t dlen_out;
    ib_status_t rc;

    switch(f->type) {
        case IB_FTYPE_BYTESTR:
            bs = ib_field_value_bytestr(f);

            rc = tfn->transform(tfn->fndata,
                                f->mp,
                                ib_bytestr_ptr(bs),
                                ib_bytestr_length(bs),
                                &data_out,
                                &dlen_out,
                                pflags);

            /* If it is modified and not done inplace, then the
             * field value needs to be updated.
             */
            if (   IB_TFN_CHECK_FMODIFIED(*pflags)
                && !IB_TFN_CHECK_FINPLACE(*pflags))
            {
                ib_bytestr_t *bs_new;

                rc = ib_bytestr_alias_mem(&bs_new, f->mp, data_out, dlen_out);
                if (rc != IB_OK) {
                    IB_FTRACE_RET_STATUS(rc);
                }

                rc = ib_field_setv(f, bs_new);
            }

            IB_FTRACE_RET_STATUS(rc);

        case IB_FTYPE_NULSTR:
            str = ib_field_value_nulstr(f),

            rc = tfn->transform(tfn->fndata,
                                f->mp,
                                (uint8_t *)str,
                                strlen(str),
                                &data_out,
                                &dlen_out,
                                pflags);

            /* If it is modified and not done inplace, then the
             * field value needs to be updated.
             *
             * NOTE: Anytime a transformation modifies data it
             *       MUST NUL terminate the data and it is a bug
             *       if this is not done.
             */
            if (   IB_TFN_CHECK_FMODIFIED(*pflags)
                && !IB_TFN_CHECK_FINPLACE(*pflags))
            {
                rc = ib_field_setv(f, data_out);
            }

            IB_FTRACE_RET_STATUS(rc);
    }


    IB_FTRACE_RET_STATUS(IB_EINVAL);
}
Esempio n. 15
0
TEST_F(OperatorTest, OperatorCallTest)
{
    ib_status_t status;
    ib_num_t call_result;
    ib_rule_t *rule = NULL; /* Unused by this operator. */
    ib_operator_inst_t *op;

    status = ib_operator_register(ib_engine,
                                  "test_op",
                                  IB_OP_FLAG_PHASE,
                                  test_create_fn,
                                  NULL,
                                  test_destroy_fn,
                                  NULL,
                                  test_execute_fn,
                                  NULL);
    ASSERT_EQ(IB_OK, status);

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "test_op",
                                     "INVALID",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_EINVAL, status);

    status = ib_operator_inst_create(ib_engine,
                                     NULL,
                                     rule,
                                     IB_OP_FLAG_PHASE,
                                     "test_op",
                                     "data",
                                     IB_OPINST_FLAG_NONE,
                                     &op);
    ASSERT_EQ(IB_OK, status);

    ib_rule_exec_t rule_exec;
    memset(&rule_exec, 0, sizeof(rule_exec));
    rule_exec.ib = ib_engine;
    rule_exec.rule = rule;

    ib_field_t *field;
    const char *matching = "data matching string";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_execute(&rule_exec, op, field, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);

    status = ib_operator_inst_destroy(op);
    ASSERT_EQ(IB_OK, status);
}
Esempio n. 16
0
void set_value(ib_field_t* f, void* in_value)
{
    ib_status_t rc = ib_field_setv(f, in_value);
    throw_if_error(rc);
}