nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

unittest_blast.c (2704B)


      1 #include <check.h>
      2 
      3 #include "substdio.h"
      4 
      5 /* provided by qmail-remote.c/blast.c */
      6 extern void blast();
      7 extern char inbuf[1024];
      8 extern substdio ssin;
      9 extern char smtptobuf[1024];
     10 extern substdio smtpto;
     11 
     12 static const char *readexpect;
     13 static const char *writeexpect;
     14 static size_t readoffs;
     15 static size_t writeoffs;
     16 
     17 int readstub(int fd, char *buf, int len)
     18 {
     19   size_t inlen = strlen(readexpect) - readoffs;
     20 
     21   ck_assert_int_eq(fd, -1);
     22   ck_assert_int_gt(len, 0);
     23 
     24   if (len < inlen)
     25     inlen = len;
     26 
     27   if (inlen > 0) {
     28     memcpy(buf, readexpect + readoffs, inlen);
     29     readoffs += inlen;
     30   }
     31 
     32   return inlen;
     33 }
     34 
     35 int writestub(int fd, const char *buf, int len)
     36 {
     37   size_t outlen = strlen(writeexpect) - writeoffs;
     38 
     39   ck_assert_int_eq(fd, -1);
     40   ck_assert_int_gt(len, 0);
     41 
     42   if (len < outlen)
     43     outlen = len;
     44 
     45 #if (CHECK_MAJOR_VERSION > 0) || (CHECK_MINOR_VERSION >= 11)
     46   ck_assert_mem_eq(writeexpect + writeoffs, buf, outlen);
     47 #else
     48   ck_assert_int_eq(memcmp(writeexpect + writeoffs, buf, outlen), 0);
     49 #endif
     50 
     51   writeoffs += outlen;
     52 
     53   return outlen;
     54 }
     55 
     56 static void ssin_setup(const char *indata, const char *outdata)
     57 {
     58   substdio tmpin = SUBSTDIO_FDBUF(readstub,-1,inbuf,sizeof(inbuf));
     59   substdio tmpto = SUBSTDIO_FDBUF(writestub,-1,smtptobuf,sizeof(smtptobuf));
     60   ssin = tmpin;
     61   smtpto = tmpto;
     62 
     63   readexpect = indata;
     64   readoffs = 0;
     65   writeexpect = outdata;
     66   writeoffs = 0;
     67 }
     68 
     69 START_TEST(test_blast_empty)
     70 {
     71   const char *dotcrlf = ".\r\n";
     72 
     73   ssin_setup("", dotcrlf);
     74 
     75   blast();
     76 
     77   ck_assert_uint_eq(writeoffs, strlen(dotcrlf));
     78 }
     79 END_TEST
     80 
     81 START_TEST(test_blast_dot)
     82 {
     83   const char *dotcrlf = "..\r\n.\r\n";
     84 
     85   ssin_setup(".\n", dotcrlf);
     86 
     87   blast();
     88 
     89   ck_assert_uint_eq(writeoffs, strlen(dotcrlf));
     90 }
     91 END_TEST
     92 
     93 START_TEST(test_blast_barecr)
     94 {
     95   const char *dotcrlf = "cr\r\nlf\r\n.\r\n";
     96 
     97   ssin_setup("cr\rlf\n", dotcrlf);
     98 
     99   blast();
    100 
    101   ck_assert_uint_eq(writeoffs, strlen(dotcrlf));
    102 }
    103 END_TEST
    104 
    105 START_TEST(test_blast_crlf)
    106 {
    107   const char *dotcrlf = "..\r\n.\r\n";
    108 
    109   ssin_setup(".\r\n", dotcrlf);
    110 
    111   blast();
    112 
    113   ck_assert_uint_eq(writeoffs, strlen(dotcrlf));
    114 }
    115 END_TEST
    116 
    117 TCase
    118 *blast_something(void)
    119 {
    120   TCase *tc = tcase_create("basic operations");
    121 
    122   tcase_add_test(tc, test_blast_empty);
    123   tcase_add_test(tc, test_blast_dot);
    124   tcase_add_test(tc, test_blast_barecr);
    125   tcase_add_test(tc, test_blast_crlf);
    126 
    127   return tc;
    128 }
    129 
    130 Suite
    131 *blast_suite(void)
    132 {
    133   Suite *s = suite_create("notqmail qmail-remote blast");
    134 
    135   suite_add_tcase(s, blast_something());
    136 
    137   return s;
    138 }
    139 
    140 int
    141 main(void)
    142 {
    143   int number_failed;
    144 
    145   SRunner *sr = srunner_create(blast_suite());
    146   srunner_run_all(sr, CK_NORMAL);
    147   number_failed = srunner_ntests_failed(sr);
    148   srunner_free(sr);
    149 
    150   return number_failed;
    151 }