FreeRDP
Loading...
Searching...
No Matches
TestIni.c
1
2#include <winpr/crt.h>
3#include <winpr/ini.h>
4
5static const char TEST_INI_01[] = "; This is a sample .ini config file\n"
6 "\n"
7 "[first_section]\n"
8 "one = 1\n"
9 "five = 5\n"
10 "animal = BIRD\n"
11 "\n"
12 "[second_section]\n"
13 "path = \"/usr/local/bin\"\n"
14 "URL = \"http://www.example.com/~username\"\n"
15 "\n";
16
17static const char TEST_INI_02[] = "[FreeRDS]\n"
18 "prefix=\"/usr/local\"\n"
19 "bindir=\"bin\"\n"
20 "sbindir=\"sbin\"\n"
21 "libdir=\"lib\"\n"
22 "datarootdir=\"share\"\n"
23 "localstatedir=\"var\"\n"
24 "sysconfdir=\"etc\"\n"
25 "\n";
26
27static const char TEST_INI_03[] = "[FreeRDS]\n"
28 "prefix=\"/usr/local\"\n"
29 "bindir=\"bin\"\n"
30 "# some illegal string\n"
31 "sbindir=\"sbin\"\n"
32 "libdir=\"lib\"\n"
33 "invalid key-value pair\n"
34 "datarootdir=\"share\"\n"
35 "localstatedir=\"var\"\n"
36 "sysconfdir=\"etc\"\n"
37 "\n";
38
39static const char TEST_INI_04[] = "[foobar]\r\n"
40 "\r\n"
41 "\r\n"
42 "prefix=\"someprefix\"\r\n";
43
44WINPR_ATTR_NODISCARD
45static BOOL test04(void)
46{
47 BOOL rc = FALSE;
48 size_t nSections = 0;
49
50 /* First Sample */
51 char** sectionNames = nullptr;
52 wIniFile* ini = IniFile_New();
53 if (!ini)
54 goto fail;
55
56 if (IniFile_ReadBuffer(ini, TEST_INI_04) < 0)
57 goto fail;
58
59 sectionNames = IniFile_GetSectionNames(ini, &nSections);
60 if (!sectionNames && (nSections > 0))
61 goto fail;
62
63 if (nSections != 1)
64 goto fail;
65
66 for (size_t i = 0; i < nSections; i++)
67 {
68 size_t nKeys = 0;
69 char** keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
70 printf("[%s]\n", sectionNames[i]);
71 if ((!keyNames && (nKeys > 0)) || (nKeys != 1))
72 {
73 free((void*)keyNames);
74 goto fail;
75 }
76 for (size_t j = 0; j < nKeys; j++)
77 {
78 const char* sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
79 printf("%s = %s\n", keyNames[j], sValue);
80 }
81 free((void*)keyNames);
82 }
83
84 const char* sValue = IniFile_GetKeyValueString(ini, "foobar", "prefix");
85
86 if (strcmp(sValue, "someprefix") != 0)
87 {
88 printf("IniFile_GetKeyValueString failure\n");
89 goto fail;
90 }
91
92 rc = TRUE;
93fail:
94 free((void*)sectionNames);
95 IniFile_Free(ini);
96 return rc;
97}
98
99int TestIni(int argc, char* argv[])
100{
101 int rc = -1;
102 size_t nKeys = 0;
103 size_t nSections = 0;
104 UINT32 iValue = 0;
105 wIniFile* ini = nullptr;
106 const char* sValue = nullptr;
107 char** keyNames = nullptr;
108 char** sectionNames = nullptr;
109
110 WINPR_UNUSED(argc);
111 WINPR_UNUSED(argv);
112
113 if (!test04())
114 return -1;
115
116 /* First Sample */
117 ini = IniFile_New();
118 if (!ini)
119 goto fail;
120
121 if (IniFile_ReadBuffer(ini, TEST_INI_01) < 0)
122 goto fail;
123
124 free((void*)sectionNames);
125 sectionNames = IniFile_GetSectionNames(ini, &nSections);
126 if (!sectionNames && (nSections > 0))
127 goto fail;
128
129 for (size_t i = 0; i < nSections; i++)
130 {
131 free((void*)keyNames);
132 keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
133 printf("[%s]\n", sectionNames[i]);
134 if (!keyNames && (nKeys > 0))
135 goto fail;
136 for (size_t j = 0; j < nKeys; j++)
137 {
138 sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
139 printf("%s = %s\n", keyNames[j], sValue);
140 }
141 }
142
143 iValue = IniFile_GetKeyValueInt(ini, "first_section", "one");
144
145 if (iValue != 1)
146 {
147 printf("IniFile_GetKeyValueInt failure\n");
148 goto fail;
149 }
150
151 iValue = IniFile_GetKeyValueInt(ini, "first_section", "five");
152
153 if (iValue != 5)
154 {
155 printf("IniFile_GetKeyValueInt failure\n");
156 goto fail;
157 }
158
159 sValue = IniFile_GetKeyValueString(ini, "first_section", "animal");
160
161 if (strcmp(sValue, "BIRD") != 0)
162 {
163 printf("IniFile_GetKeyValueString failure\n");
164 goto fail;
165 }
166
167 sValue = IniFile_GetKeyValueString(ini, "second_section", "path");
168
169 if (strcmp(sValue, "/usr/local/bin") != 0)
170 {
171 printf("IniFile_GetKeyValueString failure\n");
172 goto fail;
173 }
174
175 sValue = IniFile_GetKeyValueString(ini, "second_section", "URL");
176
177 if (strcmp(sValue, "http://www.example.com/~username") != 0)
178 {
179 printf("IniFile_GetKeyValueString failure\n");
180 goto fail;
181 }
182
183 IniFile_Free(ini);
184 /* Second Sample */
185 ini = IniFile_New();
186 if (!ini)
187 goto fail;
188 if (IniFile_ReadBuffer(ini, TEST_INI_02) < 0)
189 goto fail;
190 free((void*)sectionNames);
191 sectionNames = IniFile_GetSectionNames(ini, &nSections);
192 if (!sectionNames && (nSections > 0))
193 goto fail;
194
195 for (size_t i = 0; i < nSections; i++)
196 {
197 free((void*)keyNames);
198 keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
199 printf("[%s]\n", sectionNames[i]);
200
201 if (!keyNames && (nKeys > 0))
202 goto fail;
203 for (size_t j = 0; j < nKeys; j++)
204 {
205 sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
206 printf("%s = %s\n", keyNames[j], sValue);
207 }
208 }
209
210 IniFile_Free(ini);
211 /* Third sample - invalid input */
212 ini = IniFile_New();
213
214 if (IniFile_ReadBuffer(ini, TEST_INI_03) != -1)
215 goto fail;
216
217 rc = 0;
218fail:
219 free((void*)keyNames);
220 free((void*)sectionNames);
221 IniFile_Free(ini);
222 return rc;
223}