55 "V4,dev-type tun,link-mtu 1457,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
61 "V4,dev-type tun,link-mtu 1458,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server,comp-lzo");
65 "V4,dev-type tun,link-mtu 2,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
69 assert_string_equal(input, output);
72 input =
"V4,dev-type tun";
75 assert_string_equal(input, output);
79 "V4,dev-type tun,link-mtu 999,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server";
85 "V4,dev-type tun,link-mtu 1000,tun-mtu 1400,proto UDPv4,auth SHA1,keysize 128,key-method 2,tls-server,comp-lzo");
95 const char *teststr =
"TEMP:There are no flags here [really not]";
98 assert_string_equal(
msg,
"There are no flags here [really not]");
106 const char *teststr =
"[backoff 42,advance no]";
109 assert_string_equal(
msg,
"");
119 const char *teststr =
"[advance remote,backoff 77]:go round and round";
122 assert_string_equal(
msg,
"go round and round");
137 const char *str = (
const char *)
key;
138 const uint32_t len = (uint32_t)strlen(str);
145 return strcmp((
const char *)key1, (
const char *)
key2) == 0;
180 char wordfile[PATH_MAX] = { 0 };
183 FILE *words = fopen(wordfile,
"r");
184 assert_non_null(words);
194 if (!fgets(buf,
sizeof(buf), words))
205 if (isalnum(c) || c ==
'_')
207 assert_true(wbi < (
int)
sizeof(wordbuf));
216 ASSERT(wbi < (
int)
sizeof(wordbuf));
217 wordbuf[wbi++] =
'\0';
226 assert_string_equal(w->
word, wordbuf);
246 assert_int_equal(wordcount, 2971);
272 if (!strcmp(w->
word,
"is"))
274 assert_int_equal(w->
n, 49);
276 else if (!strcmp(w->
word,
"redistribute"))
278 assert_int_equal(w->
n, 5);
280 else if (!strcmp(w->
word,
"circumstances"))
282 assert_int_equal(w->
n, 1);
284 else if (!strcmp(w->
word,
"so"))
286 assert_int_equal(w->
n, 8);
288 else if (!strcmp(w->
word,
"BECAUSE"))
290 assert_int_equal(w->
n, 1);
349 assert_int_equal(
atoi_warn(
"1234", msglevel), 1234);
350 assert_int_equal(
atoi_warn(
"0", msglevel), 0);
351 assert_int_equal(
atoi_warn(
"-1194", msglevel), -1194);
354 assert_true(
atoi_constrained(
"1234", ¶meter,
"test", 0, INT_MAX, msglevel));
355 assert_int_equal(parameter, 1234);
357 assert_int_equal(parameter, 0);
358 assert_true(
atoi_constrained(
"-1194", ¶meter,
"test", INT_MIN, INT_MAX, msglevel));
359 assert_int_equal(parameter, -1194);
361 int64_t parameter64 = 0;
362 assert_true(
positive_atoll(
"1234", ¶meter64,
"test", msglevel));
363 assert_int_equal(parameter64, 1234);
365 assert_int_equal(parameter64, 0);
366 assert_true(
positive_atoll(
"2147483653", ¶meter64,
"test", msglevel));
367 assert_int_equal(parameter64, 2147483653);
369 assert_true(
positive_atoll(
"9223372036854775810", ¶meter64,
"test", msglevel));
370 assert_int_equal(parameter64, 9223372036854775807);
374 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '-1234' as non-negative integer");
379 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '2147483653' as non-negative integer");
382 assert_int_equal(
atoi_warn(
"2147483653", msglevel), 0);
383 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '2147483653' as integer");
387 assert_false(
atoi_constrained(
"2147483653", ¶meter,
"test", 0, INT_MAX, msglevel));
388 assert_string_equal(
mock_msg_buf,
"test: Cannot parse '2147483653' as integer");
389 assert_int_equal(parameter, -42);
393 assert_string_equal(
mock_msg_buf,
"Cannot parse argument 'foo77' as non-negative integer");
397 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '77foo' as non-negative integer");
401 assert_false(
atoi_constrained(
"foo77", ¶meter,
"test", 0, INT_MAX, msglevel));
402 assert_string_equal(
mock_msg_buf,
"test: Cannot parse 'foo77' as integer");
403 assert_int_equal(parameter, -42);
407 assert_false(
atoi_constrained(
"77foo", ¶meter,
"test", 0, INT_MAX, msglevel));
408 assert_string_equal(
mock_msg_buf,
"test: Cannot parse '77foo' as integer");
409 assert_int_equal(parameter, -42);
412 assert_int_equal(
atoi_warn(
"foo77", msglevel), 0);
413 assert_string_equal(
mock_msg_buf,
"Cannot parse argument 'foo77' as integer");
416 assert_int_equal(
atoi_warn(
"77foo", msglevel), 0);
417 assert_string_equal(
mock_msg_buf,
"Cannot parse argument '77foo' as integer");
423 assert_string_equal(
mock_msg_buf,
"test: Must be an integer between 0 and 76, not 77");
424 assert_int_equal(parameter, -42);
428 assert_false(
atoi_constrained(
"-77", ¶meter,
"test", -76, 76, msglevel));
429 assert_string_equal(
mock_msg_buf,
"test: Must be an integer between -76 and 76, not -77");
430 assert_int_equal(parameter, -42);
434 assert_false(
atoi_constrained(
"-77", ¶meter,
"test", 0, INT_MAX, msglevel));
435 assert_string_equal(
mock_msg_buf,
"test: Must be an integer >= 0, not -77");
436 assert_int_equal(parameter, -42);
440 assert_false(
atoi_constrained(
"0", ¶meter,
"test", 1, INT_MAX, msglevel));
441 assert_string_equal(
mock_msg_buf,
"test: Must be an integer >= 1, not 0");
442 assert_int_equal(parameter, -42);
452 assert_true(
win_path_in_dir(L
"C:\\openvpn_plugins\\foo.dll", L
"C:\\openvpn_plugins"));
455 assert_false(
win_path_in_dir(L
"C:\\openvpn_plugins_evil\\foo.dll", L
"C:\\openvpn_plugins"));
458 assert_true(
win_path_in_dir(L
"C:\\openvpn_plugins\\foo.dll", L
"C:\\openvpn_plugins\\"));
459 assert_false(
win_path_in_dir(L
"C:\\openvpn_plugins_evil\\foo.dll", L
"C:\\openvpn_plugins\\"));
462 assert_true(
win_path_in_dir(L
"C:\\openvpn_plugins/foo.dll", L
"C:\\openvpn_plugins"));
465 assert_true(
win_path_in_dir(L
"c:\\OPENVPN_PLUGINS\\foo.dll", L
"C:\\openvpn_plugins"));
468 assert_false(
win_path_in_dir(L
"C:\\openvpn_plugins", L
"C:\\openvpn_plugins"));
471 assert_true(
win_path_in_dir(L
"C:\\openvpn_plugins\\sub\\foo.dll", L
"C:\\openvpn_plugins"));
495 return cmocka_run_group_tests(
misc_tests, NULL, NULL);
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
#define ALLOC_OBJ_GC(dptr, type, gc)
Allocate a garbage-collected object of the given type (uninitialised).
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
Data Channel Cryptography Module.
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 uint32_t n_buckets, uint64_t(*hash_function)(const void *key, const uint8_t hash_key[HASH_KEY_LEN]), bool(*compare_function)(const void *key1, const void *key2))
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)
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, uint32_t start_bucket, uint32_t end_bucket)
static bool hash_remove(struct hash *hash, const void *key)
static void * hash_lookup(struct hash *hash, const void *key)
static uint32_t hash_n_elements(const struct hash *hash)
static uint32_t hash_n_buckets(const struct hash *hash)
msglvl_t mock_get_debug_level(void)
void mock_set_debug_level(msglvl_t level)
Mock debug level defaults to 0, which gives clean(-ish) test reports.
char mock_msg_buf[MOCK_MSG_BUF]
int atoi_warn(const char *str, msglvl_t msglevel)
Converts a str to an integer if the string can be represented as an integer number.
int positive_atoi(const char *str, msglvl_t msglevel)
Converts a str to a positive number if the string represents a postive integer number.
bool positive_atoll(const char *str, int64_t *value, const char *name, msglvl_t msglevel)
Converts a str to an integer if the string can be represented as an integer number and is >= 0.
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.
bool atoi_constrained(const char *str, int *value, const char *name, int min, int max, msglvl_t msglevel)
Converts a str to an integer if the string can be represented as an integer number and is between min...
uint64_t siphash_hash_func(const uint8_t *k, uint32_t length, const uint8_t hash_key[SIPHASH_KEY_SIZE])
Wrapper of the siphash function to be able to use it in the hash map.
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...
static 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 uint64_t word_hash_function(const void *key, const uint8_t hash_key[HASH_KEY_LEN])
static void test_win_path_in_dir(void **state)
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 void test_list(void **state)
static void test_auth_fail_temp_no_flags(void **state)
const struct CMUnitTest misc_tests[]
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)
void schedule_test(void **state)
Runs the schedule test.
bool win_path_in_dir(const WCHAR *path, const WCHAR *dir)
Check whether path resides within directory dir.