Back to index

opendkim  2.6.6
t-test49.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_test49_c_id[] = "@(#)$Id: t-test49.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 #include <unistd.h>
00020 
00021 /* libdb includes */
00022 #ifdef QUERY_CACHE
00023 # include <db.h>
00024 #endif /* QUERY_CACHE */
00025 
00026 #ifdef USE_GNUTLS
00027 # include <gnutls/gnutls.h>
00028 #endif /* USE_GNUTLS */
00029 
00030 /* libopendkim includes */
00031 #include "../dkim.h"
00032 #include "../dkim-cache.h"
00033 #include "t-testdata.h"
00034 
00035 #define       BUFRSZ 1024
00036 #define       QUERY  "Test Key"
00037 #define       DATA   "Cached Data"
00038 
00039 /*
00040 **  MAIN -- program mainline
00041 **
00042 **  Parameters:
00043 **     The usual.
00044 **
00045 **  Return value:
00046 **     Exit status.
00047 */
00048 
00049 int
00050 main(int argc, char **argv)
00051 {
00052 #ifndef QUERY_CACHE
00053        printf("*** query caching SKIPPED\n");
00054 
00055 #else /* ! QUERY_CACHE */
00056 
00057        int status;
00058        int err;
00059        u_int s1, s2, s3;
00060        size_t buflen;
00061        DB *cache;
00062        char buf[BUFRSZ + 1];
00063 
00064        printf("*** query caching\n");
00065 
00066        cache = dkim_cache_init(NULL, NULL);
00067 
00068        err = 0;
00069 
00070        printf("--- empty cache\n");
00071        buflen = sizeof buf;
00072        status = dkim_cache_query(cache, QUERY, 0, buf, &buflen, &err);
00073        assert(err == 0);
00074        assert(status == 1);
00075 
00076        printf("--- insert record\n");
00077        status = dkim_cache_insert(cache, QUERY, DATA, 3, &err);
00078        assert(err == 0);
00079        assert(status == 0);
00080 
00081        printf("--- retrieve record\n");
00082        memset(buf, '\0', sizeof buf);
00083        buflen = sizeof buf;
00084        status = dkim_cache_query(cache, QUERY, 0, buf, &buflen, &err);
00085        assert(err == 0);
00086        assert(status == 0);
00087        assert(buflen == strlen(DATA));
00088        assert(strcmp(buf, DATA) == 0);
00089 
00090        printf("--- [pause for record to expire]\n");
00091        sleep(4);
00092 
00093        printf("--- retrieve expired record\n");
00094        buflen = sizeof buf;
00095        status = dkim_cache_query(cache, QUERY, 0, buf, &buflen, &err);
00096        assert(err == 0);
00097        assert(status == 1);
00098 
00099        printf("--- expire old records\n");
00100        status = dkim_cache_expire(cache, 3, &err);
00101        assert(err == 0);
00102        assert(status == 1);
00103 
00104        printf("--- retrieve cache stats\n");
00105        dkim_cache_stats(&s1, &s2, &s3);
00106        assert(s1 == 3);
00107        assert(s2 == 1);
00108        assert(s3 == 1);
00109 
00110        dkim_cache_close(cache);
00111 #endif /* ! QUERY_CACHE */
00112 
00113        return 0;
00114 }