From: "plaisthos (Code Review)" Date: Mon, 5 Feb 2024 13:45:18 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <9b70f2062f9dda05188e3d2163f364697a1b4e3e-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="fTAdqguFjSk="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Turn dead list test code into unit test List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --fTAdqguFjSk= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld,=20 I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/446?usp=3Demail to look at the new patch set (#5). Change subject: Turn dead list test code into unit test ...................................................................... Turn dead list test code into unit test Change-Id: I7511bc43cd6a0bcb89476f27d5822ab4a78d0d21 Signed-off-by: Arne Schwabe --- M CMakeLists.txt M src/openvpn/init.c M src/openvpn/list.c M src/openvpn/list.h M tests/unit_tests/openvpn/Makefile.am M tests/unit_tests/openvpn/test_misc.c 6 files changed, 211 insertions(+), 193 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/46/446/5 diff --git a/CMakeLists.txt b/CMakeLists.txt index fdd2b01..3127611 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -747,6 +747,7 @@ tests/unit_tests/openvpn/mock_get_random.c src/openvpn/options_util.c src/openvpn/ssl_util.c + src/openvpn/list.c ) target_sources(test_ncp PRIVATE diff --git a/src/openvpn/init.c b/src/openvpn/init.c index c5cc154..52b4308 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -865,11 +865,6 @@ return false; #endif =20 -#ifdef LIST_TEST - list_test(); - return false; -#endif - #ifdef IFCONFIG_POOL_TEST ifconfig_pool_test(0x0A010004, 0x0A0100FF); return false; diff --git a/src/openvpn/list.c b/src/openvpn/list.c index 480f39d..dc4b1df 100644 --- a/src/openvpn/list.c +++ b/src/openvpn/list.c @@ -326,185 +326,6 @@ } -#ifdef LIST_TEST - -/* - * Test the hash code by implementing a simple - * word frequency algorithm. - */ - -struct word -{ - const char *word; - int n; -}; - -static uint32_t -word_hash_function(const void *key, uint32_t iv) -{ - const char *str =3D (const char *) key; - const int len =3D strlen(str); - return hash_func((const uint8_t *)str, len, iv); -} - -static bool -word_compare_function(const void *key1, const void *key2) -{ - return strcmp((const char *)key1, (const char *)key2) =3D=3D 0; -} - -static void -print_nhash(struct hash *hash) -{ - struct hash_iterator hi; - struct hash_element *he; - int count =3D 0; - - hash_iterator_init(hash, &hi, true); - - while ((he =3D hash_iterator_next(&hi))) - { - printf("%d ", (int) he->value); - ++count; - } - printf("\n"); - - hash_iterator_free(&hi); - ASSERT(count =3D=3D hash_n_elements(hash)); -} - -static void -rmhash(struct hash *hash, const char *word) -{ - hash_remove(hash, word); -} - -void -list_test(void) -{ - openvpn_thread_init(); - - { - struct gc_arena gc =3D gc_new(); - struct hash *hash =3D hash_init(10000, get_random(), word_hash_fun= ction, word_compare_function); - struct hash *nhash =3D hash_init(256, get_random(), word_hash_func= tion, word_compare_function); - - printf("hash_init n_buckets=3D%d mask=3D0x%08x\n", hash->n_buckets= , hash->mask); - - /* parse words from stdin */ - while (true) - { - char buf[256]; - char wordbuf[256]; - int wbi; - int bi; - char c; - - if (!fgets(buf, sizeof(buf), stdin)) - { - break; - } - - bi =3D wbi =3D 0; - do - { - c =3D buf[bi++]; - if (isalnum(c) || c =3D=3D '_') - { - ASSERT(wbi < (int) sizeof(wordbuf)); - wordbuf[wbi++] =3D c; - } - else - { - if (wbi) - { - struct word *w; - ASSERT(wbi < (int) sizeof(wordbuf)); - wordbuf[wbi++] =3D '\0'; - - /* word is parsed from stdin */ - - /* does it already exist in table? */ - w =3D (struct word *) hash_lookup(hash, wordbuf); - - if (w) - { - /* yes, increment count */ - ++w->n; - } - else - { - /* no, make a new object */ - ALLOC_OBJ_GC(w, struct word, &gc); - w->word =3D string_alloc(wordbuf, &gc); - w->n =3D 1; - ASSERT(hash_add(hash, w->word, w, false)); - ASSERT(hash_add(nhash, w->word, (void *) ((ran= dom() & 0x0F) + 1), false)); - } - } - wbi =3D 0; - } - } while (c); - } - -#if 1 - /* remove some words from the table */ - { - rmhash(hash, "true"); - rmhash(hash, "false"); - } -#endif - - /* output contents of hash table */ - { - int base; - int inc =3D 0; - int count =3D 0; - - for (base =3D 0; base < hash_n_buckets(hash); base +=3D inc) - { - struct hash_iterator hi; - struct hash_element *he; - inc =3D (get_random() % 3) + 1; - hash_iterator_init_range(hash, &hi, true, base, base + inc= ); - - while ((he =3D hash_iterator_next(&hi))) - { - struct word *w =3D (struct word *) he->value; - printf("%6d '%s'\n", w->n, w->word); - ++count; - } - - hash_iterator_free(&hi); - } - ASSERT(count =3D=3D hash_n_elements(hash)); - } - -#if 1 - /* test hash_remove_by_value function */ - { - int i; - for (i =3D 1; i <=3D 16; ++i) - { - printf("[%d] ***********************************\n", i); - print_nhash(nhash); - hash_remove_by_value(nhash, (void *) i, true); - } - printf("FINAL **************************\n"); - print_nhash(nhash); - } -#endif - - hash_free(hash); - hash_free(nhash); - gc_free(&gc); - } - - openvpn_thread_cleanup(); -} - -#endif /* ifdef LIST_TEST */ - /* * -------------------------------------------------------------------- * hash() -- hash a variable-length key into a 32-bit value diff --git a/src/openvpn/list.h b/src/openvpn/list.h index 94d14f2..18afc54 100644 --- a/src/openvpn/list.h +++ b/src/openvpn/list.h @@ -33,8 +33,6 @@ * client instances over various key spaces. */ -/* define this to enable special list test mode */ -/*#define LIST_TEST*/ #include "basic.h" #include "buffer.h" @@ -114,11 +112,6 @@ uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval); -#ifdef LIST_TEST -void list_test(void); - -#endif - static inline uint32_t hash_value(const struct hash *hash, const void *key) { diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index 88a694d..dfb9f6a 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -288,7 +288,8 @@ $(top_srcdir)/src/openvpn/ssl_util.c misc_testdriver_CFLAGS =3D @TEST_CFLAGS@ \ - -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn \ + -DSOURCEDIR=3D\"$(top_srcdir)\" misc_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ @@ -298,4 +299,6 @@ $(top_srcdir)/src/openvpn/options_util.c \ $(top_srcdir)/src/openvpn/ssl_util.c \ $(top_srcdir)/src/openvpn/win32-util.c \ - $(top_srcdir)/src/openvpn/platform.c + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/list.c + diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvp= n/test_misc.c index 193f131..04dbd5a 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -37,6 +37,7 @@ #include "ssl_util.h" #include "options_util.h" #include "test_common.h" +#include "list.h" static void test_compat_lzo_string(void **state) @@ -108,11 +109,215 @@ assert_int_equal(o.server_backoff_time, 77); } =20 + + +struct word +{ + const char *word; + int n; +}; + + +static uint32_t +word_hash_function(const void *key, uint32_t iv) +{ + const char *str =3D (const char *) key; + const int len =3D strlen(str); + return hash_func((const uint8_t *)str, len, iv); +} + +static bool +word_compare_function(const void *key1, const void *key2) +{ + return strcmp((const char *)key1, (const char *)key2) =3D=3D 0; +} + +static unsigned long +get_random(void) +{ + /* rand() is not very random, but it's C99 and this is just for testin= g */ + return rand(); +} + +static struct hash_element * +hash_lookup_by_value(struct hash *hash, void *value) +{ + struct hash_iterator hi; + struct hash_element *he; + struct hash_element *ret =3D NULL; + hash_iterator_init(hash, &hi); + + while ((he =3D hash_iterator_next(&hi))) + { + if (he->value =3D=3D value) + { + ret =3D he; + } + } + hash_iterator_free(&hi); + return ret; +} + +static void +test_list(void **state) +{ + +/* + * Test the hash code by implementing a simple + * word frequency algorithm. + */ + + struct gc_arena gc =3D gc_new(); + struct hash *hash =3D hash_init(10000, get_random(), word_hash_functio= n, word_compare_function); + struct hash *nhash =3D hash_init(256, get_random(), word_hash_function= , word_compare_function); + + printf("hash_init n_buckets=3D%d mask=3D0x%08x\n", hash->n_buckets, ha= sh->mask); + + char wordfile[PATH_MAX] =3D { 0 }; + openvpn_test_get_srcdir_dir(wordfile, PATH_MAX, "/../../../COPYRIGHT.G= PL" ); + + FILE *words =3D fopen(wordfile, "r"); + assert_non_null(words); + + int wordcount =3D 0; + + /* parse words from file */ + while (true) + { + char buf[256]; + char wordbuf[256]; + + if (!fgets(buf, sizeof(buf), words)) + { + break; + } + + char c =3D 0; + int bi =3D 0, wbi =3D 0; + + do + { + c =3D buf[bi++]; + if (isalnum(c) || c =3D=3D '_') + { + assert_true(wbi < (int) sizeof(wordbuf)); + wordbuf[wbi++] =3D c; + } + else + { + if (wbi) + { + wordcount++; + + ASSERT(wbi < (int) sizeof(wordbuf)); + wordbuf[wbi++] =3D '\0'; + + /* word is parsed from stdin */ + + /* does it already exist in table? */ + struct word *w =3D (struct word *) hash_lookup(hash, w= ordbuf); + + if (w) + { + assert_string_equal(w->word, wordbuf); + /* yes, increment count */ + ++w->n; + } + else + { + /* no, make a new object */ + ALLOC_OBJ_GC(w, struct word, &gc); + w->word =3D string_alloc(wordbuf, &gc); + w->n =3D 1; + assert_true(hash_add(hash, w->word, w, false)); + assert_true(hash_add(nhash, w->word, (void *) ((pt= r_type )(random() & 0x0F) + 1), false)); + } + } + wbi =3D 0; + } + } + while (c); + } + + assert_int_equal(wordcount, 2978); + + /* remove some words from the table */ + { + assert_true(hash_remove(hash, "DEFECTIVE")); + assert_false(hash_remove(hash, "false")); + } + + /* output contents of hash table */ + { + ptr_type inc =3D 0; + int count =3D 0; + + for (ptr_type base =3D 0; base < hash_n_buckets(hash); base +=3D i= nc) + { + struct hash_iterator hi; + struct hash_element *he; + inc =3D (get_random() % 3) + 1; + hash_iterator_init_range(hash, &hi, base, base + inc); + + while ((he =3D hash_iterator_next(&hi))) + { + struct word *w =3D (struct word *) he->value; + /*printf("%6d '%s'\n", w->n, w->word); */ + ++count; + /* check a few words to match prior results */ + if (!strcmp(w->word, "is")) + { + assert_int_equal(w->n, 49); + } + else if (!strcmp(w->word, "redistribute")) + { + assert_int_equal(w->n, 5); + } + else if (!strcmp(w->word, "circumstances")) + { + assert_int_equal(w->n, 1); + } + else if (!strcmp(w->word, "so")) + { + assert_int_equal(w->n, 8); + } + else if (!strcmp(w->word, "BECAUSE")) + { + assert_int_equal(w->n, 1); + } + } + + hash_iterator_free(&hi); + } + assert_int_equal(count, hash_n_elements(hash)); + } + + /* test hash_remove_by_value function */ + { + for (ptr_type i =3D 1; i <=3D 16; ++i) + { + struct hash_element *item =3D hash_lookup_by_value(nhash, (voi= d *) i); + hash_remove_by_value(nhash, (void *) i); + /* check item got removed if it was present before */ + if (item) + { + assert_null(hash_lookup_by_value(nhash, (void *) i)); + } + } + } + + hash_free(hash); + hash_free(nhash); + gc_free(&gc); +} + + 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) }; int -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/446?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: I7511bc43cd6a0bcb89476f27d5822ab4a78d0d21 Gerrit-Change-Number: 446 Gerrit-PatchSet: 5 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --fTAdqguFjSk= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos uploaded patch set #5 to this change.

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

Turn dead list test code into unit test

Change-Id: I7511bc43cd= 6a0bcb89476f27d5822ab4a78d0d21
Signed-off-by: Arne Schwabe <arne@...1386...= 227...>
---
M CMakeLists.txt
M src/openvpn/init.c
M src/open= vpn/list.c
M src/openvpn/list.h
M tests/unit_tests/openvpn/Makefile.a= m
M tests/unit_tests/openvpn/test_misc.c
6 files changed, 211 inserti= ons(+), 193 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/=
openvpn refs/changes/46/446/5
diff --git a/CMakeLists.txt b/CMakeLi=
sts.txt
index fdd2b01..3127611 100644
--- a= /CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74= 7,6 +747,7 @@
tests/unit_tests/openvpn/mock_get_ra= ndom.c
src/openvpn/options_util.c
= src/openvpn/ssl_util.c
+ src/openvpn/list.c
)
target_sources(test_ncp PRIVATE
diff --git a/src/openvpn/init.c b/src/openvpn/init.c
ind= ex c5cc154..52b4308 100644
--- a/src/openvpn/init.c<= br>+++ b/src/openvpn/init.c
@@ -865,11 +865,6 @@
return false;
#endif

-#ifdef LIST_TEST<= br>- list_test();
- return false;

-#endif
-
#ifdef IFCONFIG_POOL_TEST ifconfig_pool_test(0x0A010004, 0x0A0100FF);
= return false;
diff --git a/src/openvpn/list.c b/src/openv= pn/list.c
index 480f39d..dc4b1df 100644
---= a/src/openvpn/list.c
+++ b/src/openvpn/list.c
@@ -326,185 +326,6 @@

}


-#ifdef LIST_TEST=
-
-/*
- * Test the hash code by implementing a simple
- * word frequency algorithm.<= br>- */
-
-struct word
-{
- const char *word;
- int n;
-};

-
= -static uint32_t
-word_= hash_function(const void *key, uint32_t iv)
-{
- const char *str =3D (const char *) key;
- const int len =3D strlen(str);
- return hash_func((const uint8_t *)= str, len, iv);
-}
-
-static bool
-word_compare_function(const void *key1, const void *key2)<= /span>
-{
- return strcmp((const char *)key1, (con= st char *)key2) =3D=3D 0;
-}
-
-static void

-print_nhash(struct hash *hash)
-{
- struct hash_iterator hi;
- struct hash_element *he;
- int count =3D 0;
-
- hash_iterator_init(hash, &hi, true);
-
- while ((he =3D hash_iterator_next(&hi)))
- {

- printf("%d ", (int) he->value)= ;
- ++count;

- }
- printf("\n");
-
- hash_iterator_free(&hi);
- ASSERT(count =3D=3D hash_n_elements(has= h));
-}
-
-static void
-rmhash(struct hash *hash, const char *word)
-{
- hash_remove(hash, word);
-}
-

-void
-list_test(void)
-{
- openvpn_thread_init();
-
- {
- struct gc_arena g= c =3D gc_new();
- = struct hash *hash =3D hash_init(10000, get_random(), word_hash_function, = word_compare_function);
- struct hash *nhash =3D hash_init(256, get_random(), word_hash_fun= ction, word_compare_function);
-
- prin= tf("hash_init n_buckets=3D%d mask=3D0x%08x\n", hash->n_buckets= , hash->mask);
-
- /* parse words fr= om stdin */
- wh= ile (true)
- {
- char buf[25= 6];
- char w= ordbuf[256];
- = int wbi;
- = int bi;
- = char c;
-
- if (!fgets(buf, sizeof(= buf), stdin))

- = {
- = break;
- }
-
- bi =3D wbi =3D 0;
- do

- {
- c =3D buf[bi++];
- if (isalnum(c) || c =3D= =3D '_')
- = {
- = ASSERT(wbi < (int) sizeof(wordbuf));
- wordbuf[wbi++] =3D c;
- }- else- {
- if (wbi)
=
- {=
- stru= ct word *w;
- = ASSERT(wbi < (int) sizeof(wordbuf));
- wordbuf[wbi++] =3D = '\0';
-<= br>- /* wo= rd is parsed from stdin */
-
- = /* does it already exist in table? */
- w =3D (struct word *) hash_= lookup(hash, wordbuf);
= -
- = if (w)
- = {
- = /* yes, increment count */
- ++w->n;
= - }=
- else=
- = {
- = /* no, make a new object */
- ALLOC_OBJ_GC(w, struct word, &= amp;gc);
- = w->word =3D string_alloc(wordbuf, &gc);
- w->n= =3D 1;

- = ASSERT(hash_add(hash, w->word, w, false));
- ASSERT(ha= sh_add(nhash, w->word, (void *) ((random() & 0x0F) + 1), false));
- = }
- = }
- = wbi =3D 0;
- = }
- } w= hile (c);
- }
-
-#if 1
- /* remove some words from the table */
- {

- rmhash(hash, "true");
- rmhash(hash,= "false");
- = }
-#endif=
-
- /* output contents of hash table */

- {
- int base;
- int inc =3D 0;
- int count =3D 0;
-
- for (base =3D 0; base < hash_n_bu= ckets(hash); base +=3D inc)
- {
- = struct hash_iterator hi;
- struct hash_element *he;
- inc =3D (get_random() %= 3) + 1;
- = hash_iterator_init_range(hash, &hi, true, base, base + inc);<= br>-
- while ((he =3D hash_iterator_next(= &hi)))
- = {
- = struct word *w =3D (struct word *) he->value;
- printf("%6d '%= s'\n", w->n, w->word);
- ++count;
- }
-
- = hash_iterator_free(&hi);
- }
- ASSERT(count =3D=3D hash_n_elements(hash));=
- }
-
-#if 1
- = /* test hash_remove_by_value function */
- {
- int i;
- for (i =3D 1; i <=3D 16; ++i)
- {
- printf("[%d] ****************= *******************\n", i);
- print_nhash(nhash);
- hash_remove_by_value(nhash, (void = *) i, true);
- = }
- print= f("FINAL **************************\n");
- print_nhash(nhash);
- }

-#endif
-
- h= ash_free(hash);
- = hash_free(nhash);
- = gc_free(&gc);
- }
-
- openvpn_thread_cleanup();
-}
-
-#endif /* ifdef LIST_TEST */
-
/*
* --------------------= ------------------------------------------------
* hash()= -- hash a variable-length key into a 32-bit value
diff --g= it a/src/openvpn/list.h b/src/openvpn/list.h
index 94d14f2.= .18afc54 100644
--- a/src/openvpn/list.h
++= + b/src/openvpn/list.h
@@ -33,8 +33,6 @@
= * client instances over various key spaces.
*/
=
-/* define this= to enable special list test mode */
-/*#define LIST_TEST*/

#in= clude "basic.h"
#include "buffer.h"
@@ -114,11 +112,6 @@

uint3= 2_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval);<= br>
-#ifdef LIST= _TEST
-void list_test(v= oid);
-
-#endif

-
static inline uint32_t
<= span> hash_value(const struct hash *hash, const void *key)
= {
diff --git a/tests/unit_tests/openvpn/Makefile.am b/test= s/unit_tests/openvpn/Makefile.am
index 88a694d..dfb9f6a 100= 644
--- a/tests/unit_tests/openvpn/Makefile.am
+++ b/tests/unit_tests/openvpn/Makefile.am
@@ -288,7 +2= 88,8 @@
$(top_srcdir)/src/openvpn/ssl_util.c

misc_testdriver_CFLAGS =3D @TEST_CFLAGS@ \- -I$(top_srcdir)/include -I$(t= op_srcdir)/src/compat -I$(top_srcdir)/src/openvpn
+ -I$(top_srcdir)/include -I$(top_srcdir)/src/= compat -I$(top_srcdir)/src/openvpn \
+ -DSOURCEDIR=3D\"$(top_srcdir)\"

misc_testdriver_LDFLAGS =3D @TEST_LDFLAGS@

@@ -298,4 +299,6 @@
$(top_srcdir)/s= rc/openvpn/options_util.c \
$(top_srcdir)/src/openvpn/ssl= _util.c \
$(top_srcdir)/src/openvpn/win32-util.c \=
- $(top_srcdir)/src/openvpn/p= latform.c
+ $(top_src= dir)/src/openvpn/platform.c \
+ $(top_srcdir)/src/openvpn/list.c
+
diff --git a/tests/unit_tests/open= vpn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c
inde= x 193f131..04dbd5a 100644
--- a/tests/unit_tests/openvpn/te= st_misc.c
+++ b/tests/unit_tests/openvpn/test_misc.c=
@@ -37,6 +37,7 @@
#include "ssl_util.h"= ;
#include "options_util.h"
#in= clude "test_common.h"
+#include "list.h"

= static void
test_compat_lzo_string(void **state)@@ -108,11 +109,215 @@
assert_int_equal(o.serve= r_backoff_time, 77);
}

+
+
+= struct word
+{=
+ const char *word;
+ int n;
+};

+
+
+static uint32= _t
+word_hash_functio= n(const void *key, uint32_t iv)
+{
+ co= nst char *str =3D (const char *) key;
+ const int len =3D strlen(str);
+ return hash_func((const uint8_t *)str= , len, iv);
+}=
+
+static bool
+word_compare_function(const void *key1, const void *k= ey2)
+{
+ return strcmp((const char *)k= ey1, (const char *)key2) =3D=3D 0;
+}
+

+static unsigned long
+get_random(void)
+{
+ /* rand() is not very random, but it&= #39;s C99 and this is just for testing */
+ return rand();
+}
+static struct ha= sh_element *
+hash_lo= okup_by_value(struct hash *hash, void *value)
+{
+ struct hash_iterator hi;
+ struct hash_element *he;
+ struct hash_element *ret =3D NULL;
= + hash_iterator_init(hash, &= amp;hi);
+
= + while ((he =3D hash_iterat= or_next(&hi)))
+ = {
+ if (he-= >value =3D=3D value)
+ {
+ = ret =3D he;
+ = }
+ }=
+ hash_iterator_free(&am= p;hi);
+ return re= t;
+}
+

+static void
+test_list(void **state)
+{
+<= /span>
+/*
+ * Test the hash code by implementing= a simple
+ * word fr= equency algorithm.
+ = */
+
+ struct gc_arena gc =3D gc_new();=
+ struct hash *ha= sh =3D hash_init(10000, get_random(), word_hash_function, word_compare_func= tion);
+ struct ha= sh *nhash =3D hash_init(256, get_random(), word_hash_function, word_compare= _function);
+<= br>+ printf("hash_init = n_buckets=3D%d mask=3D0x%08x\n", hash->n_buckets, hash->mask);
+
+ char wordfile[PATH_MAX] =3D { 0 };
+ openvpn_test_get_= srcdir_dir(wordfile, PATH_MAX, "/../../../COPYRIGHT.GPL" );

+
+ FILE *words =3D fopen(wordfile, "r&= quot;);
+ assert_n= on_null(words);
+

+ int wordcount =3D 0= ;
+
+ /* parse words from file */
+ while (true)+ {
+ char buf[256];
+ char wordbuf[256];
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ if (!fgets(buf, sizeof(buf), words))
+ {
+ break;

+ }
+
+ char c =3D 0;
+ int bi =3D 0, wbi =3D 0;
+
+ do
+ = {
+ c= =3D buf[bi++];
+ = if (isalnum(c) || c =3D=3D '_')
+ {
+ assert_true(wbi < (int) sizeof(wor= dbuf));
+ = wordbuf[wbi++] =3D c;
+ }
+= else
+ = {
+ = if (wbi)
+ = {
+ = wordcount++;
+
+ = ASSERT(wbi < (int) sizeof(wordbuf));
+ wordbuf[wbi++] =3D '\0&= #39;;
+
+ /* word is pa= rsed from stdin */
+<= /span>
+ = /* does it already exist in table? */
+ struct word *w =3D (struct word *) ha= sh_lookup(hash, wordbuf);
+
+ = if (w)
+ = {
+ = assert_string_equal(w->word, wordbuf);
+ /* yes, i= ncrement count */

+ = ++w->n;
+ }
+ else
+ {
+ /* no, make a new object */
+ = ALLOC_OBJ_GC(w, struct word, &gc);
+ w->word =3D string_alloc(w= ordbuf, &gc);
+ = w->n =3D 1;
+ assert_true(hash_add(hash, w->w= ord, w, false));
+ = assert_true(hash_add(nhash, w->word, (void *) ((ptr= _type )(random() & 0x0F) + 1), false));
+ }
+ }
+ wbi =3D 0;
+ }
+ }
+ while (c);
+ }
+=
+ assert_int_equa= l(wordcount, 2978);
+=
+ /* remove some = words from the table */
+ {
+ as= sert_true(hash_remove(hash, "DEFECTIVE"));
+ assert_false(hash_remove(hash, &q= uot;false"));
+ = }
+
+ /* output contents of hash tab= le */

+ {+ ptr_type inc =3D 0;<= /span>
+ int count = =3D 0;
+
+ for (ptr_type base =3D 0= ; base < hash_n_buckets(hash); base +=3D inc)

+ {
+ struct hash_iterator hi;
+ struct hash_element *he;
+ inc =3D (g= et_random() % 3) + 1;
+ hash_iterator_init_range(hash, &hi, base, base + inc);
+
+ while ((he =3D hash_iterator_= next(&hi)))
+ = {
+ = struct word *w =3D (struct word *) he->value;
+ /*printf("%6d '= %s'\n", w->n, w->word); */
+ ++count;
+ /* check a few words to match pr= ior results */
+ = if (!strcmp(w->word, "is"))
+ {
+ assert_int_equal(w-&g= t;n, 49);
+ = }
+ = else if (!strcmp(w->word, "redistribute"))
+ {

+ assert_int_equal= (w->n, 5);
+ = }
+ = else if (!strcmp(w->word, "circumstances"))+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert_int_= equal(w->n, 1);

+ = }
+ = else if (!strcmp(w->word, "so"))
+ {
+ assert_int_equal(= w->n, 8);
+ = }
+ = else if (!strcmp(w->word, "BECAUSE"))
+ {
+ assert_int_equal(w= ->n, 1);
+ = }
+ = }
+
+ hash_iterator_free(&= ;hi);
+ }
+ assert_int_equal= (count, hash_n_elements(hash));
+ }
+
+ /* test hash_remo= ve_by_value function */
+ {
+ fo= r (ptr_type i =3D 1; i <=3D 16; ++i)
+ {
+ struct hash_element *item =3D hash_lookup_by_value(nh= ash, (void *) i);
+ = hash_remove_by_value(nhash, (void *) i);
+ /* check item got removed if it = was present before */
+ if (item)
+ {
+ = assert_null(hash_lookup_by_value(nhash, (void *) i));<= br>+ }
+ }

+ }
+
= + hash_free(hash);
+ hash_free(nhash);
+ gc_free(&gc);
+}
+
<= span style=3D"color: hsl(120, 100%, 40%);">+

const struct = CMUnitTest misc_tests[] =3D {
cmocka_unit_test(test_co= mpat_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(t= est_list)
};

int=

To view, visit change 446. To unsubscribe, or for = help writing mail filters, visit settings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I7511bc43cd6a0bcb89476f27d58= 22ab4a78d0d21
Gerrit-Change-Number: 446
Gerrit-PatchSet: 5
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: newpatchset
--fTAdqguFjSk=--