Added missing license header to two files
[profile/ivi/dlt-daemon.git] / src / tests / dlt-test-filetransfer.c
1 /*
2  * Dlt Test Client - Diagnostic Log and Trace
3  * @licence app begin@
4  *
5  * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
6  * 
7  * This program is free software; you can redistribute it and/or modify it under the terms of the 
8  * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 
11  * Public License, version 2.1, for more details.
12  * 
13  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along 
14  * with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
15  * 
16  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may 
17  * also be applicable to programs even in cases in which the program is not a library in the technical sense.
18  * 
19  * Linking DLT statically or dynamically with other modules is making a combined work based on DLT. You may 
20  * license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to 
21  * license your linked modules under the GNU Lesser General Public License, version 2.1, you 
22  * may use the program under the following exception.
23  * 
24  * As a special exception, the copyright holders of DLT give you permission to combine DLT 
25  * with software programs or libraries that are released under any license unless such a combination is not
26  * permitted by the license of such a software program or library. You may copy and distribute such a 
27  * system following the terms of the GNU Lesser General Public License, version 2.1, including this
28  * special exception, for DLT and the licenses of the other code concerned.
29  * 
30  * Note that people who make modified versions of DLT are not obligated to grant this special exception 
31  * for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, 
32  * version 2.1, gives permission to release a modified version without this exception; this exception 
33  * also makes it possible to release a modified version which carries forward this exception.
34  *
35  * @licence end@
36  */
37
38
39 /*******************************************************************************
40 **                                                                            **
41 **  SRC-MODULE: dlt-test-client.c                                             **
42 **                                                                            **
43 **  TARGET    : linux                                                         **
44 **                                                                            **
45 **  PROJECT   : DLT                                                           **
46 **                                                                            **
47 **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
48 **                                                                            **
49 **  PURPOSE   :                                                               **
50 **                                                                            **
51 **  REMARKS   :                                                               **
52 **                                                                            **
53 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
54 **                                                                            **
55 **  TO BE CHANGED BY USER [yes/no]: no                                        **
56 **                                                                            **
57 *******************************************************************************/
58
59 /*******************************************************************************
60 **                      Author Identity                                       **
61 ********************************************************************************
62 **                                                                            **
63 ** Initials     Name                       Company                            **
64 ** --------     -------------------------  ---------------------------------- **
65 **  aw          Alexander Wenzel           BMW                                **
66 *******************************************************************************/
67
68
69 #include <dlt_filetransfer.h>   /*Needed for transferring files with the dlt protocol*/
70 #include <dlt.h>                                /*Needed for dlt logging*/
71
72 //!Declare some context for the main program. It's a must have to do this, when you want to log with dlt.
73 DLT_DECLARE_CONTEXT(mainContext);
74
75 //!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.
76 DLT_DECLARE_CONTEXT(fileContext);
77
78 //!Textfile which will be transferred.
79 char *file1;
80 //!Image which will be transferred.
81 char *file2;
82 //!Not existing file which will be transferred.
83 char *file3_1;
84 //!Not existing file which will be transferred.
85 char *file3_2;
86 //!Not existing file which will be transferred.
87 char *file3_3;
88 //!Just some variables
89 int i,countPackages, transferResult, dltResult;
90
91 //!Prints the test result
92 void printTestResultPositiveExpected(const char *function, int result){
93
94         if(result >= 0){
95                 printf("%s successful\n",function);
96         }
97         else
98         {
99                 printf("%s failed\n",function);
100         }
101
102 }
103
104 //!Prints the test result
105 void printTestResultNegativeExpected(const char *function, int result){
106
107         if(result < 0){
108                 printf("%s successful\n",function);
109         }
110         else
111         {
112                 printf("%s failed\n",function);
113         }
114
115 }
116
117 //!Test the file transfer with the condition that the transferred file is smaller as the file transfer buffer using dlt_user_log_file_complete.
118 int testFile1Run1(){
119         //Just some log to the main context
120         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF1P1 - dlt_user_log_file_complete"),DLT_STRING(file1));
121
122         //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  
123         transferResult = dlt_user_log_file_complete(&fileContext,file1,0,20);
124         if(transferResult < 0 )
125         {
126                         printf("Error: dlt_user_log_file_complete\n");
127                         return transferResult;
128         }
129         //Just some log to the main context
130         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF1P1"),DLT_STRING(file1));
131         
132         printTestResultPositiveExpected(__FUNCTION__,transferResult);
133
134         return transferResult;
135 }
136
137 //!Test the file transfer with the condition that the transferred file is smaller as the file transfer buffer using single package transfer 
138 int testFile1Run2(){
139         int total_size, used_size;
140         
141         //Get the information how many packages have the file
142         countPackages = dlt_user_log_file_packagesCount(&fileContext,file1);
143         if(countPackages < 0 )
144         {
145                         printf("Error: dlt_user_log_file_packagesCount\n");
146                         printTestResultPositiveExpected(__FUNCTION__,countPackages);
147                         return -1;
148         }
149         //Just some log to the main context
150         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF1P2 - transfer single package"),DLT_STRING(file1));
151                                                 
152         //Logs the header of the file transfer. For more details see Mainpage.c. 
153         //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
154         transferResult = dlt_user_log_file_header(&fileContext,file1);
155         if(transferResult >= 0)
156         {
157                 //Loop to log all packages
158                 for(i=1;i<=countPackages;i++)
159                 {
160                         dlt_user_check_buffer(&total_size, &used_size);
161                         if((total_size - used_size) < (total_size/2))
162                         {
163                                 printf("Error: dlt_user_log_file_data\n");
164                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
165                                 break;
166                         }
167
168                         //Logs one single package to the file context   
169                         transferResult = dlt_user_log_file_data(&fileContext,file1,i,20);
170                         if(transferResult < 0)
171                         {
172                                 printf("Error: dlt_user_log_file_data\n");
173                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
174                                 return transferResult;
175                         }
176                 }
177
178                 //Logs the end of the file transfer. For more details see Mainpage.c
179                 //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.  
180                 transferResult = dlt_user_log_file_end(&fileContext,file1,0);
181                 if(transferResult < 0)
182                 {
183                         printf("Error: dlt_user_log_file_end\n");
184                         printTestResultPositiveExpected(__FUNCTION__,transferResult);
185                         return transferResult;
186                 }
187         }
188         else
189         {
190                 printf("Error: dlt_user_log_file_header\n");
191                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
192                 return transferResult;
193         }
194         
195         //Just some log to main context
196         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF1P2 - transfer single package"),DLT_STRING(file1));
197         printTestResultPositiveExpected(__FUNCTION__,transferResult);
198         return 0;
199 }
200 //!Test the file transfer with the condition that the transferred file is bigger as the file transfer buffer using dlt_user_log_file_complete.
201 int testFile2Run1(){
202         //Just some log to main context
203         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF2P1 - dlt_user_log_file_complete"),DLT_STRING(file2));
204
205         //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
206         transferResult = dlt_user_log_file_complete(&fileContext,file2,0,20);
207         if(transferResult < 0)
208         {
209                 printf("Error: dlt_user_log_file_complete\n");
210                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
211                 return transferResult;
212         }
213         //Just some log to main context
214         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF2P1"),DLT_STRING(file2));
215         printTestResultPositiveExpected(__FUNCTION__,transferResult);
216         return transferResult;
217 }
218
219 //!Test the file transfer with the condition that the transferred file is bigger as the file transfer buffer using single package transfer
220 int testFile2Run2(){
221         int total_size, used_size;
222         
223         //Get the information how many packages have the file
224         countPackages = dlt_user_log_file_packagesCount(&fileContext,file2);
225         if(countPackages < 0 )
226         {
227                         printf("Error: dlt_user_log_file_packagesCount\n");
228                         printTestResultPositiveExpected(__FUNCTION__,countPackages);
229                         return -1;
230         }
231         
232         //Just some log to the main context
233         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF2P2 - transfer single package"),DLT_STRING(file2));
234         
235         //Logs the header of the file transfer. For more details see Mainpage.c. 
236         //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
237         transferResult = dlt_user_log_file_header(&fileContext,file2);
238         if( transferResult >= 0){
239         
240                 //Loop to log all packages      
241                 for(i=1;i<=countPackages;i++)
242                 {
243                         dlt_user_check_buffer(&total_size, &used_size);
244                         if((total_size - used_size) < (total_size/2))
245                         {
246                                 printf("Error: dlt_user_log_file_data\n");
247                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
248                                 break;
249                         }
250
251                         //Logs one single package to the file context
252                         transferResult = dlt_user_log_file_data(&fileContext,file2,i,20);
253                         if(transferResult < 0)
254                         {
255                                 printf("Error: dlt_user_log_file_data\n");
256                                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
257                                 return transferResult;
258                         }
259                 }
260
261                 //Logs the end of the file transfer. For more details see Mainpage.c
262                 //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.
263                 transferResult = dlt_user_log_file_end(&fileContext,file2,0);
264                 if(transferResult < 0)
265                 {
266                         printf("Error: dlt_user_log_file_end\n");
267                         printTestResultPositiveExpected(__FUNCTION__,transferResult);
268                         return transferResult;
269                 }
270         }
271         else
272         {
273                 printf("Error: dlt_user_log_file_header\n");
274                 printTestResultPositiveExpected(__FUNCTION__,transferResult);
275                 return transferResult;
276         }
277         //Just some log to the main context
278         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF2P2"),DLT_STRING(file2));
279         printTestResultPositiveExpected(__FUNCTION__,transferResult);
280         return 0;
281 }
282
283 //!Test the file transfer with the condition that the transferred file does not exist using dlt_user_log_file_complete.
284 int testFile3Run1(){
285         
286         //Just some log to the main context
287         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P1"),DLT_STRING(file3_1));
288
289         //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
290         transferResult = dlt_user_log_file_complete(&fileContext,file3_1,0,20);
291         if(transferResult < 0)
292         {
293                 //Error expected because file doesn't exist
294                 //printf("Error: dlt_user_log_file_complete\n");
295                 //Just some log to the main context
296                 DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P1"),DLT_STRING(file3_1));
297                 printTestResultNegativeExpected(__FUNCTION__,transferResult);
298                 return transferResult;
299         }
300         printTestResultNegativeExpected(__FUNCTION__,transferResult);
301         return transferResult;
302 }
303
304
305 //!Test the file transfer with the condition that the transferred file does not exist using single package transfer
306 int testFile3Run2(){
307
308         //Get the information how many packages have the file
309         countPackages = dlt_user_log_file_packagesCount(&fileContext,file3_2);
310         if(countPackages < 0 )
311         {
312                         //Error expected because file doesn't exist
313                         //printf("Error: dlt_user_log_file_packagesCount\n");
314                         //Just some log to the main context
315                         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P1"),DLT_STRING(file3_2));
316                         printTestResultNegativeExpected(__FUNCTION__,countPackages);
317                         return -1;
318         }
319         //Just some log to the main context
320         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P1"),DLT_STRING(file3_2));
321         
322         //Logs the header of the file transfer. For more details see Mainpage.c. 
323         //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
324         transferResult = dlt_user_log_file_header(&fileContext,file3_2);
325         if( transferResult >= 0){
326         
327                 //Loop to log all packages      
328                 for(i=1;i<=countPackages;i++)
329                 {
330                         //Logs one single package to the file context
331                         transferResult = dlt_user_log_file_data(&fileContext,file3_2,i,20);
332                         if(transferResult < 0)
333                         {
334                                 printf("Error: dlt_user_log_file_data\n");
335                                 printTestResultNegativeExpected(__FUNCTION__,transferResult);
336                                 return transferResult;
337                         }
338                 }
339
340                 //Logs the end of the file transfer. For more details see Mainpage.c
341                 //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.
342                 transferResult = dlt_user_log_file_end(&fileContext,file3_2,0);
343                 if(transferResult < 0)
344                 {
345                         printf("Error: dlt_user_log_file_end\n");
346                         printTestResultNegativeExpected(__FUNCTION__,transferResult);
347                         return transferResult;
348                 }
349         }
350         printTestResultNegativeExpected(__FUNCTION__,transferResult);
351         return 0;
352 }
353
354
355 //!Logs some information about the file.
356 int testFile3Run3(){
357         
358         //Just some log to the main context
359         DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P2"),DLT_STRING(file3_3));
360         
361         //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
362         transferResult = dlt_user_log_file_infoAbout(&fileContext,file3_3);
363         if(transferResult < 0)
364         {
365                 //Error expected because file doesn't exist
366                 //printf("Error: dlt_user_log_file_infoAbout\n");
367                 //Just some log to the main context
368                 DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P2"),DLT_STRING(file3_3));
369                 printTestResultNegativeExpected(__FUNCTION__,transferResult);
370                 return transferResult;
371         }
372         printTestResultNegativeExpected(__FUNCTION__,transferResult);
373         return 0;
374 }
375
376 //!Main program dlt-test-filestransfer starts here
377 int main(void)
378 {
379         //First file contains some text
380         file1 = "/usr/share/dlt-filetransfer/dlt-test-filetransfer-file";
381         //Second file is a picture
382         file2 = "/usr/share/dlt-filetransfer/dlt-test-filetransfer-image.png";
383         //Third file doesn't exist. Just to test the reaction when the file isn't available.
384         file3_1 = "dlt-test-filetransfer-doesntExist_1";
385         //Third file doesn't exist. Just to test the reaction when the file isn't available.
386         file3_2 = "dlt-test-filetransfer-doesntExist_2";
387         //Third file doesn't exist. Just to test the reaction when the file isn't available.
388         file3_3 = "dlt-test-filetransfer-doesntExist_3";
389
390         //Register the application at the dlt-daemon
391         dltResult = DLT_REGISTER_APP("FLTR","Test Application filetransfer");
392 //      if(dltResult < 0){
393 //              printf("Error: DLT_REIGSTER_APP: FLTR\n");
394 //              return -1;
395 //      }
396         //Register the context of the main program at the dlt-daemon
397         dltResult = DLT_REGISTER_CONTEXT(mainContext,"MAIN","Main context for filetransfer test");
398 //      if(dltResult < 0){
399 //              printf("Error: DLT_REGISTER_CONTEXT: MAIN\n");
400 //              return -1;
401 //      }
402         //Register the context in which the file transfer will be logged at the dlt-daemon
403         dltResult = DLT_REGISTER_CONTEXT(fileContext,"FLTR","Test Context for filetransfer");
404 //      if(dltResult < 0){
405 //              printf("Error: DLT_REGISTER_CONTEXT:FLTR\n");
406 //              return -1;
407 //      }
408
409         //More details in corresponding methods
410         testFile1Run1();
411         testFile1Run2();
412         testFile2Run1();
413         testFile2Run2();
414         testFile3Run1();
415         testFile3Run2();
416         testFile3Run3();
417
418         //Unregister the context in which the file transfer happened from the dlt-daemon
419         DLT_UNREGISTER_CONTEXT(fileContext);
420         //Unregister the context of the main program from the dlt-daemon
421         DLT_UNREGISTER_CONTEXT(mainContext);
422         //Unregister the app from the dlt-daemon
423         DLT_UNREGISTER_APP();
424
425         return(0);
426 }