Back to index

opendkim  2.6.2
t-test120.c
Go to the documentation of this file.
00001 /*
00002 **  Copyright (c) 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_test120_c_id[] = "@(#)$Id: t-test120.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 <stdio.h>
00018 #include <string.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 
00030 #define SIG1 "v=1; a=rsa-sha1; c=relaxed/relaxed; d=example.com; s=test; t=1172620939; bh=WAB3bZtTHYLitirqQFGpaOBbkVY=; h=Content-class:Subject:Thread-Index:Date:X-MS-Has-Attach:From:To:Reply-To; b=gPbK/km0cEiwJBTjSUQ0oioRQNBMCJ6y6mSvg6S2z8xM57/BZx7I7c+eZ6IwtmCAXRMXJqiRixr9bxpcRU6KVkje3ofytiQ35bY7+h6RpV61lBFDxbMzdZfRmseGGeZGcGmmp6ICfi18f3KCiTOUrDptZ3+MVxSVeIdnVM6cLQ8="
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        u_int flags;
00046        DKIM_STAT status;
00047        uint64_t fixed_time;
00048        dkim_query_t qtype = DKIM_QUERY_FILE;
00049        DKIM *dkim;
00050        DKIM_LIB *lib;
00051        dkim_sigkey_t key;
00052        unsigned char hdr[MAXHEADER + 1];
00053        unsigned char inhdr[MAXHEADER + 1];
00054 
00055        printf("*** zero margin testing\n");
00056 
00057 #ifdef USE_GNUTLS
00058        (void) gnutls_global_init();
00059 #endif /* USE_GNUTLS */
00060 
00061        /* instantiate the library */
00062        lib = dkim_init(NULL, NULL);
00063        assert(lib != NULL);
00064 
00065 #ifdef TEST_KEEP_FILES
00066        /* set flags */
00067        flags = (DKIM_LIBFLAGS_TMPFILES|DKIM_LIBFLAGS_KEEPFILES);
00068        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FLAGS, &flags,
00069                            sizeof flags);
00070 #endif /* TEST_KEEP_FILES */
00071 
00072        key = KEY;
00073 
00074        dkim = dkim_sign(lib, JOBID "s1", NULL, key, SELECTOR, DOMAIN,
00075                         DKIM_CANON_RELAXED, DKIM_CANON_RELAXED,
00076                         DKIM_SIGN_RSASHA1, -1L, &status);
00077        assert(dkim != NULL);
00078 
00079        status = dkim_set_margin(dkim, 0);
00080        assert(status == DKIM_STAT_OK);
00081 
00082        /* fix signing time */
00083        fixed_time = 1172620939;
00084        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_FIXEDTIME,
00085                            &fixed_time, sizeof fixed_time);
00086 
00087 #define       HEADER0              "Content-class: urn:content-classes:message"
00088 #define HEADER1             "Subject: This is a sample message from a DKIM tester"
00089 #define       HEADER2              "Thread-Index: 1234567tgjdoigj"
00090 #define       HEADER3              "Date: Wed, 18 Jul 2007 10:48:38 -0700"
00091 #define       HEADER4              "X-MS-Has-Attach:"
00092 #define HEADER5             "From: \"DKIM tester\" <tester@yahoo.com>"
00093 #define       HEADER6              "To: \"DKIM tester\" <tester@yahoo.com>"
00094 #define HEADER7             "Reply-To: \"DKIM tester\" <tester@yahoo.com>"
00095 #define       BODY          "Test\r\n"
00096 
00097        status = dkim_header(dkim, HEADER0, strlen(HEADER0));
00098        assert(status == DKIM_STAT_OK);
00099 
00100        status = dkim_header(dkim, HEADER1, strlen(HEADER1));
00101        assert(status == DKIM_STAT_OK);
00102 
00103        status = dkim_header(dkim, HEADER2, strlen(HEADER2));
00104        assert(status == DKIM_STAT_OK);
00105 
00106        status = dkim_header(dkim, HEADER3, strlen(HEADER3));
00107        assert(status == DKIM_STAT_OK);
00108 
00109        status = dkim_header(dkim, HEADER4, strlen(HEADER4));
00110        assert(status == DKIM_STAT_OK);
00111 
00112        status = dkim_header(dkim, HEADER5, strlen(HEADER5));
00113        assert(status == DKIM_STAT_OK);
00114 
00115        status = dkim_header(dkim, HEADER6, strlen(HEADER6));
00116        assert(status == DKIM_STAT_OK);
00117 
00118        status = dkim_header(dkim, HEADER7, strlen(HEADER7));
00119        assert(status == DKIM_STAT_OK);
00120 
00121        status = dkim_eoh(dkim);
00122        assert(status == DKIM_STAT_OK);
00123 
00124        status = dkim_body(dkim, BODY, strlen(BODY));
00125        assert(status == DKIM_STAT_OK);
00126 
00127        status = dkim_eom(dkim, NULL);
00128        assert(status == DKIM_STAT_OK);
00129 
00130        memset(hdr, '\0', sizeof hdr);
00131        status = dkim_getsighdr(dkim, hdr, sizeof hdr, 0);
00132        assert(status == DKIM_STAT_OK);
00133        assert(strcmp(SIG1, hdr) == 0);
00134 
00135        status = dkim_free(dkim);
00136        assert(status == DKIM_STAT_OK);
00137 
00138        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYMETHOD,
00139                            &qtype, sizeof qtype);
00140        (void) dkim_options(lib, DKIM_OP_SETOPT, DKIM_OPTS_QUERYINFO,
00141                            KEYFILE, strlen(KEYFILE));
00142 
00143        dkim = dkim_verify(lib, JOBID "v1", NULL, &status);
00144        assert(dkim != NULL);
00145 
00146        status = dkim_set_margin(dkim, 0);
00147        assert(status == DKIM_STAT_INVALID);
00148 
00149        snprintf(inhdr, sizeof inhdr, "%s: %s", DKIM_SIGNHEADER, hdr);
00150        status = dkim_header(dkim, inhdr, strlen(inhdr));
00151        assert(status == DKIM_STAT_OK);
00152 
00153        status = dkim_header(dkim, HEADER0, strlen(HEADER0));
00154        assert(status == DKIM_STAT_OK);
00155 
00156        status = dkim_header(dkim, HEADER1, strlen(HEADER1));
00157        assert(status == DKIM_STAT_OK);
00158 
00159        status = dkim_header(dkim, HEADER2, strlen(HEADER2));
00160        assert(status == DKIM_STAT_OK);
00161 
00162        status = dkim_header(dkim, HEADER3, strlen(HEADER3));
00163        assert(status == DKIM_STAT_OK);
00164 
00165        status = dkim_header(dkim, HEADER4, strlen(HEADER4));
00166        assert(status == DKIM_STAT_OK);
00167 
00168        status = dkim_header(dkim, HEADER5, strlen(HEADER5));
00169        assert(status == DKIM_STAT_OK);
00170 
00171        status = dkim_header(dkim, HEADER6, strlen(HEADER6));
00172        assert(status == DKIM_STAT_OK);
00173 
00174        status = dkim_header(dkim, HEADER7, strlen(HEADER7));
00175        assert(status == DKIM_STAT_OK);
00176 
00177        status = dkim_eoh(dkim);
00178        assert(status == DKIM_STAT_OK);
00179 
00180        status = dkim_body(dkim, BODY, strlen(BODY));
00181        assert(status == DKIM_STAT_OK);
00182 
00183        status = dkim_eom(dkim, NULL);
00184        assert(status == DKIM_STAT_OK);
00185 
00186        status = dkim_set_margin(dkim, DKIM_HDRMARGIN);
00187        assert(status == DKIM_STAT_INVALID);
00188 
00189        status = dkim_free(dkim);
00190        assert(status == DKIM_STAT_OK);
00191 
00192        dkim_close(lib);
00193 
00194        return 0;
00195 }