ks_test.c

Go to the documentation of this file.
00001 /*
00002  * libkarmaclient - A C Library to the Karmasphere Reputation Server
00003  * Copyright (C) 2006 Karmasphere <http://www.karmasphere.com/>
00004  *  - Shevek <shevek@karmasphere.com>
00005  *  - Dave Stewart <dave.stewart@karmasphere.com>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  */
00021 
00029 #include "ks_config.h"
00030 
00031 #ifdef HAVE_STDIO_H
00032 #include <stdio.h>
00033 #endif
00034 
00035 #ifdef HAVE_STRING_H
00036 #include <string.h>
00037 #endif
00038 
00039 #ifdef HAVE_NETINET_IN_H
00040 #include <netinet/in.h>
00041 #endif
00042 
00043 #include "ks_bquery.h"
00044 #include "ks_response.h"
00045 #include "ks_bparse.h"
00046 
00051 static const char* tests[] = {
00052         /* these next few should pass */
00053         "32:Start of tests which should pass",
00054         "15:aaabbbcccdddeeefff",
00055         "i35e",
00056         "le", 
00057         "de",
00058         "li1ei2ei3ei4ee",
00059         "d3:foo4:arsee",
00060         "35: f305=-1*1.0 f306=-1*1.0 f531=1*1.0",
00061         "d1:d35: f305=-1*1.0 f306=-1*1.0 f531=1*1.01:vi-1ee",
00062         "d9:combinersd7:defaultd1:d35: f305=-1*1.0 f306=-1*1.0 f531=1*1.01:vi-1eee5:factsld1:d42:http://www.mail-abuse.org/cgi-bin/lookup?$1:fi305e1:vi-1eed1:d42:http://www.mail-abuse.org/cgi-bin/lookup?$1:fi306e1:vi-1eed1:d31:Listed in trusted-forwarder.org1:fi531e1:vi1eee4:timei18ee",
00063         "30:End of tests which should pass",
00064 
00065         /* these next few should fail */
00066         "",
00067         "ddee",
00068         "ddd",
00069         "lll",
00070         "ie", 
00071         "aa",
00072 };
00073 
00079 static void
00080 test_parser()
00081 {
00082         ks_string_t             *p;
00083         ks_base_t               *b;
00084         unsigned int     i;
00085 
00086         for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
00087                 printf("Parse %s\n", tests[i]);
00088                 b = ks_bparse(tests[i], strlen(tests[i]));
00089                 printf("Parsed\n");
00090                 if (b == NULL)
00091                         continue;
00092                 p = ks_string_new("", 0);
00093                 printf("Appending...\n");
00094                 ks_bappend(p, b);
00095                 printf("Appended\n");
00096                 printf("Got %s\n\n", ks_string_get(p));
00097                 ks_destroy((ks_base_t *)b);
00098                 ks_destroy((ks_base_t *)p);
00099         }
00100 }
00101 
00108 static void
00109 test_query(const char *id)
00110 {
00111         ks_bquery_t             *q;
00112         ks_string_t             *p;
00113 
00114         q = ks_bquery_new();
00115 
00116         ks_bquery_feed_add(q, "cymru.bogons");
00117         ks_bquery_feed_add(q, "spamhaus.sbl");
00118 
00119         ks_bquery_combiner_add(q, "default");
00120 
00121         ks_bquery_identity_add(q, "fred@foo.com", KS_IDT_EMAIL_ADDRESS);
00122         ks_bquery_identity_add(q, "123.45.6.7", KS_IDT_IP4_ADDRESS);
00123 
00124         if (id)
00125                 ks_bquery_id_set(q, id, strlen(id));
00126         ks_bquery_flags_set(q, 123);
00127 
00128         p = ks_bquery_packet(q);
00129 
00130         /* Send this packet over the network. */
00131         printf("Packet is %s\n", ks_string_get(p));
00132 
00133         ks_destroy((ks_base_t *)q);
00134         ks_destroy((ks_base_t *)p);
00135 }
00136 
00142 static void
00143 test_response()
00144 {
00145         ks_response_t   *e;
00146         ks_combination_t*c;
00147         ks_fact_t               *f;
00148         const char              *s;
00149         int                              i;
00150 
00151         /* This packet was received over the network. */
00152         s = "d9:combinersd7:defaultd1:d35: f305=-1*1.0 f306=-1*1.0 f531=1*1.01:vi-1eee5:factsld1:d42:http://www.mail-abuse.org/cgi-bin/lookup?$1:fi305e1:vi-1eed1:d42:http://www.mail-abuse.org/cgi-bin/lookup?$1:fi306e1:vi-1eed1:d31:Listed in trusted-forwarder.org1:fi531e1:vi1eee4:timei18ee";
00153 
00154         printf("Response data %s\n", s);
00155         e = ks_response_bparse(s, strlen(s));
00156 
00157         c = ks_response_combination(e, "default");
00158         printf("Score: %ld, data %s\n",
00159                                         ks_combination_score(c),
00160                                         ks_combination_data(c));
00161 
00162         for (i = 0; i < ks_response_facts_length(e); i++) {
00163                 f = ks_response_fact(e, i);
00164                 printf("fact %d: %ld, data %s\n",
00165                                                 i,
00166                                                 ks_fact_score(f),
00167                                                 ks_fact_data(f));
00168         }
00169 
00170         ks_destroy((ks_base_t *)e);
00171 }
00172 
00178 int
00179 main(void)
00180 {
00181         test_parser();
00182         test_query("fooIDID");
00183         test_query(NULL);
00184         test_query(NULL);
00185         test_query(NULL);
00186         test_response();
00187 
00188         return 0;
00189 }

Generated on Wed May 9 01:01:56 2007 for libkarmaclient by  doxygen 1.5.1