Attention is currently required from: flichtenheld. Hello flichtenheld,
I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/876?usp=email to review the following change. Change subject: Add unit tests for atoi parsing options helper ...................................................................... Add unit tests for atoi parsing options helper Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d --- M tests/unit_tests/openvpn/mock_msg.c M tests/unit_tests/openvpn/mock_msg.h M tests/unit_tests/openvpn/test_misc.c 3 files changed, 108 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76/876/1 diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests/unit_tests/openvpn/mock_msg.c index a291f8f..99224f8 100644 --- a/tests/unit_tests/openvpn/mock_msg.c +++ b/tests/unit_tests/openvpn/mock_msg.c @@ -37,10 +37,16 @@ #include "errlevel.h" #include "error.h" +#include "mock_msg.h" unsigned int x_debug_level = 0; /* Default to (almost) no debugging output */ +unsigned int print_x_debug_level = 0; + bool fatal_error_triggered = false; +char mock_msg_buf[MOCK_MSG_BUF]; + + void mock_set_debug_level(int level) { @@ -48,6 +54,18 @@ } int +mock_get_debug_level(void) +{ + return x_debug_level; +} + +void +mock_set_print_debug_level(int level) +{ + print_x_debug_level = level; +} + +int get_debug_level(void) { return x_debug_level; @@ -62,8 +80,13 @@ fatal_error_triggered = true; printf("FATAL ERROR:"); } - vprintf(format, arglist); - printf("\n"); + CLEAR(mock_msg_buf); + if ((flags & M_DEBUG_LEVEL) <= print_x_debug_level) + { + vprintf(format, arglist); + printf("\n"); + } + vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist); } void diff --git a/tests/unit_tests/openvpn/mock_msg.h b/tests/unit_tests/openvpn/mock_msg.h index be5f2e5..25b7aa2 100644 --- a/tests/unit_tests/openvpn/mock_msg.h +++ b/tests/unit_tests/openvpn/mock_msg.h @@ -31,4 +31,18 @@ */ void mock_set_debug_level(int level); +#define MOCK_MSG_BUF 1024 + +extern bool fatal_error_triggered; +extern char mock_msg_buf[MOCK_MSG_BUF]; + +void +mock_set_debug_level(int level); + +int +mock_get_debug_level(void); + +void +mock_set_print_debug_level(int level); + #endif /* MOCK_MSG */ diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c index 563d4ae..56e9d98 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -38,6 +38,7 @@ #include "options_util.h" #include "test_common.h" #include "list.h" +#include "mock_msg.h" static void test_compat_lzo_string(void **state) @@ -311,13 +312,80 @@ gc_free(&gc); } +static void +test_atoi_variants(void **state) +{ + assert_true(valid_integer("1234", true)); + assert_true(valid_integer("1234", false)); + assert_true(valid_integer("0", false)); + assert_true(valid_integer("0", true)); + assert_true(valid_integer("-777", false)); + assert_false(valid_integer("-777", true)); + + assert_false(valid_integer("-777foo", false)); + assert_false(valid_integer("-777foo", true)); + + assert_false(valid_integer("foo777", true)); + assert_false(valid_integer("foo777", false)); + + /* 2**31 + 5 , just outside of signed int range */ + assert_false(valid_integer("2147483653", true)); + assert_false(valid_integer("2147483653", false)); + assert_false(valid_integer("-2147483653", true)); + assert_false(valid_integer("-2147483653", false)); + + + int msglevel = D_LOW; + int saved_log_level = mock_get_debug_level(); + mock_set_debug_level(D_LOW); + + /* check happy path */ + assert_int_equal(positive_atoi("1234", msglevel), 1234); + assert_int_equal(positive_atoi("0", msglevel), 0); + + assert_int_equal(atoi_warn("1234", msglevel), 1234); + assert_int_equal(atoi_warn("0", msglevel), 0); + assert_int_equal(atoi_warn("-1194", msglevel), -1194); + + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("-1234", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '-1234' as non-negative integer"); + + /* 2**31 + 5 , just outside of signed int range */ + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("2147483653", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '2147483653' as non-negative integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("2147483653", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '2147483653' as integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("foo77", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as non-negative integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("77foo", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as non-negative integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("foo77", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("77foo", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as integer"); + + mock_set_debug_level(saved_log_level); +} const struct CMUnitTest misc_tests[] = { cmocka_unit_test(test_compat_lzo_string), cmocka_unit_test(test_auth_fail_temp_no_flags), cmocka_unit_test(test_auth_fail_temp_flags), cmocka_unit_test(test_auth_fail_temp_flags_msg), - cmocka_unit_test(test_list) + cmocka_unit_test(test_list), + cmocka_unit_test(test_atoi_variants) }; int -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/876?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d Gerrit-Change-Number: 876 Gerrit-PatchSet: 1 Gerrit-Owner: plaisthos <arne-open...@rfc2549.org> Gerrit-Reviewer: flichtenheld <fr...@lichtenheld.com> Gerrit-CC: openvpn-devel <openvpn-devel@lists.sourceforge.net> Gerrit-Attention: flichtenheld <fr...@lichtenheld.com> Gerrit-MessageType: newchange
_______________________________________________ Openvpn-devel mailing list Openvpn-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openvpn-devel