source: test/test-setcaplen.c @ 51276bd

cachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file since 51276bd was 568a341, checked in by Shane Alcock <salcock@…>, 4 years ago

Update test suite for pcapng

Also added a new test for set_capture_length(). Setting the caplen
for pcapng is a bit more complicated than most formats, so I
needed a test for that format anyway. Figured I may as well add
tests for some of the other truncatable formats as well.

I've also added a 'complex.pcapng' test trace -- I haven't written
any tests that use it but it is useful for checking if you can
still read the full variety of pcapng records.

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 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#include <stdio.h>
42#include <stdlib.h>
43#include <assert.h>
44#include <string.h>
45#include <sys/types.h>
46#include <time.h>
47#include <string.h>
48
49#include "dagformat.h"
50#include "libtrace.h"
51
52void iferr(libtrace_t *trace)
53{
54        libtrace_err_t err = trace_get_err(trace);
55        if (err.err_num==0)
56                return;
57        printf("Error: %s\n",err.problem);
58        exit(1);
59}
60
61void iferrout(libtrace_out_t *trace)
62{
63        libtrace_err_t err = trace_get_err_output(trace);
64        if (err.err_num==0)
65                return;
66        printf("Error: %s\n",err.problem);
67        exit(1);
68}
69
70char *lookup_uri(const char *type) 
71{
72        if (!strcmp(type,"erf"))
73                return "erf:traces/100_packets.erf";
74        if (!strcmp(type,"pcap"))
75                return "pcap:traces/100_packets.pcap";
76        if (!strcmp(type,"pcapng"))
77                return "pcapng:traces/100_packets.pcapng";
78        if (!strcmp(type,"wtf"))
79                return "wtf:traces/wed.wtf";
80        if (!strcmp(type,"rtclient"))
81                return "rtclient:chasm";
82        if (!strcmp(type,"pcapfile"))
83                return "pcapfile:traces/100_packets.pcap";
84        if (!strcmp(type,"pcapfilens"))
85                return "pcapfile:traces/100_packetsns.pcap";
86        if (!strcmp(type,"legacyatm"))
87                return "legacyatm:traces/legacyatm.gz";
88        if (!strcmp(type,"legacypos"))
89                return "legacypos:traces/legacypos.gz";
90        if (!strcmp(type,"legacyeth"))
91                return "legacyeth:traces/legacyeth.gz";
92        if (!strcmp(type, "duck"))
93                return "duck:traces/100_packets.duck";
94        if (!strcmp(type, "tsh"))
95                return "tsh:traces/10_packets.tsh.gz";
96        if (!strcmp(type, "sll1"))
97                return "pcapfile:traces/sll.pcap.gz";
98        if (!strcmp(type, "sll2"))
99                return "pcapfile:traces/100_sll.pcap";
100        return "unknown";
101}
102
103char *lookup_out_uri(const char *type) {
104        if (!strcmp(type,"erf"))
105                return "erf:traces/100_packets.out.erf";
106        if (!strcmp(type,"pcap"))
107                return "pcap:traces/100_packets.out.pcap";
108        if (!strcmp(type,"pcapfile"))
109                return "pcapfile:traces/100_packets.out.pcap";
110        if (!strcmp(type,"wtf"))
111                return "wtf:traces/wed.out.wtf";
112        if (!strcmp(type,"duck"))
113                return "duck:traces/100_packets.out.duck";
114        return "unknown";
115}
116
117static int time_changed(libtrace_packet_t *packet, 
118                libtrace_packet_t *packet2) {
119
120        struct timeval tv1, tv2;
121
122        tv1 = trace_get_timeval(packet);
123        tv2 = trace_get_timeval(packet2);
124       
125        if (tv1.tv_sec != tv2.tv_sec || 
126                tv1.tv_usec - tv2.tv_usec > 1 ||
127                tv2.tv_usec - tv1.tv_usec > 1) { 
128                printf("Timestamps differ: %u.%u vs %u.%u\n",
129                                tv1.tv_sec, tv1.tv_usec, 
130                                tv2.tv_sec, tv2.tv_usec);
131
132                return 1;
133        }
134        return 0;
135
136}
137
138static int length_changed(libtrace_packet_t *packet, 
139                libtrace_packet_t *packet2) {
140
141        uint16_t wlen_1 = trace_get_wire_length(packet);
142        uint16_t wlen_2 = trace_get_wire_length(packet2);
143
144        if (wlen_1 != wlen_2)
145                return true;
146
147        return false;
148
149}
150
151int main(int argc, char *argv[]) {
152        int psize = 0;
153        int error = 0;
154        int count = 0;
155        int level = 0;
156        int expected = 100;
157        libtrace_t *trace,*trace2;
158        libtrace_out_t *outtrace;
159        libtrace_packet_t *packet,*packet2;
160        const char *trace1name;
161        const char *trace2name;
162
163        trace = trace_create(lookup_uri(argv[1]));
164        iferr(trace);
165
166        if (strcmp(argv[1],"rtclient")==0)
167                expected=101;
168
169        outtrace = trace_create_output(lookup_out_uri(argv[2]));
170        iferrout(outtrace);
171
172        level=0;
173        trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level);
174        if (trace_is_err_output(outtrace)) {
175                trace_perror_output(outtrace,"WARNING: ");
176        }
177
178        trace_start(trace);
179        iferr(trace);
180        trace_start_output(outtrace);
181        iferrout(outtrace);
182       
183        packet=trace_create_packet();
184        for (;;) {
185                if ((psize = trace_read_packet(trace, packet)) <0) {
186                        error = 1;
187                        break;
188                }
189                if (psize == 0) {
190                        error = 0;
191                        break;
192                }
193
194                trace_set_capture_length(packet, 32);
195                if (trace_write_packet(outtrace,packet) > 0)
196                        count ++;
197                iferrout(outtrace);
198                if (count>100)
199                        break;
200        }
201        trace_destroy_packet(packet);
202        if (error == 0) {
203                if (count != expected) {
204                        printf("failure: %d packets expected, %d seen\n",expected,count);
205                        error = 1;
206                }
207        } else {
208                iferr(trace);
209        }
210        trace_destroy(trace);
211        trace_destroy_output(outtrace);
212
213        if (error)
214                return error;
215
216        /* Now read it back in again and check it's all kosher */
217        trace1name = lookup_uri(argv[1]);
218        trace = trace_create(trace1name);
219        iferr(trace);
220        trace_start(trace);
221        trace2name = lookup_out_uri(argv[2]);
222        trace2 = trace_create(trace2name);
223        iferr(trace2);
224        trace_start(trace2);
225        iferr(trace2);
226        packet=trace_create_packet();
227        packet2=trace_create_packet();
228        count=0;
229        while(trace_read_packet(trace,packet)>0) {
230                int err;
231
232                if (IS_LIBTRACE_META_PACKET(packet))
233                        continue;
234
235                ++count;
236                do {
237                        if ((err=trace_read_packet(trace2,packet2))<1) {
238                                printf("premature EOF on destination, %i from %s, %i from %s\n",count,lookup_uri(argv[1]),count-1,lookup_out_uri(argv[2]));
239                                iferr(trace2);
240                                error=1;
241                                break;
242                        }
243                } while (IS_LIBTRACE_META_PACKET(packet2));
244
245                /* The capture length might be snapped down to the wire length */
246                if (trace_get_capture_length(packet) == 32) {
247                        printf("original packet has been snapped?!\n");
248                        abort();
249                }
250
251                if (trace_get_capture_length(packet2) != 32) {
252                        printf("failed to properly snap packet\n");
253                        abort();
254                }
255
256                if (length_changed(packet, packet2)) {
257                        printf("\t%s\t%s\n",
258                                trace1name,
259                                trace2name);
260                        printf("caplen\t%zd\t%zd\t%+zd\n",
261                                trace_get_capture_length(packet),
262                                trace_get_capture_length(packet2),
263                                trace_get_capture_length(packet2)-trace_get_capture_length(packet));
264                        printf("wirelen\t%zd\t%zd\t%+zd\n",
265                                trace_get_wire_length(packet),
266                                trace_get_wire_length(packet2),
267                                trace_get_wire_length(packet2)-trace_get_wire_length(packet));
268                        printf("link\t%d\t%d\n",
269                                trace_get_link_type(packet),
270                                trace_get_link_type(packet2));
271                        abort();
272                }
273
274                if (time_changed(packet, packet2)) {
275                        error = 1;
276                        break;
277                }
278
279        }
280        if (trace_read_packet(trace2,packet2)>0) {
281                printf("Extra packets after EOF\n");
282                error=1;
283        }
284        trace_destroy(trace);
285        trace_destroy(trace2);
286        trace_destroy_packet(packet);
287        trace_destroy_packet(packet2);
288
289        printf("snapped %d packets\n", count);
290
291        return error;
292}
Note: See TracBrowser for help on using the repository browser.