From: "plaisthos (Code Review)" Date: Mon, 27 Jan 2025 12:01:46 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <2f6f608cb61cccee430ea675de28ededf19a7e4f-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="By0wqAz3Mks="; 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: flichtenheld Cc: openvpn-devel --By0wqAz3Mks= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=3Demail 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 =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,13 @@ fatal_error_triggered =3D true; printf("FATAL ERROR:"); } - vprintf(format, arglist); - printf("\n"); + CLEAR(mock_msg_buf); + if ((flags & M_DEBUG_LEVEL) <=3D 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/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: 1 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --By0wqAz3Mks= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos would like flichtenheld to review this change.

Vi= ew Change

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 deletio= ns(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/change=
s/76/876/1
diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests=
/unit_tests/openvpn/mock_msg.c
index a291f8f..99224f8 10064= 4
--- 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 d= ebugging output */
+u= nsigned int print_x_debug_level =3D 0;
+
bool fatal_error_triggered =3D false;<= /span>

+ch= ar mock_msg_buf[MOCK_MSG_BUF];
+
+<= br> void
mock_set_debug_level(int level)
<= span> {

@@ -48,6 +54,18 @@
}

int
+mock_get_debug_level(void)
+{
+ r= eturn x_debug_level;
= +}
+
+void
+mock_set_print_debug_level(int level)
+{

+ print_x_debug_level =3D level;
+}
+
+int
get_debug_level(void)
{
<= span> return x_debug_level;
@@ -62,8 +80,13 @@ fatal_error_triggered =3D true;
p= rintf("FATAL ERROR:");
}
- vprintf(format, arglist);
<= span style=3D"color: hsl(0, 100%, 40%);">- printf("\n");

+ CLEAR(mock_msg_buf);<= /span>
+ if ((flags &= M_DEBUG_LEVEL) <=3D print_x_debug_level)
+ {
+ vprintf(format, arglist);
+ printf("\n");
+ }
+ vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), fo= rmat, arglist);
}

void<= /span>
diff --git a/tests/unit_tests/openvpn/mock_msg.h b/tests/un= it_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 @@<= /span>
*/
void mock_set_debug_level(int level);=

+#= define MOCK_MSG_BUF 1024
+
+extern bool fa= tal_error_triggered;
= +extern char mock_msg_buf[MOCK_MSG_BUF];
+
+void
+mock_set_debu= g_level(int level);
+=
+int
+mock_get_debug_level(void);
<= br>+
+void
+mock_set_print_debug_level(int level);
+
#endif /* MOCK_MSG */
diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/un= it_tests/openvpn/test_misc.c
index 563d4ae..56e9d98 100644<= /span>
--- a/tests/unit_tests/openvpn/test_misc.c
= +++ b/tests/unit_tests/openvpn/test_misc.c
@@ -38,6 +38,7 @= @
#include "options_util.h"
#in= clude "test_common.h"
#include "list.h"= ;
+#include "moc= k_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));
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_true(valid_integer(&= quot;0", false));

+ assert_true(valid_integer("-77= 7", false));
+ = assert_false(valid_integer("-777", true));
+
+ assert_false(valid_integer("-777foo", false))= ;
+ assert_false(v= alid_integer("-777foo", true));
+
+ assert_false(valid_integer(&q= uot;foo777", false));
+
+ /* 2**31= + 5 , just outside of signed int range */
+ assert_false(valid_integer("2147483653"= ;, true));
+ asser= t_false(valid_integer("2147483653", false));
+ assert_false(valid_integer("-21= 47483653", 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(po= sitive_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", msg= level), 0);
+ asse= rt_int_equal(atoi_warn("-1194", msglevel), -1194);
+

+ CLEAR(mock_msg_buf);
+ assert_int_equal(positive_atoi("-1234&q= uot;, msglevel), 0);
= + assert_string_equal(mock_msg_buf, "Cannot parse argument '-12= 34' 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 pars= e argument '2147483653' as non-negative integer");
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ CLEAR(mock_msg_buf);
+ assert_int_equal(atoi_warn("21474836= 53", 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 i= nteger");
+
+ CLEAR(mock_msg_buf);=
+ assert_int_equa= l(positive_atoi("77foo", msglevel), 0);
+ assert_string_equal(mock_msg_buf, "C= annot parse argument '77foo' as non-negative integer");=
+
+ CLEAR(mock_msg_buf);
+ assert_int_equal(atoi_warn("foo= 77", msglevel), 0);
+ assert_string_equal(mock_msg_buf, "Cannot parse argument '= ;foo77' as integer");
+
+ CLEA= R(mock_msg_buf);
+ = assert_int_equal(atoi_warn("77foo", msglevel), 0);
+ assert_string_equal(mock_msg_= buf, "Cannot parse argument '77foo' as integer");
<= br>+
+ mock_set_debug_level(saved_log_level);
+}

const struct CMUnitTest misc_tests[] =3D {
= cmocka_unit_test(test_compat_lzo_string),
cmocka_un= it_test(test_auth_fail_temp_no_flags),
cmocka_unit_tes= t(test_auth_fail_temp_flags),
cmocka_unit_test(test_au= th_fail_temp_flags_msg),
+ cmocka_unit_test(test_list),
+ cmocka_unit_test(test_atoi_variants)=
};

int

To view, visit change 876. To unsubscribe, or for help writing mai= l filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ieee368e325d7f9c367fd91fee0f= d3e281ee0855d
Gerrit-Change-Number: 876
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--By0wqAz3Mks=--