test_ini.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*****************************************************************************
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * $Id$
  17. *
  18. *****************************************************************************/
  19. #include "common.h"
  20. #include "parse_ini.h"
  21. #include "utils_base.h"
  22. #include "tap.h"
  23. void my_free(char *string) {
  24. if (string != NULL) {
  25. printf("string:\n\t|%s|\n", string);
  26. free(string);
  27. }
  28. }
  29. char*
  30. list2str(np_arg_list *optlst)
  31. {
  32. char *optstr=NULL;
  33. /* Put everything as a space-separated string */
  34. while (optlst) {
  35. asprintf(&optstr, "%s%s ", optstr?optstr:"", optlst->arg);
  36. optlst=optlst->next;
  37. }
  38. /* Strip last whitespace */
  39. optstr[strlen(optstr)-1]='\0';
  40. return optstr;
  41. }
  42. int
  43. main (int argc, char **argv)
  44. {
  45. char *optstr=NULL;
  46. plan_tests(4);
  47. optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
  48. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "config-tiny.ini's section as expected");
  49. my_free(optstr);
  50. optstr=list2str(np_get_defaults("@./config-tiny.ini", "section"));
  51. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name, without specific");
  52. my_free(optstr);
  53. /* This test currently crashes */
  54. /*
  55. optstr=np_get_defaults("section_unknown@./config-tiny.ini", "section");
  56. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name over specified one");
  57. my_free(optstr);
  58. */
  59. optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
  60. ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
  61. my_free(optstr);
  62. /* These tests currently crash parse_ini.c */
  63. /*
  64. optstr=np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk");
  65. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
  66. my_free(optstr);
  67. optstr=np_get_defaults("section2@./config-tiny.ini", "check_disk");
  68. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
  69. my_free(optstr);
  70. optstr=np_get_defaults("section3@./config-tiny.ini", "check_disk");
  71. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
  72. my_free(optstr);
  73. */
  74. optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
  75. ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
  76. my_free(optstr);
  77. /* This test crashes at the moment. I think it is not expecting single character parameter names */
  78. /*
  79. optstr=np_get_defaults("check_mysql2@./config-tiny.ini", "check_disk");
  80. ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
  81. my_free(optstr);
  82. */
  83. return exit_status();
  84. }