giuliomoro@44
|
1 #include "../../include/UdpServer.h"
|
giuliomoro@44
|
2 #include "../../include/UdpClient.h"
|
giuliomoro@44
|
3 #include <unistd.h>
|
giuliomoro@44
|
4
|
giuliomoro@44
|
5 int test1(UdpServer *server, UdpClient *client){
|
giuliomoro@44
|
6 int buffer;
|
giuliomoro@44
|
7 int tot=100;
|
giuliomoro@44
|
8 int errors=0;
|
giuliomoro@44
|
9 for(int n=0; n<tot; n++){
|
giuliomoro@53
|
10 if(client->send(&n,sizeof(int))!=1){
|
giuliomoro@53
|
11 printf("error: while sending\n");
|
giuliomoro@53
|
12 errors++;
|
giuliomoro@53
|
13 }
|
giuliomoro@53
|
14 }
|
giuliomoro@53
|
15 for(int n=0; n<tot; n++){
|
giuliomoro@53
|
16 if(server->read(&buffer,sizeof(int))<0){
|
giuliomoro@53
|
17 printf("error: unable to read\n");
|
giuliomoro@53
|
18 errors++;
|
giuliomoro@53
|
19 continue;
|
giuliomoro@53
|
20 };
|
giuliomoro@44
|
21 if(n!=buffer){
|
giuliomoro@44
|
22 printf("error: %d!=%d\n",n,buffer);
|
giuliomoro@44
|
23 errors++;
|
giuliomoro@44
|
24 }
|
giuliomoro@44
|
25 }
|
giuliomoro@44
|
26 int n=server->emptySocket();
|
giuliomoro@44
|
27 if(n!=0)
|
giuliomoro@53
|
28 printf("Error: the socket had %d bytes\n",n);
|
giuliomoro@44
|
29 return errors;
|
giuliomoro@44
|
30 }
|
giuliomoro@44
|
31 int compareStrings(char * str1, char * str2){
|
giuliomoro@44
|
32 if(strlen(str1)!=strlen(str2))
|
giuliomoro@44
|
33 return -1;
|
giuliomoro@44
|
34 for(int n=0; n<strlen(str1); n++){
|
giuliomoro@44
|
35 if(str1[n]!=str2[n])
|
giuliomoro@44
|
36 return -1;
|
giuliomoro@44
|
37 }
|
giuliomoro@44
|
38 return 0;
|
giuliomoro@44
|
39 }
|
giuliomoro@44
|
40
|
giuliomoro@44
|
41 int test2(UdpServer *server, UdpClient *client){
|
giuliomoro@44
|
42 char buffer[1000];
|
giuliomoro@44
|
43 int tot=100;
|
giuliomoro@44
|
44 int errors=0;
|
giuliomoro@44
|
45 for(int n=0; n<tot; n++){
|
giuliomoro@44
|
46 int num=sprintf(buffer,"%08.8f",n/1000.0);
|
giuliomoro@44
|
47 client->send(&buffer,sizeof(char)*(num+1));
|
giuliomoro@44
|
48 }
|
giuliomoro@44
|
49 char auxBuffer[100];
|
giuliomoro@44
|
50 for(int n=0; n<tot; n++){
|
giuliomoro@44
|
51 int num=sprintf(auxBuffer,"%08.8f",n/1000.0);
|
giuliomoro@44
|
52 server->read(&buffer,num*sizeof(char));
|
giuliomoro@44
|
53 if(compareStrings(auxBuffer,buffer)==-1){
|
giuliomoro@44
|
54 printf("error: %s!=%s\n",auxBuffer,buffer);
|
giuliomoro@44
|
55 errors++;
|
giuliomoro@44
|
56 }
|
giuliomoro@44
|
57 }
|
giuliomoro@44
|
58
|
giuliomoro@44
|
59 return errors;
|
giuliomoro@44
|
60 }
|
giuliomoro@44
|
61
|
giuliomoro@44
|
62 int test3(UdpServer *server, UdpClient *client){
|
giuliomoro@44
|
63 char buffer[1000];
|
giuliomoro@44
|
64 int tot=100;
|
giuliomoro@44
|
65 int errors=0;
|
giuliomoro@44
|
66 int totNum=0;
|
giuliomoro@44
|
67 for(int n=0; n<tot; n++){
|
giuliomoro@44
|
68 int num=sprintf(buffer,"%.8f",n/1000.0);
|
giuliomoro@44
|
69 client->send(&buffer,sizeof(char)*(num+1));
|
giuliomoro@44
|
70 totNum+=1+num;
|
giuliomoro@44
|
71 }
|
giuliomoro@44
|
72 int n=server->emptySocket();
|
giuliomoro@44
|
73 if(n!=totNum){
|
giuliomoro@44
|
74 errors=1;
|
giuliomoro@44
|
75 printf("retrieved bytes differs from sent bytes: %d!=%d\n",n,totNum);
|
giuliomoro@44
|
76 }
|
giuliomoro@44
|
77 return errors;
|
giuliomoro@44
|
78 }
|
giuliomoro@44
|
79
|
giuliomoro@44
|
80
|
giuliomoro@44
|
81 int main(){
|
giuliomoro@53
|
82 int port=1234;
|
giuliomoro@53
|
83 char serverName[]="127.0.0.1";
|
giuliomoro@44
|
84 UdpServer server(port);
|
giuliomoro@53
|
85 UdpClient client(port,serverName);
|
giuliomoro@44
|
86 int errors=0;
|
giuliomoro@53
|
87 int ret=0;
|
giuliomoro@44
|
88 ret=test1(&server,&client);
|
giuliomoro@44
|
89 errors+=ret;
|
giuliomoro@44
|
90 if(ret)
|
giuliomoro@44
|
91 printf("test1 failed with %d errors\n", ret);
|
giuliomoro@44
|
92 else
|
giuliomoro@44
|
93 printf("test1 passed\n");
|
giuliomoro@44
|
94
|
giuliomoro@44
|
95 ret=test2(&server,&client);
|
giuliomoro@44
|
96 errors+=ret;
|
giuliomoro@44
|
97 if(ret)
|
giuliomoro@44
|
98 printf("test2 failed with %d errors\n", ret);
|
giuliomoro@44
|
99 else
|
giuliomoro@44
|
100 printf("test2 passed\n");
|
giuliomoro@44
|
101
|
giuliomoro@44
|
102 ret=test3(&server,&client);
|
giuliomoro@44
|
103 errors+=ret;
|
giuliomoro@44
|
104 if(ret)
|
giuliomoro@44
|
105 printf("test3 failed with %d errors\n", ret);
|
giuliomoro@44
|
106 else
|
giuliomoro@44
|
107 printf("test3 passed\n");
|
giuliomoro@53
|
108 //now test if the setPort and setServer methods work
|
giuliomoro@53
|
109 client.~UdpClient();
|
giuliomoro@53
|
110 server.~UdpServer();
|
giuliomoro@53
|
111 port=1235;
|
giuliomoro@53
|
112 UdpServer server1;
|
giuliomoro@53
|
113 UdpClient client1;
|
giuliomoro@53
|
114 client1.setPort(port);
|
giuliomoro@53
|
115 client1.setServer(serverName);
|
giuliomoro@53
|
116 if(server1.bindToPort(port)==false){
|
giuliomoro@53
|
117 printf("unable to bind to port %d\n",port);
|
giuliomoro@53
|
118 errors+=1;
|
giuliomoro@53
|
119 }
|
giuliomoro@53
|
120 ret=test1(&server1, &client1);
|
giuliomoro@53
|
121 errors+=ret;
|
giuliomoro@53
|
122 if(ret)
|
giuliomoro@53
|
123 printf("test1 failed with %d errors\n", ret);
|
giuliomoro@53
|
124 else
|
giuliomoro@53
|
125 printf("test1 passed\n");
|
giuliomoro@44
|
126
|
giuliomoro@44
|
127 return errors;
|
giuliomoro@44
|
128 }
|