anntp

a nntp implementation in pure C99
Log | Files | Refs | README | LICENSE

commit c4f111a64f40e064147610b2bc9163544af6d666
parent c852f5a5ddee8fdc235954e4ecc0f65d88b400ec
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date:   Thu,  9 Apr 2026 17:01:01 +0200

tests: don't ping eternal-september every time

Diffstat:
Mtests/run-tests.sh | 38+++++++++++++++++++++++++++++---------
Mtests/test.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
2 files changed, 84 insertions(+), 27 deletions(-)

diff --git a/tests/run-tests.sh b/tests/run-tests.sh @@ -1,14 +1,34 @@ #!/bin/sh -# -# run-tests.sh -- test runner -# +set -eu -if ! [ -f "./tests" ]; then - make -fi +# ---------------------------- +# Build +# ---------------------------- +echo "> building tests..." + +make -B + +echo "> build OK" +echo "" -LOGIN="$1" -PASS="$2" +export NNTP_HOST="${NNTP_HOST:-news.eternal-september.org}" +export NNTP_PORT_PLAIN="${NNTP_PORT_PLAIN:-119}" +export NNTP_PORT_TLS="${NNTP_PORT_TLS:-563}" +export NNTP_GROUP="${NNTP_GROUP:-eternal-september.talk}" -NNTP_USER="$LOGIN" NNTP_PASS="$PASS" ./tests +echo "> running tests..." +echo " HOST: $NNTP_HOST" +echo " GROUP: $NNTP_GROUP" +echo "" + +./tests +EXIT_CODE=$? + +echo "" +if [ $EXIT_CODE -eq 0 ]; then + echo "> all tests passed" +else + echo "> tests failed" +fi +exit $EXIT_CODE diff --git a/tests/test.c b/tests/test.c @@ -9,6 +9,44 @@ static unsigned int nfailed = 0; static unsigned int ntests = 0; +static const char* +nntp_host(void) +{ + const char* host = getenv("NNTP_HOST"); + return host ? host : "news.eternal-september.org"; +} + +static const char* +nntp_port_plain(void) +{ + const char* port = getenv("NNTP_PORT_PLAIN"); + return port ? port : "119"; +} + +static const char* +nntp_port_tls(void) +{ + const char* port = getenv("NNTP_PORT_TLS"); + return port ? port : "563"; +} + +static const char* +nntp_group(void) +{ + const char* group = getenv("NNTP_GROUP"); + return group ? group : "eternal-september.talk"; +} + +static AnntpConnection* +mktestconn(bool tls) +{ + return anntp_mkconn( + nntp_host(), + tls ? nntp_port_tls() : nntp_port_plain(), + tls + ); +} + void require(bool cond, const char* desc) { @@ -26,7 +64,7 @@ require(bool cond, const char* desc) void test_connection_tcp(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "plaintext, unencrypted connections"); anntp_freeconn(c); @@ -36,7 +74,7 @@ void test_connection_tls(void) { #ifdef ANNTP_TLS - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "563", true); + AnntpConnection* c = mktestconn(true); require(c != NULL, "encrypted connections"); anntp_freeconn(c); @@ -48,7 +86,7 @@ test_connection_tls(void) void test_read(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); char l[256] = { 0 }; require(anntp_readline(c, l, 256) > 0, "reading"); @@ -60,7 +98,7 @@ void test_read_tls(void) { #ifdef ANNTP_TLS - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "563", true); + AnntpConnection* c = mktestconn(true); require(c != NULL, "connection for read tls test"); char l[256] = { 0 }; @@ -75,8 +113,9 @@ test_read_tls(void) void test_auth(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for AUTH test"); + const char* login = getenv("NNTP_LOGIN"); const char* pass = getenv("NNTP_PASS"); if (!login || !pass) { @@ -106,7 +145,7 @@ count_cb(const char* line, void* extra) void test_list(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for LIST test"); char line[512]; @@ -131,14 +170,14 @@ test_list(void) void test_group(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for GROUP test"); char line[256]; anntp_readline(c, line, sizeof(line)); AnntpGroup g; - int err = anntp_group(c, "eternal-september.talk", &g); + int err = anntp_group(c, nntp_group(), &g); require(err == ANE_OK, "groups"); anntp_freeconn(c); @@ -147,14 +186,14 @@ test_group(void) void test_article(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for ARTICLE test"); char line[256]; anntp_readline(c, line, sizeof(line)); AnntpGroup g; - int err = anntp_group(c, "eternal-september.talk", &g); + int err = anntp_group(c, nntp_group(), &g); require(err == ANE_OK, "group for article"); AnntpArticle art; @@ -183,20 +222,18 @@ article_count_cb(const char* line, void* extra) void test_article_cb(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for ARTICLE_CB test"); char line[256]; anntp_readline(c, line, sizeof(line)); - /* select a group */ AnntpGroup g; - int err = anntp_group(c, "eternal-september.talk", &g); + int err = anntp_group(c, nntp_group(), &g); require(err == ANE_OK, "group for article_cb"); size_t count = 0; - /* try reading first available article */ err = anntp_article_cb(c, g.first, article_count_cb, &count); require(err == ANE_OK, "anntp_article_cb execution"); @@ -208,14 +245,14 @@ test_article_cb(void) void test_overview(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for OVERVIEW test"); char line[256]; anntp_readline(c, line, sizeof(line)); AnntpGroup g; - int err = anntp_group(c, "eternal-september.talk", &g); + int err = anntp_group(c, nntp_group(), &g); require(err == ANE_OK, "group for overview"); AnntpOverview ov; @@ -247,14 +284,14 @@ overview_count_cb(const char* line, void* extra) void test_overview_cb(void) { - AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false); + AnntpConnection* c = mktestconn(false); require(c != NULL, "connection for OVERVIEW_CB test"); char line[256]; anntp_readline(c, line, sizeof(line)); AnntpGroup g; - int err = anntp_group(c, "eternal-september.talk", &g); + int err = anntp_group(c, nntp_group(), &g); require(err == ANE_OK, "group for overview_cb"); size_t count = 0;