From: "cron2 (Code Review)" Date: Tue, 28 Jan 2025 18:58:40 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <925fab951611e9898cce85e07e7817a6c61e6dea-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="vQBitHy8ny4="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Add unit tests for atoi parsing options helper List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --vQBitHy8ny4= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#3) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/876?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by flichtenheld Change subject: Add unit tests for atoi parsing options helper ...................................................................... Add unit tests for atoi parsing options helper Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d Signed-off-by: Arne Schwabe Acked-by: Frank Lichtenheld Message-Id: <20250128175855.12289-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30= 671.html Signed-off-by: Gert Doering --- 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, 109 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76/876/3 diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests/unit_tests/openvpn= /mock_msg.c index a291f8f..b6ec744 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 =3D 0; /* Default to (almost) no debugging outp= ut */ +unsigned int print_x_debug_level =3D 0; + bool fatal_error_triggered =3D 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 =3D level; +} + +int get_debug_level(void) { return x_debug_level; @@ -62,8 +80,14 @@ fatal_error_triggered =3D true; printf("FATAL ERROR:"); } - vprintf(format, arglist); - printf("\n"); + CLEAR(mock_msg_buf); + vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist); + + if ((flags & M_DEBUG_LEVEL) <=3D print_x_debug_level) + { + printf("%s", mock_msg_buf); + printf("\n"); + } } void diff --git a/tests/unit_tests/openvpn/mock_msg.h b/tests/unit_tests/openvpn= /mock_msg.h index be5f2e5..bc2ee5d 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 2048 + +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/openvp= n/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 =3D D_LOW; + int saved_log_level =3D 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 no= n-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 no= n-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 no= n-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 in= teger"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("77foo", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as in= teger"); + + mock_set_debug_level(saved_log_level); +} const struct CMUnitTest misc_tests[] =3D { 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=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d Gerrit-Change-Number: 876 Gerrit-PatchSet: 3 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --vQBitHy8ny4= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #3 to the change originally cr= eated by plaisthos.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by flichtenheld

Add unit tests for atoi parsing options helpe=
r

Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d
Signed-off= -by: Arne Schwabe <arne@...1227...>
Acked-by: Frank Lichtenheld &l= t;frank@...2641...>
Message-Id: <20250128175855.12289-1-gert@...1711...= 96...>
URL: https://www.mail-archive.com/openvpn-devel@...1357...= rge.net/msg30671.html
Signed-off-by: Gert Doering <gert@...1296...>= ;
---
M tests/unit_tests/openvpn/mock_msg.c
M tests/unit_tests/ope= nvpn/mock_msg.h
M tests/unit_tests/openvpn/test_misc.c
3 files change= d, 109 insertions(+), 3 deletions(-)

git pull ssh://gerrit.openvp=
n.net:29418/openvpn refs/changes/76/876/3
diff --git a/tests/unit_t=
ests/openvpn/mock_msg.c b/tests/unit_tests/openvpn/mock_msg.c
index a291f8f..b6ec744 100644
--- a/tests/unit_tests/ope= nvpn/mock_msg.c
+++ b/tests/unit_tests/openvpn/mock_msg.c
@@ -37,10 +37,16 @@

#incl= ude "errlevel.h"
#include "error.h"
+#include "mock_ms= g.h"

unsigned int x_debug_level =3D= 0; /* Default to (almost) no debugging output */
+unsigned int print_x_debug_level =3D 0;
+
bool fa= tal_error_triggered =3D false;

+char mock_msg_buf[MOCK_MSG_BUF];
+

+
void
mock_set_deb= ug_level(int level)
{
@@ -48,6 +54,18 @@
}

int
+mock_get_debug_level(void)
<= span style=3D"color: hsl(120, 100%, 40%);">+{

+ return x_debug_level;
+}
+
+voi= d
+mock_set_print_deb= ug_level(int level)
+= {
+ print_x_debug_= level =3D level;
+}
+
+int
get_debug_level(voi= d)
{
return x_debug_level;
= @@ -62,8 +80,14 @@
fatal_error_triggered =3D= true;
printf("FATAL ERROR:"); }
- vpri= ntf(format, arglist);
-= printf("\n");
+ CLEAR(mock_msg_buf);
+ vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, argl= ist);
+
+ if ((flags & M_DEBUG_LEVE= L) <=3D print_x_debug_level)

+ {
+ = printf("%s", mock_msg_buf);
+ printf("\n");
+ }
}

void
diff --git a/tests/unit_tests/ope= nvpn/mock_msg.h b/tests/unit_tests/openvpn/mock_msg.h
index= be5f2e5..bc2ee5d 100644
--- a/tests/unit_tests/openvpn/moc= k_msg.h
+++ b/tests/unit_tests/openvpn/mock_msg.h@@ -31,4 +31,18 @@
*/
void mock_s= et_debug_level(int level);

+#define MOCK_MSG_BUF 2048
+
+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);
+<= /span>
+void
+mock_set_print_debug_level(int leve= l);

+
#endif /* MOCK_MSG */

diff --git a/tests/unit_tests/openv= pn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c
index= 563d4ae..56e9d98 100644
--- a/tests/unit_tests/openvpn/tes= t_misc.c
+++ b/tests/unit_tests/openvpn/test_misc.c<= br>@@ -38,6 +38,7 @@
#include "options_util.h&q= uot;
#include "test_common.h"
#= include "list.h"
+#include "mock_msg.h"

s= tatic 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&= quot;, false));
+ = assert_true(valid_integer("0", false));
+ assert_true(valid_integer("0", = true));
+ assert_t= rue(valid_integer("-777", false));
+ assert_false(valid_integer("-777", t= rue));
+
+ assert_false(valid_integer(&= quot;-777foo", false));
+ assert_false(valid_integer("-777foo", true));=
+
+ assert_false(valid_integer("foo777&q= uot;, true));
+ as= sert_false(valid_integer("foo777", false));
+
+ /* 2**31 + 5 , just outside of signed int range */
+ assert_false(valid_in= teger("2147483653", true));
+ assert_false(valid_integer("2147483653", fa= lse));
+ assert_fa= lse(valid_integer("-2147483653", true));
+ assert_false(valid_integer("-214748= 3653", false));
= +
+
+ int msglevel =3D D_LOW;+ int saved_log_level =3D m= ock_get_debug_level();
+
+ = /* check happy path */
+ assert_int_equal(positive_atoi("1234", msglevel), 1234);<= /span>
+ assert_int_equal= (positive_atoi("0", msglevel), 0);
+
+ assert_int_equal(atoi_warn("1234", msglevel), 1234);

+ assert_int_equal(a= toi_warn("0", msglevel), 0);
+ assert_int_equal(atoi_warn("-1194", msglev= el), -1194);
+=
+ CLEAR(mock_msg_buf);
+ assert_int_equal(= positive_atoi("-1234", msglevel), 0);
+ assert_string_equal(mock_msg_buf, "Can= not parse argument '-1234' as non-negative integer");+
+ /* 2**31 + 5 , just outside of signed int r= ange */
+ CLEAR(mo= ck_msg_buf);
+ ass= ert_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_equ= al(atoi_warn("2147483653", msglevel), 0);
+ assert_string_equal(mock_msg_buf, &qu= ot;Cannot parse argument '2147483653' as integer");
= +
+ CLEAR(mock_msg_buf);
+ assert_int_equal(positive_atoi("foo= 77", 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_strin= g_equal(mock_msg_buf, "Cannot parse argument '77foo' as non-ne= gative integer");
+ CLEAR(mock_m= sg_buf);
+ assert_= int_equal(atoi_warn("foo77", msglevel), 0);
+ assert_string_equal(mock_msg_buf, &qu= ot;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= 9; as integer");
+
+ mock_set_debu= g_level(saved_log_level);
+}

const struct CMUnitTest misc_tes= ts[] =3D {
cmocka_unit_test(test_compat_lzo_string),
cmocka_unit_test(test_auth_fail_temp_no_flags),<= br> cmocka_unit_test(test_auth_fail_temp_flags),
= cmocka_unit_test(test_auth_fail_temp_flags_msg),
- cmocka_unit_test(test_list)
<= span style=3D"color: hsl(120, 100%, 40%);">+ cmocka_unit_test(test_list)= ,

+ cmocka_unit_te= st(test_atoi_variants)
};

int

To view, visit change 876. To unsubsc= ribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ieee368e325d7f9c367fd91fee0f= d3e281ee0855d
Gerrit-Change-Number: 876
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--vQBitHy8ny4=--