test_ini.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "utils_base.h"
  21. #include "parse_ini.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(11);
  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. /*
  58. * This check is expected to die - It's commented so we can eventually put
  59. * it in a separate file for testing the return value
  60. */
  61. /* optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
  62. ok( 0, "die if section isn't found");
  63. my_free(optstr); */
  64. optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
  65. ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
  66. my_free(optstr);
  67. optstr=list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk"));
  68. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
  69. my_free(optstr);
  70. optstr=list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk"));
  71. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
  72. my_free(optstr);
  73. optstr=list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk"));
  74. ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
  75. my_free(optstr);
  76. optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
  77. ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
  78. my_free(optstr);
  79. optstr=list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk"));
  80. ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
  81. my_free(optstr);
  82. optstr=list2str(np_get_defaults("check space_and_flags@./plugin.ini", "check_disk"));
  83. ok( !strcmp(optstr, "--foo=bar -a -b --bar"), "plugin.ini space in stanza and flag arguments");
  84. my_free(optstr);
  85. optstr=list2str(np_get_defaults("Section Two@./config-dos.ini", "check_disk"));
  86. ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-dos.ini's Section Two as expected");
  87. my_free(optstr);
  88. optstr=list2str(np_get_defaults("section_twice@./plugin.ini", "check_disk"));
  89. ok( !strcmp(optstr, "--foo=bar --bar=foo"), "plugin.ini's section_twice defined twice in the file");
  90. my_free(optstr);
  91. return exit_status();
  92. }