OpenVPN
tests
unit_tests
plugins
auth-pam
test_search_and_replace.c
Go to the documentation of this file.
1
#include <stdio.h>
2
#include <unistd.h>
3
#include <stdlib.h>
4
#include <stdarg.h>
5
#include <string.h>
6
#include <setjmp.h>
7
#include <cmocka.h>
8
9
#include "
utils.h
"
10
11
static
void
12
pass_any_null_param__returns_null
(
void
**state)
13
{
14
15
char
DUMMY[] =
"DUMMY"
;
16
17
assert_null(
searchandreplace
(NULL, DUMMY, DUMMY));
18
assert_null(
searchandreplace
(DUMMY, NULL, DUMMY));
19
assert_null(
searchandreplace
(DUMMY, DUMMY, NULL));
20
}
21
22
static
void
23
pass_any_empty_string__returns_null
(
void
**state)
24
{
25
26
char
DUMMY[] =
"DUMMY"
;
27
char
EMPTY[] =
""
;
28
29
assert_null(
searchandreplace
(EMPTY, DUMMY, DUMMY));
30
assert_null(
searchandreplace
(DUMMY, EMPTY, DUMMY));
31
assert_null(
searchandreplace
(DUMMY, DUMMY, EMPTY));
32
}
33
34
static
void
35
replace_single_char__one_time__match_is_replaced
(
void
**state)
36
{
37
char
*replaced =
searchandreplace
(
"X"
,
"X"
,
"Y"
);
38
39
assert_non_null(replaced);
40
assert_string_equal(
"Y"
, replaced);
41
42
free(replaced);
43
}
44
45
static
void
46
replace_single_char__multiple_times__match_all_matches_are_replaced
(
void
**state)
47
{
48
char
*replaced =
searchandreplace
(
"XaX"
,
"X"
,
"Y"
);
49
50
assert_non_null(replaced);
51
assert_string_equal(
"YaY"
, replaced);
52
53
free(replaced);
54
}
55
56
static
void
57
replace_longer_text__multiple_times__match_all_matches_are_replaced
(
void
**state)
58
{
59
char
*replaced =
searchandreplace
(
"XXaXX"
,
"XX"
,
"YY"
);
60
61
assert_non_null(replaced);
62
assert_string_equal(
"YYaYY"
, replaced);
63
64
free(replaced);
65
}
66
67
static
void
68
pattern_not_found__returns_original
(
void
**state)
69
{
70
char
*replaced =
searchandreplace
(
"abc"
,
"X"
,
"Y"
);
71
72
assert_non_null(replaced);
73
assert_string_equal(
"abc"
, replaced);
74
75
free(replaced);
76
}
77
78
79
int
80
main
(
void
)
81
{
82
const
struct
CMUnitTest tests[] = {
83
cmocka_unit_test(
pass_any_null_param__returns_null
),
84
cmocka_unit_test(
pass_any_empty_string__returns_null
),
85
cmocka_unit_test(
replace_single_char__one_time__match_is_replaced
),
86
cmocka_unit_test(
replace_single_char__multiple_times__match_all_matches_are_replaced
),
87
cmocka_unit_test(
replace_longer_text__multiple_times__match_all_matches_are_replaced
),
88
cmocka_unit_test(
pattern_not_found__returns_original
),
89
};
90
91
return
cmocka_run_group_tests_name(
"searchandreplace"
, tests, NULL, NULL);
92
}
replace_single_char__multiple_times__match_all_matches_are_replaced
static void replace_single_char__multiple_times__match_all_matches_are_replaced(void **state)
Definition:
test_search_and_replace.c:46
pass_any_empty_string__returns_null
static void pass_any_empty_string__returns_null(void **state)
Definition:
test_search_and_replace.c:23
utils.h
main
int main(void)
Definition:
test_search_and_replace.c:80
pass_any_null_param__returns_null
static void pass_any_null_param__returns_null(void **state)
Definition:
test_search_and_replace.c:12
searchandreplace
char * searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
Read 'tosearch', replace all occurrences of 'searchfor' with 'replacewith' and return a pointer to th...
Definition:
utils.c:43
pattern_not_found__returns_original
static void pattern_not_found__returns_original(void **state)
Definition:
test_search_and_replace.c:68
replace_single_char__one_time__match_is_replaced
static void replace_single_char__one_time__match_is_replaced(void **state)
Definition:
test_search_and_replace.c:35
replace_longer_text__multiple_times__match_all_matches_are_replaced
static void replace_longer_text__multiple_times__match_all_matches_are_replaced(void **state)
Definition:
test_search_and_replace.c:57
Generated by
1.8.17