source: test/test-wlen.c @ ec19a99

develop
Last change on this file since ec19a99 was ec19a99, checked in by Shane Alcock <salcock@…>, 22 months ago

Add test case for trace_get_wire_length()

Given that there is often some confusion around the wire length
and whether we count the FCS as wire length or not (and what do
we do for formats that don't save the FCS, i.e. pcap), we should
really have a test that will at least alert us if someone has
changed how wire length is calculated for one of the formats.

  • Property mode set to 100644
File size: 4.5 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-rtclient.c,v 1.2 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#include <stdio.h>
41#include <stdlib.h>
42#include <assert.h>
43#include <string.h>
44#include <sys/types.h>
45#include <time.h>
46#include <string.h>
47
48#include "dagformat.h"
49#include "libtrace.h"
50#include "libpacketdump.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
61const char *lookup_uri(const char *type) {
62        if (strchr(type,':') || strchr(type,'/'))
63                return type;
64        if (!strcmp(type,"erf"))
65                return "erf:traces/100_packets.erf";
66        if (!strcmp(type,"rawerf"))
67                return "rawerf:traces/100_packets.erf";
68        if (!strcmp(type,"pcap"))
69                return "pcap:traces/100_packets.pcap";
70        if (!strcmp(type,"pcapng"))
71                return "pcap:traces/100_packets.pcapng";
72        if (!strcmp(type,"pcapfile"))
73                return "pcapfile:traces/100_packets.pcap";
74        if (!strcmp(type,"pcapfilens"))
75                return "pcapfile:traces/100_packetsns.pcap";
76        if (!strcmp(type, "legacyatm"))
77                return "legacyatm:traces/legacyatm.gz";
78        if (!strcmp(type, "legacypos"))
79                return "legacypos:traces/legacypos.gz";
80        if (!strcmp(type, "legacyeth"))
81                return "legacyeth:traces/legacyeth.gz";
82        if (!strcmp(type, "tsh"))
83                return "tsh:traces/10_packets.tsh.gz";
84        return type;
85}
86
87int get_expected_wlen(const char *type) {
88        if (!strcmp(type,"erf"))
89                return 30960;
90        if (!strcmp(type,"rawerf"))
91                return 30960;
92        if (!strcmp(type,"pcap"))
93                return 31360;
94        if (!strcmp(type,"pcapng"))
95                return 31360;
96        if (!strcmp(type,"pcapfile"))
97                return 31360;
98        if (!strcmp(type,"pcapfilens"))
99                return 31360;
100        if (!strcmp(type, "legacyatm"))
101                return 5300;
102        if (!strcmp(type, "legacypos"))
103                return 57456;
104        if (!strcmp(type, "legacyeth"))
105                return 30372;
106        if (!strcmp(type, "tsh"))
107                return 67341;
108        return 0;
109}
110
111int main(int argc, char *argv[]) {
112        int psize = 0;
113        int error = 0;
114        int count = 0;
115        int expected = 100;
116        size_t expectedwlen = 0;
117        size_t totalwlen = 0;
118        libtrace_t *trace;
119        libtrace_packet_t *packet;
120
121        if (argc<2) {
122                fprintf(stderr,"usage: %s type\n",argv[0]);
123                return 1;
124        }
125
126        trace = trace_create(lookup_uri(argv[1]));
127        iferr(trace);
128
129        trace_start(trace);
130        iferr(trace);
131
132        expectedwlen = get_expected_wlen(argv[1]);
133
134        packet=trace_create_packet();
135        for (;;) {
136                size_t wlen;
137                if ((psize = trace_read_packet(trace, packet)) <0) {
138                        error = 1;
139                        iferr(trace);
140                        break;
141                }
142                if (psize == 0) {
143                        error = 0;
144                        break;
145                }
146               
147                if ((wlen = trace_get_wire_length(packet)) <= 0) {
148                        error = 0;
149                        break;
150                }
151
152                count ++;
153                totalwlen += wlen;
154                if (count>100)
155                        break;
156        }
157        trace_destroy_packet(packet);
158        if (error == 0) {
159                if (count == expected) {
160                        printf("success: %d packets read\n",expected);
161                } else {
162                        printf("failure: %d packets expected, %d seen\n",expected,count);
163                        error = 1;
164                }
165                if (totalwlen == expectedwlen) {
166                        printf("success: %zu wire bytes read\n",expectedwlen);
167                } else {
168                        printf("failure: %zu wire bytes expected, %zu seen\n",expectedwlen,totalwlen);
169                        error = 1;
170                }
171
172        } else {
173                iferr(trace);
174        }
175        trace_destroy(trace);
176        return error;
177}
Note: See TracBrowser for help on using the repository browser.