test_disk.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_disk.h"
  21. #include "tap.h"
  22. #include "regex.h"
  23. void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list,
  24. char *regstr, int cflags, int expect,
  25. char *desc);
  26. int
  27. main (int argc, char **argv)
  28. {
  29. struct name_list *exclude_filesystem=NULL;
  30. struct name_list *exclude_fstype=NULL;
  31. struct name_list *dummy_mountlist = NULL;
  32. struct name_list *temp_name;
  33. struct parameter_list *paths = NULL;
  34. struct parameter_list *p, *prev, *last;
  35. struct mount_entry *dummy_mount_list;
  36. struct mount_entry *me;
  37. struct mount_entry **mtail = &dummy_mount_list;
  38. int cflags = REG_NOSUB | REG_EXTENDED;
  39. int found = 0, count = 0;
  40. plan_tests(33);
  41. ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
  42. np_add_name(&exclude_filesystem, "/var/log");
  43. ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
  44. ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
  45. np_add_name(&exclude_filesystem, "/home");
  46. ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
  47. ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
  48. ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
  49. np_add_name(&exclude_fstype, "iso9660");
  50. ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now");
  51. ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
  52. /*
  53. for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
  54. printf("Name: %s\n", temp_name->name);
  55. }
  56. */
  57. me = (struct mount_entry *) malloc(sizeof *me);
  58. me->me_devname = strdup("/dev/c0t0d0s0");
  59. me->me_mountdir = strdup("/");
  60. *mtail = me;
  61. mtail = &me->me_next;
  62. me = (struct mount_entry *) malloc(sizeof *me);
  63. me->me_devname = strdup("/dev/c1t0d1s0");
  64. me->me_mountdir = strdup("/var");
  65. *mtail = me;
  66. mtail = &me->me_next;
  67. me = (struct mount_entry *) malloc(sizeof *me);
  68. me->me_devname = strdup("/dev/c2t0d0s0");
  69. me->me_mountdir = strdup("/home");
  70. *mtail = me;
  71. mtail = &me->me_next;
  72. np_test_mount_entry_regex(dummy_mount_list, strdup("/"),
  73. cflags, 3, strdup("a"));
  74. np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"),
  75. cflags, 3,strdup("regex on dev names:"));
  76. np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"),
  77. cflags, 0,
  78. strdup("regex on non existant dev/path:"));
  79. np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"),
  80. cflags | REG_ICASE,0,
  81. strdup("regi on non existant dev/path:"));
  82. np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"),
  83. cflags, 3,
  84. strdup("partial devname regex match:"));
  85. np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"),
  86. cflags, 1,
  87. strdup("partial devname regex match:"));
  88. np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"),
  89. cflags | REG_ICASE, 1,
  90. strdup("partial devname regi match:"));
  91. np_test_mount_entry_regex(dummy_mount_list, strdup("home"),
  92. cflags, 1,
  93. strdup("partial pathname regex match:"));
  94. np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"),
  95. cflags | REG_ICASE, 1,
  96. strdup("partial pathname regi match:"));
  97. np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"),
  98. cflags, 2,
  99. strdup("grouped regex pathname match:"));
  100. np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"),
  101. cflags | REG_ICASE, 2,
  102. strdup("grouped regi pathname match:"));
  103. np_add_parameter(&paths, "/home/groups");
  104. np_add_parameter(&paths, "/var");
  105. np_add_parameter(&paths, "/tmp");
  106. np_add_parameter(&paths, "/home/tonvoon");
  107. np_add_parameter(&paths, "/dev/c2t0d0s0");
  108. np_set_best_match(paths, dummy_mount_list, FALSE);
  109. for (p = paths; p; p = p->name_next) {
  110. struct mount_entry *temp_me;
  111. temp_me = p->best_match;
  112. if (! strcmp(p->name, "/home/groups")) {
  113. ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
  114. } else if (! strcmp(p->name, "/var")) {
  115. ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
  116. } else if (! strcmp(p->name, "/tmp")) {
  117. ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
  118. } else if (! strcmp(p->name, "/home/tonvoon")) {
  119. ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
  120. } else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
  121. ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
  122. }
  123. }
  124. paths = NULL; /* Bad boy - should free, but this is a test suite */
  125. np_add_parameter(&paths, "/home/groups");
  126. np_add_parameter(&paths, "/var");
  127. np_add_parameter(&paths, "/tmp");
  128. np_add_parameter(&paths, "/home/tonvoon");
  129. np_add_parameter(&paths, "/home");
  130. np_set_best_match(paths, dummy_mount_list, TRUE);
  131. for (p = paths; p; p = p->name_next) {
  132. if (! strcmp(p->name, "/home/groups")) {
  133. ok( ! p->best_match , "/home/groups correctly not found");
  134. } else if (! strcmp(p->name, "/var")) {
  135. ok( p->best_match, "/var found");
  136. } else if (! strcmp(p->name, "/tmp")) {
  137. ok(! p->best_match, "/tmp correctly not found");
  138. } else if (! strcmp(p->name, "/home/tonvoon")) {
  139. ok(! p->best_match, "/home/tonvoon not found");
  140. } else if (! strcmp(p->name, "/home")) {
  141. ok( p->best_match, "/home found");
  142. }
  143. }
  144. /* test deleting first element in paths */
  145. paths = np_del_parameter(paths, NULL);
  146. for (p = paths; p; p = p->name_next) {
  147. if (! strcmp(p->name, "/home/groups"))
  148. found = 1;
  149. }
  150. ok(found == 0, "first element successfully deleted");
  151. found = 0;
  152. p=paths;
  153. while (p) {
  154. if (! strcmp(p->name, "/tmp"))
  155. p = np_del_parameter(p, prev);
  156. else {
  157. prev = p;
  158. p = p->name_next;
  159. }
  160. }
  161. for (p = paths; p; p = p->name_next) {
  162. if (! strcmp(p->name, "/tmp"))
  163. found = 1;
  164. if (p->name_next)
  165. prev = p;
  166. else
  167. last = p;
  168. }
  169. ok(found == 0, "/tmp element successfully deleted");
  170. p = np_del_parameter(last, prev);
  171. for (p = paths; p; p = p->name_next) {
  172. if (! strcmp(p->name, "/home"))
  173. found = 1;
  174. last = p;
  175. count++;
  176. }
  177. ok(found == 0, "last (/home) element successfully deleted");
  178. ok(count == 2, "two elements remaining");
  179. return exit_status();
  180. }
  181. void
  182. np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc)
  183. {
  184. int matches = 0;
  185. regex_t re;
  186. struct mount_entry *me;
  187. if (regcomp(&re,regstr, cflags) == 0) {
  188. for (me = dummy_mount_list; me; me= me->me_next) {
  189. if(np_regex_match_mount_entry(me,&re))
  190. matches++;
  191. }
  192. ok( matches == expect,
  193. "%s '%s' matched %i/3 entries. ok: %i/3",
  194. desc, regstr, expect, matches);
  195. } else
  196. ok ( false, "regex '%s' not compileable", regstr);
  197. }