1818***/
1919
2020#include "conf-parser.h"
21+ #include "fd-util.h"
22+ #include "fileio.h"
2123#include "log.h"
2224#include "macro.h"
2325#include "string-util.h"
@@ -227,7 +229,91 @@ static void test_config_parse_iec_uint64(void) {
227229 assert_se (config_parse_iec_uint64 (NULL , "/this/file" , 11 , "Section" , 22 , "Size" , 0 , "4.5M" , & offset , NULL ) == 0 );
228230}
229231
232+ static const char * const config_file [] = {
233+ "[Section]\n"
234+ "setting1=1\n" ,
235+
236+ "[Section]\n"
237+ "setting1=1" , /* no terminating newline */
238+
239+ "\n\n\n\n[Section]\n\n\n"
240+ "setting1=1" , /* some whitespace, no terminating newline */
241+
242+ "[Section]\n"
243+ "[Section]\n"
244+ "setting1=1\n"
245+ "setting1=2\n"
246+ "setting1=1\n" , /* repeated settings */
247+
248+ "[Section]\n"
249+ "setting1=1\\\n" /* normal continuation */
250+ "2\\\n"
251+ "3\n" ,
252+
253+ "[Section]\n"
254+ "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */
255+ "\\\\2\n" , /* note that C requires one level of escaping, so the
256+ * parser gets "…1 BS BS BS NL BS BS 2 NL", which
257+ * it translates into "…1 BS BS SP BS BS 2" */
258+ };
259+
260+ static void test_config_parse (unsigned i , const char * s ) {
261+ char name [] = "/tmp/test-conf-parser.XXXXXX" ;
262+ int fd , r ;
263+ _cleanup_fclose_ FILE * f = NULL ;
264+ _cleanup_free_ char * setting1 = NULL ;
265+
266+ const ConfigTableItem items [] = {
267+ { "Section" , "setting1" , config_parse_string , 0 , & setting1 },
268+ {}
269+ };
270+
271+ log_info ("== %s[%i] ==" , __func__ , i );
272+
273+ fd = mkostemp_safe (name );
274+ assert_se (fd >= 0 );
275+ assert_se ((size_t ) write (fd , s , strlen (s )) == strlen (s ));
276+
277+ assert_se (lseek (fd , 0 , SEEK_SET ) == 0 );
278+ assert_se (f = fdopen (fd , "r" ));
279+
280+ /*
281+ int config_parse(const char *unit,
282+ const char *filename,
283+ FILE *f,
284+ const char *sections,
285+ ConfigItemLookup lookup,
286+ const void *table,
287+ bool relaxed,
288+ bool allow_include,
289+ bool warn,
290+ void *userdata)
291+ */
292+
293+ r = config_parse (NULL , name , f ,
294+ "Section\0" ,
295+ config_item_table_lookup , items ,
296+ false, false, true, NULL );
297+ assert_se (r == 0 );
298+
299+ switch (i ) {
300+ case 0 ... 3 :
301+ assert_se (streq (setting1 , "1" ));
302+ break ;
303+
304+ case 4 :
305+ assert_se (streq (setting1 , "1 2 3" ));
306+ break ;
307+
308+ case 5 :
309+ assert_se (streq (setting1 , "1\\\\ \\\\2" ));
310+ break ;
311+ }
312+ }
313+
230314int main (int argc , char * * argv ) {
315+ unsigned i ;
316+
231317 log_parse_environment ();
232318 log_open ();
233319
@@ -244,5 +330,8 @@ int main(int argc, char **argv) {
244330 test_config_parse_nsec ();
245331 test_config_parse_iec_uint64 ();
246332
333+ for (i = 0 ; i < ELEMENTSOF (config_file ); i ++ )
334+ test_config_parse (i , config_file [i ]);
335+
247336 return 0 ;
248337}
0 commit comments