ossp-pkg/sio/sio_test.c
1.8
/*
** OSSP sio - Stream I/O
** Copyright (c) 2002-2003 Cable & Wireless Deutschland <http://www.cw.com/de/>
** Copyright (c) 2002-2003 The OSSP Project <http://www.ossp.org/>
** Copyright (c) 2002-2003 Ralf S. Engelschall <rse@engelschall.com>
**
** This file is part of OSSP sio, a layered stream I/O library
** which can be found at http://www.ossp.org/pkg/lib/sio/.
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
** the above copyright notice and this permission notice appear in all
** copies.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** sio_test.c: stream I/O library test suite
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "ts.h"
#include "al.h"
#include "sio.h"
#if ENABLE_BIO
#include <openssl/ssl.h>
#include <openssl/bio.h>
extern BIO_METHOD *BIO_s_socket();
extern sio_module_t sio_module_bio;
#endif
#if ENABLE_SA
#include "sa.h"
extern sio_module_t sio_module_sa;
#endif
extern sio_module_t sio_module_null;
extern sio_module_t sio_module_hole;
extern sio_module_t sio_module_buffer;
extern sio_module_t sio_module_zlib;
extern sio_module_t sio_module_sio;
extern sio_module_t sio_module_fd;
extern sio_module_t sio_module_hello;
extern sio_module_t sio_module_sillymux;
#define EVAL(name,rc,rc0,block) \
ts_test_check(TS_CTX, name); \
block \
if (rc != rc0) \
ts_test_fail(TS_CTX, "%s -> %d[%s] (expected %d[%s])\n", \
rc, sio_error(rc), rc0, sio_error(rc0))
#define EVAL0(name,block) EVAL(name,rc,SIO_OK,block)
sio_rc_t readloop(sio_t *sio, char *buf, size_t len, size_t *actualp)
{
sio_rc_t rc;
size_t actual, total = 0;
while (len > 0) {
rc = sio_read(sio, buf, len, &actual);
if (rc != SIO_OK)
break;
buf += actual;
total += actual;
len -= actual;
}
*actualp = total;
return rc;
}
TS_TEST(test_sio_buffer)
{
sio_rc_t rc;
sio_t *sio;
sio_stage_t *sios_buffer, *sios_hole;
size_t bufsize = 1000; /* output/input buffer size */
int i,wcount = 100;
char S[] = "Hello world\n";
size_t actual, len = strlen(S);
EVAL0("sio_create", {
rc = sio_create(&sio);
});
if (rc != SIO_OK) return;
EVAL0("sio_create_stage(&sios_hole)", {
rc = sio_create_stage(sio, &sio_module_hole, &sios_hole);
});
if (rc != SIO_OK) return;
EVAL0("sio_create_stage(&sios_buffer)", {
rc = sio_create_stage(sio, &sio_module_buffer, &sios_buffer);
});
if (rc != SIO_OK) return;
EVAL0("sio_configure_stage(sios_buffer, outputsize)", {
rc = sio_configure_stage(sio, sios_buffer, "outputsize", &bufsize);
});
if (rc != SIO_OK) return;
EVAL0("sio_attach(sios_hole)", {
rc = sio_attach(sio, sios_hole, SIO_MODE_WRITE);
});
if (rc != SIO_OK) return;
EVAL0("sio_attach(sios_buffer)", {
rc = sio_attach(sio, sios_buffer, SIO_MODE_WRITE);
});
if (rc != SIO_OK) return;
for (i=0; i<wcount; ++i) {
EVAL0("sio_write", {
rc = sio_write(sio, S, len, &actual);
});
if (rc != SIO_OK) break;
if (actual != len) {
ts_test_fail(TS_CTX, "sio_write result %d (expected %d)\n",
(int)actual, (int)len);
break;
}
}
EVAL0("sio_detach(sios_hole)", {
rc = sio_detach(sio, sios_hole);
});
EVAL0("sio_detach(sios_buffer)", {
rc = sio_detach(sio, sios_buffer);
});
EVAL0("sio_destroy_stage", {
rc = sio_destroy_stage(sio, sios_buffer);
});
EVAL0("sio_destroy_stage(sios_hole)", {
rc = sio_destroy_stage(sio, sios_hole);
});
EVAL0("sio_destroy", {
rc = sio_destroy(sio);
});
}
TS_TEST(test_sio_fd)
{
sio_rc_t rc;
sio_t *sio;
sio_stage_t *sios_buffer, *sios_fd;
size_t bufsize = 1000; /* output buffer size */
size_t buflen = 81; /* fd input buffer size */
int fd;
int i,wcount = 100;
char S[] = "Hello world\n";
char buf[sizeof(S)];
size_t actual, len = strlen(S);
char tempfile[] = "./sio_test_tmpfile.XXXXXX";
int do_unlink = 0;
EVAL0("sio_create", {
rc = sio_create(&sio);
});
if (rc != SIO_OK) return;
EVAL0("sio_create_stage(&sios_fd)", {
rc = sio_create_stage(sio, &sio_module_fd, &sios_fd);
});
if (rc != SIO_OK) return;
EVAL0("sio_create_stage(&sios_buffer)", {
rc = sio_create_stage(sio, &sio_module_buffer, &sios_buffer);
});
if (rc != SIO_OK) return;
EVAL0("sio_configure_stage(sios_fd, buflen)", {
rc = sio_configure_stage(sio, sios_fd, "buflen", &buflen);
});
if (rc != SIO_OK) return;
EVAL0("sio_configure_stage(sios_buffer)", {
rc = sio_configure_stage(sio, sios_buffer, "outputsize", &buflen);
});
if (rc != SIO_OK) return;
EVAL0("sio_configure_stage(sios_buffer, inputsize)", {
rc = sio_configure_stage(sio, sios_buffer, "inputsize", &bufsize);
});
if (rc != SIO_OK) return;
/*
* WRITE phase
*/
mktemp(tempfile);
fd = open(tempfile, O_CREAT|O_EXCL|O_WRONLY, 0600);
if (fd < 0) {
ts_test_fail(TS_CTX, "cannot create temporary file \"%s\" (%s)\n",
tempfile, strerror(errno));
} else {
do_unlink = 1;
EVAL0("sio_configure_stage(sios_fd, fd)", {
rc = sio_configure_stage(sio, sios_fd, "fd", &fd);
});
if (rc != SIO_OK) goto badwrite;
EVAL0("sio_attach(sios_fd)", {
rc = sio_attach(sio, sios_fd, SIO_MODE_READWRITE);
});
if (rc != SIO_OK) goto badwrite;
EVAL0("sio_attach(sios_buffer)", {
rc = sio_attach(sio, sios_buffer, SIO_MODE_READWRITE);
});
if (rc != SIO_OK) goto badwrite2;
for (i=0; i<wcount; ++i) {
EVAL0("sio_write", {
rc = sio_write(sio, S, len, &actual);
});
if (rc != SIO_OK) break;
if (actual != len) {
ts_test_fail(TS_CTX, "sio_write result %d (expected %d)\n",
(int)actual, (int)len);
break;
}
}
EVAL0("sio_push", {
rc = sio_push(sio);
});
EVAL0("sio_detach(sios_buffer)", {
rc = sio_detach(sio, sios_buffer);
});
badwrite2:
EVAL0("sio_detach(sios_fd)", {
rc = sio_detach(sio, sios_fd);
});
badwrite:
close(fd);
}
fd = open(tempfile, O_RDONLY);
if (fd < 0) {
ts_test_fail(TS_CTX, "cannot read temporary file \"%s\" (%s)\n",
tempfile, strerror(errno));
} else {
EVAL0("sio_configure_stage(sios_fd, fd)", {
rc = sio_configure_stage(sio, sios_fd, "fd", &fd);
});
if (rc != SIO_OK) goto badread;
EVAL0("sio_attach(sios_fd)", {
rc = sio_attach(sio, sios_fd, SIO_MODE_READWRITE);
});
if (rc != SIO_OK) goto badread;
EVAL0("sio_attach(sios_buffer)", {
rc = sio_attach(sio, sios_buffer, SIO_MODE_READWRITE);
});
if (rc != SIO_OK) goto badread2;
for (i=0; i<wcount; ++i) {
EVAL0("sio_read", {
rc = readloop(sio, buf, len, &actual);
});
if (rc != SIO_OK) break;
if (actual != len) {
ts_test_fail(TS_CTX, "sio_read result %d (expected %d)\n",
(int)actual, (int)len);
break;
}
buf[actual] = '\0';
if (strcmp(buf, S)) {
ts_test_fail(TS_CTX, "sio_read data mismatch at loop %d\n",
i);
break;
}
}
EVAL0("sio_detach(sios_buffer)", {
rc = sio_detach(sio, sios_buffer);
});
badread2:
EVAL0("sio_detach(sios_fd)", {
rc = sio_detach(sio, sios_fd);
});
badread:
close(fd);
}
if (do_unlink && unlink(tempfile) < 0) {
ts_test_fail(TS_CTX, "cannot unlink temporary file \"%s\" (%s)\n",
tempfile, strerror(errno));
}
/*
* common cleanup
*/
EVAL0("sio_destroy_stage", {
rc = sio_destroy_stage(sio, sios_buffer);
});
EVAL0("sio_destroy_stage(sios_fd)", {
rc = sio_destroy_stage(sio, sios_fd);
});
EVAL0("sio_destroy", {
rc = sio_destroy(sio);
});
}
TS_TEST(test_sio_pipe)
{
}
TS_TEST(test_sio_sio)
{
}
TS_TEST(test_sio_hello)
{
}
TS_TEST(test_sio_zlib)
{
}
#if ENABLE_SA
TS_TEST(test_sio_sa)
{
}
#endif
#if ENABLE_BIO
TS_TEST(test_sio_bio)
{
}
#endif
int main(int argc, char *argv[])
{
ts_suite_t *ts;
int n;
ts = ts_suite_new("OSSP sio (Stream I/O)");
ts_suite_test(ts, test_sio_buffer, "stream I/O buffering");
ts_suite_test(ts, test_sio_fd, "stream I/O file");
ts_suite_test(ts, test_sio_pipe, "stream I/O pipe");
ts_suite_test(ts, test_sio_sio, "stream I/O multiplexing");
ts_suite_test(ts, test_sio_hello, "stream I/O hello protocol");
#if ENABLE_ZLIB
ts_suite_test(ts, test_sio_zlib, "stream I/O zlib compression");
#endif
#if ENABLE_SA
ts_suite_test(ts, test_sio_sa, "stream I/O socket abstraction");
#endif
#if ENABLE_BIO
ts_suite_test(ts, test_sio_bio, "stream I/O ssl adapter");
#endif
n = ts_suite_run(ts);
ts_suite_free(ts);
return n;
}