source: test/test-dir.c @ 7248cdc

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 7248cdc was 7248cdc, checked in by Perry Lorier <perry@…>, 14 years ago

Be more aggressive with finding errors
Remove useless #include

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id: test-pcap-to-erf.c,v 1.3 2006/02/27 03:41:12 perry Exp $
28 *
29 */
30#ifndef WIN32
31#  include <sys/time.h>
32#  include <netinet/in.h>
33#  include <netinet/in_systm.h>
34#  include <netinet/tcp.h>
35#  include <netinet/ip.h>
36#  include <netinet/ip_icmp.h>
37#  include <arpa/inet.h>
38#  include <sys/socket.h>
39#endif
40
41/* This test is a bit more complicated.  It takes a pcap file (which doesn't
42 * have any direction tagging), then tags it (causing a promotion), then
43 * writes it to an erf file (which should demote it again).
44 */
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <assert.h>
49#include <string.h>
50#include <sys/types.h>
51#include <time.h>
52#include <string.h>
53
54#include "libtrace.h"
55
56void iferr(libtrace_t *trace)
57{
58        libtrace_err_t err = trace_get_err(trace);
59        if (err.err_num==0)
60                return;
61        printf("Error: %s\n",err.problem);
62        exit(1);
63}
64
65void iferrout(libtrace_out_t *trace)
66{
67        libtrace_err_t err = trace_get_err_output(trace);
68        if (err.err_num==0)
69                return;
70        printf("Error: %s\n",err.problem);
71        exit(1);
72}
73
74char *lookup_uri(const char *type) 
75{
76        if (!strcmp(type,"erf"))
77                return "erf:traces/100_packets.erf";
78        if (!strcmp(type,"pcap"))
79                return "pcap:traces/100_packets.pcap";
80        if (!strcmp(type,"wtf"))
81                return "wtf:traces/wed.wtf";
82        if (!strcmp(type,"rtclient"))
83                return "rtclient:chasm";
84        if (!strcmp(type,"pcapfile"))
85                return "pcapfile:traces/100_packets.pcap";
86        if (!strcmp(type, "duck"))
87                return "duck:traces/100_packets.duck";
88        return "unknown";
89}
90
91char *lookup_out_uri(const char *type) {
92        if (!strcmp(type,"erf"))
93                return "erf:traces/100_packets.out.erf";
94        if (!strcmp(type,"pcap"))
95                return "pcap:traces/100_packets.out.pcap";
96        if (!strcmp(type,"wtf"))
97                return "wtf:traces/wed.out.wtf";
98        if (!strcmp(type,"duck"))
99                return "duck:traces/100_packets.out.duck";
100        return "unknown";
101}
102
103int main(int argc, char *argv[]) {
104        int psize = 0;
105        int error = 0;
106        int count = 0;
107        int level = 0;
108        int expected = 100;
109        libtrace_t *trace;
110        libtrace_out_t *outtrace;
111        libtrace_packet_t *packet;
112
113        trace = trace_create(lookup_uri("pcap"));
114        iferr(trace);
115
116        outtrace = trace_create_output(lookup_out_uri("erf"));
117        iferrout(outtrace);
118
119        level=0;
120        trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level);
121        iferrout(outtrace);
122
123        trace_start(trace);
124        iferr(trace);
125        trace_start_output(outtrace);
126        iferrout(outtrace);
127       
128        packet=trace_create_packet();
129        for (;;) {
130                if ((psize = trace_read_packet(trace, packet)) <0) {
131                        error = 1;
132                        break;
133                }
134                if (psize == 0) {
135                        error = 0;
136                        break;
137                }
138                count ++;
139                /* Force promotion */
140                if (trace_get_source_port(packet)==80) {
141                        trace_set_direction(packet,TRACE_DIR_OUTGOING);
142                        assert(trace_get_direction(packet)==TRACE_DIR_OUTGOING);
143                        assert(trace_get_source_port(packet)==80);
144                }
145                else {
146                        trace_set_direction(packet,TRACE_DIR_INCOMING);
147                        assert(trace_get_direction(packet)==TRACE_DIR_INCOMING);
148                }
149                /* And then force demotion */
150                trace_write_packet(outtrace,packet);
151                iferrout(outtrace);
152                if (count>100)
153                        break;
154        }
155        trace_destroy_packet(packet);
156        if (error == 0) {
157                if (count != expected) {
158                        printf("failure: %d packets expected, %d seen\n",expected,count);
159                        error = 1;
160                }
161        } else {
162                iferr(trace);
163        }
164        trace_destroy(trace);
165        trace_destroy_output(outtrace);
166
167        if (error)
168                return error;
169
170        /* Now read it back in again and check it's all kosher */
171        trace = trace_create(lookup_out_uri("erf"));
172        iferr(trace);
173        trace_start(trace);
174        iferr(trace);
175        packet=trace_create_packet();
176        count=0;
177        int err;
178        while((err=trace_read_packet(trace,packet))>0) {
179                ++count;
180                if (!((trace_get_source_port(packet)==80)
181                        ^ (trace_get_direction(packet)==TRACE_DIR_INCOMING))) {
182
183                        printf("Direction tagging broken\n");
184                        error=1;
185                        break;
186                }
187
188        }
189        trace_destroy(trace);
190        trace_destroy_packet(packet);
191
192        return error;
193}
Note: See TracBrowser for help on using the repository browser.