anntp

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

test.c (3607B)


      1 #include <stdbool.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #define ANNTP_IMPLEMENTATION
      7 #include "anntp.h"
      8 
      9 static unsigned int nfailed = 0;
     10 static unsigned int ntests = 0;
     11 
     12 void
     13 require(bool cond, const char* desc)
     14 {
     15 	ntests++;
     16 	if (!cond) {
     17 		fprintf(stderr, "> testing `%s'... FAIL!\n", desc);
     18 		nfailed++;
     19 	} else {
     20 		fprintf(stderr, "> testing `%s'... ok\n", desc);
     21 	}
     22 }
     23 
     24 /*********************************************************************************************************************/
     25 
     26 void
     27 test_connection_tcp(void)
     28 {
     29 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
     30 	require(c != NULL, "plaintext, unencrypted connections");
     31 
     32 	anntp_freeconn(c);
     33 }
     34 
     35 void
     36 test_connection_tls(void)
     37 {
     38 #ifdef ANNTP_TLS
     39 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "563", true);
     40 	require(c != NULL, "encrypted connections");
     41 
     42 	anntp_freeconn(c);
     43 #else
     44 	require(true, "skipped [TLS disabled]");
     45 #endif
     46 }
     47 
     48 void
     49 test_read(void)
     50 {
     51 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
     52 
     53 	char l[256] = { 0 };
     54 	require(anntp_readline(c, l, 256) > 0, "reading");
     55 
     56 	anntp_freeconn(c);
     57 }
     58 
     59 void
     60 test_read_tls(void)
     61 {
     62 #ifdef ANNTP_TLS
     63 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "563", true);
     64 	require(c != NULL, "connection for read tls test");
     65 
     66 	char l[256] = { 0 };
     67 	require(anntp_readline(c, l, 255) > 0, "reading line over TLS");
     68 
     69 	anntp_freeconn(c);
     70 #else
     71 	require(true, "skipped [TLS disabled]");
     72 #endif
     73 }
     74 
     75 void
     76 test_auth(void)
     77 {
     78 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
     79 	require(c != NULL, "connection for AUTH test");
     80 	const char* login = getenv("NNTP_LOGIN");
     81 	const char* pass = getenv("NNTP_PASS");
     82 	if (!login || !pass) {
     83 		require(true, "skipped auth test, NNTP_LOGIN or NNTP_PASS are unset");
     84 		return;
     85 	}
     86 
     87 	char line[256];
     88 	anntp_readline(c, line, sizeof(line));
     89 
     90 	int err = anntp_auth(c, login, pass);
     91 
     92 	require(err == ANE_OK, "authentication");
     93 
     94 	anntp_freeconn(c);
     95 }
     96 
     97 static int
     98 count_cb(const char* line, void* extra)
     99 {
    100 	size_t* count = (size_t*)extra;
    101 	(*count)++;
    102 	(void)line;
    103 	return 0;
    104 }
    105 
    106 void
    107 test_list(void)
    108 {
    109 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
    110 	require(c != NULL, "connection for LIST test");
    111 
    112 	char line[512];
    113 	ssize_t n = anntp_readline(c, line, sizeof(line));
    114 	require(n > 0, "read greeting before LIST");
    115 
    116 	n = anntp_writeline(c, "LIST");
    117 	require(n > 0, "send LIST");
    118 
    119 	n = anntp_readline(c, line, sizeof(line));
    120 	require(n > 0, "read LIST response");
    121 	require(strncmp(line, "215", 3) == 0, "LIST response code");
    122 
    123 	size_t count = 0;
    124 	int err = anntp_readdot_cb(c, count_cb, &count);
    125 	require(err == ANE_OK, "anntp_readdot_cb");
    126 	require(count > 0, "received at least one LIST entry");
    127 
    128 	anntp_freeconn(c);
    129 }
    130 
    131 void
    132 test_group(void)
    133 {
    134 	AnntpConnection* c = anntp_mkconn("news.eternal-september.org", "119", false);
    135 	require(c != NULL, "connection for GROUP test");
    136 
    137 	char line[256];
    138 	anntp_readline(c, line, sizeof(line));
    139 
    140 	AnntpGroup g;
    141 	int err = anntp_group(c, "eternal-september.talk", &g);
    142 	require(err == ANE_OK, "groups");
    143 
    144 	anntp_freeconn(c);
    145 }
    146 
    147 /*********************************************************************************************************************/
    148 
    149 int
    150 main(void)
    151 {
    152 	anntp_init();
    153 
    154 	test_connection_tcp();
    155 	test_connection_tls();
    156 	test_read();
    157 	test_read_tls();
    158 	test_auth();
    159 	test_group();
    160 	test_list();
    161 
    162 	printf("> INFO: %u out of %u tests failed (%u succeeded).\n",
    163 			nfailed, ntests, ntests - nfailed);
    164 
    165 	return nfailed > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
    166 }
    167