testc_ini.c 2.3 KB

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