This repository was archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcheck-inifile.c
More file actions
139 lines (112 loc) · 4.3 KB
/
check-inifile.c
File metadata and controls
139 lines (112 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* This file is part of libnica.
*
* Copyright © 2016-2017 Intel Corporation
*
* libnica is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*/
#define _GNU_SOURCE
#include <check.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "hashmap.c"
#include "inifile.c"
START_TEST(nc_inifile_open_test)
{
autofree(NcHashmap) *f = NULL;
const char *t_path = TOP_DIR "/tests/ini/wellformed.ini";
f = nc_ini_file_parse(t_path);
fail_if(f == NULL, "Failed to parse wellformed.ini");
}
END_TEST
START_TEST(nc_inifile_good_test)
{
const char *wellformed[] = { TOP_DIR "/tests/ini/wellformed.ini",
TOP_DIR "/tests/ini/valid_padding.ini",
TOP_DIR "/tests/ini/escaped_chars.ini" };
for (size_t i = 0; i < sizeof(wellformed) / sizeof(wellformed[0]); i++) {
autofree(NcHashmap) *f = NULL;
char *ret = NULL;
f = nc_ini_file_parse(wellformed[i]);
fail_if(f == NULL, "Failed to parse wellformed.ini");
fail_if(!nc_hashmap_contains(f, "John"), "INI File missing \"John\" section");
ret = nc_hashmap_get(nc_hashmap_get(f, "John"), "alive");
fail_if(!ret, "Failed to get known value from INI file");
fail_if(!streq(ret, "true"), "Incorrect value in INI file");
fail_if(!nc_hashmap_contains(f, "Alice"), "INI File missing \"Alice\" section");
ret = nc_hashmap_get(nc_hashmap_get(f, "Alice"), "alive");
fail_if(!ret, "Failed to get known value from INI file");
fail_if(!streq(ret, "false"), "Incorrect value in INI file #2");
ret = nc_hashmap_get(nc_hashmap_get(f, "John"), "Random");
fail_if(ret, "Got unexpected key in section");
ret = nc_hashmap_get(nc_hashmap_get(f, "Bob"), "Random");
fail_if(ret, "Got unexpected section");
ret = nc_hashmap_get(nc_hashmap_get(f, "Barry"), "slogan");
fail_if(!ret, "Failed to get known value from INI file");
fail_if(!streq(ret, "Hello World"), "Incorrect value in INI file #3");
}
}
END_TEST
START_TEST(nc_inifile_bad_test)
{
const char *t_path = TOP_DIR "/tests/ini/sectionless.ini";
autofree(NcHashmap) *f = NULL;
f = nc_ini_file_parse(t_path);
fail_if(f != NULL, "Parsed illegal sectionless INI file");
t_path = TOP_DIR "/tests/ini/empty_key.ini";
f = nc_ini_file_parse(t_path);
fail_if(f != NULL, "Parsed illegal INI file with empty keys");
t_path = TOP_DIR "/tests/ini/just_assign.ini";
f = nc_ini_file_parse(t_path);
fail_if(f != NULL, "Parsed illegal assign-only INI file");
t_path = TOP_DIR "/tests/ini/broken_section_start.ini";
f = nc_ini_file_parse(t_path);
fail_if(f != NULL, "Parsed illegal broken-section-start INI File");
t_path = TOP_DIR "/tests/ini/broken_section_end.ini";
f = nc_ini_file_parse(t_path);
fail_if(f != NULL, "Parsed illegal broken-section-end INI File");
}
END_TEST
static Suite *core_suite(void)
{
Suite *s = NULL;
TCase *tc = NULL;
s = suite_create("nc_inifile");
tc = tcase_create("nc_inifile_functions");
tcase_add_test(tc, nc_inifile_open_test);
tcase_add_test(tc, nc_inifile_good_test);
tcase_add_test(tc, nc_inifile_bad_test);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
Suite *s;
SRunner *sr;
int fail;
s = core_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_VERBOSE);
fail = srunner_ntests_failed(sr);
srunner_free(sr);
if (fail > 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=8 tabstop=8 expandtab:
* :indentSize=8:tabSize=8:noTabs=true:
*/