encodedecodetest.c

Go to the documentation of this file.
00001 /**
00002  * @file encodedecodetest.c
00003  * Unit tests for CCNx C library.
00004  *
00005  * A CCNx program.
00006  *
00007  * Copyright (C) 2009-2011 Palo Alto Research Center, Inc.
00008  *
00009  * This work is free software; you can redistribute it and/or modify it under
00010  * the terms of the GNU General Public License version 2 as published by the
00011  * Free Software Foundation.
00012  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00015  * for more details. You should have received a copy of the GNU General Public
00016  * License along with this program; if not, write to the
00017  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020  
00021 #include <fcntl.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <string.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 
00029 #include <ccn/ccn.h>
00030 #include <ccn/bloom.h>
00031 #include <ccn/uri.h>
00032 #include <ccn/digest.h>
00033 #include <ccn/keystore.h>
00034 #include <ccn/signing.h>
00035 #include <ccn/random.h>
00036 
00037 struct path {
00038     int count;
00039     char * comps[];
00040 };
00041 struct path * path_create(char * strpath) {
00042     char * p = strpath;
00043     int slash_count = 0;
00044     int i = 0;
00045     struct path * path;
00046 
00047     if (strlen(strpath) < 1) {
00048         return NULL;
00049     }
00050     while (*p != '\0') {
00051         if (*p++ == '/') slash_count++;
00052     }
00053     path = malloc(sizeof(int) + sizeof(char *)*(slash_count+1));
00054     path->comps[0] = strtok(strdup(strpath), "/");
00055     path->count = 0;
00056     while (path->comps[i++]) {
00057         path->comps[i] = strtok(NULL, "/");
00058         path->count++;
00059     }
00060     return path;
00061 }
00062 void path_destroy(struct path **path) {
00063     free(*path);
00064     *path = NULL;
00065 }
00066 
00067 int 
00068 encode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, struct ccn_charbuf *signed_info, const void *pkey) {
00069     struct ccn_charbuf *path = ccn_charbuf_create();
00070     int i;
00071     int res;
00072 
00073     if (path == NULL || ccn_name_init(path) == -1) {
00074         fprintf(stderr, "Failed to allocate or initialize content path\n");
00075         return -1;
00076     }
00077 
00078     for (i = 0; i < name_path->count; i++) {
00079         ccn_name_append_str(path, name_path->comps[i]);
00080     }
00081 
00082     res = ccn_encode_ContentObject(message, path, signed_info, data, len, NULL, pkey);
00083 
00084     if (res != 0) {
00085         fprintf(stderr, "Failed to encode ContentObject\n");
00086     }
00087 
00088     ccn_charbuf_destroy(&path);
00089     return (res);
00090 }
00091 
00092 int 
00093 decode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, const void *verkey) {
00094     struct ccn_parsed_ContentObject content;
00095     struct ccn_indexbuf *comps = ccn_indexbuf_create();
00096     const unsigned char * content_value;
00097     size_t content_length;
00098     
00099     int res = 0;
00100     int i;
00101     
00102     memset(&content, 0x33, sizeof(content));
00103 
00104     if (ccn_parse_ContentObject(message->buf, message->length, &content, comps) != 0) {
00105         printf("Decode failed to parse object\n");
00106         res = -1;
00107     }
00108 
00109     if (comps->n-1 != name_path->count) {
00110         printf("Decode got wrong number of path components: %d vs. %d\n", 
00111                (int)(comps->n-1), name_path->count);
00112         res = -1;
00113     }
00114     for (i=0; i<name_path->count; i++) {
00115         if (ccn_name_comp_strcmp(message->buf, comps, i, name_path->comps[i]) != 0) {
00116             printf("Decode mismatch on path component %d\n", i);
00117             res = -1;
00118         }
00119     }
00120     if (ccn_content_get_value(message->buf, message->length, &content,
00121                               &content_value, &content_length) != 0) {
00122         printf("Cannot retrieve content value\n");
00123         res = -1;
00124     } else if (content_length != len) {
00125         printf("Decode mismatch on content length %d vs. %d\n", 
00126                (int)content_length, (int)len);
00127         res = -1;
00128     } else if (memcmp(content_value, data, len) != 0) {
00129         printf("Decode mismatch of content\n");
00130         res = -1;
00131     }
00132 
00133     if (ccn_verify_signature(message->buf, message->length, &content, verkey) != 1) {
00134         printf("Signature did not verify\n");
00135         res = -1;
00136     }
00137 
00138     ccn_indexbuf_destroy(&comps);
00139     return res;
00140     
00141 }
00142 
00143 int
00144 expected_res(int res, char code)
00145 {
00146     if (code == '*')
00147         return(1);
00148     if (code == '-')
00149         return(res < 0);
00150     if (code == '+')
00151         return(res > 0);
00152     if ('0' <= code && code <= '9')
00153         return(res == (code - '0'));
00154     abort(); // test program bug!!!
00155     /* NOTREACHED */
00156 }
00157 
00158 static char all_chars_percent_encoded[256 * 3 + 1]; /* Computed */
00159 
00160 static void init_all_chars_percent_encoded(void) {
00161     struct ccn_charbuf *c;
00162     int i;
00163     c = ccn_charbuf_create();
00164     for (i = 0; i < 256; i+=2) {
00165         ccn_charbuf_putf(c, "%%%02x%%%02X", i, i+1);
00166     }
00167     if (c->length >= sizeof(all_chars_percent_encoded))
00168         c->length = sizeof(all_chars_percent_encoded) - 1;
00169     memcpy(all_chars_percent_encoded, c->buf, c->length);
00170     ccn_charbuf_destroy(&c);
00171 }
00172 
00173 static const char *all_chars_percent_encoded_canon =
00174  "ccnx:/"
00175  "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"
00176  "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"
00177  "%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F"
00178  "0123456789%3A%3B%3C%3D%3E%3F"
00179  "%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_"
00180  "%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F"
00181  "%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F"
00182  "%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F"
00183  "%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF"
00184  "%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF"
00185  "%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF"
00186  "%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF"
00187  "%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF"
00188  "%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF";
00189 
00190 int
00191 main (int argc, char *argv[]) {
00192     struct ccn_charbuf *buffer = ccn_charbuf_create();
00193     struct ccn_charbuf *signed_info = ccn_charbuf_create();
00194     struct ccn_skeleton_decoder dd = {0};
00195     ssize_t res;
00196     char *outname = NULL;
00197     int fd;
00198     int result = 0;
00199     char * contents[] = {"INVITE sip:foo@parc.com SIP/2.0\nVia: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK519044721\nFrom: <sip:jthornto@13.2.117.52>;tag=2105643453\nTo: Test User <sip:foo@parc.com>\nCall-ID: 119424355@127.0.0.1\nCSeq: 20 INVITE\nContact: <sip:jthornto@127.0.0.1:5060>\nMax-Forwards: 70\nUser-Agent: Linphone-1.7.1/eXosip\nSubject: Phone call\nExpires: 120\nAllow: INVITE, ACK, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\nContent-Type: application/sdp\nContent-Length:   448\n\nv=0\no=jthornto 123456 654321 IN IP4 127.0.0.1\ns=A conversation\nc=IN IP4 127.0.0.1\nt=0 0\nm=audio 7078 RTP/AVP 111 110 0 3 8 101\na=rtpmap:111 speex/16000/1\na=rtpmap:110 speex/8000/1\na=rtpmap:0 PCMU/8000/1\na=rtpmap:3 GSM/8000/1\na=rtpmap:8 PCMA/8000/1\na=rtpmap:101 telephone-event/8000\na=fmtp:101 0-11\nm=video 9078 RTP/AVP 97 98 99\na=rtpmap:97 theora/90000\na=rtpmap:98 H263-1998/90000\na=fmtp:98 CIF=1;QCIF=1\na=rtpmap:99 MP4V-ES/90000\n", 
00200  
00201                          "Quaer #%2d zjduer  badone",
00202                          "",
00203                          NULL};
00204     char * paths[] = { "/sip/protocol/parc.com/domain/foo/principal/invite/verb/119424355@127.0.0.1/id", 
00205                        "/d/e/f",
00206                        "/zero/length/content",
00207                        NULL};
00208     struct path * cur_path = NULL;
00209     struct ccn_keystore *keystore = ccn_keystore_create();
00210     char *home = getenv("HOME");
00211     char *keystore_suffix = "/.ccnx/.ccnx_keystore";
00212     char *keystore_name = NULL;
00213 
00214     int i;
00215 
00216     if (argc == 3 && strcmp(argv[1], "-o") == 0) {
00217         outname = argv[2];
00218     } else {
00219         printf("Usage: %s -o <outfilename>\n", argv[0]);
00220         exit(1);
00221     }
00222 
00223     if (home == NULL) {
00224         printf("Unable to determine home directory for keystore\n");
00225         exit(1);
00226     }
00227     keystore_name = calloc(1, strlen(home) + strlen(keystore_suffix) + 1);
00228     
00229     strcat(keystore_name, home);
00230     strcat(keystore_name, keystore_suffix);
00231 
00232     if (0 != ccn_keystore_init(keystore, keystore_name, "Th1s1sn0t8g00dp8ssw0rd.")) {
00233         printf("Failed to initialize keystore\n");
00234         exit(1);
00235     }
00236 
00237     printf("Creating signed_info\n");
00238     res = ccn_signed_info_create(signed_info,
00239                                  /*pubkeyid*/ccn_keystore_public_key_digest(keystore),
00240                                  /*publisher_key_id_size*/ccn_keystore_public_key_digest_length(keystore),
00241                                  /*datetime*/NULL,
00242                                  /*type*/CCN_CONTENT_GONE,
00243                                  /*freshness*/ 42,
00244                                  /*finalblockid*/NULL,
00245                                  /*keylocator*/NULL);
00246     if (res < 0) {
00247         printf("Failed to create signed_info!\n");
00248     }
00249     
00250     res = ccn_skeleton_decode(&dd, signed_info->buf, signed_info->length);
00251     if (!(res == signed_info->length && dd.state == 0)) {
00252         printf("Failed to decode signed_info!  Result %d State %d\n", (int)res, dd.state);
00253         result = 1;
00254     }
00255     memset(&dd, 0, sizeof(dd));
00256     printf("Done with signed_info\n");
00257 
00258     printf("Encoding sample message data length %d\n", (int)strlen(contents[0]));
00259     cur_path = path_create(paths[0]);
00260     if (encode_message(buffer, cur_path, contents[0], strlen(contents[0]), signed_info, ccn_keystore_private_key(keystore))) {
00261         printf("Failed to encode message!\n");
00262     } else {
00263         printf("Encoded sample message length is %d\n", (int)buffer->length);
00264 
00265         res = ccn_skeleton_decode(&dd, buffer->buf, buffer->length);
00266         if (!(res == buffer->length && dd.state == 0)) {
00267             printf("Failed to decode!  Result %d State %d\n", (int)res, dd.state);
00268             result = 1;
00269         }
00270         if (outname != NULL) {
00271             fd = open(outname, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
00272             if (fd == -1)
00273                 perror(outname);
00274             res = write(fd, buffer->buf, buffer->length);
00275             close(fd);
00276         }
00277         if (decode_message(buffer, cur_path, contents[0], strlen(contents[0]), ccn_keystore_public_key(keystore)) != 0) {
00278             result = 1;
00279         }
00280         printf("Expect signature verification failure: ");
00281         if (buffer->length >= 20)
00282             buffer->buf[buffer->length - 20] += 1;
00283         if (decode_message(buffer, cur_path, contents[0], strlen(contents[0]), ccn_keystore_public_key(keystore)) == 0) {
00284             result = 1;
00285         }
00286     }
00287     path_destroy(&cur_path);
00288     ccn_charbuf_destroy(&buffer);
00289     printf("Done with sample message\n");
00290     
00291     /* Now exercise as unit tests */
00292     
00293     for (i = 0; paths[i] != NULL && contents[i] != NULL; i++) {
00294         printf("Unit test case %d\n", i);
00295         cur_path = path_create(paths[i]);
00296         buffer = ccn_charbuf_create();
00297         if (encode_message(buffer, cur_path, contents[i], strlen(contents[i]), signed_info, ccn_keystore_private_key(keystore))) {
00298             printf("Failed encode\n");
00299             result = 1;
00300         } else if (decode_message(buffer, cur_path, contents[i], strlen(contents[i]), ccn_keystore_public_key(keystore))) {
00301             printf("Failed decode\n");
00302             result = 1;
00303         }
00304         path_destroy(&cur_path);
00305         ccn_charbuf_destroy(&buffer);
00306     }
00307     
00308     /* Test the uri encode / decode routines */
00309         
00310     init_all_chars_percent_encoded();
00311     const char *uri_tests[] = {
00312         "_+4", "ccnx:/this/is/a/test",       "",     "ccnx:/this/is/a/test",
00313         ".+4", "../test2?x=2",              "?x=2", "ccnx:/this/is/a/test2",
00314         "_-X", "../should/error",           "",     "",
00315         "_+2", "/missing/scheme",           "",     "ccnx:/missing/scheme",
00316         ".+0", "../../../../../././#/",     "#/",   "ccnx:/",
00317         ".+1", all_chars_percent_encoded,   "",     all_chars_percent_encoded_canon,
00318         "_+1", all_chars_percent_encoded_canon, "", all_chars_percent_encoded_canon,
00319         ".+4", "ccnx:/.../.%2e./...././.....///?...", "?...", "ccnx:/.../.../..../.....",
00320         "_-X", "/%3G?bad-pecent-encode",    "",     "",
00321         "_-X", "/%3?bad-percent-encode",    "",     "",
00322         "_-X", "/%#bad-percent-encode",    "",     "",
00323         "_+3", "ccnx://joe@example.com:42/ignore/host/part of uri", "", "ccnx:/ignore/host/part%20of%20uri",
00324         NULL, NULL, NULL, NULL
00325     };
00326     const char **u;
00327     struct ccn_charbuf *uri_out = ccn_charbuf_create();
00328     buffer = ccn_charbuf_create();
00329     for (u = uri_tests; *u != NULL; u += 4, i++) {
00330         printf("Unit test case %d\n", i);
00331         if (u[0][0] != '.')
00332             buffer->length = 0;
00333         res = ccn_name_from_uri(buffer, u[1]);
00334         if (!expected_res(res, u[0][1])) {
00335             printf("Failed: ccn_name_from_uri wrong res %d\n", (int)res);
00336             result = 1;
00337         }
00338         if (res >= 0) {
00339             if (res > strlen(u[1])) {
00340                 printf("Failed: ccn_name_from_uri long res %d\n", (int)res);
00341                 result = 1;
00342             }
00343             else if (0 != strcmp(u[1] + res, u[2])) {
00344                 printf("Failed: ccn_name_from_uri expecting leftover '%s', got '%s'\n", u[2], u[1] + res);
00345                 result = 1;
00346             }
00347             uri_out->length = 0;
00348             res = ccn_uri_append(uri_out, buffer->buf, buffer->length, 1);
00349             if (!expected_res(res, u[0][2])) {
00350                 printf("Failed: ccn_uri_append wrong res %d\n", (int)res);
00351                 result = 1;
00352             }
00353             if (res >= 0) {
00354                 if (uri_out->length != strlen(u[3])) {
00355                     printf("Failed: ccn_uri_append produced wrong number of characters\n");
00356                     result = 1;
00357                 }
00358                 ccn_charbuf_reserve(uri_out, 1)[0] = 0;
00359                 if (0 != strcmp((const char *)uri_out->buf, u[3])) {
00360                     printf("Failed: ccn_uri_append produced wrong output\n");
00361                     printf("Expected: %s\n", u[3]);
00362                     printf("  Actual: %s\n", (const char *)uri_out->buf);
00363                     result = 1;
00364                 }
00365             }
00366         }
00367     }
00368     ccn_charbuf_destroy(&buffer);
00369     ccn_charbuf_destroy(&uri_out);
00370     printf("Name marker tests\n");
00371     do {
00372         const char *expected_uri = "ccnx:/example.com/.../%01/%FE/%01%02%03%04%05%06%07%08/%FD%10%10%10%10%1F%FF/%00%81";
00373         const char *expected_chopped_uri = "ccnx:/example.com/.../%01/%FE";
00374         const char *expected_bumped_uri = "ccnx:/example.com/.../%01/%FF";
00375         const char *expected_bumped2_uri = "ccnx:/example.com/.../%01/%00%00";
00376 
00377         printf("Unit test case %d\n", i++);
00378         buffer = ccn_charbuf_create();
00379         uri_out = ccn_charbuf_create();
00380         res = ccn_name_init(buffer);
00381         res |= ccn_name_append_str(buffer, "example.com");
00382         res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 0);
00383         res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 1);
00384         res |= ccn_name_append_numeric(buffer, 0xFE, 0);
00385         res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 0x0102030405060708ULL);
00386         res |= ccn_name_append_numeric(buffer, CCN_MARKER_VERSION, 0x101010101FFFULL);
00387         res |= ccn_name_append_numeric(buffer, CCN_MARKER_SEQNUM, 129);
00388         res |= ccn_uri_append(uri_out, buffer->buf, buffer->length, 1);
00389         if (res < 0) {
00390             printf("Failed: name marker tests had negative res\n");
00391             result = 1;
00392         }
00393         if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_uri)) {
00394             printf("Failed: name marker tests produced wrong output\n");
00395             printf("Expected: %s\n", expected_uri);
00396             printf("  Actual: %s\n", (const char *)uri_out->buf);
00397             result = 1;
00398         }
00399         res = ccn_name_chop(buffer, NULL, 100);
00400         if (res != -1) {
00401             printf("Failed: ccn_name_chop did not produce error \n");
00402             result = 1;
00403         }
00404         res = ccn_name_chop(buffer, NULL, 4);
00405         if (res != 4) {
00406             printf("Failed: ccn_name_chop got wrong length\n");
00407             result = 1;
00408         }
00409         uri_out->length = 0;
00410         ccn_uri_append(uri_out, buffer->buf, buffer->length, 1);
00411         if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_chopped_uri)) {
00412             printf("Failed: ccn_name_chop botch\n");
00413             printf("Expected: %s\n", expected_chopped_uri);
00414             printf("  Actual: %s\n", (const char *)uri_out->buf);
00415             result = 1;
00416         }
00417         res = ccn_name_next_sibling(buffer);
00418         if (res != 4) {
00419             printf("Failed: ccn_name_next_sibling got wrong length\n");
00420             result = 1;
00421         }
00422         uri_out->length = 0;
00423         ccn_uri_append(uri_out, buffer->buf, buffer->length, 1);
00424         if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_bumped_uri)) {
00425             printf("Failed: ccn_name_next_sibling botch\n");
00426             printf("Expected: %s\n", expected_bumped_uri);
00427             printf("  Actual: %s\n", (const char *)uri_out->buf);
00428             result = 1;
00429         }
00430         ccn_name_next_sibling(buffer);
00431         uri_out->length = 0;
00432         ccn_uri_append(uri_out, buffer->buf, buffer->length, 1);
00433         if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_bumped2_uri)) {
00434             printf("Failed: ccn_name_next_sibling botch\n");
00435             printf("Expected: %s\n", expected_bumped2_uri);
00436             printf("  Actual: %s\n", (const char *)uri_out->buf);
00437             result = 1;
00438         }
00439         ccn_charbuf_destroy(&buffer);
00440         ccn_charbuf_destroy(&uri_out);
00441     } while (0);
00442     printf("Message digest tests\n");
00443     do {
00444         printf("Unit test case %d\n", i++);
00445         struct ccn_digest *dg = ccn_digest_create(CCN_DIGEST_SHA256);
00446         if (dg == NULL) {
00447             printf("Failed: ccn_digest_create returned NULL\n");
00448             result = 1;
00449             break;
00450         }
00451         printf("Unit test case %d\n", i++);
00452         const unsigned char expected_digest[] = {
00453             0xb3, 0x82, 0xcd, 0xb0, 0xe9, 0x5d, 0xf7, 0x3b, 0xe7, 0xdc, 0x19, 0x81, 0x3a, 0xfd, 0xdf, 0x89, 0xfb, 0xd4, 0xd4, 0xa0, 0xdb, 0x11, 0xa6, 0xba, 0x24, 0x16, 0x5b, 0xad, 0x9d, 0x90, 0x72, 0xb0
00454         };
00455         unsigned char actual_digest[sizeof(expected_digest)] = {0};
00456         const char *data = "Content-centric";
00457         if (ccn_digest_size(dg) != sizeof(expected_digest)) {
00458             printf("Failed: wrong digest size\n");
00459             result = 1;
00460             break;
00461         }
00462         printf("Unit test case %d\n", i++);
00463         ccn_digest_init(dg);
00464         res = ccn_digest_update(dg, data, strlen(data));
00465         if (res != 0)
00466             printf("Warning: check res %d\n", (int)res);
00467         printf("Unit test case %d\n", i++);
00468         res = ccn_digest_final(dg, actual_digest, sizeof(expected_digest));
00469         if (res != 0)
00470             printf("Warning: check res %d\n", (int)res);
00471         if (0 != memcmp(actual_digest, expected_digest, sizeof(expected_digest))) {
00472             printf("Failed: wrong digest\n");
00473             result = 1;
00474             break;
00475         }
00476     } while (0);
00477     printf("Really basic PRNG test\n");
00478     do {
00479         unsigned char r1[42];
00480         unsigned char r2[42];
00481         printf("Unit test case %d\n", i++);
00482         ccn_add_entropy(&i, sizeof(i), 0); /* Not much entropy, really. */
00483         ccn_random_bytes(r1, sizeof(r1));
00484         memcpy(r2, r1, sizeof(r2));
00485         ccn_random_bytes(r2, sizeof(r2));
00486         if (0 == memcmp(r1, r2, sizeof(r2))) {
00487             printf("Failed: badly broken PRNG\n");
00488             result = 1;
00489             break;
00490         }
00491     } while (0);
00492     printf("Bloom filter tests\n");
00493     do {
00494         unsigned char seed1[4] = "1492";
00495         const char *a[13] = {
00496             "one", "two", "three", "four",
00497             "five", "six", "seven", "eight",
00498             "nine", "ten", "eleven", "twelve",
00499             "thirteen"
00500         };
00501         struct ccn_bloom *b1 = NULL;
00502         struct ccn_bloom *b2 = NULL;
00503         int j, k, t1, t2;
00504         unsigned short us;
00505         
00506         printf("Unit test case %d\n", i++);
00507         b1 = ccn_bloom_create(13, seed1);
00508         
00509         for (j = 0; j < 13; j++)
00510             if (ccn_bloom_match(b1, a[j], strlen(a[j]))) break;
00511         if (j < 13) {
00512             printf("Failed: \"%s\" matched empty Bloom filter\n", a[j]);
00513             result = 1;
00514             break;
00515         }
00516         printf("Unit test case %d\n", i++);
00517         for (j = 0; j < 13; j++)
00518             ccn_bloom_insert(b1, a[j], strlen(a[j]));
00519         for (j = 0; j < 13; j++)
00520             if (!ccn_bloom_match(b1, a[j], strlen(a[j]))) break;
00521         if (j < 13) {
00522             printf("Failed: \"%s\" not found when it should have been\n", a[j]);
00523             result = 1;
00524             break;
00525         }
00526         printf("Unit test case %d\n", i++);
00527         for (j = 0, k = 0; j < 13; j++)
00528             if (ccn_bloom_match(b1, a[j]+1, strlen(a[j]+1)))
00529                 k++;
00530         if (k > 0) {
00531             printf("Mmm, found %d false positives\n", k);
00532             if (k > 2) {
00533                 result = 1;
00534                 break;
00535             }
00536         }
00537         unsigned char seed2[5] = "aqfb\0";
00538         for (; seed2[3] <= 'f'; seed2[3]++) {
00539             printf("Unit test case %d (%4s)    ", i++, seed2);
00540             b2 = ccn_bloom_create(13, seed2);
00541             for (j = 0; j < 13; j++)
00542                 ccn_bloom_insert(b2, a[j], strlen(a[j]));
00543             for (j = 0, k = 0, us = ~0; us > 0; us--) {
00544                 t1 = ccn_bloom_match(b1, &us, sizeof(us));
00545                 t2 = ccn_bloom_match(b2, &us, sizeof(us));
00546                 j += (t1 | t2);
00547                 k += (t1 & t2);
00548             }
00549             printf("either=%d both=%d wiresize=%d\n", j, k, ccn_bloom_wiresize(b1));
00550             if (k > 12) {
00551                 printf("Failed: Bloom seeding may not be effective\n");
00552                 result = 1;
00553             }
00554             ccn_bloom_destroy(&b2);
00555         }
00556         ccn_bloom_destroy(&b1);
00557     } while (0);
00558     printf("ccn_sign_content() tests\n");
00559     do {
00560         struct ccn *h = ccn_create();
00561         struct ccn_charbuf *co = ccn_charbuf_create();
00562         struct ccn_signing_params sparm = CCN_SIGNING_PARAMS_INIT;
00563         struct ccn_parsed_ContentObject pco = {0};
00564         struct ccn_charbuf *name = ccn_charbuf_create();
00565         
00566         printf("Unit test case %d\n", i++);
00567         ccn_name_from_uri(name, "ccnx:/test/data/%00%42");
00568         res = ccn_sign_content(h, co, name, NULL, "DATA", 4);
00569         if (res != 0) {
00570             printf("Failed: res == %d\n", (int)res);
00571             result = 1;
00572         }
00573         sparm.template_ccnb = ccn_charbuf_create();
00574         res = ccn_parse_ContentObject(co->buf, co->length, &pco, NULL);
00575         if (res != 0) {
00576             printf("Failed: ccn_parse_ContentObject res == %d\n", (int)res);
00577             result = 1;
00578             break;
00579         }
00580         ccn_charbuf_append(sparm.template_ccnb,
00581             co->buf + pco.offset[CCN_PCO_B_SignedInfo],
00582             pco.offset[CCN_PCO_E_SignedInfo] - pco.offset[CCN_PCO_B_SignedInfo]);
00583         sparm.sp_flags = CCN_SP_TEMPL_TIMESTAMP;
00584         printf("Unit test case %d\n", i++);
00585         res = ccn_sign_content(h, co, name, &sparm, "DATA", 4);
00586         if (res != 0) {
00587             printf("Failed: res == %d\n", (int)res);
00588             result = 1;
00589         }
00590         printf("Unit test case %d\n", i++);
00591         sparm.sp_flags = -1;
00592         res = ccn_sign_content(h, co, name, &sparm, "DATA", 4);
00593         if (res != -1) {
00594             printf("Failed: res == %d\n", (int)res);
00595             result = 1;
00596         }
00597         ccn_charbuf_destroy(&name);
00598         ccn_charbuf_destroy(&sparm.template_ccnb);
00599         ccn_charbuf_destroy(&co);
00600         ccn_destroy(&h);
00601     } while (0);
00602     printf("link tests\n");
00603     do {
00604         struct ccn_charbuf *l = ccn_charbuf_create();
00605         struct ccn_charbuf *name = ccn_charbuf_create();
00606         struct ccn_parsed_Link pl = {0};
00607         struct ccn_buf_decoder decoder;
00608         struct ccn_buf_decoder *d;
00609         struct ccn_indexbuf *comps = ccn_indexbuf_create();
00610         printf("Unit test case %d\n", i++);
00611         ccn_name_from_uri(name, "ccnx:/test/link/name");
00612         ccnb_append_Link(l, name, "label", NULL);
00613         d = ccn_buf_decoder_start(&decoder, l->buf, l->length);
00614         res = ccn_parse_Link(d, &pl, comps);
00615         if (res != 3 /* components in name */) {
00616             printf("Failed: ccn_parse_Link res == %d\n", (int)res);
00617             result = 1;
00618         }        
00619     } while (0);
00620     
00621     exit(result);
00622 }

Generated on Thu Feb 16 00:44:00 2012 for Content-Centric Networking in C by  doxygen 1.5.6