Back to index

opendkim  2.6.6
t-test113.c
Go to the documentation of this file.
00001 /*
00002 **  Copyright (c) 2005-2008 Sendmail, Inc. and its suppliers.
00003 **    All rights reserved.
00004 **
00005 **  Copyright (c) 2009, 2011, The OpenDKIM Project.  All rights reserved.
00006 */
00007 
00008 #ifndef lint
00009 static char t_test113_c_id[] = "@(#)$Id: t-test113.c,v 1.2 2009/12/08 19:14:27 cm-msk Exp $";
00010 #endif /* !lint */
00011 
00012 #include "build-config.h"
00013 
00014 /* system includes */
00015 #include <sys/types.h>
00016 #include <assert.h>
00017 #include <string.h>
00018 #include <stdio.h>
00019 
00020 #ifdef USE_GNUTLS
00021 # include <gnutls/gnutls.h>
00022 #endif /* USE_GNUTLS */
00023 
00024 /* libopendkim includes */
00025 #include "../dkim-internal.h"
00026 #include "../dkim-util.h"
00027 
00028 #define       TESTBUFRSZ    4096
00029 #define       TESTJOBID     "x"
00030 #define       TESTSTRING    "Hello, world!\n"
00031 
00032 /*
00033 **  MAIN -- program mainline
00034 **
00035 **  Parameters:
00036 **     The usual.
00037 **
00038 **  Return value:
00039 **     Exit status.
00040 */
00041 
00042 int
00043 main(int argc, char **argv)
00044 {
00045        int c;
00046        DKIM_STAT status;
00047        DKIM_LIB *lib;
00048        DKIM *dkim;
00049        struct dkim_dstring *dstring;
00050        char *p;
00051        char testbuf[TESTBUFRSZ + 1];
00052 
00053        printf("*** exercise dstring functions\n");
00054 
00055 #ifdef USE_GNUTLS
00056        (void) gnutls_global_init();
00057 #endif /* USE_GNUTLS */
00058 
00059        /* instantiate the library */
00060        lib = dkim_init(NULL, NULL);
00061        assert(lib != NULL);
00062        dkim = dkim_verify(lib, TESTJOBID, NULL, &status);
00063        assert(dkim != NULL);
00064 
00065        /* make a dstring */
00066        dstring = dkim_dstring_new(dkim, 0, 0);
00067        assert(dstring != NULL);
00068 
00069        /* confirm that it's empty */
00070        assert(dkim_dstring_len(dstring) == 0);
00071        p = dkim_dstring_get(dstring);
00072        assert(p != NULL);
00073        assert(p[0] == '\0');
00074 
00075        /* put something in it */
00076        for (c = 0; c < sizeof testbuf; c++)
00077               testbuf[c] = (random() % 94) + 32;
00078        testbuf[sizeof testbuf - 1] = '\0';
00079        assert(dkim_dstring_copy(dstring, testbuf));
00080        assert(dkim_dstring_len(dstring) == sizeof testbuf - 1);
00081        p = dkim_dstring_get(dstring);
00082        assert(p != NULL);
00083        assert(strlen(p) == sizeof testbuf - 1);
00084        assert(strcmp(p, testbuf) == 0);
00085 
00086        /* blank it */
00087        dkim_dstring_blank(dstring);
00088        p = dkim_dstring_get(dstring);
00089        assert(p != NULL);
00090        assert(p[0] == '\0');
00091 
00092        /* put something small in it using "cat" */
00093        assert(dkim_dstring_cat(dstring, TESTSTRING));
00094        p = dkim_dstring_get(dstring);
00095        assert(p != NULL);
00096        assert(strcmp(p, TESTSTRING) == 0);
00097        assert(dkim_dstring_cat(dstring, TESTSTRING));
00098        p = dkim_dstring_get(dstring);
00099        assert(p != NULL);
00100        assert(strcmp(p, TESTSTRING TESTSTRING) == 0);
00101 
00102        /* try cat1 */
00103        dkim_dstring_blank(dstring);
00104        assert(dkim_dstring_cat1(dstring, 'H'));
00105        assert(dkim_dstring_len(dstring) == 1);
00106        p = dkim_dstring_get(dstring);
00107        assert(p != NULL);
00108        assert(strcmp(p, "H") == 0);
00109 
00110        /* try catn */
00111        dkim_dstring_blank(dstring);
00112        assert(dkim_dstring_catn(dstring, TESTSTRING, 5));
00113        assert(dkim_dstring_len(dstring) == 5);
00114        p = dkim_dstring_get(dstring);
00115        assert(p != NULL);
00116        assert(strcmp(p, "Hello") == 0);
00117 
00118        /* start over */
00119        dkim_dstring_free(dstring);
00120        dstring = dkim_dstring_new(dkim, 0, (sizeof testbuf) / 2);
00121        assert(dstring != NULL);
00122 
00123        /* try an oversized append */
00124        assert(!dkim_dstring_copy(dstring, testbuf));
00125 
00126        /* clean up */
00127        dkim_dstring_free(dstring);
00128        dkim_free(dkim);
00129        dkim_close(lib);
00130 
00131        return 0;
00132 }