663c5a6d1018bf58003eb130bf6502ee83f6769c
[cascardo/ovs.git] / tests / test-flows.c
1 #include <config.h>
2 #include "flow.h"
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "openflow/openflow.h"
7 #include "timeval.h"
8 #include "ofpbuf.h"
9 #include "ofp-print.h"
10 #include "pcap.h"
11 #include "util.h"
12 #include "vlog.h"
13
14 #undef NDEBUG
15 #include <assert.h>
16
17 int
18 main(int argc UNUSED, char *argv[])
19 {
20     struct ofp_match expected_match;
21     FILE *flows, *pcap;
22     int retval;
23     int n = 0, errors = 0;
24
25     set_program_name(argv[0]);
26     time_init();
27     vlog_init();
28
29     flows = stdin;
30     pcap = fdopen(3, "rb");
31     if (!pcap) {
32         ovs_fatal(errno, "failed to open fd 3 for reading");
33     }
34
35     retval = pcap_read_header(pcap);
36     if (retval) {
37         ovs_fatal(retval > 0 ? retval : 0, "reading pcap header failed");
38     }
39
40     while (fread(&expected_match, sizeof expected_match, 1, flows)) {
41         struct ofpbuf *packet;
42         struct ofp_match extracted_match;
43         flow_t flow;
44
45         n++;
46
47         retval = pcap_read(pcap, &packet);
48         if (retval == EOF) {
49             ovs_fatal(0, "unexpected end of file reading pcap file");
50         } else if (retval) {
51             ovs_fatal(retval, "error reading pcap file");
52         }
53
54         flow_extract(packet, 1, &flow);
55         flow_to_match(&flow, 0, &extracted_match);
56
57         if (memcmp(&expected_match, &extracted_match, sizeof expected_match)) {
58             char *exp_s = ofp_match_to_string(&expected_match, 2);
59             char *got_s = ofp_match_to_string(&extracted_match, 2);
60             errors++;
61             printf("mismatch on packet #%d (1-based).\n", n);
62             printf("Packet:\n");
63             ofp_print_packet(stdout, packet->data, packet->size, packet->size);
64             printf("Expected flow:\n%s\n", exp_s);
65             printf("Actually extracted flow:\n%s\n", got_s);
66             printf("\n");
67             free(exp_s);
68             free(got_s);
69         }
70
71         ofpbuf_delete(packet);
72     }
73     printf("checked %d packets, %d errors\n", n, errors);
74     return errors != 0;
75 }
76