commit 89393f236eaeddf25bdcd336c22610fdd1330abf
parent 79b9d2183507ad8e668083eebb289219cd60cbd3
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date: Sat, 21 Mar 2026 22:56:49 +0100
Add some tests
Diffstat:
| M | anntp.h | | | 1 | + |
| M | tests/test.c | | | 147 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
2 files changed, 135 insertions(+), 13 deletions(-)
diff --git a/anntp.h b/anntp.h
@@ -187,6 +187,7 @@ ANNTP_API AnntpConnection* anntp_mkconn(const char* host, const char* port, Bool
ANNTP_API void anntp_freeconn(AnntpConnection* conn);
ANNTP_API ssize_t anntp_read(AnntpConnection* conn, uchar_t* buf, size_t bufsize);
ANNTP_API ssize_t anntp_write(AnntpConnection* conn, const uchar_t* buf, size_t bufsize);
+ANNTP_API int anntp_group(AnntpConnection* conn, const char* group, AnntpGroup* out_group);
ANNTP_API ssize_t anntp_write_all(AnntpConnection* conn, const uchar_t* buf, size_t bufsize);
ANNTP_API ssize_t anntp_writeline(AnntpConnection* conn, const char* buf);
ANNTP_API ssize_t anntp_readline(AnntpConnection* conn, char* buf, size_t maxlen);
diff --git a/tests/test.c b/tests/test.c
@@ -1,19 +1,19 @@
-#undef _NDEBUG_
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+
#define ANNTP_IMPLEMENTATION
-#include <anntp.h>
+#include "anntp.h"
static unsigned int nfailed = 0;
static unsigned int ntests = 0;
void
-assert(bool cond, char* desc)
+require(bool cond, const char* desc)
{
ntests++;
-
- if (!(cond)) {
+ if (!cond) {
fprintf(stderr, "> testing `%s'... FAIL!\n", desc);
nfailed++;
} else {
@@ -24,23 +24,144 @@ assert(bool cond, char* desc)
/*********************************************************************************************************************/
void
-tests(void)
+test_connection_tcp(void)
+{
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
+ require(c != NULL, "plaintext, unencrypted connections");
+
+ anntp_freeconn(c);
+}
+
+void
+test_connection_tls(void)
+{
+#ifdef ANNTP_TLS
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "563", true);
+ require(c != NULL, "encrypted connections");
+
+ anntp_freeconn(c);
+#else
+ require(true, "skipped [TLS disabled]");
+#endif
+}
+
+void
+test_read(void)
+{
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
+
+ char l[256] = { 0 };
+ require(anntp_readline(c, l, 256) > 0, "reading");
+
+ anntp_freeconn(c);
+}
+
+void
+test_read_tls(void)
+{
+#ifdef ANNTP_TLS
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "563", true);
+ require(c != NULL, "connection for read tls test");
+
+ char l[256] = { 0 };
+ require(anntp_readline(c, l, 255) > 0, "reading line over TLS");
+
+ anntp_freeconn(c);
+#else
+ require(true, "skipped [TLS disabled]");
+#endif
+}
+
+void
+test_auth(void)
+{
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
+ require(c != NULL, "connection for AUTH test");
+ const char* login = getenv("NNTP_LOGIN");
+ const char* pass = getenv("NNTP_PASS");
+ if (!login || !pass) {
+ require(true, "skipped auth test, NNTP_LOGIN or NNTP_PASS are unset");
+ return;
+ }
+
+ char line[256];
+ anntp_readline(c, line, sizeof(line));
+
+ int err = anntp_auth(c, login, pass);
+
+ require(err == ANE_OK, "authentication");
+
+ anntp_freeconn(c);
+}
+
+void
+test_group(void)
{
-
- AnntpConnection* tc = anntp_mkconn("news.eternal-september.org", "119", true);
- assert(tc != NULL, "make connection");
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", 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);
+ require(err == ANE_OK, "groups");
+
+ anntp_freeconn(c);
+}
+
+static int
+count_cb(const char* line, void* extra)
+{
+ size_t* count = (size_t*)extra;
+ (*count)++;
+ (void)line;
+ return 0;
+}
+
+void
+test_list(void)
+{
+ AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
+ require(c != NULL, "connection for LIST test");
+
+ char line[512];
+ ssize_t n = anntp_readline(c, line, sizeof(line));
+ require(n > 0, "read greeting before LIST");
+
+ n = anntp_writeline(c, "LIST");
+ require(n > 0, "send LIST");
+
+ n = anntp_readline(c, line, sizeof(line));
+ require(n > 0, "read LIST response");
+ require(strncmp(line, "215", 3) == 0, "LIST response code");
+
+ size_t count = 0;
+ int err = anntp_readdot_cb(c, count_cb, &count);
+ require(err == ANE_OK, "anntp_readdot_cb");
+ require(count > 0, "received at least one LIST entry");
+
+ anntp_freeconn(c);
}
/*********************************************************************************************************************/
int
-main(int argc, char** argv)
+main(void)
{
anntp_init();
- (void)argc; (void)argv;
- tests();
- printf("> INFO: %d out of %d tests failed (%d succeeded).\n", nfailed, ntests, nfailed - ntests);
+ test_connection_tcp();
+ test_connection_tls();
+ test_read();
+ test_read_tls();
+ test_auth();
+ test_group();
+ test_list();
+
+ printf("> INFO: %u out of %u tests failed (%u succeeded).\n",
+ nfailed, ntests, ntests - nfailed);
+
return nfailed > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}