From: "plaisthos (Code Review)" Date: Mon, 27 Jan 2025 12:51:53 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <9faf31aa2ce31f7a6cc1ca3c0808c87ce08f397b-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ItHYAzryGqo="; 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: cron2 , flichtenheld Cc: openvpn-devel --ItHYAzryGqo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello cron2, flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/876?usp=3Demail to look at the new patch set (#2). 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 --- 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/2 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: 2 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --ItHYAzryGqo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

plaisthos uploaded patch set #2 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/876?usp=3Demail">View Chang= e

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...>
---
M tests/unit_tests/open= vpn/mock_msg.c
M tests/unit_tests/openvpn/mock_msg.h
M tests/unit_tes= ts/openvpn/test_misc.c
3 files changed, 109 insertions(+), 3 deletions(-= )

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76=
/876/2
diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests/uni=
t_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 @@<= /span>

#include "errlevel.h" #include "error.h"
+#include "mock_msg.h"
<= br> unsigned int x_debug_level =3D 0; /* Default to (almost) no debug= ging output */
+unsig= ned int print_x_debug_level =3D 0;
+
bool fatal_error_triggered =3D false;

+char m= ock_msg_buf[MOCK_MSG_BUF];
+
+
<= span> void

mock_set_debug_level(int level)
{

@@ -48,6 +54,18 @@
}
=
int
+mock_get_debug_level(void)
+{
+ retur= n x_debug_level;
+}
+
+void
+mock_set_print_debug_level(int level)
+{
+ print_x_debug_level =3D level;
+}
+
+in= t
get_debug_level(void)
{
return x_debug_level;

@@ -62,8 +80,14 @@
fatal_error_triggered =3D true;

print= f("FATAL ERROR:");
}
- vprintf(format, arglist);
- printf("\n");<= br>+ 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_m= sg_buf);
+ pri= ntf("\n");
= + }
}

voiddiff --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 MO= CK_MSG_BUF 2048
+
+extern bool fatal_error= _triggered;
+extern c= har mock_msg_buf[MOCK_MSG_BUF];
+
+void
+mock_set_debug_level(i= nt level);
++int
+mock_get_debug_level(void);
+
+void
+mock_set_print_debug_level(int level);
+
#endif /* MOCK_MSG */
<= span>diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/o= penvpn/test_misc.c

index 563d4ae..56e9d98 100644
= --- a/tests/unit_tests/openvpn/test_misc.c
+++ b/test= s/unit_tests/openvpn/test_misc.c
@@ -38,6 +38,7 @@ #include "options_util.h"
#include &quo= t;test_common.h"
#include "list.h"+#include "mock_msg.h&qu= ot;

static void
test_co= mpat_lzo_string(void **state)
@@ -311,13 +312,80 @@<= br> gc_free(&gc);
}
=
+static void
+test_atoi_variants(void **state)
+{
+ assert_true(valid_integer("123= 4", true));
+ = assert_true(valid_integer("1234", false));
+ assert_true(valid_integer("0&quo= t;, false));
+ ass= ert_true(valid_integer("0", true));
+ assert_true(valid_integer("-777", f= alse));
+ assert_f= alse(valid_integer("-777", true));
+
+ assert_false(valid_integer("-777foo", false));+ assert_false(valid_integ= er("-777foo", true));
+
+ ass= ert_false(valid_integer("foo777", true));
+ assert_false(valid_integer("foo7= 77", false));
+<= /span>
+ /* 2**31 + 5 , j= ust 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 saved_log_level =3D mock_get_debug_level();
+ mock_set_debug_level(D_LOW);=

+
+ /* check happy path */
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_int_equal(positive_a= toi("1234", msglevel), 1234);
+ assert_int_equal(positive_atoi("0", msgle= vel), 0);
++ assert_int_equal(atoi_war= n("1234", msglevel), 1234);
+ assert_int_equal(atoi_warn("0", msglevel), = 0);
+ assert_int_e= qual(atoi_warn("-1194", msglevel), -1194);
+
+ CLEAR(mock_msg_buf);
+ assert_int_equal(positive_atoi("-1234", m= sglevel), 0);
+ as= sert_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 argum= ent '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 '214748= 3653' as integer");
+
+ CLEAR(= mock_msg_buf);
+ a= ssert_int_equal(positive_atoi("foo77", msglevel), 0);
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_string_equal(mock_ms= g_buf, "Cannot parse argument 'foo77' as non-negative integer&= quot;);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ CLEAR(mock_msg_buf);=
+ assert_int_equal(posit= ive_atoi("77foo", msglevel), 0);
+ assert_string_equal(mock_msg_buf, "Cannot p= arse 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&= #39; as integer");
+
+ CLEAR(mock_= msg_buf);
+ assert= _int_equal(atoi_warn("77foo", msglevel), 0);
+ assert_string_equal(mock_msg_buf, &q= uot;Cannot parse argument '77foo' as integer");
+
+ mock_set_debug_level(saved_log_level);
= +}

= const struct CMUnitTest misc_tests[] =3D {
cmoc= ka_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 change 876. To unsubscribe, or for help writing mail filte= rs, visit settings.

<= div itemscope itemtype=3D"http://schema.org/EmailMessage">
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ieee368e325d7f9c367fd91fee0f= d3e281ee0855d
Gerrit-Change-Number: 876
Gerrit-PatchSet: 2
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-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--ItHYAzryGqo=--