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); }
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); }
TEST_F(TestIronBeeModuleRulesLua, operator_test) { const ib_operator_t *op; void *instance_data; ib_num_t result; ib_field_t* field1; const char* op_name = "test_module_rules_lua.lua"; const char* rule_name = "luarule001"; char* str1 = (char *)ib_mpool_strdup(ib_engine->mp, "string 1"); ASSERT_TRUE(str1); // Create field 1. ASSERT_EQ(IB_OK, ib_field_create( &field1, ib_engine->mp, IB_FIELD_NAME("field1"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(str1) ) ); /* Configure the operator. */ configureIronBee(); // Ensure that the operator exists. ASSERT_EQ(IB_OK, ib_operator_lookup(ib_engine, op_name, &op) ); ASSERT_EQ(IB_OK, ib_operator_inst_create(op, ib_context_main(ib_engine), IB_OP_CAPABILITY_NON_STREAM, rule_name, &instance_data)); performTx(); // Attempt to match. ASSERT_EQ(IB_OK, ib_operator_inst_execute(op, instance_data, ib_tx, field1, NULL, &result)); // This time we should succeed. ASSERT_TRUE(result); }
int ConstOperatorInstance::execute( Transaction tx, ConstField input, Field capture ) const { ib_num_t result; throw_if_error( ib_operator_inst_execute( ib(), tx.ib(), input.ib(), capture.ib(), &result ) ); return static_cast<int>(result); }
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); }
TEST_F(PcreModuleTest, test_pcre_operator) { ib_field_t *outfield; ib_num_t result; ib_field_t *capture; const ib_operator_t *op; ib_operator_inst_t *opinst; ASSERT_EQ(IB_OK, ib_operator_lookup(ib_engine, IB_S2SL("pcre"), &op)); // Create the operator instance. ASSERT_EQ( IB_OK, ib_operator_inst_create( &opinst, ib_engine_mm_main_get(ib_engine), ib_context_main(ib_engine), op, IB_OP_CAPABILITY_NONE, "string\\s2" ) ); // Attempt to match. ASSERT_EQ( IB_OK, ib_operator_inst_execute( opinst, rule_exec1.tx, field1, NULL, &result ) ); // We should fail. ASSERT_FALSE(result); // Attempt to match again. ASSERT_EQ( IB_OK, ib_operator_inst_execute( opinst, rule_exec1.tx, field2, NULL, &result ) ); // This time we should succeed. ASSERT_TRUE(result); // Should be no capture set */ outfield = getTarget1(IB_TX_CAPTURE":0"); ASSERT_FALSE(outfield); // Create the operator instance. ASSERT_EQ( IB_OK, ib_operator_inst_create( &opinst, ib_engine_mm_main_get(ib_engine), ib_context_main(ib_engine), op, IB_OP_CAPABILITY_NONE, "(string 2)" ) ); // Attempt to match. ASSERT_EQ( IB_OK, ib_operator_inst_execute( opinst, rule_exec1.tx, field1, NULL, &result ) ); // We should fail. ASSERT_FALSE(result); // Attempt to match again. ASSERT_EQ( IB_OK, ib_operator_inst_execute( opinst, rule_exec1.tx, field2, NULL, &result ) ); // This time we should succeed. ASSERT_TRUE(result); // Should be no capture (CAPTURE flag not set for rule 1) outfield = getTarget1(IB_TX_CAPTURE":0"); ASSERT_FALSE(outfield); // Create the operator instance. ASSERT_EQ( IB_OK, ib_operator_inst_create( &opinst, ib_engine_mm_main_get(ib_engine), ib_context_main(ib_engine), op, IB_OP_CAPABILITY_NONE, "(string 2)" ) ); ASSERT_EQ(IB_OK, ib_capture_acquire( rule_exec2.tx, NULL, &capture)); // Attempt to match again. ASSERT_EQ( IB_OK, ib_operator_inst_execute( opinst, rule_exec1.tx, field2, capture, &result ) ); // This time we should succeed. ASSERT_TRUE(result); // Should be a capture (CAPTURE flag is set for rule 2) outfield = getTarget1(IB_TX_CAPTURE":0"); ASSERT_TRUE(outfield); }