48 const char *input =
"V4,dev-type tun,link-mtu 1457,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
52 assert_string_equal(output,
"V4,dev-type tun,link-mtu 1458,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server,comp-lzo");
55 input =
"V4,dev-type tun,link-mtu 2,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
59 assert_string_equal(input, output);
62 input =
"V4,dev-type tun";
65 assert_string_equal(input, output);
68 input =
"V4,dev-type tun,link-mtu 999,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
72 assert_string_equal(output,
"V4,dev-type tun,link-mtu 1000,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server,comp-lzo");
82 const char *teststr =
"TEMP:There are no flags here [really not]";
85 assert_string_equal(
msg,
"There are no flags here [really not]");
93 const char *teststr =
"[backoff 42,advance no]";
96 assert_string_equal(
msg,
"");
106 const char *teststr =
"[advance remote,backoff 77]:go round and round";
109 assert_string_equal(
msg,
"go round and round");
125 const char *str = (
const char *)
key;
126 const int len = strlen(str);
127 return hash_func((
const uint8_t *)str, len, iv);
133 return strcmp((
const char *)
key1, (
const char *)
key2) == 0;
177 char wordfile[PATH_MAX] = { 0 };
180 FILE *words = fopen(wordfile,
"r");
181 assert_non_null(words);
191 if (!fgets(buf,
sizeof(buf), words))
202 if (isalnum(c) || c ==
'_')
204 assert_true(wbi < (
int)
sizeof(wordbuf));
213 ASSERT(wbi < (
int)
sizeof(wordbuf));
214 wordbuf[wbi++] =
'\0';
223 assert_string_equal(w->
word, wordbuf);
243 assert_int_equal(wordcount, 2978);
269 if (!strcmp(w->
word,
"is"))
271 assert_int_equal(w->
n, 49);
273 else if (!strcmp(w->
word,
"redistribute"))
275 assert_int_equal(w->
n, 5);
277 else if (!strcmp(w->
word,
"circumstances"))
279 assert_int_equal(w->
n, 1);
281 else if (!strcmp(w->
word,
"so"))
283 assert_int_equal(w->
n, 8);
285 else if (!strcmp(w->
word,
"BECAUSE"))
287 assert_int_equal(w->
n, 1);
338 int msglevel =
D_LOW;
346 assert_int_equal(
atoi_warn(
"1234", msglevel), 1234);
347 assert_int_equal(
atoi_warn(
"0", msglevel), 0);
348 assert_int_equal(
atoi_warn(
"-1194", msglevel), -1194);
352 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '-1234' as non-negative integer");
357 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '2147483653' as non-negative integer");
360 assert_int_equal(
atoi_warn(
"2147483653", msglevel), 0);
361 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '2147483653' as integer");
365 assert_string_equal(
mock_msg_buf,
"Cannot parse argument 'foo77' as non-negative integer");
369 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '77foo' as non-negative integer");
372 assert_int_equal(
atoi_warn(
"foo77", msglevel), 0);
373 assert_string_equal(
mock_msg_buf,
"Cannot parse argument 'foo77' as integer");
376 assert_int_equal(
atoi_warn(
"77foo", msglevel), 0);
377 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '77foo' as integer");
395 return cmocka_run_group_tests(
misc_tests, NULL, NULL);
char * string_alloc(const char *str, struct gc_arena *gc)
#define ALLOC_OBJ_GC(dptr, type, gc)
static void gc_free(struct gc_arena *a)
static struct gc_arena gc_new(void)
static const char *const key1
void hash_iterator_free(struct hash_iterator *hi)
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
void hash_iterator_init(struct hash *hash, struct hash_iterator *hi)
struct hash * hash_init(const int n_buckets, const uint32_t iv, uint32_t(*hash_function)(const void *key, uint32_t iv), bool(*compare_function)(const void *key1, const void *key2))
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket, int end_bucket)
uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
void hash_free(struct hash *hash)
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
void hash_remove_by_value(struct hash *hash, void *value)
static bool hash_remove(struct hash *hash, const void *key)
static void * hash_lookup(struct hash *hash, const void *key)
static int hash_n_elements(const struct hash *hash)
static int hash_n_buckets(const struct hash *hash)
int mock_get_debug_level(void)
char mock_msg_buf[MOCK_MSG_BUF]
void mock_set_debug_level(int level)
Mock debug level defaults to 0, which gives clean(-ish) test reports.
int positive_atoi(const char *str, int msglevel)
Converts a str to a positive number if the string represents a postive integer number.
int atoi_warn(const char *str, int msglevel)
Converts a str to an integer if the string can be represented as an integer number.
const char * parse_auth_failed_temp(struct options *o, const char *reason)
bool valid_integer(const char *str, bool positive)
Checks if the string is a valid integer by checking if it can be converted to an integer.
const char * options_string_compat_lzo(const char *options, struct gc_arena *gc)
Takes a locally produced OCC string for TLS server mode and modifies as if the option comp-lzo was en...
Garbage collection arena used to keep track of dynamically allocated memory.
Container for bidirectional cipher and HMAC key material.
Container for unidirectional cipher and HMAC key material.
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
void openvpn_test_get_srcdir_dir(char *buf, size_t bufsize, const char *filename)
Helper function to get a file path from the unit test directory to open it or pass its path to anothe...
static void test_atoi_variants(void **state)
static void test_auth_fail_temp_flags(void **state)
static void test_compat_lzo_string(void **state)
static uint32_t word_hash_function(const void *key, uint32_t iv)
static void test_list(void **state)
static void test_auth_fail_temp_no_flags(void **state)
const struct CMUnitTest misc_tests[]
static unsigned long get_random(void)
static bool word_compare_function(const void *key1, const void *key2)
static struct hash_element * hash_lookup_by_value(struct hash *hash, void *value)
static void test_auth_fail_temp_flags_msg(void **state)