Back to index

opendkim  2.6.4
t-test57.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_test57_c_id[] = "@(#)$Id: t-test57.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 <sys/time.h>
00017 #include <assert.h>
00018 #include <string.h>
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 #ifdef USE_GNUTLS
00023 # include <gnutls/gnutls.h>
00024 #endif /* USE_GNUTLS */
00025 
00026 /* libopendkim includes */
00027 #include "../dkim.h"
00028 #include "t-testdata.h"
00029 #include "../dkim-strl.h"
00030 
00031 #define       MAXHEADER     4096
00032 
00033 #define SIG2 "v=1; a=rsa-sha256; c=relaxed/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=TRy6u0KYixKENCPL95G1Y1uwclQ9rWthX6rFgE7gWb8=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID;\r\n\tb=aMDMwcz08Na0zYLrEZnUMBUipMh3blE+pWiBSvX+t2ynaDUGTqd27xuR5JddL9y1T\r\n\t 6h0LkUMIcijsPBWa1WWfKL23QceksV75Y1/Tt6gmVi3SdVAoJbbZImrOaAQ3YQAn64\r\n\t dp1vI0Z8gSashYlwy+7gtmVSLAj/fpcAF6yNDPuE="
00034 
00035 /*
00036 **  MAIN -- program mainline
00037 **
00038 **  Parameters:
00039 **     The usual.
00040 **
00041 **  Return value:
00042 **     Exit status.
00043 */
00044 
00045 int
00046 main(int argc, char **argv)
00047 {
00048        u_int wlen = LARGEBODYSIZE;
00049        u_int remain;
00050 #ifdef TEST_KEEP_FILES
00051        u_int flags;
00052 #endif /* TEST_KEEP_FILES */
00053        DKIM_STAT status;
00054        uint64_t fixed_time;
00055        u_char *start;
00056        u_char *end;
00057        DKIM *dkim;
00058        DKIM_LIB *lib;
00059        dkim_sigkey_t key;
00060        struct timeval t;
00061        unsigned char hdr[MAXHEADER + 1];
00062        unsigned char body[LARGEBODYSIZE];
00063 
00064 #ifdef USE_GNUTLS
00065        (void) gnutls_global_init();
00066 #endif /* USE_GNUTLS */
00067 
00068        /* instantiate the library */
00069        lib = dkim_init(NULL, NULL);
00070        assert(lib != NULL);
00071 
00072        if (!dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00073        {
00074               printf("*** relaxed/simple rsa-sha256 signing random chunk sizes SKIPPED\n");
00075               dkim_close(lib);
00076               return 0;
00077        }
00078 
00079        printf("*** relaxed/simple rsa-sha256 signing random chunk sizes\n");
00080 
00081 #ifdef TEST_KEEP_FILES
00082        /* set flags */
00083        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00084        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00085                            sizeof flags);
00086 #endif /* TEST_KEEP_FILES */
00087 
00088        key = KEY;
00089 
00090        dkim = dkim_sign(lib, JOBID, NULL, key, SELECTOR, DOMAIN,
00091                         DKIM_CANON_RELAXED, DKIM_CANON_SIMPLE,
00092                         DKIM_SIGN_RSASHA256, -1L, &status);
00093        assert(dkim != NULL);
00094 
00095        /* fix signing time */
00096        fixed_time = 1172620939;
00097        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FIXEDTIME,
00098                            &fixed_time, sizeof fixed_time);
00099 
00100        status = dkim_header(dkim, HEADER02, strlen(HEADER02));
00101        assert(status == DKIM_STAT_OK);
00102 
00103        status = dkim_header(dkim, HEADER03, strlen(HEADER03));
00104        assert(status == DKIM_STAT_OK);
00105 
00106        status = dkim_header(dkim, HEADER04, strlen(HEADER04));
00107        assert(status == DKIM_STAT_OK);
00108 
00109        status = dkim_header(dkim, HEADER05, strlen(HEADER05));
00110        assert(status == DKIM_STAT_OK);
00111 
00112        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00113        assert(status == DKIM_STAT_OK);
00114 
00115        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00116        assert(status == DKIM_STAT_OK);
00117 
00118        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00119        assert(status == DKIM_STAT_OK);
00120 
00121        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00122        assert(status == DKIM_STAT_OK);
00123 
00124        status = dkim_eoh(dkim);
00125        assert(status == DKIM_STAT_OK);
00126 
00127        memset(body, '\0', sizeof body);
00128 
00129        while (wlen > strlen(GIBBERISH))
00130        {
00131               strlcat(body, GIBBERISH, sizeof body);
00132               wlen -= strlen(GIBBERISH);
00133        }
00134 
00135        /* random-length chunks */
00136        (void) gettimeofday(&t, NULL);
00137        srandom(t.tv_usec);
00138        start = body;
00139        remain = strlen(body);
00140        end = body + remain;
00141        while (start < end)
00142        {
00143               wlen = random() % sizeof body + 1;
00144               if (wlen > remain)
00145                      wlen = remain;
00146               printf("%d... ", wlen);
00147               status = dkim_body(dkim, start, wlen);
00148               assert(status == DKIM_STAT_OK);
00149               start += wlen;
00150               remain -= wlen;
00151        }
00152 
00153        printf("done!\n");
00154 
00155        status = dkim_body(dkim, CRLF, strlen(CRLF));
00156        assert(status == DKIM_STAT_OK);
00157 
00158        status = dkim_eom(dkim, NULL);
00159        assert(status == DKIM_STAT_OK);
00160 
00161        memset(hdr, '\0', sizeof hdr);
00162        status = dkim_getsighdr(dkim, hdr, sizeof hdr,
00163                                strlen(DKIM_SIGNHEADER) + 2);
00164        assert(status == DKIM_STAT_OK);
00165        assert(strcmp(SIG2, hdr) == 0);
00166 
00167        status = dkim_free(dkim);
00168        assert(status == DKIM_STAT_OK);
00169 
00170        dkim_close(lib);
00171 
00172        return 0;
00173 }