test_ini.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. np_arg_list *optltmp;
  34. /* Put everything as a space-separated string */
  35. asprintf(&optstr, "");
  36. while (optlst) {
  37. asprintf(&optstr, "%s%s ", optstr, optlst->arg);
  38. optltmp=optlst;
  39. optlst=optlst->next;
  40. free(optltmp);
  41. }
  42. /* Strip last whitespace */
  43. if (strlen(optstr)>1) optstr[strlen(optstr)-1]='\0';
  44. return optstr;
  45. }
  46. int
  47. main (int argc, char **argv)
  48. {
  49. char *optstr=NULL;
  50. plan_tests(12);
  51. optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
  52. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected");
  53. my_free(optstr);
  54. optstr=list2str(np_get_defaults("@./config-tiny.ini", "section"));
  55. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name, without specific");
  56. my_free(optstr);
  57. optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
  58. ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "Used default section name over specified one");
  59. my_free(optstr);
  60. optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
  61. ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
  62. my_free(optstr);
  63. optstr=list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk"));
  64. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
  65. my_free(optstr);
  66. optstr=list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk"));
  67. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
  68. my_free(optstr);
  69. optstr=list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk"));
  70. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
  71. my_free(optstr);
  72. optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
  73. ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
  74. my_free(optstr);
  75. optstr=list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk"));
  76. ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
  77. my_free(optstr);
  78. optstr=list2str(np_get_defaults("check space_and_flags@./plugin.ini", "check_disk"));
  79. ok( !strcmp(optstr, "--foo=bar -a -b --bar"), "plugin.ini space in stanza and flag arguments");
  80. my_free(optstr);
  81. optstr=list2str(np_get_defaults("Section Two@./config-dos.ini", "check_disk"));
  82. ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-dos.ini's Section Two as expected");
  83. my_free(optstr);
  84. optstr=list2str(np_get_defaults("section_twice@./plugin.ini", "check_disk"));
  85. ok( !strcmp(optstr, "--foo=bar --bar=foo"), "plugin.ini's section_twice defined twice in the file");
  86. my_free(optstr);
  87. return exit_status();
  88. }