test_disk.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. ******************************************************************************/
  15. #include "common.h"
  16. #include "utils_disk.h"
  17. #include "tap.h"
  18. #include "regex.h"
  19. void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
  20. char *regstr, int cflags, int expect,
  21. char *desc);
  22. int
  23. main (int argc, char **argv)
  24. {
  25. struct name_list *exclude_filesystem=NULL;
  26. struct name_list *exclude_fstype=NULL;
  27. struct name_list *dummy_mountlist = NULL;
  28. struct name_list *temp_name;
  29. struct parameter_list *paths = NULL;
  30. struct parameter_list *p;
  31. struct mount_entry *dummy_mount_list;
  32. struct mount_entry *me;
  33. struct mount_entry **mtail = &dummy_mount_list;
  34. int cflags = REG_NOSUB | REG_EXTENDED;
  35. plan_tests(29);
  36. ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
  37. np_add_name(&exclude_filesystem, "/var/log");
  38. ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
  39. ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
  40. np_add_name(&exclude_filesystem, "/home");
  41. ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
  42. ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
  43. ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
  44. np_add_name(&exclude_fstype, "iso9660");
  45. ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
  46. ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
  47. /*
  48. for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
  49. printf("Name: %s\n", temp_name->name);
  50. }
  51. */
  52. me = (struct mount_entry *) malloc(sizeof *me);
  53. me->me_devname = strdup("/dev/c0t0d0s0");
  54. me->me_mountdir = strdup("/");
  55. *mtail = me;
  56. mtail = &me->me_next;
  57. me = (struct mount_entry *) malloc(sizeof *me);
  58. me->me_devname = strdup("/dev/c1t0d1s0");
  59. me->me_mountdir = strdup("/var");
  60. *mtail = me;
  61. mtail = &me->me_next;
  62. me = (struct mount_entry *) malloc(sizeof *me);
  63. me->me_devname = strdup("/dev/c2t0d0s0");
  64. me->me_mountdir = strdup("/home");
  65. *mtail = me;
  66. mtail = &me->me_next;
  67. np_test_mount_entry_regex(dummy_mount_list, strdup("/"),
  68. cflags, 3, strdup("a"));
  69. np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"),
  70. cflags, 3,strdup("regex on dev names:"));
  71. np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"),
  72. cflags, 0,
  73. strdup("regex on non existant dev/path:"));
  74. np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"),
  75. cflags | REG_ICASE,0,
  76. strdup("regi on non existant dev/path:"));
  77. np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"),
  78. cflags, 3,
  79. strdup("partial devname regex match:"));
  80. np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"),
  81. cflags, 1,
  82. strdup("partial devname regex match:"));
  83. np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"),
  84. cflags | REG_ICASE, 1,
  85. strdup("partial devname regi match:"));
  86. np_test_mount_entry_regex(dummy_mount_list, strdup("home"),
  87. cflags, 1,
  88. strdup("partial pathname regex match:"));
  89. np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"),
  90. cflags | REG_ICASE, 1,
  91. strdup("partial pathname regi match:"));
  92. np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"),
  93. cflags, 2,
  94. strdup("grouped regex pathname match:"));
  95. np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"),
  96. cflags | REG_ICASE, 2,
  97. strdup("grouped regi pathname match:"));
  98. np_add_parameter(&paths, "/home/groups");
  99. np_add_parameter(&paths, "/var");
  100. np_add_parameter(&paths, "/tmp");
  101. np_add_parameter(&paths, "/home/tonvoon");
  102. np_add_parameter(&paths, "/dev/c2t0d0s0");
  103. np_set_best_match(paths, dummy_mount_list, FALSE);
  104. for (p = paths; p; p = p->name_next) {
  105. struct mount_entry *temp_me;
  106. temp_me = p->best_match;
  107. if (! strcmp(p->name, "/home/groups")) {
  108. ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
  109. } else if (! strcmp(p->name, "/var")) {
  110. ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
  111. } else if (! strcmp(p->name, "/tmp")) {
  112. ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
  113. } else if (! strcmp(p->name, "/home/tonvoon")) {
  114. ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
  115. } else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
  116. ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
  117. }
  118. }
  119. paths = NULL; /* Bad boy - should free, but this is a test suite */
  120. np_add_parameter(&paths, "/home/groups");
  121. np_add_parameter(&paths, "/var");
  122. np_add_parameter(&paths, "/tmp");
  123. np_add_parameter(&paths, "/home/tonvoon");
  124. np_add_parameter(&paths, "/home");
  125. np_set_best_match(paths, dummy_mount_list, TRUE);
  126. for (p = paths; p; p = p->name_next) {
  127. if (! strcmp(p->name, "/home/groups")) {
  128. ok( ! p->best_match , "/home/groups correctly not found");
  129. } else if (! strcmp(p->name, "/var")) {
  130. ok( p->best_match, "/var found");
  131. } else if (! strcmp(p->name, "/tmp")) {
  132. ok(! p->best_match, "/tmp correctly not found");
  133. } else if (! strcmp(p->name, "/home/tonvoon")) {
  134. ok(! p->best_match, "/home/tonvoon not found");
  135. } else if (! strcmp(p->name, "/home")) {
  136. ok( p->best_match, "/home found");
  137. }
  138. }
  139. return exit_status();
  140. }
  141. void
  142. np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
  143. {
  144. int matches = 0;
  145. regex_t re;
  146. struct mount_entry *me;
  147. if (regcomp(&re,regstr, cflags) == 0) {
  148. for (me = dummy_mount_list; me; me= me->me_next) {
  149. if(np_regex_match_mount_entry(me,&re))
  150. matches++;
  151. }
  152. ok( matches == expect,
  153. "%s '%s' matched %i/3 entries. ok: %i/3",
  154. desc, regstr, expect, matches);
  155. } else
  156. ok ( false, "regex '%s' not compileable", regstr);
  157. }