1dfee48457e91b2558e4a2210f820512bf8e158f
[profile/ivi/dlt-daemon.git] / src / tests / dlt-test-filetransfer.c
1 #include <dlt_filetransfer.h>   /*Needed for transferring files with the dlt protocol*/
2 #include <dlt.h>                                /*Needed for dlt logging*/
3
4 //!Declare some context for the main program. It's a must have to do this, when you want to log with dlt.
5 DLT_DECLARE_CONTEXT(mainContext);
6
7 //!Declare some context for the file transfer. It's not a must have to do this, but later you can set a filter on this context in the dlt viewer.
8 DLT_DECLARE_CONTEXT(fileContext);
9
10 //!Textfile which will be transferred.
11 char *file1;
12 //!Image which will be transferred.
13 char *file2;
14 //!Not existing file which will be transferred.
15 char *file3_1;
16 //!Not existing file which will be transferred.
17 char *file3_2;
18 //!Not existing file which will be transferred.
19 char *file3_3;
20 //!Just some variables
21 int i,countPackages, transferResult, dltResult;
22
23 //!Prints the test result
24 void printTestResultPositiveExpected(const char *function, int result){
25
26         if(result >= 0){
27                 printf("%s successful\n",function);
28         }
29         else
30         {
31                 printf("%s failed\n",function);
32         }
33
34 }
35
36 //!Prints the test result
37 void printTestResultNegativeExpected(const char *function, int result){
38
39         if(result < 0){
40                 printf("%s successful\n",function);
41         }
42         else
43         {
44                 printf("%s failed\n",function);
45         }
46
47 }
48
49 //!Test the file transfer with the condition that the transferred file is smaller as the file transfer buffer using dlt_user_log_file_complete.
50 int testFile1Run1(){
51         //Just some log to the main context
52         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF1P1 - dlt_user_log_file_complete"),DLT_STRING(file1));
53
54         //Here's the line where the dlt file transfer is called. The method call needs a context, the absolute file path, will the file be deleted after transfer and the timeout between the packages  
55         transferResult = dlt_user_log_file_complete(&fileContext,file1,0,20);
56         if(transferResult < 0 )
57         {
58                         printf("Error: dlt_user_log_file_complete\n");
59                         return transferResult;
60         }
61         //Just some log to the main context
62         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF1P1"),DLT_STRING(file1));
63         
64         printTestResultPositiveExpected(__FUNCTION__,transferResult);
65
66         return transferResult;
67 }
68
69 //!Test the file transfer with the condition that the transferred file is smaller as the file transfer buffer using single package transfer 
70 int testFile1Run2(){
71         int total_size, used_size;
72         
73         //Get the information how many packages have the file
74         countPackages = dlt_user_log_file_packagesCount(&fileContext,file1);
75         if(countPackages < 0 )
76         {
77                         printf("Error: dlt_user_log_file_packagesCount\n");
78                         printTestResultPositiveExpected(__FUNCTION__,countPackages);
79                         return -1;
80         }
81         //Just some log to the main context
82         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF1P2 - transfer single package"),DLT_STRING(file1));
83                                                 
84         //Logs the header of the file transfer. For more details see Mainpage.c. 
85         //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
86         transferResult = dlt_user_log_file_header(&fileContext,file1);
87         if(transferResult >= 0)
88         {
89                 //Loop to log all packages
90                 for(i=1;i<=countPackages;i++)
91                 {
92                         dlt_user_check_buffer(&total_size, &used_size);
93                         if((total_size - used_size) < (total_size/2))
94                         {
95                                 printf("Error: dlt_user_log_file_data\n");
96                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
97                                 break;
98                         }
99
100                         //Logs one single package to the file context   
101                         transferResult = dlt_user_log_file_data(&fileContext,file1,i,20);
102                         if(transferResult < 0)
103                         {
104                                 printf("Error: dlt_user_log_file_data\n");
105                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
106                                 return transferResult;
107                         }
108                 }
109
110                 //Logs the end of the file transfer. For more details see Mainpage.c
111                 //The end gives just information about the file serial number but is needed to signal that the file transfer has correctly finished and needed for the file transfer plugin of the dlt viewer.  
112                 transferResult = dlt_user_log_file_end(&fileContext,file1,0);
113                 if(transferResult < 0)
114                 {
115                         printf("Error: dlt_user_log_file_end\n");
116                         printTestResultPositiveExpected(__FUNCTION__,transferResult);
117                         return transferResult;
118                 }
119         }
120         else
121         {
122                 printf("Error: dlt_user_log_file_header\n");
123                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
124                 return transferResult;
125         }
126         
127         //Just some log to main context
128         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF1P2 - transfer single package"),DLT_STRING(file1));
129         printTestResultPositiveExpected(__FUNCTION__,transferResult);
130         return 0;
131 }
132 //!Test the file transfer with the condition that the transferred file is bigger as the file transfer buffer using dlt_user_log_file_complete.
133 int testFile2Run1(){
134         //Just some log to main context
135         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF2P1 - dlt_user_log_file_complete"),DLT_STRING(file2));
136
137         //Here's the line where the dlt file transfer is called. The method call needs a context, the absolute file path, will the file be deleted after transfer and the timeout between the packages
138         transferResult = dlt_user_log_file_complete(&fileContext,file2,0,20);
139         if(transferResult < 0)
140         {
141                 printf("Error: dlt_user_log_file_complete\n");
142                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
143                 return transferResult;
144         }
145         //Just some log to main context
146         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF2P1"),DLT_STRING(file2));
147         printTestResultPositiveExpected(__FUNCTION__,transferResult);
148         return transferResult;
149 }
150
151 //!Test the file transfer with the condition that the transferred file is bigger as the file transfer buffer using single package transfer
152 int testFile2Run2(){
153         int total_size, used_size;
154         
155         //Get the information how many packages have the file
156         countPackages = dlt_user_log_file_packagesCount(&fileContext,file2);
157         if(countPackages < 0 )
158         {
159                         printf("Error: dlt_user_log_file_packagesCount\n");
160                         printTestResultPositiveExpected(__FUNCTION__,countPackages);
161                         return -1;
162         }
163         
164         //Just some log to the main context
165         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF2P2 - transfer single package"),DLT_STRING(file2));
166         
167         //Logs the header of the file transfer. For more details see Mainpage.c. 
168         //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
169         transferResult = dlt_user_log_file_header(&fileContext,file2);
170         if( transferResult >= 0){
171         
172                 //Loop to log all packages      
173                 for(i=1;i<=countPackages;i++)
174                 {
175                         dlt_user_check_buffer(&total_size, &used_size);
176                         if((total_size - used_size) < (total_size/2))
177                         {
178                                 printf("Error: dlt_user_log_file_data\n");
179                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
180                                 break;
181                         }
182
183                         //Logs one single package to the file context
184                         transferResult = dlt_user_log_file_data(&fileContext,file2,i,20);
185                         if(transferResult < 0)
186                         {
187                                 printf("Error: dlt_user_log_file_data\n");
188                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
189                                 return transferResult;
190                         }
191                 }
192
193                 //Logs the end of the file transfer. For more details see Mainpage.c
194                 //The end gives just information about the file serial number but is needed to signal that the file transfer has correctly finished and needed for the file transfer plugin of the dlt viewer.
195                 transferResult = dlt_user_log_file_end(&fileContext,file2,0);
196                 if(transferResult < 0)
197                 {
198                         printf("Error: dlt_user_log_file_end\n");
199                         printTestResultPositiveExpected(__FUNCTION__,transferResult);
200                         return transferResult;
201                 }
202         }
203         else
204         {
205                 printf("Error: dlt_user_log_file_header\n");
206                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
207                 return transferResult;
208         }
209         //Just some log to the main context
210         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF2P2"),DLT_STRING(file2));
211         printTestResultPositiveExpected(__FUNCTION__,transferResult);
212         return 0;
213 }
214
215 //!Test the file transfer with the condition that the transferred file does not exist using dlt_user_log_file_complete.
216 int testFile3Run1(){
217         
218         //Just some log to the main context
219         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P1"),DLT_STRING(file3_1));
220
221         //Here's the line where the dlt file transfer is called. The method call needs a context, the absolute file path, will the file be deleted after transfer and the timeout between the packages
222         transferResult = dlt_user_log_file_complete(&fileContext,file3_1,0,20);
223         if(transferResult < 0)
224         {
225                 //Error expected because file doesn't exist
226                 //printf("Error: dlt_user_log_file_complete\n");
227                 //Just some log to the main context
228                 DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P1"),DLT_STRING(file3_1));
229                 printTestResultNegativeExpected(__FUNCTION__,transferResult);
230                 return transferResult;
231         }
232         printTestResultNegativeExpected(__FUNCTION__,transferResult);
233         return transferResult;
234 }
235
236
237 //!Test the file transfer with the condition that the transferred file does not exist using single package transfer
238 int testFile3Run2(){
239
240         //Get the information how many packages have the file
241         countPackages = dlt_user_log_file_packagesCount(&fileContext,file3_2);
242         if(countPackages < 0 )
243         {
244                         //Error expected because file doesn't exist
245                         //printf("Error: dlt_user_log_file_packagesCount\n");
246                         //Just some log to the main context
247                         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P1"),DLT_STRING(file3_2));
248                         printTestResultNegativeExpected(__FUNCTION__,countPackages);
249                         return -1;
250         }
251         //Just some log to the main context
252         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P1"),DLT_STRING(file3_2));
253         
254         //Logs the header of the file transfer. For more details see Mainpage.c. 
255         //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
256         transferResult = dlt_user_log_file_header(&fileContext,file3_2);
257         if( transferResult >= 0){
258         
259                 //Loop to log all packages      
260                 for(i=1;i<=countPackages;i++)
261                 {
262                         //Logs one single package to the file context
263                         transferResult = dlt_user_log_file_data(&fileContext,file3_2,i,20);
264                         if(transferResult < 0)
265                         {
266                                 printf("Error: dlt_user_log_file_data\n");
267                                 printTestResultNegativeExpected(__FUNCTION__,transferResult);
268                                 return transferResult;
269                         }
270                 }
271
272                 //Logs the end of the file transfer. For more details see Mainpage.c
273                 //The end gives just information about the file serial number but is needed to signal that the file transfer has correctly finished and needed for the file transfer plugin of the dlt viewer.
274                 transferResult = dlt_user_log_file_end(&fileContext,file3_2,0);
275                 if(transferResult < 0)
276                 {
277                         printf("Error: dlt_user_log_file_end\n");
278                         printTestResultNegativeExpected(__FUNCTION__,transferResult);
279                         return transferResult;
280                 }
281         }
282         printTestResultNegativeExpected(__FUNCTION__,transferResult);
283         return 0;
284 }
285
286
287 //!Logs some information about the file.
288 int testFile3Run3(){
289         
290         //Just some log to the main context
291         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P2"),DLT_STRING(file3_3));
292         
293         //Here's the line where the dlt file file info is called. The method call logs some information to dlt about the file, filesize, file serial number and number of packages
294         transferResult = dlt_user_log_file_infoAbout(&fileContext,file3_3);
295         if(transferResult < 0)
296         {
297                 //Error expected because file doesn't exist
298                 //printf("Error: dlt_user_log_file_infoAbout\n");
299                 //Just some log to the main context
300                 DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P2"),DLT_STRING(file3_3));
301                 printTestResultNegativeExpected(__FUNCTION__,transferResult);
302                 return transferResult;
303         }
304         printTestResultNegativeExpected(__FUNCTION__,transferResult);
305         return 0;
306 }
307
308 //!Main program dlt-test-filestransfer starts here
309 int main(void)
310 {
311         //First file contains some text
312         file1 = "/usr/share/dlt-filetransfer/dlt-test-filetransfer-file";
313         //Second file is a picture
314         file2 = "/usr/share/dlt-filetransfer/dlt-test-filetransfer-image.png";
315         //Third file doesn't exist. Just to test the reaction when the file isn't available.
316         file3_1 = "dlt-test-filetransfer-doesntExist_1";
317         //Third file doesn't exist. Just to test the reaction when the file isn't available.
318         file3_2 = "dlt-test-filetransfer-doesntExist_2";
319         //Third file doesn't exist. Just to test the reaction when the file isn't available.
320         file3_3 = "dlt-test-filetransfer-doesntExist_3";
321
322         //Register the application at the dlt-daemon
323         dltResult = DLT_REGISTER_APP("FLTR","Test Application filetransfer");
324 //      if(dltResult < 0){
325 //              printf("Error: DLT_REIGSTER_APP: FLTR\n");
326 //              return -1;
327 //      }
328         //Register the context of the main program at the dlt-daemon
329         dltResult = DLT_REGISTER_CONTEXT(mainContext,"MAIN","Main context for filetransfer test");
330 //      if(dltResult < 0){
331 //              printf("Error: DLT_REGISTER_CONTEXT: MAIN\n");
332 //              return -1;
333 //      }
334         //Register the context in which the file transfer will be logged at the dlt-daemon
335         dltResult = DLT_REGISTER_CONTEXT(fileContext,"FLTR","Test Context for filetransfer");
336 //      if(dltResult < 0){
337 //              printf("Error: DLT_REGISTER_CONTEXT:FLTR\n");
338 //              return -1;
339 //      }
340
341         //More details in corresponding methods
342         testFile1Run1();
343         testFile1Run2();
344         testFile2Run1();
345         testFile2Run2();
346         testFile3Run1();
347         testFile3Run2();
348         testFile3Run3();
349
350         //Unregister the context in which the file transfer happened from the dlt-daemon
351         DLT_UNREGISTER_CONTEXT(fileContext);
352         //Unregister the context of the main program from the dlt-daemon
353         DLT_UNREGISTER_CONTEXT(mainContext);
354         //Unregister the app from the dlt-daemon
355         DLT_UNREGISTER_APP();
356
357         return(0);
358 }