test_get_next_pkt.c 4.17 KB
Newer Older
Arne Øslebø's avatar
Arne Øslebø committed
1
2
#include <stdio.h>
#include <unistd.h>
3
4
#include <mapi.h>
#include "test.h"
's avatar
committed
5

6
int main(int argc, char *argv[])
Arne Øslebø's avatar
Arne Øslebø committed
7
8
9
10
{
	int fd;
	int fid;
	struct mapipkt *pkt;
11
12
13
	int err_no =0 , flag=0;
	char error[512];
	
14
	if(argc!=2)
Arne Øslebø's avatar
Arne Øslebø committed
15
16
17
18
	{
		printf("\nWrong arguments\n");
		return -1;
	}
19
20
	if ((fd = mapi_create_flow(argv[1]))<0){
		fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
21
		mapi_read_error( &err_no, error);
22
23
24
25
26
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((fid=mapi_apply_function(fd, "TO_BUFFER"))<0){
's avatar
committed
27
	  	fprintf(stderr, "Could not apply TO_BUFFER to flow %d\n", fd);
28
		mapi_read_error( &err_no, error);
29
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
Arne Øslebø's avatar
Arne Øslebø committed
30
31
32
		return -1;
	}

33
34
35
DOT;	
	if(mapi_connect(fd)<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
36
		mapi_read_error( &err_no, error);
37
38
39
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
Arne Øslebø's avatar
Arne Øslebø committed
40

41
42
43
44
DOT;	
/*
 * Sanity check
 */
45
46
47
48
49
50
	if((pkt=mapi_get_next_pkt(fd, fid)) != NULL){
		printf("Got packet\n");
	}
	else{
		printf("\nError in mapi_get_next_packet\n");
	}
Arne Øslebø's avatar
Arne Øslebø committed
51

52
53
54

	if(mapi_close_flow(fd)<0){
		fprintf(stderr,"Close flow failed\n");
55
		mapi_read_error( &err_no, error);
56
57
58
59
60
61
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;

Arne Øslebø's avatar
Arne Øslebø committed
62
63

	printf("\nmapi_get_next_packet OK\n");
64
65
66
67
68
69
	
	/*
 	 * Error reporting checking 
 	 */
	fd =0;
	if(mapi_get_next_pkt(fd , fid) == NULL){
70
		mapi_read_error( &err_no, error);
71
72
73
74
75
76
77
		printf("Testing error case1: Erorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6147){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}

	}
78
79
80
DOT;	
	if ((fd = mapi_create_flow(argv[1]))<0){
		fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
81
		mapi_read_error( &err_no, error);
82
83
84
85
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
86
	if(mapi_get_next_pkt(fd , fid) == NULL){
87
		mapi_read_error( &err_no, error);
88
89
90
91
92
93
94
		printf("Testing error case2: Erorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6143){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}

	}
95
96
97
DOT;	
	if(mapi_close_flow(fd)<0){
		fprintf(stderr,"Close flow failed\n");
98
		mapi_read_error( &err_no, error);
99
100
101
102
103
104
105
106
107
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	/*
	 * Offline tests
	 */
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<0){
		fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
108
		mapi_read_error( &err_no, error);
109
110
111
112
113
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((fid=mapi_apply_function(fd, "TO_BUFFER"))<0){
's avatar
committed
114
	  	fprintf(stderr, "Could not apply TO_BUFFER to flow %d\n", fd);
115
		mapi_read_error( &err_no, error);
116
117
118
119
120
121
122
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	
	if(mapi_connect(fd)<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
123
		mapi_read_error( &err_no, error);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	
/*
 * Sanity check
 */
	if((pkt=mapi_get_next_pkt(fd, fid)) != NULL){
		  if(pkt->caplen!=60)
			    fprintf(stderr, "WARNING:sanity check failed packet size not the expected one %d\n" , pkt->caplen);
	}else 
		fprintf(stderr, "WARNING:sanity check failed NULL packet returned\n");
DOT;	
	/*
 	 * Error reporting checking 
 	 */
	if(mapi_get_next_pkt(-12 , fid) == NULL){
142
		mapi_read_error( &err_no, error);
143
144
145
146
147
148
149
150
		printf("Testing error case3: Erorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6147){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;	
	if(mapi_get_next_pkt(fd , -1234) == NULL){
151
		mapi_read_error( &err_no, error);
152
153
154
155
156
157
158
159
160
		printf("Testing error case4: Erorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6147){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;
	if(mapi_close_flow(fd)<0){
		fprintf(stderr,"Close flow failed\n");
161
		mapi_read_error( &err_no, error);
162
163
164
165
166
167
		fprintf(stderr,"Erorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;

Arne Øslebø's avatar
Arne Øslebø committed
168

169
170
171
172
173
	if(!flag)
		printf("\nGet Next Packet Error Checking OK\n");
	else 
		printf("\nGet Next Packet Error Checking :FAILED:\n");
	
Arne Øslebø's avatar
Arne Øslebø committed
174
175
	return 0;
}