Back to index

opendkim  2.6.6
t-test70.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_test70_c_id[] = "@(#)$Id: t-test70.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.h"
00026 #include "t-testdata.h"
00027 
00028 #define       MAXHEADER     4096
00029 #define       MAXHDRCNT     64
00030 
00031 #define SIG2 "v=1; a=rsa-sha256; c=relaxed/simple; d=example.com; s=test;\r\n\tt=1172620939; bh=yHBAX+3IwxTZIynBuB/5tlsBInJq9n8qz5fgAycHi80=;\r\n\th=Received:Received:Received:From:To:Date:Subject:Message-ID; z=Rec\r\n\teived:=20received=20data=201|Received:=20received=20data=202|Receiv\r\n\ted:=20received=20data=203=20part=201=0D=0A=09=20data=203=20part=202\r\n\t|From:=20Murray=20S.=20Kucherawy=20<msk@sendmail.com>|To:=20Sendmai\r\n\tl=20Test=20Address=20<sa-test@sendmail.net>|Date:=20Thu,=2005=20May\r\n\t=202005=2011:59:09=20-0700|Subject:=20DKIM=20test=20message|Message\r\n\t-ID:=20<439094BF.5010709@sendmail.com>; b=UfBRGUZXr6mCdxVNeavejTTWd\r\n\tWwZWarsUi90kj6K7AJWy4IWhYDpLPCt5tEYIQa4A6B/SkXHremA1QORVn8SW+7Z9xP4\r\n\tLNReV78biYbUnlncfHrEL3K7G3rR5bpa3bfNhdtGSVItLAg/f2XjHqiOQztjz1i4C/p\r\n\tD/pBm8XSWzlA="
00032 
00033 #define       ALTHEADER02   "Received: received data 1 fghij"
00034 #define       ALTHEADER03   "Received: received data 2 klmno"
00035 #define       ALTHEADER04   "Received: received data 3 part 1\r\n\t data 3 part 2 pqrst"
00036 #define ALTHEADER05  "From: Murray S Kucherawy <msk@sendmail.com>"
00037 
00038 /*
00039 **  MAIN -- program mainline
00040 **
00041 **  Parameters:
00042 **     The usual.
00043 **
00044 **  Return value:
00045 **     Exit status.
00046 */
00047 
00048 int
00049 main(int argc, char **argv)
00050 {
00051 #ifdef TEST_KEEP_FILES
00052        u_int flags;
00053 #endif /* TEST_KEEP_FILES */
00054        int nhdrs;
00055        int nsigs;
00056        int ndiffs = 0;
00057        DKIM_STAT status;
00058        DKIM *dkim;
00059        DKIM_LIB *lib;
00060        DKIM_SIGINFO **sigs;
00061        struct dkim_hdrdiff *diffs = NULL;
00062        dkim_canon_t hc;
00063        dkim_query_t qtype = DKIM_QUERY_FILE;
00064        unsigned char hdr[MAXHEADER + 1];
00065        unsigned char *ohdrs[MAXHDRCNT];
00066 
00067 #ifdef USE_GNUTLS
00068        (void) gnutls_global_init();
00069 #endif /* USE_GNUTLS */
00070 
00071        /* instantiate the library */
00072        lib = dkim_init(NULL, NULL);
00073        assert(lib != NULL);
00074 
00075        if (!dkim_libfeature(lib, DKIM_FEATURE_DIFFHEADERS) ||
00076            !dkim_libfeature(lib, DKIM_FEATURE_SHA256))
00077        {
00078               printf("*** relaxed/simple rsa-sha256 verifying with header diffing SKIPPED\n");
00079               dkim_close(lib);
00080               return 0;
00081        }
00082 
00083        printf("*** relaxed/simple rsa-sha256 verifying with header diffing\n");
00084 
00085 #ifdef TEST_KEEP_FILES
00086        /* set flags */
00087        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00088        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00089                            sizeof flags);
00090 #endif /* TEST_KEEP_FILES */
00091 
00092        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00093                            &qtype, sizeof qtype);
00094        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00095                            KEYFILE, strlen(KEYFILE));
00096 
00097        dkim = dkim_verify(lib, JOBID, NULL, &status);
00098        assert(dkim != NULL);
00099 
00100        snprintf(hdr, sizeof hdr, "%s: %s", DKIM_SIGNHEADER, SIG2);
00101        status = dkim_header(dkim, hdr, strlen(hdr));
00102        assert(status == DKIM_STAT_OK);
00103 
00104        status = dkim_header(dkim, ALTHEADER02, strlen(ALTHEADER02));
00105        assert(status == DKIM_STAT_OK);
00106 
00107        status = dkim_header(dkim, ALTHEADER03, strlen(ALTHEADER03));
00108        assert(status == DKIM_STAT_OK);
00109 
00110        status = dkim_header(dkim, ALTHEADER04, strlen(ALTHEADER04));
00111        assert(status == DKIM_STAT_OK);
00112 
00113        status = dkim_header(dkim, ALTHEADER05, strlen(ALTHEADER05));
00114        assert(status == DKIM_STAT_OK);
00115 
00116        status = dkim_header(dkim, HEADER06, strlen(HEADER06));
00117        assert(status == DKIM_STAT_OK);
00118 
00119        status = dkim_header(dkim, HEADER07, strlen(HEADER07));
00120        assert(status == DKIM_STAT_OK);
00121 
00122        status = dkim_header(dkim, HEADER08, strlen(HEADER08));
00123        assert(status == DKIM_STAT_OK);
00124 
00125        status = dkim_header(dkim, HEADER09, strlen(HEADER09));
00126        assert(status == DKIM_STAT_OK);
00127 
00128        status = dkim_eoh(dkim);
00129        assert(status == DKIM_STAT_OK);
00130 
00131        status = dkim_body(dkim, BODY00, strlen(BODY00));
00132        assert(status == DKIM_STAT_OK);
00133 
00134        status = dkim_body(dkim, BODY01, strlen(BODY01));
00135        assert(status == DKIM_STAT_OK);
00136 
00137        status = dkim_body(dkim, BODY01A, strlen(BODY01A));
00138        assert(status == DKIM_STAT_OK);
00139        status = dkim_body(dkim, BODY01B, strlen(BODY01B));
00140        assert(status == DKIM_STAT_OK);
00141        status = dkim_body(dkim, BODY01C, strlen(BODY01C));
00142        assert(status == DKIM_STAT_OK);
00143        status = dkim_body(dkim, BODY01D, strlen(BODY01D));
00144        assert(status == DKIM_STAT_OK);
00145        status = dkim_body(dkim, BODY01E, strlen(BODY01E));
00146        assert(status == DKIM_STAT_OK);
00147 
00148        status = dkim_body(dkim, BODY02, strlen(BODY02));
00149        assert(status == DKIM_STAT_OK);
00150 
00151        status = dkim_body(dkim, BODY03, strlen(BODY03));
00152        assert(status == DKIM_STAT_OK);
00153 
00154        status = dkim_body(dkim, BODY04, strlen(BODY04));
00155        assert(status == DKIM_STAT_OK);
00156 
00157        status = dkim_body(dkim, BODY03, strlen(BODY03));
00158        assert(status == DKIM_STAT_OK);
00159 
00160        status = dkim_body(dkim, BODY03, strlen(BODY03));
00161        assert(status == DKIM_STAT_OK);
00162 
00163        status = dkim_body(dkim, BODY05, strlen(BODY05));
00164        assert(status == DKIM_STAT_OK);
00165 
00166        status = dkim_body(dkim, BODY03, strlen(BODY03));
00167        assert(status == DKIM_STAT_OK);
00168 
00169        status = dkim_body(dkim, BODY03, strlen(BODY03));
00170        assert(status == DKIM_STAT_OK);
00171 
00172        status = dkim_eom(dkim, NULL);
00173        assert(status == DKIM_STAT_BADSIG);
00174 
00175        status = dkim_getsiglist(dkim, &sigs, &nsigs);
00176        assert(status == DKIM_STAT_OK);
00177        assert(sigs != NULL);
00178        assert(sigs[0] != NULL);
00179        assert(nsigs == 1);
00180 
00181        nhdrs = MAXHDRCNT;
00182 
00183        status = dkim_sig_getcanons(sigs[0], &hc, NULL);
00184        assert(status == DKIM_STAT_OK);
00185        assert(hc == DKIM_CANON_RELAXED);
00186 
00187        status = dkim_ohdrs(dkim, sigs[0], ohdrs, &nhdrs);
00188        assert(status == DKIM_STAT_OK);
00189        assert(nhdrs == 8);
00190        assert(strcmp(ohdrs[0], HEADER02) == 0);
00191        assert(strcmp(ohdrs[1], HEADER03) == 0);
00192        assert(strcmp(ohdrs[2], HEADER04) == 0);
00193        assert(strcmp(ohdrs[3], HEADER05) == 0);
00194        assert(strcmp(ohdrs[4], HEADER06) == 0);
00195        assert(strcmp(ohdrs[5], HEADER07) == 0);
00196        assert(strcmp(ohdrs[6], HEADER08) == 0);
00197        assert(strcmp(ohdrs[7], HEADER09) == 0);
00198 
00199        assert(dkim_sig_getcanons(sigs[0], &hc, NULL) == DKIM_STAT_OK);
00200        status = dkim_diffheaders(dkim, hc, 5, (char **) ohdrs, nhdrs,
00201                                  &diffs, &ndiffs);
00202        assert(status == DKIM_STAT_OK);
00203        assert(ndiffs == 1);
00204        assert(strcmp(diffs[0].hd_old, ohdrs[3]) == 0);
00205        assert(strcmp(diffs[0].hd_new, ALTHEADER05) == 0);
00206 
00207        status = dkim_free(dkim);
00208        assert(status == DKIM_STAT_OK);
00209 
00210        dkim_close(lib);
00211 
00212        return 0;
00213 }