Back to index

opendkim  2.6.6
t-test65.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_test65_c_id[] = "@(#)$Id: t-test65.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 #include <stdlib.h>
00020 
00021 #ifdef USE_GNUTLS
00022 # include <gnutls/gnutls.h>
00023 #endif /* USE_GNUTLS */
00024 
00025 /* libopendkim includes */
00026 #include "../dkim.h"
00027 #include "t-testdata.h"
00028 
00029 #define       MAXHEADER     4096
00030 
00031 #define SIG2 "v=1; a=rsa-sha1; c=relaxed/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=ll/0h2aWgG+D3ewmE4Y3pY7Ukz8=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID;\r\n\tb=Q4G/ki/5soDXGxs43JfV+qEKDr5X3GgTDNeZqWL3zLLC5DXWWzmnKRcU8NH4Wsfkh\r\n\t o5tMo4NRmqnB2eZtozsyXdHo2ekUPLxuAQJomM4JHaPTfsraHwkibQIkPpW5hf/Rc2\r\n\t 0QgP48iQBjxqcOSn/Vwk5QDup4Qj1vgOxBqTqwdg="
00032 
00033 struct malloc_track
00034 {
00035        size_t               mt_size;
00036        void *               mt_ptr;
00037        struct malloc_track *       mt_next;
00038 };
00039 
00040 unsigned int mtsize;
00041 unsigned int mtcount;
00042 struct malloc_track *mtstack;
00043 
00044 /*
00045 **  DEBUG_INIT -- initialize tracking malloc() wrapper
00046 **
00047 **  Parameters:
00048 **     None.
00049 **
00050 **  Return value:
00051 **     None.
00052 */
00053 
00054 void
00055 debug_init(void)
00056 {
00057        mtstack = NULL;
00058        mtsize = 0;
00059        mtcount = 0;
00060 }
00061 
00062 /*
00063 **  DEBUG_MALLOC -- tracking malloc() wrapper
00064 **
00065 **  Parameters:
00066 **     closure -- memory closure (not used)
00067 **     nbytes -- how many bytes to get
00068 **
00069 **  Return value:
00070 **     Pointer to allocated memory.
00071 */
00072 
00073 void *
00074 debug_malloc(void *closure, size_t nbytes)
00075 {
00076        struct malloc_track *new;
00077        void *ptr;
00078 
00079        assert(nbytes > 0);
00080 
00081        new = (void *) malloc(sizeof(struct malloc_track));
00082        if (new == NULL)
00083               return NULL;
00084 
00085        ptr = (void *) malloc(nbytes);
00086        if (new == NULL)
00087               return NULL;
00088 
00089        new->mt_next = mtstack;
00090        new->mt_ptr = ptr;
00091        new->mt_size = nbytes;
00092        mtstack = new;
00093        mtsize++;
00094        mtcount++;
00095 
00096        return ptr;
00097 }
00098 
00099 /*
00100 **  DEBUG_FREE -- tracking wrapper for free()
00101 **
00102 **  Parameters:
00103 **     closure -- memory closure (not used)
00104 **     ptr -- pointer to free
00105 **
00106 **  Return value:
00107 **     None.
00108 */
00109 
00110 void
00111 debug_free(void *closure, void *ptr)
00112 {
00113        struct malloc_track *mt;
00114        struct malloc_track *last;
00115 
00116        assert(ptr != NULL);
00117        assert(mtstack != NULL);
00118 
00119        mt = mtstack;
00120        last = NULL;
00121        while (mt != NULL)
00122        {
00123               if (mt->mt_ptr == ptr)
00124               {
00125                      if (mt == mtstack)
00126                             mtstack = mt->mt_next;
00127                      else
00128                             last->mt_next = mt->mt_next;
00129 
00130                      free(mt);
00131                      free(ptr);
00132                      mtsize--;
00133                      return;
00134               }
00135 
00136               last = mt;
00137               mt = mt->mt_next;
00138        }
00139 
00140        assert(0);
00141 }
00142 
00143 /*
00144 **  DEBUG_DUMP -- return contents of malloc tracking
00145 **
00146 **  Parameters:
00147 **     out -- stream to which to write
00148 **
00149 **  Return value:
00150 **     None.
00151 */
00152 
00153 void
00154 debug_dump(FILE *out)
00155 {
00156        struct malloc_track *mt;
00157 
00158        assert(out != NULL);
00159 
00160        fprintf(out, "--- %u allocation(s) recorded\n", mtcount);
00161 
00162        if (mtstack != NULL)
00163        {
00164               fprintf(out, "--- %u dangling allocation(s):\n", mtsize);
00165 
00166               mt = mtstack;
00167               while (mt != NULL)
00168               {
00169                      fprintf(out, "\t%p %d\n", mt->mt_ptr, mt->mt_size);
00170                      mt = mt->mt_next;
00171               }
00172        }
00173 }
00174 /*
00175 **  MAIN -- program mainline
00176 **
00177 **  Parameters:
00178 **     The usual.
00179 **
00180 **  Return value:
00181 **     Exit status.
00182 */
00183 
00184 int
00185 main(int argc, char **argv)
00186 {
00187 #ifdef TEST_KEEP_FILES
00188        u_int flags;
00189 #endif /* TEST_KEEP_FILES */
00190        DKIM_STAT status;
00191        uint64_t fixed_time;
00192        DKIM *dkim;
00193        DKIM_LIB *lib;
00194        dkim_sigkey_t key;
00195        unsigned char hdr[MAXHEADER + 1];
00196 
00197        printf("*** relaxed/simple rsa-sha1 signing with leak detection\n");
00198 
00199        debug_init();
00200 
00201 #ifdef USE_GNUTLS
00202        (void) gnutls_global_init();
00203 #endif /* USE_GNUTLS */
00204 
00205        /* instantiate the library */
00206        lib = dkim_init(debug_malloc, debug_free);
00207        assert(lib != NULL);
00208 
00209 #ifdef TEST_KEEP_FILES
00210        /* set flags */
00211        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00212        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00213                            sizeof flags);
00214 #endif /* TEST_KEEP_FILES */
00215 
00216        key = KEY;
00217 
00218        dkim = dkim_sign(lib, JOBID, NULL, key, SELECTOR, DOMAIN,
00219                         DKIM_CANON_RELAXED, DKIM_CANON_SIMPLE,
00220                         DKIM_SIGN_RSASHA1, -1L, &status);
00221        assert(dkim != NULL);
00222 
00223        /* fix signing time */
00224        fixed_time = 1172620939;
00225        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FIXEDTIME,
00226                            &fixed_time, sizeof fixed_time);
00227 
00228        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00229        assert(status == DKIM_STAT_OK);
00230 
00231        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00232        assert(status == DKIM_STAT_OK);
00233 
00234        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00235        assert(status == DKIM_STAT_OK);
00236 
00237        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00238        assert(status == DKIM_STAT_OK);
00239 
00240        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00241        assert(status == DKIM_STAT_OK);
00242 
00243        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00244        assert(status == DKIM_STAT_OK);
00245 
00246        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00247        assert(status == DKIM_STAT_OK);
00248 
00249        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00250        assert(status == DKIM_STAT_OK);
00251 
00252        status = dkim_eoh(dkim);
00253        assert(status == DKIM_STAT_OK);
00254 
00255        status = dkim_body(dkim, BODY00, strlen(BODY00));
00256        assert(status == DKIM_STAT_OK);
00257 
00258        status = dkim_body(dkim, BODY01, strlen(BODY01));
00259        assert(status == DKIM_STAT_OK);
00260 
00261        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00262        assert(status == DKIM_STAT_OK);
00263        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00264        assert(status == DKIM_STAT_OK);
00265        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00266        assert(status == DKIM_STAT_OK);
00267        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00268        assert(status == DKIM_STAT_OK);
00269        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00270        assert(status == DKIM_STAT_OK);
00271 
00272        status = dkim_body(dkim, BODY02, strlen(BODY02));
00273        assert(status == DKIM_STAT_OK);
00274 
00275        status = dkim_body(dkim, BODY03, strlen(BODY03));
00276        assert(status == DKIM_STAT_OK);
00277 
00278        status = dkim_body(dkim, BODY04, strlen(BODY04));
00279        assert(status == DKIM_STAT_OK);
00280 
00281        status = dkim_body(dkim, BODY03, strlen(BODY03));
00282        assert(status == DKIM_STAT_OK);
00283 
00284        status = dkim_body(dkim, BODY03, strlen(BODY03));
00285        assert(status == DKIM_STAT_OK);
00286 
00287        status = dkim_body(dkim, BODY05, strlen(BODY05));
00288        assert(status == DKIM_STAT_OK);
00289 
00290        status = dkim_body(dkim, BODY03, strlen(BODY03));
00291        assert(status == DKIM_STAT_OK);
00292 
00293        status = dkim_body(dkim, BODY03, strlen(BODY03));
00294        assert(status == DKIM_STAT_OK);
00295 
00296        status = dkim_eom(dkim, NULL);
00297        assert(status == DKIM_STAT_OK);
00298 
00299        memset(hdr, '\0', sizeof hdr);
00300        status = dkim_getsighdr(dkim, hdr, sizeof hdr,
00301                                strlen(DKIM_SIGNHEADER) + 2);
00302        assert(status == DKIM_STAT_OK);
00303        assert(strcmp(SIG2, hdr) == 0);
00304 
00305        status = dkim_free(dkim);
00306        assert(status == DKIM_STAT_OK);
00307 
00308        debug_dump(stdout);
00309        assert(mtsize == 0);
00310        assert(mtstack == NULL);
00311 
00312        dkim_close(lib);
00313 
00314        return 0;
00315 }