mirror of
https://github.com/rkd77/elinks.git
synced 2025-01-03 14:57:44 -05:00
Make a test-ftp-parser test script and remove the 'built-in' one
This commit is contained in:
parent
9debe3b3ae
commit
d048bb4888
@ -3,4 +3,18 @@ include $(top_builddir)/Makefile.config
|
||||
|
||||
OBJS = ftp.o parse.o
|
||||
|
||||
TEST_PROGS = ftp-parser
|
||||
|
||||
TESTDEPS = \
|
||||
$(top_builddir)/src/osdep/stub.o \
|
||||
$(top_builddir)/src/protocol/date.o \
|
||||
$(top_builddir)/src/protocol/ftp/parse.o \
|
||||
$(top_builddir)/src/util/conv.o \
|
||||
$(top_builddir)/src/util/error.o \
|
||||
$(top_builddir)/src/util/hash.o \
|
||||
$(top_builddir)/src/util/memdebug.o \
|
||||
$(top_builddir)/src/util/memory.o \
|
||||
$(top_builddir)/src/util/string.o \
|
||||
$(top_builddir)/src/util/time.o
|
||||
|
||||
include $(top_srcdir)/Makefile.lib
|
||||
|
69
src/protocol/ftp/ftp-parser.c
Normal file
69
src/protocol/ftp/ftp-parser.c
Normal file
@ -0,0 +1,69 @@
|
||||
/* Tool for testing the FTP parser */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "elinks.h"
|
||||
|
||||
#include "protocol/ftp/parse.h"
|
||||
|
||||
|
||||
void die(const char *msg, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
if (msg) {
|
||||
va_start(args, msg);
|
||||
vfprintf(stderr, msg, args);
|
||||
fputs("\n", stderr);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
exit(!!NULL);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct ftp_file_info ftp_info = INIT_FTP_FILE_INFO;
|
||||
unsigned char *response = "";
|
||||
int responselen = 0;
|
||||
int i;
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
char *arg = argv[i];
|
||||
|
||||
if (strncmp(arg, "--", 2))
|
||||
break;
|
||||
|
||||
arg += 2;
|
||||
|
||||
if (!strncmp(arg, "response", 8)) {
|
||||
arg += 8;
|
||||
if (*arg == '=') {
|
||||
arg++;
|
||||
response = arg;
|
||||
} else {
|
||||
i++;
|
||||
if (i >= argc)
|
||||
die("--response expects a string");
|
||||
response = argv[i];
|
||||
}
|
||||
responselen = strlen(response);
|
||||
|
||||
} else {
|
||||
die("Unknown argument '%s'", arg - 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (parse_ftp_file_info(&ftp_info, response, responselen))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
@ -1140,24 +1140,6 @@ 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;
|
||||
@ -1180,8 +1162,7 @@ ftp_process_dirlist(struct cache_entry *cached, off_t *pos,
|
||||
if (bufp && buf[bufp - 1] == ASCII_CR) bufp--;
|
||||
} else {
|
||||
if (!bufp || (!last && bufl < FTP_BUF_SIZE)) {
|
||||
end_ftp_dirlist_processing();
|
||||
return get_ftp_dirlist_offset(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret += bufp;
|
||||
@ -1200,8 +1181,7 @@ ftp_process_dirlist(struct cache_entry *cached, off_t *pos,
|
||||
retv = display_dir_entry(cached, pos, tries, colorize_dir,
|
||||
dircolor, &ftp_info);
|
||||
if (retv < 0) {
|
||||
end_ftp_dirlist_processing();
|
||||
return get_ftp_dirlist_offset(ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG_FTP_PARSER
|
||||
|
@ -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)
|
||||
|
||||
@ -59,16 +62,7 @@ parse_ftp_number(unsigned char **src, unsigned char *end, long from, long to)
|
||||
/* Parser for the EPLF format (see http://pobox.com/~djb/proto/eplf.txt).
|
||||
*
|
||||
* Lines end with \r\n (CR-LF), but that is handled elsewhere.
|
||||
*
|
||||
* Some example EPLF response, with the filename separator (tab) displayed as a
|
||||
* space:
|
||||
*/
|
||||
#ifdef DEBUG_FTP_PARSER
|
||||
static unsigned char ftp_eplf_responses[] =
|
||||
"+i8388621.48594,m825718503,r,s280,\tdjb.html\r\n"
|
||||
"+i8388621.50690,m824255907,/,\t514\r\n"
|
||||
"+i8388621.48598,m824253270,r,s612,\t514.html\r\n";
|
||||
#endif
|
||||
|
||||
enum ftp_eplf {
|
||||
FTP_EPLF_FILENAME = ASCII_TAB, /* Filename follows */
|
||||
@ -128,27 +122,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 +402,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 +518,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 <DIR> licensed\r\n"
|
||||
"07-18-00 10:16AM <DIR> 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 +597,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)
|
||||
|
@ -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
|
||||
|
123
src/protocol/ftp/test-ftp-parser
Normal file
123
src/protocol/ftp/test-ftp-parser
Normal file
@ -0,0 +1,123 @@
|
||||
#!/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"
|
||||
|
||||
|
||||
#############################################################################
|
||||
#
|
||||
# EPLF response
|
||||
#
|
||||
|
||||
test_ftp_response_expect_success \
|
||||
'EPLF responses' \
|
||||
$'
|
||||
+i8388621.48594,m825718503,r,s280,\tdjb.html\r\n
|
||||
+i8388621.50690,m824255907,/,\t514\r\n
|
||||
+i8388621.48598,m824253270,r,s612,\t514.html\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 <DIR> licensed\r\n
|
||||
07-18-00 10:16AM <DIR> pub\r\n
|
||||
04-14-00 03:47PM 589 readme.htm\r\n"
|
||||
|
||||
|
||||
test_done
|
Loading…
Reference in New Issue
Block a user