test_disk.c 7.6 KB

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