@@ -554,23 +554,42 @@ static void meta_probability(void **state)
554554 assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
555555 BF_MATCHER_EQ , "0%" ));
556556 assert_non_null (matcher );
557- assert_int_equal (* (uint8_t * )bf_matcher_payload (matcher ), 0 );
557+ assert_int_equal (bf_matcher_payload_len (matcher ), sizeof (float ));
558+ assert_true (* (float * )bf_matcher_payload (matcher ) == 0.0f );
558559 bf_matcher_dump (matcher , & prefix );
559560 bf_matcher_free (& matcher );
560561
561562 // Test with 50%
562563 assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
563564 BF_MATCHER_EQ , "50%" ));
564565 assert_non_null (matcher );
565- assert_int_equal (* (uint8_t * )bf_matcher_payload (matcher ), 50 );
566+ assert_true (* (float * )bf_matcher_payload (matcher ) == 50.0f );
566567 bf_matcher_dump (matcher , & prefix );
567568 bf_matcher_free (& matcher );
568569
569570 // Test with 100%
570571 assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
571572 BF_MATCHER_EQ , "100%" ));
572573 assert_non_null (matcher );
573- assert_int_equal (* (uint8_t * )bf_matcher_payload (matcher ), 100 );
574+ assert_true (* (float * )bf_matcher_payload (matcher ) == 100.0f );
575+ bf_matcher_dump (matcher , & prefix );
576+ bf_matcher_free (& matcher );
577+
578+ // Test with floating-point value
579+ assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
580+ BF_MATCHER_EQ , "33.33%" ));
581+ assert_non_null (matcher );
582+ assert_true (* (float * )bf_matcher_payload (matcher ) > 33.32f );
583+ assert_true (* (float * )bf_matcher_payload (matcher ) < 33.34f );
584+ bf_matcher_dump (matcher , & prefix );
585+ bf_matcher_free (& matcher );
586+
587+ // Test with small floating-point value
588+ assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
589+ BF_MATCHER_EQ , "0.1%" ));
590+ assert_non_null (matcher );
591+ assert_true (* (float * )bf_matcher_payload (matcher ) > 0.09f );
592+ assert_true (* (float * )bf_matcher_payload (matcher ) < 0.11f );
574593 bf_matcher_dump (matcher , & prefix );
575594}
576595
@@ -584,6 +603,10 @@ static void meta_probability_invalid(void **state)
584603 assert_err (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
585604 BF_MATCHER_EQ , "101%" ));
586605
606+ // Test with value slightly over 100%
607+ assert_err (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
608+ BF_MATCHER_EQ , "100.01%" ));
609+
587610 // Test without % sign
588611 assert_err (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
589612 BF_MATCHER_EQ , "50" ));
@@ -593,6 +616,46 @@ static void meta_probability_invalid(void **state)
593616 BF_MATCHER_EQ , "-10%" ));
594617}
595618
619+ static void meta_probability_pack_unpack (void * * state )
620+ {
621+ _free_bf_matcher_ struct bf_matcher * source = NULL ;
622+ _free_bf_matcher_ struct bf_matcher * destination = NULL ;
623+ _free_bf_wpack_ bf_wpack_t * wpack = NULL ;
624+ _free_bf_rpack_ bf_rpack_t * rpack = NULL ;
625+ const void * data ;
626+ size_t data_len ;
627+
628+ (void )state ;
629+
630+ // Test pack/unpack with integer percentage
631+ assert_ok (bf_matcher_new_from_raw (& source , BF_MATCHER_META_PROBABILITY ,
632+ BF_MATCHER_EQ , "50%" ));
633+ assert_ok (bf_wpack_new (& wpack ));
634+ assert_ok (bf_matcher_pack (source , wpack ));
635+ assert_ok (bf_wpack_get_data (wpack , & data , & data_len ));
636+
637+ assert_ok (bf_rpack_new (& rpack , data , data_len ));
638+ assert_ok (bf_matcher_new_from_pack (& destination , bf_rpack_root (rpack )));
639+
640+ assert_true (bft_matcher_equal (source , destination ));
641+ bf_matcher_free (& source );
642+ bf_matcher_free (& destination );
643+ bf_wpack_free (& wpack );
644+ bf_rpack_free (& rpack );
645+
646+ // Test pack/unpack with floating-point percentage
647+ assert_ok (bf_matcher_new_from_raw (& source , BF_MATCHER_META_PROBABILITY ,
648+ BF_MATCHER_EQ , "33.33%" ));
649+ assert_ok (bf_wpack_new (& wpack ));
650+ assert_ok (bf_matcher_pack (source , wpack ));
651+ assert_ok (bf_wpack_get_data (wpack , & data , & data_len ));
652+
653+ assert_ok (bf_rpack_new (& rpack , data , data_len ));
654+ assert_ok (bf_matcher_new_from_pack (& destination , bf_rpack_root (rpack )));
655+
656+ assert_true (bft_matcher_equal (source , destination ));
657+ }
658+
596659static void meta_sport_dport (void * * state )
597660{
598661 _free_bf_matcher_ struct bf_matcher * matcher = NULL ;
@@ -1231,7 +1294,7 @@ static void print_functions(void **state)
12311294 ops -> print (bf_matcher_payload (matcher ));
12321295 bf_matcher_free (& matcher );
12331296
1234- // Test _bf_print_probability via ops
1297+ // Test _bf_print_probability via ops (integer value)
12351298 assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
12361299 BF_MATCHER_EQ , "50%" ));
12371300 ops = bf_matcher_get_ops (BF_MATCHER_META_PROBABILITY , BF_MATCHER_EQ );
@@ -1240,7 +1303,16 @@ static void print_functions(void **state)
12401303 ops -> print (bf_matcher_payload (matcher ));
12411304 bf_matcher_free (& matcher );
12421305
1243- // Test _bf_print_flow_probability via ops (integer value)
1306+ // Test _bf_print_probability via ops (fractional value)
1307+ assert_ok (bf_matcher_new_from_raw (& matcher , BF_MATCHER_META_PROBABILITY ,
1308+ BF_MATCHER_EQ , "33.33%" ));
1309+ ops = bf_matcher_get_ops (BF_MATCHER_META_PROBABILITY , BF_MATCHER_EQ );
1310+ assert_non_null (ops );
1311+ assert_non_null (ops -> print );
1312+ ops -> print (bf_matcher_payload (matcher ));
1313+ bf_matcher_free (& matcher );
1314+
1315+ // Test _bf_print_probability via ops (flow_probability, integer value)
12441316 assert_ok (bf_matcher_new_from_raw (
12451317 & matcher , BF_MATCHER_META_FLOW_PROBABILITY , BF_MATCHER_EQ , "50%" ));
12461318 ops = bf_matcher_get_ops (BF_MATCHER_META_FLOW_PROBABILITY , BF_MATCHER_EQ );
@@ -1249,7 +1321,7 @@ static void print_functions(void **state)
12491321 ops -> print (bf_matcher_payload (matcher ));
12501322 bf_matcher_free (& matcher );
12511323
1252- // Test _bf_print_flow_probability via ops (fractional value)
1324+ // Test _bf_print_probability via ops (flow_probability, fractional value)
12531325 assert_ok (bf_matcher_new_from_raw (
12541326 & matcher , BF_MATCHER_META_FLOW_PROBABILITY , BF_MATCHER_EQ , "33.33%" ));
12551327 ops = bf_matcher_get_ops (BF_MATCHER_META_FLOW_PROBABILITY , BF_MATCHER_EQ );
@@ -1659,6 +1731,7 @@ int main(void)
16591731 cmocka_unit_test (meta_l3_proto ),
16601732 cmocka_unit_test (meta_probability ),
16611733 cmocka_unit_test (meta_probability_invalid ),
1734+ cmocka_unit_test (meta_probability_pack_unpack ),
16621735 cmocka_unit_test (meta_sport_dport ),
16631736 cmocka_unit_test (meta_sport_dport_range ),
16641737 cmocka_unit_test (meta_sport_dport_range_invalid ),
0 commit comments