testc_ini.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "faux/str.h"
  4. #include "faux/ini.h"
  5. #include "faux/testc_helpers.h"
  6. int testc_faux_ini_good(void) {
  7. char *path = NULL;
  8. path = getenv("TESTC_TMPDIR");
  9. if (path)
  10. printf("Env var is [%s]\n", path);
  11. return 0;
  12. }
  13. int testc_faux_ini_bad(void) {
  14. printf("Some debug information here\n");
  15. return -1;
  16. }
  17. int testc_faux_ini_signal(void) {
  18. char *p = NULL;
  19. printf("%s\n", p);
  20. return -1;
  21. }
  22. int testc_faux_ini_parse(void) {
  23. // Source INI file
  24. const char *src_file =
  25. "# Comment\n"
  26. "DISTRIB_ID=Ubuntu\n"
  27. "DISTRIB_RELEASE=18.04\n"
  28. "DISTRIB_CODENAME=bionic\n"
  29. "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
  30. "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
  31. "WO_QUOTES_VAR = qwerty\n"
  32. "WO_QUOTES_VAR2 = qwerty 98989898\n"
  33. "EMPTY_VAR3 = \n"
  34. "EMPTY_VAR4 =\n"
  35. " EMPTY_VAR5 = \"\"\t \n"
  36. " ANOTHER_VAR6 = \"Normal var\"\t \n"
  37. "\tTABBED_VAR = \"Normal tabbed var\"\t \n"
  38. "# Another comment\n"
  39. " # Yet another comment\n"
  40. "\t# Tabbed comment\n"
  41. "VAR_WITHOUT_EOL=zxcvbnm"
  42. ;
  43. // Etalon file
  44. const char *etalon_file =
  45. "ANOTHER_VAR6=\"Normal var\"\n"
  46. "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
  47. "DISTRIB_CODENAME=bionic\n"
  48. "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
  49. "DISTRIB_ID=Ubuntu\n"
  50. "DISTRIB_RELEASE=18.04\n"
  51. "TABBED_VAR=\"Normal tabbed var\"\n"
  52. "VAR_WITHOUT_EOL=zxcvbnm\n"
  53. "WO_QUOTES_VAR=qwerty\n"
  54. "WO_QUOTES_VAR2=qwerty\n"
  55. "\"test space\"=\"lk lk lk \"\n"
  56. ;
  57. int ret = -1; // Pessimistic return value
  58. faux_ini_t *ini = NULL;
  59. faux_ini_node_t *iter = NULL;
  60. const faux_pair_t *pair = NULL;
  61. char *src_fn = NULL;
  62. char *dst_fn = NULL;
  63. char *etalon_fn = NULL;
  64. unsigned num_entries = 0;
  65. // Prepare files
  66. src_fn = faux_testc_tmpfile_deploy(src_file);
  67. etalon_fn = faux_testc_tmpfile_deploy(etalon_file);
  68. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  69. ini = faux_ini_new();
  70. if (faux_ini_parse_file(ini, src_fn) < 0) {
  71. fprintf(stderr, "Can't parse INI file %s\n", src_fn);
  72. goto parse_error;
  73. }
  74. iter = faux_ini_iter(ini);
  75. while ((pair = faux_ini_each(&iter))) {
  76. num_entries++;
  77. printf("[%s] = [%s]\n", faux_pair_name(pair), faux_pair_value(pair));
  78. }
  79. if (10 != num_entries) {
  80. fprintf(stderr, "Wrong number of entries %u\n", num_entries);
  81. goto parse_error;
  82. }
  83. faux_ini_set(ini, "test space", "lk lk lk ");
  84. if (faux_ini_write_file(ini, dst_fn) < 0) {
  85. fprintf(stderr, "Can't write INI file %s\n", dst_fn);
  86. goto parse_error;
  87. }
  88. ret = 0; // success
  89. parse_error:
  90. faux_ini_free(ini);
  91. faux_str_free(dst_fn);
  92. faux_str_free(src_fn);
  93. faux_str_free(etalon_fn);
  94. return ret;
  95. }