]> sjero.net Git - wget/commitdiff
[svn] Restricted operational semantics of frontcmp and proclist from generic strings...
authormtortonesi <devnull@localhost>
Wed, 15 Mar 2006 14:55:29 +0000 (06:55 -0800)
committermtortonesi <devnull@localhost>
Wed, 15 Mar 2006 14:55:29 +0000 (06:55 -0800)
src/ChangeLog
src/http.c
src/recur.c
src/test.c
src/test.h
src/utils.c
src/utils.h

index abbf2e2858e121ec204e54dffd5217df2583135a..846fa4b2c92a00edb1b7d21d833c55b372efb7e7 100644 (file)
@@ -1,3 +1,22 @@
+2006-03-15  Mauro Tortonesi  <mauro@ferrara.linux.it>
+
+       * utils.c: Restricted operational semantics of frontcmp and proclist
+       from generic strings to directory names and them to subdir_p and
+       dir_matches_p respectively.  Applied George Ogata's one line patch to
+       restrict algorithm of subdir_p to full directory name matching.  Added
+       testcases for subdir_p and dir_matches_p.
+
+       * utils.h: Changed all frontcmp occurrences to subdir_p.
+       
+       * recur.c: Ditto.
+       
+       * test.c: Changed type returned by test functions from char * to const
+       char *.  Added test_subdir_p and test_dir_matches_p to the list of
+       tests to run.
+
+       * http.c (test_parse_content_disposition): Changed return type from
+       char * to const char *. 
+
 2006-03-14  Mauro Tortonesi  <mauro@ferrara.linux.it>
 
        * recur.c (struct queue_element): Changed type of html_allowed member
 2006-03-14  Mauro Tortonesi  <mauro@ferrara.linux.it>
 
        * recur.c (struct queue_element): Changed type of html_allowed member
index 4752ce3d7eec4ef7910681f63929aa513a22a27b..d28ee7a87231a79ded111c37d5b3c5d0a781a63f 100644 (file)
@@ -2929,7 +2929,7 @@ http_cleanup (void)
 
 #ifdef TESTING
 
 
 #ifdef TESTING
 
-char *
+const char *
 test_parse_content_disposition()
 {
   int i;
 test_parse_content_disposition()
 {
   int i;
@@ -2953,8 +2953,6 @@ test_parse_content_disposition()
                  res == test_array[i].result
                  && (res == false 
                      || 0 == strcmp (test_array[i].filename, filename)));
                  res == test_array[i].result
                  && (res == false 
                      || 0 == strcmp (test_array[i].filename, filename)));
-
-      /* printf ("test %d: %s\n", i, res == false ? "false" : filename); */
     }
 
   return NULL;
     }
 
   return NULL;
index 641174d69d23ded46be218c8b1aa5b11645c061b..1e277ca37dfa75663a8902926772dc2b5e67e35b 100644 (file)
@@ -482,7 +482,7 @@ download_child_p (const struct urlpos *upos, struct url *parent, int depth,
       && u->port == start_url_parsed->port
       && !(opt.page_requisites && upos->link_inline_p))
     {
       && u->port == start_url_parsed->port
       && !(opt.page_requisites && upos->link_inline_p))
     {
-      if (!frontcmp (start_url_parsed->dir, u->dir))
+      if (!subdir_p (start_url_parsed->dir, u->dir))
        {
          DEBUGP (("Going to \"%s\" would escape \"%s\" with no_parent on.\n",
                   u->dir, start_url_parsed->dir));
        {
          DEBUGP (("Going to \"%s\" would escape \"%s\" with no_parent on.\n",
                   u->dir, start_url_parsed->dir));
index 06cd0a4b03e187bd7ae18b8609ce133b8e3f17fe..9f54a3e4a73fb37f33d1f5d9188e7ff4fcbd3393 100644 (file)
@@ -35,12 +35,18 @@ so, delete this exception statement from your version.  */
 #error "TESTING not set!!!"
 #endif
 
 #error "TESTING not set!!!"
 #endif
 
+const char *test_parse_content_disposition();
+const char *test_subdir_p();
+const char *test_dir_matches_p();
+
 int tests_run;
 
 int tests_run;
 
-static char *
+static const char *
 all_tests()
 {
   mu_run_test (test_parse_content_disposition);
 all_tests()
 {
   mu_run_test (test_parse_content_disposition);
+  mu_run_test (test_subdir_p);
+  mu_run_test (test_dir_matches_p);
   
   return NULL;
 }
   
   return NULL;
 }
@@ -49,7 +55,7 @@ all_tests()
 int
 main (void) 
 {
 int
 main (void) 
 {
-  char *result = all_tests();
+  const char *result = all_tests();
   
   if (result != NULL) 
     {
   
   if (result != NULL) 
     {
index 60e03c791be4e2db9e1bc718453dbf5677590e1c..9037ea12d4927f935d03a1acadf5c297e7f28a9a 100644 (file)
@@ -32,7 +32,7 @@ so, delete this exception statement from your version.  */
 
 /* from MinUnit */
 #define mu_assert(message, test) do { if (!(test)) return message; } while (0)
 
 /* from MinUnit */
 #define mu_assert(message, test) do { if (!(test)) return message; } while (0)
-#define mu_run_test(test) do { char *message = test(); tests_run++; \
+#define mu_run_test(test) do { const char *message = test(); tests_run++; \
                                if (message) return message; } while (0)
 
 extern int tests_run;
                                if (message) return message; } while (0)
 
 extern int tests_run;
index 667efce63c99e37fab48fbd08e44fb690dc59383..723103415fa80f38117b6931ec2355e6b2a6476a 100644 (file)
@@ -81,6 +81,10 @@ so, delete this exception statement from your version.  */
 #include "utils.h"
 #include "hash.h"
 
 #include "utils.h"
 #include "hash.h"
 
+#ifdef TESTING
+#include "test.h"
+#endif 
+
 /* Utility function: like xstrdup(), but also lowercases S.  */
 
 char *
 /* Utility function: like xstrdup(), but also lowercases S.  */
 
 char *
@@ -677,46 +681,49 @@ acceptable (const char *s)
   return true;
 }
 
   return true;
 }
 
-/* Compare S1 and S2 frontally; S2 must begin with S1.  E.g. if S1 is
-   `/something', frontcmp() will return true only if S2 begins with
-   `/something'.  */
+/* Check if D2 is a subdirectory of D1.  E.g. if D1 is `/something', subdir_p()
+   will return true if and only if D2 begins with `/something/' or is exactly 
+   '/something'.  */
 bool
 bool
-frontcmp (const char *s1, const char *s2)
+subdir_p (const char *d1, const char *d2)
 {
   if (!opt.ignore_case)
 {
   if (!opt.ignore_case)
-    for (; *s1 && *s2 && (*s1 == *s2); ++s1, ++s2)
+    for (; *d1 && *d2 && (*d1 == *d2); ++d1, ++d2)
       ;
   else
       ;
   else
-    for (; *s1 && *s2 && (TOLOWER (*s1) == TOLOWER (*s2)); ++s1, ++s2)
+    for (; *d1 && *d2 && (TOLOWER (*d1) == TOLOWER (*d2)); ++d1, ++d2)
       ;
       ;
-  return *s1 == '\0';
+  
+  return *d1 == '\0' && (*d2 == '\0' || *d2 == '/');
 }
 
 }
 
-/* Iterate through STRLIST, and return the first element that matches
-   S, through wildcards or front comparison (as appropriate).  */
-static char *
-proclist (char **strlist, const char *s)
+/* Iterate through DIRLIST (which must be NULL-terminated), and return the
+   first element that matches DIR, through wildcards or front comparison (as
+   appropriate).  */
+static bool
+dir_matches_p (char **dirlist, const char *dir)
 {
   char **x;
   int (*matcher) (const char *, const char *, int)
     = opt.ignore_case ? fnmatch_nocase : fnmatch;
 
 {
   char **x;
   int (*matcher) (const char *, const char *, int)
     = opt.ignore_case ? fnmatch_nocase : fnmatch;
 
-  for (x = strlist; *x; x++)
+  for (x = dirlist; *x; x++)
     {
       /* Remove leading '/' */
       char *p = *x + (**x == '/');
       if (has_wildcards_p (p))
        {
     {
       /* Remove leading '/' */
       char *p = *x + (**x == '/');
       if (has_wildcards_p (p))
        {
-         if (matcher (p, s, FNM_PATHNAME) == 0)
+         if (matcher (p, dir, FNM_PATHNAME) == 0)
            break;
        }
       else
        {
            break;
        }
       else
        {
-         if (frontcmp (p, s))
+         if (subdir_p (p, dir))
            break;
        }
     }
            break;
        }
     }
-  return *x;
+      
+  return *x ? true : false;
 }
 
 /* Returns whether DIRECTORY is acceptable for download, wrt the
 }
 
 /* Returns whether DIRECTORY is acceptable for download, wrt the
@@ -733,12 +740,12 @@ accdir (const char *directory)
     ++directory;
   if (opt.includes)
     {
     ++directory;
   if (opt.includes)
     {
-      if (!proclist (opt.includes, directory))
+      if (!dir_matches_p (opt.includes, directory))
        return false;
     }
   if (opt.excludes)
     {
        return false;
     }
   if (opt.excludes)
     {
-      if (proclist (opt.excludes, directory))
+      if (dir_matches_p (opt.excludes, directory))
        return false;
     }
   return true;
        return false;
     }
   return true;
@@ -2118,3 +2125,60 @@ print_decimal (double number)
 
   return buf;
 }
 
   return buf;
 }
+
+#ifdef TESTING
+
+const char *
+test_subdir_p()
+{
+  int i;
+  struct {
+    char *d1;
+    char *d2;
+    bool result;
+  } test_array[] = {
+    { "/somedir", "/somedir", true },
+    { "/somedir", "/somedir/d2", true },
+    { "/somedir/d1", "/somedir", false },
+  };
+  
+  for (i = 0; i < countof(test_array); ++i) 
+    {
+      bool res = subdir_p (test_array[i].d1, test_array[i].d2);
+
+      mu_assert ("test_subdir_p: wrong result", 
+                 res == test_array[i].result);
+    }
+
+  return NULL;
+}
+
+const char *
+test_dir_matches_p()
+{
+  int i;
+  struct {
+    char *dirlist[3];
+    char *dir;
+    bool result;
+  } test_array[] = {
+    { { "/somedir", "/someotherdir", NULL }, "somedir", true },
+    { { "/somedir", "/someotherdir", NULL }, "anotherdir", false },
+    { { "/somedir", "/*otherdir", NULL }, "anotherdir", true },
+    { { "/somedir/d1", "/someotherdir", NULL }, "somedir/d1", true },
+    { { "/somedir/d1", "/someotherdir", NULL }, "d1", false },
+  };
+  
+  for (i = 0; i < countof(test_array); ++i) 
+    {
+      bool res = dir_matches_p (test_array[i].dirlist, test_array[i].dir);
+      
+      mu_assert ("test_dir_matches_p: wrong result", 
+                 res == test_array[i].result);
+    }
+
+  return NULL;
+}
+
+#endif /* TESTING */
+
index db0edb0f359e2dd58a47f8088d439090b617f571..fc6057291f45f486ca8000ab11bbfd5961faab39 100644 (file)
@@ -51,7 +51,7 @@ char *xstrdup_lower (const char *);
 
 char *strdupdelim (const char *, const char *);
 char **sepstring (const char *);
 
 char *strdupdelim (const char *, const char *);
 char **sepstring (const char *);
-bool frontcmp (const char *, const char *);
+bool subdir_p (const char *, const char *);
 void fork_to_background (void);
 
 char *aprintf (const char *, ...) GCC_FORMAT_ATTR (1, 2);
 void fork_to_background (void);
 
 char *aprintf (const char *, ...) GCC_FORMAT_ATTR (1, 2);