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