basicparsetest.c

Go to the documentation of this file.
00001 /**
00002  * @file basicparsetest.c
00003  * 
00004  * A CCNx test program.
00005  *
00006  * Copyright (C) 2009 Palo Alto Research Center, Inc.
00007  *
00008  * This work is free software; you can redistribute it and/or modify it under
00009  * the terms of the GNU General Public License version 2 as published by the
00010  * Free Software Foundation.
00011  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00014  * for more details. You should have received a copy of the GNU General Public
00015  * License along with this program; if not, write to the
00016  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include <stddef.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 
00026 #include <ccn/ccn.h>
00027 #include <ccn/charbuf.h>
00028 #include <ccn/coding.h>
00029 #include <ccn/face_mgmt.h>
00030 #include <ccn/sockcreate.h>
00031 #include <ccn/reg_mgmt.h>
00032 #include <ccn/header.h>
00033 
00034 /**
00035  * This is for testing.
00036  *
00037  * Reads ccnb-encoded data from stdin and 
00038  * tries parsing with various parsers, and when successful turns
00039  * the result back into ccnb and tests for goodness.
00040  *
00041  */
00042 int
00043 main (int argc, char **argv)
00044 {
00045     unsigned char buf[8800];
00046     ssize_t size;
00047     struct ccn_face_instance *face_instance;
00048     struct ccn_forwarding_entry *forwarding_entry;
00049     struct ccn_header *header;
00050     int res = 1;
00051     struct ccn_charbuf *c = ccn_charbuf_create();
00052     int i;
00053     struct ccn_parsed_interest parsed_interest = {0};
00054     struct ccn_parsed_interest *pi = &parsed_interest;
00055     struct ccn_parsed_Link parsed_link = {0};
00056     struct ccn_parsed_Link *pl = &parsed_link;
00057     struct ccn_buf_decoder decoder;
00058     struct ccn_buf_decoder *d;
00059 
00060 
00061     size = read(0, buf, sizeof(buf));
00062     if (size < 0)
00063         exit(0);
00064     
00065     face_instance = ccn_face_instance_parse(buf, size);
00066     if (face_instance != NULL) {
00067         printf("face_instance OK\n");
00068         c->length = 0;
00069         res = ccnb_append_face_instance(c, face_instance);
00070         if (res != 0)
00071             printf("face_instance append failed\n");
00072         if (memcmp(buf, c->buf, c->length) != 0)
00073             printf("face_instance mismatch\n");
00074         ccn_face_instance_destroy(&face_instance);
00075         face_instance = ccn_face_instance_parse(c->buf, c->length);
00076         if (face_instance == NULL) {
00077             printf("face_instance reparse failed\n");
00078             res = 1;
00079         }
00080     }
00081     ccn_face_instance_destroy(&face_instance);
00082     
00083     forwarding_entry = ccn_forwarding_entry_parse(buf, size);
00084     if (forwarding_entry != NULL) {
00085         printf("forwarding_entry OK\n");
00086         c->length = 0;
00087         res = ccnb_append_forwarding_entry(c, forwarding_entry);
00088         if (res != 0)
00089             printf("forwarding_entry append failed\n");
00090         if (memcmp(buf, c->buf, c->length) != 0)
00091             printf("forwarding_entry mismatch\n");
00092         ccn_forwarding_entry_destroy(&forwarding_entry);
00093         forwarding_entry = ccn_forwarding_entry_parse(c->buf, c->length);
00094         if (forwarding_entry == NULL) {
00095             printf("forwarding_entry reparse failed\n");
00096             res = 1;
00097         }
00098     }
00099     ccn_forwarding_entry_destroy(&forwarding_entry);
00100     
00101     header = ccn_header_parse(buf, size);
00102     if (header != NULL) {
00103         printf("header OK\n");
00104         c->length = 0;
00105         res = ccnb_append_header(c, header);
00106         if (res != 0)
00107             printf("header append failed\n");
00108         if (memcmp(buf, c->buf, c->length) != 0)
00109             printf("header mismatch\n");
00110         ccn_header_destroy(&header);
00111         header = ccn_header_parse(c->buf, c->length);
00112         if (header == NULL) {
00113             printf("header reparse failed\n");
00114             res = 1;
00115         }
00116     }
00117     ccn_header_destroy(&header);
00118 
00119     i = ccn_parse_interest(buf, size, pi, NULL);
00120     if (i >= 0) {
00121         res = 0;
00122         printf("interest OK lifetime %jd (%d seconds)\n",
00123                ccn_interest_lifetime(buf, pi),
00124                ccn_interest_lifetime_seconds(buf, pi));
00125     }
00126 
00127     d = ccn_buf_decoder_start(&decoder, buf, size);
00128     i = ccn_parse_Link(d, pl, NULL);
00129     if (i >= 0) {
00130         res = 0;
00131         printf("link OK\n");
00132     }
00133 
00134     d = ccn_buf_decoder_start(&decoder, buf, size);
00135     i = ccn_parse_Collection_start(d);
00136     if (i >= 0) {
00137         while ((i = ccn_parse_Collection_next(d, pl, NULL)) > 0) {
00138           printf("collection link OK\n");
00139         }
00140         if (i == 0) {
00141             res = 0;
00142             printf("collection OK\n");
00143         }
00144     }
00145     if (res != 0) {
00146         printf("URP\n");
00147     }
00148     exit(res);
00149 }

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