This commit is contained in:
biosvos
2026-08-07 17:38:18 +09:00
commit 873193a243
9613 changed files with 2755992 additions and 0 deletions
@@ -0,0 +1,245 @@
Wed Aug 25 19:27:26 2004 Joe Orton <joe@manyfish.co.uk>
* child.c (reset_socket): New function.
Mon Jul 5 18:38:08 2004 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Print 'xfail' for expected failures.
Tue Oct 7 21:19:56 2003 Joe Orton <joe@manyfish.co.uk>
* child.c (close_socket): New function.
(server_child, spawn_server_repeat): Use it.
Fri Jul 25 12:13:59 2003 Joe Orton <joe@manyfish.co.uk>
Add support for test type which is expected to fail
memory leak checks.
* tests.h (T_XLEAKY, T_EXPECT_LEAKS): New defines.
* test.c (main) [NEON_MEMLEAK]: If T_EXPECT_LEAKS is set, fail if
the test did not leak memory.
Wed Jun 18 20:10:45 2003 Joe Orton <joe@manyfish.co.uk>
* child.c (server_child, spawn_server_repeat): Adapt for new
socket API.
Sun Mar 9 17:52:11 2003 Joe Orton <joe@manyfish.co.uk>
* test.h (T_EXPECT_FAIL): New constant.
(T_XFAIL): New test function wrapper.
* tests.c (main): Handle expected failures.
Sat Mar 1 21:04:35 2003 Joe Orton <joe@manyfish.co.uk>
Extend the ne_test structure with a 'flags' field which can
optionally request leak checking at run-time.
* tests.h (ne_test): Add 'flags' field.
(T_CHECK_LEAKS): New flag.
(T): Use T_CHECK_LEAKS flag by default.
(T_LEAKY): Like T, but with no flags set.
* tests.c (main) [NEON_MEMLEAK]: If leak checking is requested,
if a test passes, but leaks memory, fail the test.
Wed Feb 26 21:52:15 2003 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Test the "disable debugging" mode of
ne_debug_init, NE_DBG_FLUSH, and a ne_debug() with no output.
Fri Aug 23 22:54:35 2002 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Call ne_sock_init after ne_debug_init, so that
debugging messages are caught from ne_sock_init. Print a warning
message if ne_sock_init fails.
Wed Aug 21 13:29:20 2002 Joe Orton <joe@manyfish.co.uk>
* tests.h (ONCMP): New macro.
Mon Aug 19 16:53:20 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (lookup_localhost): Just use inet_addr to resolve
127.0.0.1.
Sun Aug 18 13:50:30 2002 Joe Orton <joe@manyfish.co.uk>
* tests.c (TEST_DEBUG): Add NE_DBG_SSL.
Sat Aug 10 10:19:18 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (server_send): Fix declaration.
(discard_body): Use NE_FMT_SSIZE_T for print ssize_t's.
Sat Jul 6 08:42:37 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (discard_body): New function.
Sun Jun 30 10:26:33 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (server_send): New function.
(discard_request): Fail with appropriate error.
Sun Jun 30 09:03:51 2002 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Reap server after each test has run.
Sun Jun 30 09:00:43 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (reap_server): Set `child' to 0 so child can't be reaped
twice.
Sun Jun 23 12:09:09 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (serve_file): Use large buffer when sending in chunked
mode to support large chunk sizes.
Sun Jun 23 09:35:09 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (serve_file): Use NE_FMT_OFF_T and NE_FMT_SSIZE_T.
Thu May 30 21:57:39 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (minisleep): Export function.
Sun May 19 18:23:19 2002 Joe Orton <joe@manyfish.co.uk>
* child.c, tests.c: s/sock_/ne_sock_/, s/SOCK_/NE_SOCK_/ for
socket layer API change.
* child.h (SEND_STRING): New macro.
Sun May 19 08:57:21 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (lookup_hostname): Conditionally use hstrerror().
Mon Feb 25 20:54:56 2002 Joe Orton <joe@manyfish.co.uk>
* tests.c (t_context): Use ne_vsnprintf.
Mon Feb 11 21:52:23 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (lookup_hostname): New function.
(server_child, do_listen): Pass around struct in_addr argument.
(spawn_server_addr): Renamed from spawn_server, take bind_local
flag to use localhost or "real" hostname to bind to.
(spawn_server): New function, use spawn_server
Mon Feb 11 20:51:27 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (minisleep) [HAVE_USLEEP]: Use nice short usleep()
rather than boring long sleep().
Sat Feb 2 14:15:25 2002 Joe Orton <joe@manyfish.co.uk>
* child.c (spawn_server_repeat): Exit child process (with failure)
if the server callback fails.
Fri Jan 4 22:06:17 2002 Joe Orton <joe@manyfish.co.uk>
* tests.h: Add SKIPREST result value.
* tests.c (TEST_DEBUG): Add NE_DBG_LOCKS. (main): Support
SKIPREST.
Tue Jan 1 20:36:58 2002 Joe Orton <joe@manyfish.co.uk>
* tests.h: Make test_suite symbol have external linkage.
Sat Nov 10 22:28:55 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c, test.h: Export name of test suite.
Sun Nov 4 13:56:42 2001 Joe Orton <joe@manyfish.co.uk>
* child.c (discard_request): Support retrieving arbitrary header
values from request via want_request, got_request globals.
Wed Oct 24 21:41:39 2001 Joe Orton <joe@manyfish.co.uk>
* tests.h (ONV): New macro. (ON, ONN): Redefine in terms of ONV.
Wed Oct 24 20:44:59 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (main, t_warning, segv): Use colours when stdout is a
terminal device.
Wed Oct 24 20:36:38 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (t_context, t_warning): Renamed from i_am, warning.
(t_context): Take printf varargs.
* tests.h (ONN, ON): Update, simplify.
* child.c: Update.
Tue Oct 23 22:15:17 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Vertically align results after warnings.
Tue Oct 2 23:36:44 2001 Joe Orton <joe@manyfish.co.uk>
* child.c (do_listen): Refactored from server_socket, only does
bind/listen. (spawn_server): Moved awaken/accept calls here.
(spawn_server_repeat, dead_server): New functions.
Sun Sep 30 10:14:35 2001 Joe Orton <joe@manyfish.co.uk>
* tests.h: Use a function/name structure for tests, add 'T' macro
for easily writing initializers. Rename 'name' global variable to
'test_context' to avoid parameter name collisions.
* child.c (spawn_server): Update accordingly.
* tests.c (i_am): Update accordingly. (main): Update; prettify
output using new 'name' from test structure. Cope better when all
tests in a suite are skipped.
Sat Sep 29 11:04:40 2001 Joe Orton <joe@manyfish.co.uk>
* child.c (serve_file): If 'chunks' is set in argument object,
then deliver the file as a series of chunks.
Thu Sep 27 20:28:45 2001 Joe Orton <joe@manyfish.co.uk>
* child.c (serve_file): New function.
Thu Sep 27 20:28:41 2001 Joe Orton <joe@manyfish.co.uk>
* child.c (discard_request): Reset clength.
Mon Aug 27 00:31:09 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (test_num): Expose test counter. (segv): Handle
segfault nicely.
Mon Aug 27 00:30:20 2001 Joe Orton <joe@manyfish.co.uk>
* child.c (discard_request): New function, from request.c in
neon/test.
Wed Aug 8 22:09:21 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c, test.h: Only define test_argc/argv once.
Mon Jul 16 16:30:28 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (warning): Take printf-style arguments list.
Mon Jul 16 16:16:08 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Cope with skipped tests properly.
Mon Jul 16 16:00:59 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (warning): New function. (main): Cope with warnings.
Sun Jul 8 16:09:33 2001 Joe Orton <joe@manyfish.co.uk>
* tests.c (main): Export argc/argv as test_argc, test_argv.
@@ -0,0 +1,4 @@
Simple test framework for neon; licensed under the GNU GPL.
Copyright (C) 2001-2002 Joe Orton
@@ -0,0 +1,478 @@
/*
Framework for testing with a server process
Copyright (C) 2001-2004, Joe Orton <joe@manyfish.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include "ne_socket.h"
#include "ne_utils.h"
#include "ne_string.h"
#include "tests.h"
#include "child.h"
static pid_t child = 0;
int clength;
static struct in_addr lh_addr = {0}, hn_addr = {0};
const char *want_header = NULL;
got_header_fn got_header = NULL;
char *local_hostname = NULL;
/* If we have pipe(), then use a pipe between the parent and child to
* know when the child is ready to accept incoming connections.
* Otherwise use boring sleep()s trying to avoid the race condition
* between listen() and connect() in the two processes. */
#ifdef HAVE_PIPE
#define USE_PIPE 1
#endif
int lookup_localhost(void)
{
/* this will break if a system is set up so that `localhost' does
* not resolve to 127.0.0.1, but... */
lh_addr.s_addr = inet_addr("127.0.0.1");
return OK;
}
int lookup_hostname(void)
{
char buf[BUFSIZ];
struct hostent *ent;
local_hostname = NULL;
ONV(gethostname(buf, BUFSIZ) < 0,
("gethostname failed: %s", strerror(errno)));
ent = gethostbyname(buf);
#ifdef HAVE_HSTRERROR
ONV(ent == NULL,
("could not resolve `%s': %s", buf, hstrerror(h_errno)));
#else
ONV(ent == NULL, ("could not resolve `%s'", buf));
#endif
local_hostname = ne_strdup(ent->h_name);
return OK;
}
static int do_listen(struct in_addr addr, int port)
{
int ls = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in saddr = {0};
int val = 1;
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (void *)&val, sizeof(int));
saddr.sin_addr = addr;
saddr.sin_port = htons(port);
saddr.sin_family = AF_INET;
if (bind(ls, (struct sockaddr *)&saddr, sizeof(saddr))) {
printf("bind failed: %s\n", strerror(errno));
return -1;
}
if (listen(ls, 5)) {
printf("listen failed: %s\n", strerror(errno));
return -1;
}
return ls;
}
void minisleep(void)
{
#ifdef HAVE_USLEEP
usleep(500);
#else
sleep(1);
#endif
}
int reset_socket(ne_socket *sock)
{
#ifdef SO_LINGER
/* Stevens' magic trick to send an RST on close(). */
struct linger l = {1, 0};
return setsockopt(ne_sock_fd(sock), SOL_SOCKET, SO_LINGER, &l, sizeof l);
#else
return 1;
#endif
}
/* close 'sock', performing lingering close to avoid premature RST. */
static int close_socket(ne_socket *sock)
{
#ifdef HAVE_SHUTDOWN
char buf[20];
int fd = ne_sock_fd(sock);
shutdown(fd, 0);
while (ne_sock_read(sock, buf, sizeof buf) > 0);
#endif
return ne_sock_close(sock);
}
/* This runs as the child process. */
static int server_child(int readyfd, struct in_addr addr, int port,
server_fn callback, void *userdata)
{
ne_socket *s = ne_sock_create();
int ret, listener;
in_child();
listener = do_listen(addr, port);
if (listener < 0)
return FAIL;
#ifdef USE_PIPE
/* Tell the parent we're ready for the request. */
if (write(readyfd, "a", 1) != 1) abort();
#endif
ONN("accept failed", ne_sock_accept(s, listener));
ret = callback(s, userdata);
close_socket(s);
return ret;
}
int spawn_server(int port, server_fn fn, void *ud)
{
return spawn_server_addr(1, port, fn, ud);
}
int spawn_server_addr(int bind_local, int port, server_fn fn, void *ud)
{
int fds[2];
struct in_addr addr;
addr = bind_local?lh_addr:hn_addr;
#ifdef USE_PIPE
if (pipe(fds)) {
perror("spawn_server: pipe");
return FAIL;
}
#else
/* avoid using uninitialized variable. */
fds[0] = fds[1] = 0;
#endif
child = fork();
ONN("fork server", child == -1);
if (child == 0) {
/* this is the child. */
int ret;
ret = server_child(fds[1], addr, port, fn, ud);
#ifdef USE_PIPE
close(fds[0]);
close(fds[1]);
#endif
/* print the error out otherwise it gets lost. */
if (ret) {
printf("server child failed: %s\n", test_context);
}
/* and quit the child. */
exit(ret);
} else {
char ch;
#ifdef USE_PIPE
if (read(fds[0], &ch, 1) < 0)
perror("parent read");
close(fds[0]);
close(fds[1]);
#else
minisleep();
#endif
return OK;
}
}
int spawn_server_repeat(int port, server_fn fn, void *userdata, int n)
{
int fds[2];
#ifdef USE_PIPE
if (pipe(fds)) {
perror("spawn_server: pipe");
return FAIL;
}
#else
/* avoid using uninitialized variable. */
fds[0] = fds[1] = 0;
#endif
child = fork();
if (child == 0) {
/* this is the child. */
int listener, count = 0;
in_child();
listener = do_listen(lh_addr, port);
#ifdef USE_PIPE
if (write(fds[1], "Z", 1) != 1) abort();
#endif
close(fds[1]);
close(fds[0]);
/* Loop serving requests. */
while (++count < n) {
ne_socket *sock = ne_sock_create();
int ret;
NE_DEBUG(NE_DBG_HTTP, "child awaiting connection #%d.\n", count);
ONN("accept failed", ne_sock_accept(sock, listener));
ret = fn(sock, userdata);
close_socket(sock);
NE_DEBUG(NE_DBG_HTTP, "child served request, %d.\n", ret);
if (ret) {
printf("server child failed: %s\n", test_context);
exit(-1);
}
/* don't send back notification to parent more than
* once. */
}
NE_DEBUG(NE_DBG_HTTP, "child aborted.\n");
close(listener);
exit(-1);
} else {
char ch;
/* this is the parent. wait for the child to get ready */
#ifdef USE_PIPE
if (read(fds[0], &ch, 1) < 0)
perror("parent read");
close(fds[0]);
close(fds[1]);
#else
minisleep();
#endif
}
return OK;
}
int dead_server(void)
{
int status;
if (waitpid(child, &status, WNOHANG)) {
/* child quit already! */
return FAIL;
}
NE_DEBUG(NE_DBG_HTTP, "child has not quit.\n");
return OK;
}
int await_server(void)
{
int status;
(void) wait(&status);
/* so that we aren't reaped by mistake. */
child = 0;
ONN("error from server process", WEXITSTATUS(status));
return OK;
}
int reap_server(void)
{
int status;
if (child != 0) {
(void) kill(child, SIGTERM);
minisleep();
(void) wait(&status);
child = 0;
}
return OK;
}
ssize_t server_send(ne_socket *sock, const char *str, size_t len)
{
NE_DEBUG(NE_DBG_HTTP, "Sending: %.*s\n", (int)len, str);
return ne_sock_fullwrite(sock, str, len);
}
int discard_request(ne_socket *sock)
{
char buffer[1024];
size_t offset = want_header?strlen(want_header):0;
clength = 0;
NE_DEBUG(NE_DBG_HTTP, "Discarding request...\n");
do {
ONV(ne_sock_readline(sock, buffer, 1024) < 0,
("error reading line: %s", ne_sock_error(sock)));
NE_DEBUG(NE_DBG_HTTP, "[req] %s", buffer);
if (strncasecmp(buffer, "content-length:", 15) == 0) {
clength = atoi(buffer + 16);
}
if (got_header != NULL && want_header != NULL &&
strncasecmp(buffer, want_header, offset) == 0 &&
buffer[offset] == ':') {
char *value = buffer + offset + 1;
if (*value == ' ') value++;
got_header(ne_shave(value, "\r\n"));
}
} while (strcmp(buffer, "\r\n") != 0);
return OK;
}
int discard_body(ne_socket *sock)
{
while (clength > 0) {
char buf[BUFSIZ];
size_t bytes = clength;
ssize_t ret;
if (bytes > sizeof(buf)) bytes = sizeof(buf);
NE_DEBUG(NE_DBG_HTTP, "Discarding %" NE_FMT_SIZE_T " bytes.\n",
bytes);
ret = ne_sock_read(sock, buf, bytes);
ONV(ret < 0, ("socket read failed (%" NE_FMT_SSIZE_T "): %s",
ret, ne_sock_error(sock)));
clength -= ret;
NE_DEBUG(NE_DBG_HTTP, "Got %" NE_FMT_SSIZE_T " bytes.\n", ret);
}
NE_DEBUG(NE_DBG_HTTP, "Discard successful.\n");
return OK;
}
int serve_file(ne_socket *sock, void *ud)
{
char buffer[BUFSIZ];
struct stat st;
struct serve_file_args *args = ud;
ssize_t ret;
int fd;
CALL(discard_request(sock));
ne_sock_fullread(sock, buffer, clength);
fd = open(args->fname, O_RDONLY);
if (fd < 0) {
SEND_STRING(sock,
"HTTP/1.0 404 File Not Found\r\n"
"Content-Length: 0\r\n\r\n");
return 0;
}
ONN("fstat fd", fstat(fd, &st));
SEND_STRING(sock, "HTTP/1.0 200 OK\r\n");
if (args->chunks) {
sprintf(buffer, "Transfer-Encoding: chunked\r\n");
} else {
sprintf(buffer, "Content-Length: %" NE_FMT_OFF_T "\r\n",
st.st_size);
}
if (args->headers) {
strcat(buffer, args->headers);
}
strcat(buffer, "\r\n");
SEND_STRING(sock, buffer);
NE_DEBUG(NE_DBG_HTTP, "Serving %s (%" NE_FMT_OFF_T " bytes).\n",
args->fname, st.st_size);
if (args->chunks) {
char buf[1024];
while ((ret = read(fd, &buf, args->chunks)) > 0) {
/* this is a small integer, cast it explicitly to avoid
* warnings with printing an ssize_t. */
sprintf(buffer, "%x\r\n", (unsigned int)ret);
SEND_STRING(sock, buffer);
ONN("writing body", ne_sock_fullwrite(sock, buf, ret));
SEND_STRING(sock, "\r\n");
}
SEND_STRING(sock, "0\r\n\r\n");
} else {
while ((ret = read(fd, buffer, BUFSIZ)) > 0) {
ONN("writing body", ne_sock_fullwrite(sock, buffer, ret));
}
}
ONN("error reading from file", ret < 0);
(void) close(fd);
return OK;
}
@@ -0,0 +1,117 @@
/*
Framework for testing with a server process
Copyright (C) 2001-2004, Joe Orton <joe@manyfish.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef CHILD_H
#define CHILD_H 1
#include "config.h"
#ifdef HAVE_STRING_H
#include <string.h> /* for strlen() */
#endif
#include "ne_socket.h"
/* Test which does DNS lookup on "localhost": this must be the first
* named test. */
int lookup_localhost(void);
/* Test which looks up real local hostname. */
int lookup_hostname(void);
/* set to local hostname if lookup_hostname succeeds. */
extern char *local_hostname;
/* Callback for spawn_server. */
typedef int (*server_fn)(ne_socket *sock, void *userdata);
/* Spawns server child process:
* - forks child process.
* - child process listens on localhost at given port.
* - when you connect to it, 'fn' is run...
* fn is passed the client/server socket as first argument,
* and userdata as second.
* - the socket is closed when 'fn' returns, so don't close in in 'fn'.
*/
int spawn_server(int port, server_fn fn, void *userdata);
/* Like spawn_server; if bind_local is non-zero, binds server to
* localhost, otherwise, binds server to real local hostname. (must
* have called lookup_localhost or lookup_hostname as approprate
* beforehand). */
int spawn_server_addr(int bind_local, int port, server_fn fn, void *userdata);
/* Like spawn server except will continue accepting connections and
* processing requests, up to 'n' times. If 'n' is reached, then the
* child process exits with a failure status. */
int spawn_server_repeat(int port, server_fn fn, void *userdata, int n);
/* Blocks until child process exits, and gives return code of 'fn'. */
int await_server(void);
/* Kills child process. */
int reap_server(void);
/* Returns non-zero if server process has already died. */
int dead_server(void);
/* If discard_request comes across a header called 'want_header', it
* will call got_header passing the header field value. */
extern const char *want_header;
typedef void (*got_header_fn)(char *value);
extern got_header_fn got_header;
/* Send string to child; ne_sock_fullwrite with debugging. */
ssize_t server_send(ne_socket *sock, const char *data, size_t len);
/* Utility macro: send given string down socket. */
#define SEND_STRING(sock, str) server_send((sock), (str), strlen((str)))
/* Tries to ensure that the socket will be closed using RST rather
* than FIN. */
int reset_socket(ne_socket *sock);
/* Utility function: discard request. Sets context on error. */
int discard_request(ne_socket *sock);
/* Utility function: discard request body. Sets context on error. */
int discard_body(ne_socket *sock);
struct serve_file_args {
const char *fname;
const char *headers;
int chunks;
};
/* Utility function: callback for spawn_server: pass pointer to
* serve_file_args as userdata, and args->fname is served as a 200
* request. If args->headers is non-NULL, it must be a set of
* CRLF-terminated lines which is added in to the response headers.
* If args->chunks is non-zero, the file is delivered using chunks of
* that size. */
int serve_file(ne_socket *sock, void *ud);
/* set to value of C-L header by discard_request. */
extern int clength;
/* Sleep for a short time. */
void minisleep(void);
#endif /* CHILD_H */
@@ -0,0 +1,19 @@
#!/bin/sh
rm -f debug.log
rm -f child.log
# for shared builds.
LD_LIBRARY_PATH=../src/.libs:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
for f in $*; do
if ./$f; then
:
else
echo FAILURE
exit 1
fi
done
exit 0
@@ -0,0 +1,351 @@
/*
Stupidly simple test framework
Copyright (C) 2001-2002, 2004, Joe Orton <joe@manyfish.co.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/signal.h>
#include <stdio.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "ne_string.h"
#include "ne_utils.h"
#include "ne_socket.h"
#include "tests.h"
#include "child.h"
char test_context[BUFSIZ];
int have_context = 0;
static FILE *child_debug, *debug;
char **test_argv;
int test_argc;
const char *test_suite;
int test_num;
/* statistics for all tests so far */
static int passes = 0, fails = 0, skipped = 0, warnings = 0;
/* per-test globals: */
static int warned, aborted = 0;
static const char *test_name; /* current test name */
static int use_colour = 0;
/* resource for ANSI escape codes:
* http://www.isthe.com/chongo/tech/comp/ansi_escapes.html */
#define COL(x) do { if (use_colour) printf("\033[" x "m"); } while (0)
#define NOCOL COL("00")
void t_context(const char *context, ...)
{
va_list ap;
va_start(ap, context);
ne_vsnprintf(test_context, BUFSIZ, context, ap);
va_end(ap);
have_context = 1;
}
void t_warning(const char *str, ...)
{
va_list ap;
COL("43;01"); printf("WARNING:"); NOCOL;
putchar(' ');
va_start(ap, str);
vprintf(str, ap);
va_end(ap);
warnings++;
warned++;
putchar('\n');
}
#define TEST_DEBUG \
(NE_DBG_HTTP | NE_DBG_SOCKET | NE_DBG_HTTPBODY | NE_DBG_HTTPAUTH | \
NE_DBG_LOCKS | NE_DBG_XMLPARSE | NE_DBG_XML | NE_DBG_SSL)
#define W(m) do { if (write(0, m, strlen(m)) < 0) exit(99); } while(0)
#define W_RED(m) do { if (use_colour) W("\033[41;37;01m"); \
W(m); if (use_colour) W("\033[00m\n"); } while (0);
/* Signal handler for child processes. */
static void child_segv(int signo)
{
signal(SIGSEGV, SIG_DFL);
signal(SIGABRT, SIG_DFL);
W_RED("Fatal signal in child!");
kill(getpid(), SIGSEGV);
minisleep();
}
/* Signal handler for parent process. */
static void parent_segv(int signo)
{
signal(SIGSEGV, SIG_DFL);
signal(SIGABRT, SIG_DFL);
if (signo == SIGSEGV) {
W_RED("FAILED - segmentation fault");
} else if (signo == SIGABRT) {
W_RED("ABORTED");
}
reap_server();
kill(getpid(), SIGSEGV);
minisleep();
}
void in_child(void)
{
ne_debug_init(child_debug, TEST_DEBUG);
NE_DEBUG(TEST_DEBUG, "**** Child forked for test %s ****\n", test_name);
signal(SIGSEGV, child_segv);
signal(SIGABRT, child_segv);
}
int main(int argc, char *argv[])
{
int n;
static const char dots[] = "......................";
/* get basename(argv[0]) */
test_suite = strrchr(argv[0], '/');
if (test_suite == NULL) {
test_suite = argv[0];
} else {
test_suite++;
}
#if defined(HAVE_ISATTY) && defined(STDOUT_FILENO)
if (isatty(STDOUT_FILENO)) {
use_colour = 1;
}
#endif
test_argc = argc;
test_argv = argv;
debug = fopen("debug.log", "a");
if (debug == NULL) {
fprintf(stderr, "%s: Could not open debug.log: %s\n", test_suite,
strerror(errno));
return -1;
}
child_debug = fopen("child.log", "a");
if (child_debug == NULL) {
fprintf(stderr, "%s: Could not open child.log: %s\n", test_suite,
strerror(errno));
fclose(debug);
return -1;
}
if (tests[0].fn == NULL) {
printf("-> no tests found in `%s'\n", test_suite);
return -1;
}
/* install special SEGV handler. */
signal(SIGSEGV, parent_segv);
signal(SIGABRT, parent_segv);
/* test the "no-debugging" mode of ne_debug. */
ne_debug_init(NULL, 0);
NE_DEBUG(TEST_DEBUG, "This message should go to /dev/null");
/* enable debugging for real. */
ne_debug_init(debug, TEST_DEBUG);
NE_DEBUG(TEST_DEBUG | NE_DBG_FLUSH, "Version string: %s\n",
ne_version_string());
/* another silly test. */
NE_DEBUG(0, "This message should also go to /dev/null");
if (ne_sock_init()) {
COL("43;01"); printf("WARNING:"); NOCOL;
printf(" Socket library initalization failed.\n");
}
printf("-> running `%s':\n", test_suite);
for (n = 0; !aborted && tests[n].fn != NULL; n++) {
int result, is_xfail = 0;
#ifdef NEON_MEMLEAK
size_t allocated = ne_alloc_used;
int is_xleaky = 0;
#endif
test_name = tests[n].name;
printf("%2d. %s%.*s ", n, test_name,
(int) (strlen(dots) - strlen(test_name)), dots);
have_context = 0;
test_num = n;
warned = 0;
fflush(stdout);
NE_DEBUG(TEST_DEBUG, "******* Running test %d: %s ********\n",
n, test_name);
/* run the test. */
result = tests[n].fn();
#ifdef NEON_MEMLEAK
/* issue warnings for memory leaks, if requested */
if ((tests[n].flags & T_CHECK_LEAKS) && result == OK &&
ne_alloc_used > allocated) {
t_context("memory leak of %" NE_FMT_SIZE_T " bytes",
ne_alloc_used - allocated);
fprintf(debug, "Blocks leaked: ");
ne_alloc_dump(debug);
result = FAIL;
} else if (tests[n].flags & T_EXPECT_LEAKS && result == OK &&
ne_alloc_used == allocated) {
t_context("expected memory leak not detected");
result = FAIL;
} else if (tests[n].flags & T_EXPECT_LEAKS && result == OK) {
fprintf(debug, "Blocks leaked (expected): ");
ne_alloc_dump(debug);
is_xleaky = 1;
}
#endif
if (tests[n].flags & T_EXPECT_FAIL) {
if (result == OK) {
t_context("test passed but expected failure");
result = FAIL;
} else if (result == FAIL) {
result = OK;
is_xfail = 1;
}
}
/* align the result column if we've had warnings. */
if (warned) {
printf(" %s ", dots);
}
switch (result) {
case OK:
if (is_xfail) {
COL("32;07");
printf("xfail");
} else {
COL("32");
printf("pass");
}
NOCOL;
if (warned) {
printf(" (with %d warning%s)", warned, (warned > 1)?"s":"");
}
#ifdef NEON_MEMLEAK
if (is_xleaky) {
printf(" (with expected leak, %" NE_FMT_SIZE_T " bytes)",
ne_alloc_used - allocated);
}
#endif
putchar('\n');
passes++;
break;
case FAILHARD:
aborted = 1;
/* fall-through */
case FAIL:
COL("41;37;01"); printf("FAIL"); NOCOL;
if (have_context) {
printf(" (%s)", test_context);
}
putchar('\n');
fails++;
break;
case SKIPREST:
aborted = 1;
/* fall-through */
case SKIP:
COL("44;37;01"); printf("SKIPPED"); NOCOL;
if (have_context) {
printf(" (%s)", test_context);
}
putchar('\n');
skipped++;
break;
default:
COL("41;37;01"); printf("OOPS"); NOCOL;
printf(" unexpected test result `%d'\n", result);
break;
}
reap_server();
}
/* discount skipped tests */
if (skipped) {
printf("-> %d %s.\n", skipped,
skipped==1?"test was skipped":"tests were skipped");
n -= skipped;
if (passes + fails != n) {
printf("-> ARGH! Number of test results does not match "
"number of tests.\n"
"-> ARGH! Test Results are INRELIABLE.\n");
}
}
/* print the summary. */
if (skipped && n == 0) {
printf("<- all tests skipped for `%s'.\n", test_suite);
} else {
printf("<- summary for `%s': "
"of %d tests run: %d passed, %d failed. %.1f%%\n",
test_suite, n, passes, fails, 100*(float)passes/n);
if (warnings) {
printf("-> %d warning%s issued.\n", warnings,
warnings==1?" was":"s were");
}
}
if (fclose(debug)) {
fprintf(stderr, "Error closing debug.log: %s\n", strerror(errno));
fails = 1;
}
if (fclose(child_debug)) {
fprintf(stderr, "Error closing child.log: %s\n", strerror(errno));
fails = 1;
}
ne_sock_exit();
return fails;
}
@@ -0,0 +1,128 @@
/*
Stupidly simple test framework
Copyright (C) 2001-2005, Joe Orton <joe@manyfish.co.uk>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#ifndef TESTS_H
#define TESTS_H 1
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <stdio.h>
#define OK 0
#define FAIL 1
#define FAILHARD 2 /* fail and skip all succeeding tests in this suite. */
#define SKIP 3 /* test was skipped because precondition was not met */
#define SKIPREST 4 /* skipped, and skip all succeeding tests in suite */
/* A test function. Must return any of OK, FAIL, FAILHARD, SKIP, or
* SKIPREST. May call t_warning() any number of times. If not
* returning OK, optionally call t_context to provide an error
* message. */
typedef int (*test_func)(void);
typedef struct {
test_func fn; /* the function to test. */
const char *name; /* the name of the test. */
int flags;
} ne_test;
/* possible values for flags: */
#define T_CHECK_LEAKS (1) /* check for memory leaks */
#define T_EXPECT_FAIL (2) /* expect failure */
#define T_EXPECT_LEAKS (4) /* expect memory leak failures */
/* array of tests to run: must be defined by each test suite. */
extern ne_test tests[];
/* define a test function which has the same name as the function,
* and does check for memory leaks. */
#define T(fn) { fn, #fn, T_CHECK_LEAKS }
/* define a test function which is expected to return FAIL. */
#define T_XFAIL(fn) { fn, #fn, T_EXPECT_FAIL | T_CHECK_LEAKS }
/* define a test function which isn't checked for memory leaks. */
#define T_LEAKY(fn) { fn, #fn, 0 }
/* define a test function which is expected to fail memory leak checks */
#define T_XLEAKY(fn) { fn, #fn, T_EXPECT_LEAKS }
/* current test number */
extern int test_num;
/* name of test suite */
extern const char *test_suite;
/* Provide result context message. */
void t_context(const char *ctx, ...)
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif /* __GNUC__ */
;
extern char test_context[];
/* the command-line arguments passed in to the test suite: */
extern char **test_argv;
extern int test_argc;
/* child process should call this. */
void in_child(void);
/* issue a warning. */
void t_warning(const char *str, ...)
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif /* __GNUC__ */
;
/* Macros for easily writing is-not-zero comparison tests; the ON*
* macros fail the function if a comparison is not zero.
*
* ONV(x,vs) takes a comparison X, and a printf varargs list for
* the failure message.
* e.g. ONV(strcmp(bar, "foo"), ("bar was %s not 'foo'", bar))
*
* ON(x) takes a comparison X, and uses the line number for the failure
* message. e.g. ONV(strcmp(bar, "foo"))
*
* ONN(n, x) takes a comparison X, and a flat string failure message.
* e.g. ONN("foo was wrong", strcmp(bar, "foo")) */
#define ONV(x,vs) do { if ((x)) { t_context vs; return FAIL; } } while (0)
#define ON(x) ONV((x), ("line %d", __LINE__ ))
#define ONN(n,x) ONV((x), (n))
/* ONCMP(exp, act, ctx, name): 'exp' is the expected string, 'act' is the
* actual string for some field 'name'. Succeeds if strcmp(exp,act)
* == 0 or both are NULL. */
#define ONCMP(exp, act, ctx, name) do { \
ONV(exp && !act, ("%s: " name " was NULL, expected '%s'", ctx, exp)); \
ONV(!exp && act, ("%s: " name " was '%s', expected NULL", ctx, act)); \
ONV(exp && strcmp(exp, act), ("%s: " name " was '%s' not '%s'", ctx, act, exp)); \
} while (0)
/* return immediately with result of test 'x' if it fails. */
#define CALL(x) do { int t_ret = (x); if (t_ret != OK) return t_ret; } while (0)
/* PRECOND: skip current test if condition 'x' is not true. */
#define PRECOND(x) do { if (!(x)) { return SKIP; } } while (0)
#endif /* TESTS_H */