test_ini.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. asprintf(&optstr, "");
  35. while (optlst) {
  36. asprintf(&optstr, "%s%s ", optstr, optlst->arg);
  37. optlst=optlst->next;
  38. }
  39. /* Strip last whitespace */
  40. if (strlen(optstr)>1) optstr[strlen(optstr)-1]='\0';
  41. return optstr;
  42. }
  43. int
  44. main (int argc, char **argv)
  45. {
  46. char *optstr=NULL;
  47. plan_tests(9);
  48. optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
  49. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "config-tiny.ini's section as expected");
  50. my_free(optstr);
  51. optstr=list2str(np_get_defaults("@./config-tiny.ini", "section"));
  52. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name, without specific");
  53. my_free(optstr);
  54. optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
  55. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name over specified one");
  56. my_free(optstr);
  57. optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
  58. ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
  59. my_free(optstr);
  60. optstr=list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk"));
  61. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
  62. my_free(optstr);
  63. optstr=list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk"));
  64. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
  65. my_free(optstr);
  66. optstr=list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk"));
  67. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
  68. my_free(optstr);
  69. optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
  70. ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
  71. my_free(optstr);
  72. optstr=list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk"));
  73. ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
  74. my_free(optstr);
  75. return exit_status();
  76. }