diff --git a/src/protocol/ftp/Makefile b/src/protocol/ftp/Makefile
index bbdcedb1d..c7726f8f5 100644
--- a/src/protocol/ftp/Makefile
+++ b/src/protocol/ftp/Makefile
@@ -3,4 +3,17 @@ include $(top_builddir)/Makefile.config
OBJS = ftp.o parse.o
+TEST_PROGS = ftp-parser
+TESTDEPS = \
+ $(top_builddir)/src/protocol/ftp/parse.o \
+ $(top_builddir)/src/protocol/date.o \
+ $(top_builddir)/src/util/error.o \
+ $(top_builddir)/src/osdep/stub.o \
+ $(top_builddir)/src/util/hash.o \
+ $(top_builddir)/src/util/memdebug.o \
+ $(top_builddir)/src/util/string.o \
+ $(top_builddir)/src/util/conv.o \
+ $(top_builddir)/src/util/memory.o \
+ $(top_builddir)/src/util/time.o
+
include $(top_srcdir)/Makefile.lib
diff --git a/src/protocol/ftp/ftp.c b/src/protocol/ftp/ftp.c
index 92b79eb04..55d268fd7 100644
--- a/src/protocol/ftp/ftp.c
+++ b/src/protocol/ftp/ftp.c
@@ -1140,24 +1140,9 @@ ftp_process_dirlist(struct cache_entry *cached, off_t *pos,
int *tries, int colorize_dir, unsigned char *dircolor)
{
int ret = 0;
-#ifdef DEBUG_FTP_PARSER
- static int debug_ftp_parser = 1;
- int buflen_orig = buflen;
- unsigned char *response_orig = NULL;
- if (debug_ftp_parser) {
- buffer = get_ftp_debug_parse_responses(buffer, buflen);
- buflen = strlen(buffer);
- response_orig = buffer;
- debug_ftp_parser = 0;
- }
-
-#define end_ftp_dirlist_processing() do { mem_free_if(response_orig); } while (0)
-#define get_ftp_dirlist_offset(retval) int_min(retval, buflen_orig)
-#else
#define end_ftp_dirlist_processing() /* Nothing to free */
#define get_ftp_dirlist_offset(retval) (retval)
-#endif
while (1) {
struct ftp_file_info ftp_info = INIT_FTP_FILE_INFO;
diff --git a/src/protocol/ftp/parse.c b/src/protocol/ftp/parse.c
index e2f18f4ba..78fea3341 100644
--- a/src/protocol/ftp/parse.c
+++ b/src/protocol/ftp/parse.c
@@ -32,6 +32,9 @@
#include "util/time.h"
+/* Examples of what the FTP parser is supposed to handle (and not handle) can
+ * be found in the test-ftp-parser file. */
+
#define skip_space_end(src, end) \
do { while ((src) < (end) && *(src) == ' ') (src)++; } while (0)
@@ -128,27 +131,6 @@ parse_ftp_eplf_response(struct ftp_file_info *info, unsigned char *src, int len)
/* Parser for UNIX-style listing: */
-#ifdef DEBUG_FTP_PARSER
-static unsigned char ftp_unix_responses[] =
- /* ftp.freebsd.org response */
- "drwxrwxr-x 3 0 0 512 Apr 17 2003 pub\r\n"
- /* UNIX-style listing, without inum and without blocks: */
- "-rw-r--r-- 1 root other 531 Jan 29 03:26 README\r\n"
- "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc\r\n"
- "dr-xr-xr-x 2 root 512 Apr 8 1994 etc\r\n"
- "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin\r\n"
- /* Also produced by Microsoft's FTP servers for Windows: */
- "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z\r\n"
- "d--------- 1 owner group 0 May 9 19:45 Softlib\r\n"
- /* Also WFTPD for MSDOS: */
- "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp\r\n"
- /* Also NetWare: */
- "d [R----F--] supervisor 512 Jan 16 18:53 login\r\n"
- "- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe\r\n"
- /* Also NetPresenz for the Mac: */
- "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit\r\n"
- "drwxrwxr-x folder 2 May 10 1996 network\r\n";
-#endif
enum ftp_unix {
FTP_UNIX_PERMISSIONS,
@@ -429,15 +411,6 @@ parse_ftp_unix_response(struct ftp_file_info *info, unsigned char *src, int len)
/* Parser for VMS-style MultiNet (some spaces removed from examples): */
-#ifdef DEBUG_FTP_PARSER
-static unsigned char ftp_vms_responses[] =
- "00README.TXT;1 2 30-DEC-1996 17:44 [SYSTEM] (RWED,RWED,RE,RE)\r\n"
- "CORE.DIR;1 1 8-SEP-1996 16:09 [SYSTEM] (RWE,RWE,RE,RE)\r\n"
- /* And non-MutliNet VMS: */
- "CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)\r\n"
- /* A garbage line which should fail: */
- "EA95_0PS.GZ;1 No privilege for attempted operation\r\n";
-#endif
/* Converts VMS symbolic permissions to number-style ones, e.g. string
* RWED,RWE,RE to 755. "D" (delete) is taken to be equal to "W" (write).
@@ -554,12 +527,6 @@ parse_ftp_vms_response(struct ftp_file_info *info, unsigned char *src, int len)
/* Parser for the MSDOS-style format: */
-#ifdef DEBUG_FTP_PARSER
-static unsigned char ftp_winnt_responses[] =
- "04-27-00 09:09PM
licensed\r\n"
- "07-18-00 10:16AM pub\r\n"
- "04-14-00 03:47PM 589 readme.htm\r\n";
-#endif
struct ftp_file_info *
parse_ftp_winnt_response(struct ftp_file_info *info, unsigned char *src, int len)
@@ -639,26 +606,6 @@ parse_ftp_winnt_response(struct ftp_file_info *info, unsigned char *src, int len
return info;
}
-#ifdef DEBUG_FTP_PARSER
-unsigned char *
-get_ftp_debug_parse_responses(unsigned char *buffer, int buflen)
-{
- struct string response;
-
- if (!init_string(&response))
- return NULL;
-
- add_to_string(&response, ftp_eplf_responses);
- add_to_string(&response, ftp_unix_responses);
- add_to_string(&response, ftp_vms_responses);
- add_to_string(&response, ftp_winnt_responses);
-
- add_bytes_to_string(&response, buffer, buflen);
-
- return response.source;
-}
-#endif
-
struct ftp_file_info *
parse_ftp_file_info(struct ftp_file_info *info, unsigned char *src, int len)
diff --git a/src/protocol/ftp/parse.h b/src/protocol/ftp/parse.h
index e802895de..9e44d93ab 100644
--- a/src/protocol/ftp/parse.h
+++ b/src/protocol/ftp/parse.h
@@ -38,14 +38,4 @@ struct ftp_file_info {
struct ftp_file_info *
parse_ftp_file_info(struct ftp_file_info *info, unsigned char *src, int len);
-/* Define to feed debug FTP responses into the parser. Then point ELinks to an
- * FTP server to run the tests. */
-#if 0
-#define DEBUG_FTP_PARSER
-#endif
-
-#ifdef DEBUG_FTP_PARSER
-unsigned char *get_ftp_debug_parse_responses(unsigned char *buffer, int buflen);
-#endif
-
#endif
diff --git a/src/protocol/ftp/test-ftp-parser b/src/protocol/ftp/test-ftp-parser
new file mode 100644
index 000000000..0da91a620
--- /dev/null
+++ b/src/protocol/ftp/test-ftp-parser
@@ -0,0 +1,111 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Jonas Fonseca
+#
+
+test_description='Test parsing of FTP responses.
+
+It tests responses from several different FTP servers.
+'
+
+. "$TEST_LIB"
+
+CRNL="\r\n"
+
+test_ftp_response_expect_success () {
+ desc="$1"; shift
+ response="$1"; shift
+
+ response="$(echo "$response" | sed -n '2,$p')"
+
+ test_expect_success "$desc" \
+ "ftp-parser --response '$response'"
+}
+
+test_ftp_response_expect_failure () {
+ desc="$1"; shift
+ response="$1"; shift
+
+ response="$(echo "$response" | sed -n '2,$p')"
+
+ test_expect_failure "$desc" \
+ "ftp-parser --response '$response'"
+}
+
+#############################################################################
+#
+# Parser for UNIX-style listing:
+#
+
+test_ftp_response_expect_success \
+'ftp.freebsd.org response' \
+"
+drwxrwxr-x 3 0 0 512 Apr 17 2003 pub\r\n"
+
+test_ftp_response_expect_success \
+'UNIX-style listing, without inum and without blocks' \
+"
+-rw-r--r-- 1 root other 531 Jan 29 03:26 README\r\n
+dr-xr-xr-x 2 root other 512 Apr 8 1994 etc\r\n
+dr-xr-xr-x 2 root 512 Apr 8 1994 etc\r\n
+lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin\r\n"
+
+test_ftp_response_expect_success \
+"Response produced by Microsoft's FTP servers for Windows" \
+"
+---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z\r\n
+d--------- 1 owner group 0 May 9 19:45 Softlib\r\n"
+
+test_ftp_response_expect_success \
+'Response from WFTPD for MSDOS' \
+"
+-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp\r\n"
+
+test_ftp_response_expect_success \
+'Response from NetWare' \
+"
+d[R----F--] supervisor 512 Jan 16 18:53 login\r\n
+- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe\r\n"
+
+test_ftp_response_expect_success \
+'Response from NetPresenz for the Mac' \
+"
+-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit\r\n
+drwxrwxr-x folder 2 May 10 1996 network\r\n"
+
+
+#############################################################################
+#
+# Parser for VMS-style MultiNet (some spaces removed from examples)
+#
+
+test_ftp_response_expect_success \
+'Basic VMS responses' \
+"
+00README.TXT;1 2 30-DEC-1996 17:44 [SYSTEM] (RWED,RWED,RE,RE)\r\n
+CORE.DIR;1 1 8-SEP-1996 16:09 [SYSTEM] (RWE,RWE,RE,RE)\r\n"
+
+test_ftp_response_expect_success \
+'Response from non-MutliNet VMS' \
+"
+CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)\r\n"
+
+test_ftp_response_expect_failure \
+'A garbage line which should fail' \
+"
+EA95_0PS.GZ;1 No privilege for attempted operation\r\n"
+
+#############################################################################
+#
+# Parser for the MSDOS-style format
+#
+
+test_ftp_response_expect_success \
+'Basic MSDOS-style format' \
+"
+04-27-00 09:09PM licensed\r\n
+07-18-00 10:16AM pub\r\n
+04-14-00 03:47PM 589 readme.htm\r\n"
+
+
+test_done