testc_ini.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Prepare files
  65. src_fn = faux_testc_tmpfile_deploy(src_file);
  66. etalon_fn = faux_testc_tmpfile_deploy(etalon_file);
  67. dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
  68. ini = faux_ini_new();
  69. if (faux_ini_parse_file(ini, src_fn) < 0) {
  70. fprintf(stderr, "Can't parse INI file %s\n", src_fn);
  71. goto parse_error;
  72. }
  73. iter = faux_ini_iter(ini);
  74. while ((pair = faux_ini_each(&iter))) {
  75. printf("[%s] = [%s]\n", faux_pair_name(pair), faux_pair_value(pair));
  76. }
  77. faux_ini_set(ini, "test space", "lk lk lk ");
  78. if (faux_ini_write_file(ini, dst_fn) < 0) {
  79. fprintf(stderr, "Can't write INI file %s\n", dst_fn);
  80. goto parse_error;
  81. }
  82. if (faux_testc_file_cmp(dst_fn, etalon_fn) != 0) {
  83. fprintf(stderr, "Generated file %s is not equal to etalon %s\n",
  84. dst_fn, etalon_fn);
  85. goto parse_error;
  86. }
  87. ret = 0; // success
  88. parse_error:
  89. faux_ini_free(ini);
  90. faux_str_free(dst_fn);
  91. faux_str_free(src_fn);
  92. faux_str_free(etalon_fn);
  93. return ret;
  94. }