Back to index

opendkim  2.6.6
t-test64.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_test64_c_id[] = "@(#)$Id: t-test64.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=; h=Received:Received:\r\n\t Received:From:To:Date:Subject:Message-ID; b=bj9kVUbnBYfe9sVzH9lT45\r\n\tTFKO3eQnDbXLfgmgu/b5QgxcnhT9ojnV2IAM4KUO8+hOo5sDEu5Co/0GASH0vHpSV4P\r\n\t377Iwew3FxvLpHsVbVKgXzoKD4QSbHRpWNxyL6LypaaqFa96YqjXuYXr0vpb88hticn\r\n\t6I16//WThMz8fMU="
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 /*
00176 **  MAIN -- program mainline
00177 **
00178 **  Parameters:
00179 **     The usual.
00180 **
00181 **  Return value:
00182 **     Exit status.
00183 */
00184 
00185 int
00186 main(int argc, char **argv)
00187 {
00188 #ifdef TEST_KEEP_FILES
00189        u_int flags;
00190 #endif /* TEST_KEEP_FILES */
00191        DKIM_STAT status;
00192        DKIM *dkim;
00193        DKIM_LIB *lib;
00194        dkim_query_t qtype = DKIM_QUERY_FILE;
00195        unsigned char hdr[MAXHEADER + 1];
00196 
00197        printf("*** relaxed/simple rsa-sha1 verifying 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        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00217                            &qtype, sizeof qtype);
00218        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00219                            KEYFILE, strlen(KEYFILE));
00220 
00221        dkim = dkim_verify(lib, JOBID, NULL, &status);
00222        assert(dkim != NULL);
00223 
00224        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00225        status = dkim_header(dkim, hdr, strlen(hdr));
00226        assert(status == DKIM_STAT_OK);
00227 
00228        status = dkim_header(dkim, HEADER01, strlen(HEADER01));
00229        assert(status == DKIM_STAT_OK);
00230 
00231        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00232        assert(status == DKIM_STAT_OK);
00233 
00234        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00235        assert(status == DKIM_STAT_OK);
00236 
00237        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00238        assert(status == DKIM_STAT_OK);
00239 
00240        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00241        assert(status == DKIM_STAT_OK);
00242 
00243        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00244        assert(status == DKIM_STAT_OK);
00245 
00246        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00247        assert(status == DKIM_STAT_OK);
00248 
00249        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00250        assert(status == DKIM_STAT_OK);
00251 
00252        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00253        assert(status == DKIM_STAT_OK);
00254 
00255        status = dkim_eoh(dkim);
00256        assert(status == DKIM_STAT_OK);
00257 
00258        status = dkim_body(dkim, BODY00, strlen(BODY00));
00259        assert(status == DKIM_STAT_OK);
00260 
00261        status = dkim_body(dkim, BODY01, strlen(BODY01));
00262        assert(status == DKIM_STAT_OK);
00263 
00264        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00265        assert(status == DKIM_STAT_OK);
00266        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00267        assert(status == DKIM_STAT_OK);
00268        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00269        assert(status == DKIM_STAT_OK);
00270        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00271        assert(status == DKIM_STAT_OK);
00272        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00273        assert(status == DKIM_STAT_OK);
00274 
00275        status = dkim_body(dkim, BODY02, strlen(BODY02));
00276        assert(status == DKIM_STAT_OK);
00277 
00278        status = dkim_body(dkim, BODY03, strlen(BODY03));
00279        assert(status == DKIM_STAT_OK);
00280 
00281        status = dkim_body(dkim, BODY04, strlen(BODY04));
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, BODY03, strlen(BODY03));
00288        assert(status == DKIM_STAT_OK);
00289 
00290        status = dkim_body(dkim, BODY05, strlen(BODY05));
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_body(dkim, BODY03, strlen(BODY03));
00297        assert(status == DKIM_STAT_OK);
00298 
00299        status = dkim_eom(dkim, NULL);
00300        assert(status == DKIM_STAT_OK);
00301 
00302        status = dkim_free(dkim);
00303        assert(status == DKIM_STAT_OK);
00304 
00305        debug_dump(stdout);
00306        assert(mtsize == 0);
00307        assert(mtstack == NULL);
00308 
00309        dkim_close(lib);
00310 
00311        return 0;
00312 }