init und deinit is not blocking anymore when they will be called multiple times
[profile/ivi/persistence-client-library.git] / test / persistence_client_library_test.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
4  * Company         XS Embedded GmbH
5  *****************************************************************************/
6 /******************************************************************************
7  * This Source Code Form is subject to the terms of the
8  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
9  * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
11  /**
12  * @file           persistence_client_library_test.c
13  * @ingroup        Persistence client library test
14  * @author         Ingo Huerner
15  * @brief          Test of persistence client library
16  * @see            
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>     /* exit */
24 #include <time.h>
25 #include <fcntl.h>
26 #include <sys/mman.h>
27
28 #include <dlt/dlt.h>
29 #include <dlt/dlt_common.h>
30
31 #include "persCheck.h"
32
33
34 #include "../include/persistence_client_library_file.h"
35 #include "../include/persistence_client_library_key.h"
36 #include "../include/persistence_client_library.h"
37
38
39 // protected header, should be used only be persistence components
40 // included here for testing purpose
41 #include "../include_protected/persistence_client_library_db_access.h"
42
43
44 #define BUF_SIZE     64
45 #define NUM_OF_FILES 3
46 #define READ_SIZE    1024
47
48 /// application id
49 char gTheAppId[MaxAppNameLen] = {0};
50
51 // definition of weekday
52 char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
53
54
55
56 /**
57  * Test the key value interface using different logicalDB id's, users and seats.
58  * Each resource below has an entry in the resource configuration table where the
59  * storage location (cached or write through) and type (e.g. custom) has been configured.
60  */
61 START_TEST(test_GetData)
62 {
63    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
64    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
65    X_TEST_REPORT_REFERENCE("NONE");
66    X_TEST_REPORT_DESCRIPTION("Test of get data");
67    X_TEST_REPORT_TYPE(GOOD);
68
69    int ret = 0;
70    unsigned int shutdownReg = (PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL);
71
72    unsigned char buffer[READ_SIZE] = {0};
73
74    ret = pclInitLibrary(gTheAppId, shutdownReg);
75    x_fail_unless(ret <= 1, "Failed to init PCL");
76
77 #if 1
78    /**
79     * Logical DB ID: 0xFF with user 0 and seat 0
80     *       ==> local value accessible by all users (user 0, seat 0)
81     */
82    ret = pclKeyReadData(0xFF, "pos/last_position",         0, 0, buffer, READ_SIZE);
83    x_fail_unless(strncmp((char*)buffer, "CACHE_ +48° 10' 38.95\", +8° 44' 39.06\"",
84                strlen((char*)buffer)) == 0, "Buffer not correctly read");
85    x_fail_unless(ret = strlen("CACHE_ +48° 10' 38.95\", +8° 44' 39.06\""));
86
87    memset(buffer, 0, READ_SIZE);
88
89    /**
90     * Logical DB ID: 0xFF with user 0 and seat 0
91     *       ==> local value accessible by all users (user 0, seat 0)
92     */
93    ret = pclKeyReadData(0xFF, "language/country_code",         0, 0, buffer, READ_SIZE);
94    x_fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data: secure!",
95                strlen((char*)buffer)) == 0, "Buffer not correctly read");
96    x_fail_unless(ret = strlen("Custom plugin -> plugin_get_data_handle"));
97
98    memset(buffer, 0, READ_SIZE);
99
100
101    /**
102     * Logical DB ID: 0 with user 3 and seat 0
103     *       ==> public shared user value (user 3, seat 0)
104     */
105    ret = pclKeyReadData(0,    "language/current_language", 3, 0, buffer, READ_SIZE);
106    x_fail_unless(strncmp((char*)buffer, "CACHE_ Kisuaheli", strlen((char*)buffer)) == 0, "Buffer not correctly read");
107
108    memset(buffer, 0, READ_SIZE);
109
110    /**
111     * Logical DB ID: 0xFF with user 3 and seat 2
112     *       ==> local USER value (user 3, seat 2)
113     */
114    ret = pclKeyReadData(0xFF, "status/open_document",      3, 2, buffer, READ_SIZE);
115    x_fail_unless(strncmp((char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0, "Buffer not correctly read");
116
117    memset(buffer, 0, READ_SIZE);
118
119    /**
120     * Logical DB ID: 0x20 with user 4 and seat 0
121     *       ==> shared user value accessible by a group (user 4 and seat 0)
122     */
123    ret = pclKeyReadData(0x20, "address/home_address",      4, 0, buffer, READ_SIZE);
124    x_fail_unless(strncmp((char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0, "Buffer not correctly read");
125
126    memset(buffer, 0, READ_SIZE);
127
128    /**
129     * Logical DB ID: 0xFF with user 0 and seat 0
130     *       ==> local value accessible by ALL USERS (user 0, seat 0)
131     */
132    ret = pclKeyReadData(0xFF, "pos/last_satellites",       0, 0, buffer, READ_SIZE);
133    x_fail_unless(strncmp((char*)buffer, "WT_ 17", strlen((char*)buffer)) == 0, "Buffer not correctly read");
134
135    memset(buffer, 0, READ_SIZE);
136
137    /**
138     * Logical DB ID: 0x84 with user 4 and seat 0
139     *       ==> shared user value accessible by A GROUP (user 4 and seat 0)
140     */
141    ret = pclKeyReadData(0x84, "links/last_link",           2, 0, buffer, READ_SIZE);
142    x_fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/brooklyn", strlen((char*)buffer)) == 0, "Buffer not correctly read");
143
144    memset(buffer, 0, READ_SIZE);
145
146    /**
147     * Logical DB ID: 0x84 with user 2 and seat 1
148     *       ==> local merge value
149     */
150    ret = pclKeyReadData(0x84, "links/last_link",           2, 1, buffer, READ_SIZE);
151    x_fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read");
152 #endif
153    pclDeinitLibrary();
154 }
155 END_TEST
156
157
158
159 /**
160  * Test the key value  h a n d l e  interface using different logicalDB id's, users and seats
161  * Each resource below has an entry in the resource configuration table where
162  * the storage location (cached or write through) and type (e.g. custom) has bee configured.
163  */
164 START_TEST (test_GetDataHandle)
165 {
166    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
167    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
168    X_TEST_REPORT_REFERENCE("NONE");
169    X_TEST_REPORT_DESCRIPTION("Test of get data handle");
170    X_TEST_REPORT_TYPE(GOOD);
171
172    int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0;
173    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
174
175    unsigned char buffer[READ_SIZE] = {0};
176    struct tm *locTime;
177
178    char sysTimeBuffer[128];
179
180    ret = pclInitLibrary(gTheAppId, shutdownReg);
181    x_fail_unless(ret <= 1, "Failed to init PCL");
182 #if 1
183    time_t t = time(0);
184
185    locTime = localtime(&t);
186
187    snprintf(sysTimeBuffer, 128, "TimeAndData: \"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900),
188                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
189
190
191    // open handle ---------------------------------------------------
192    /**
193     * Logical DB ID: 0xFF with user 0 and seat 0
194     *       ==> local value accessible by ALL USERS (user 0, seat 0)
195     */
196    handle = pclKeyHandleOpen(0xFF, "posHandle/last_position", 0, 0);
197    x_fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");
198
199    ret = pclKeyHandleReadData(handle, buffer, READ_SIZE);
200    x_fail_unless(strncmp((char*)buffer, "WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\"", ret-1) == 0, "Buffer not correctly read => 1");
201
202    size = pclKeyHandleGetSize(handle);
203    x_fail_unless(size == strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
204    // ---------------------------------------------------------------------------------------------
205
206
207    // open handle ---------------------------------------------------
208    /**
209     * Logical DB ID: 0xFF with user 3 and seat 2
210     *       ==> local USER value (user 3, seat 2)
211     */
212    handle2 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
213    x_fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
214
215    size = pclKeyHandleWriteData(handle2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
216    x_fail_unless(size == strlen(sysTimeBuffer));
217    // close
218    ret = pclKeyHandleClose(handle2);
219    // ---------------------------------------------------------------------------------------------
220
221
222    // open handle ---------------------------------------------------
223    /**
224     * Logical DB ID: 0xFF with user 0 and seat 0
225     *       ==> local value accessible by ALL USERS (user 0, seat 0)
226     */
227 #if 0 // plugin test case
228    memset(buffer, 0, READ_SIZE);
229    handle4 = pclKeyHandleOpen(0xFF, "language/country_code", 0, 0);
230    printf("H A N D L E: %d\n", handle4);
231    x_fail_unless(handle4 >= 0, "Failed to open handle /language/country_code");
232
233    ret = pclKeyHandleReadData(handle4, buffer, READ_SIZE);
234    x_fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle: secure!", -1) == 0, "Buffer not correctly read => 2");
235
236    size = pclKeyHandleGetSize(handle4);
237    x_fail_unless(size = strlen("Custom plugin -> plugin_get_data_handle"));
238
239    ret = pclKeyHandleWriteData(handle4, (unsigned char*)"Only dummy implementation behind custom library", READ_SIZE);
240 #endif
241    // ---------------------------------------------------------------------------------------------
242
243
244    // open handle ---------------------------------------------------
245    /**
246     * Logical DB ID: 0xFF with user 3 and seat 2
247     *       ==> local USER value (user 3, seat 2)
248     */
249    handle3 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
250    x_fail_unless(handle3 >= 0, "Failed to open handle /statusHandle/open_document");
251
252    ret = pclKeyHandleReadData(handle3, buffer, READ_SIZE);
253    x_fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read => 3");
254
255    size = pclKeyHandleGetSize(handle3);
256    x_fail_unless(size = strlen(sysTimeBuffer));
257    // ---------------------------------------------------------------------------------------------
258
259
260    // close handle
261    ret = pclKeyHandleClose(handle);
262    ret = pclKeyHandleClose(handle3);
263    ret = pclKeyHandleClose(handle4);
264 #endif
265    pclDeinitLibrary();
266 }
267 END_TEST
268
269
270 /*
271  * Write data to a key using the key interface.
272  * First write data to different keys and after
273  * read the data for verification.
274  */
275 START_TEST(test_SetData)
276 {
277    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
278    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
279    X_TEST_REPORT_REFERENCE("NONE");
280    X_TEST_REPORT_DESCRIPTION("Test of set data");
281    X_TEST_REPORT_TYPE(GOOD);
282
283    int ret = 0;
284    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
285    unsigned char buffer[READ_SIZE]  = {0};
286    char write1[READ_SIZE] = {0};
287    char write2[READ_SIZE] = {0};
288    char sysTimeBuffer[256];
289
290    struct tm *locTime;
291
292    ret = pclInitLibrary(gTheAppId, shutdownReg);
293    x_fail_unless(ret <= 1, "Failed to init PCL");
294 #if 1
295    time_t t = time(0);
296
297    locTime = localtime(&t);
298
299    // write data
300    snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900),
301                                                                  locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
302
303    /**
304     * Logical DB ID: 0xFF with user 1 and seat 2
305     *       ==> local USER value (user 1, seat 2)
306     * Resource ID: 69
307     */
308
309    ret = pclKeyWriteData(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
310    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
311 #if 1
312    snprintf(write1, 128, "%s %s", "/70",  sysTimeBuffer);
313    /**
314     * Logical DB ID: 0xFF with user 1 and seat 2
315     *       ==> local USER value (user 1, seat 2)
316     * Resource ID: 70
317     */
318    ret = pclKeyWriteData(0xFF, "70", 1, 2, (unsigned char*)write1, strlen(write1));
319    x_fail_unless(ret == strlen(write1), "Wrong write size");
320
321    snprintf(write2, 128, "%s %s", "/key_70",  sysTimeBuffer);
322    /**
323     * Logical DB ID: 0xFF with user 1 and seat 2
324     *       ==> local USER value (user 1, seat 2)
325     * Resource ID: key_70
326     */
327    ret = pclKeyWriteData(0xFF, "key_70", 1, 2, (unsigned char*)write2, strlen(write2));
328    x_fail_unless(ret == strlen(write2), "Wrong write size");
329
330
331    /*******************************************************************************************************************************************/
332    /* used for changed notification testing */
333    /*******************************************************************************************************************************************/
334    /**
335     * Logical DB ID: 0x84 with user 2 and seat 1
336     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
337     *
338     *       ==> used for shared testing
339     */
340    //printf("Write data to trigger change notification\n");
341    ret = pclKeyWriteData(0x84, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
342    x_fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
343
344    /**
345     * Logical DB ID: 0x84 with user 2 and seat 1
346     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
347     *
348     *       ==> used for shared testing
349     */
350    //printf("Write data to trigger change notification\n");
351    ret = pclKeyWriteData(0x84, "links/last_link3",  3, 2, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
352    x_fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
353
354    /**
355     * Logical DB ID: 0x84 with user 2 and seat 1
356     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
357     *
358     *       ==> used for shared testing
359     */
360    //printf("Write data to trigger change notification\n");
361    ret = pclKeyWriteData(0x84, "links/last_link4",  4, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
362    x_fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
363    /*******************************************************************************************************************************************/
364    /*******************************************************************************************************************************************/
365
366
367    /*
368     * now read the data written in the previous steps to the keys
369     * and verify data has been written correctly.
370     */
371    memset(buffer, 0, READ_SIZE);
372
373    ret = pclKeyReadData(0xFF, "69", 1, 2, buffer, READ_SIZE);
374    x_fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
375    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
376
377    memset(buffer, 0, READ_SIZE);
378
379    ret = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
380    x_fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
381    x_fail_unless(ret == strlen(write1), "Wrong read size");
382
383    memset(buffer, 0, READ_SIZE);
384
385    ret = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
386    x_fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
387    x_fail_unless(ret == strlen(write2), "Wrong read size");
388 #endif
389 #endif
390    pclDeinitLibrary();
391 }
392 END_TEST
393
394
395
396 /**
397  * Write data to a key using the key interface.
398  * The key is not in the persistence resource table.
399  * The key sill then be stored to the location local and cached.
400  */
401 START_TEST(test_SetDataNoPRCT)
402 {
403    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
404    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
405    X_TEST_REPORT_REFERENCE("NONE");
406    X_TEST_REPORT_DESCRIPTION("Test of set data no PRCT");
407    X_TEST_REPORT_TYPE(GOOD);
408
409    int ret = 0;
410    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
411    unsigned char buffer[READ_SIZE] = {0};
412    struct tm *locTime;
413
414    ret = pclInitLibrary(gTheAppId, shutdownReg);
415    x_fail_unless(ret <= 1, "Failed to init PCL");
416 #if 1
417    time_t t = time(0);
418
419    char sysTimeBuffer[128];
420
421    locTime = localtime(&t);
422
423    snprintf(sysTimeBuffer, 128, "TimeAndData: \"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900),
424                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
425
426    /**
427     * Logical DB ID: 0xFF with user 1 and seat 2
428     *       ==> local USER value (user 1, seat 2)
429     */
430    ret = pclKeyWriteData(0xFF, "NoPRCT", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
431    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
432    //printf("Write Buffer : %s\n", sysTimeBuffer);
433
434    // read data again and and verify datat has been written correctly
435    memset(buffer, 0, READ_SIZE);
436
437    ret = pclKeyReadData(0xFF, "NoPRCT", 1, 2, buffer, READ_SIZE);
438    x_fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
439    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
440    //printf("read buffer  : %s\n", buffer);
441 #endif
442    pclDeinitLibrary();
443 }
444 END_TEST
445
446
447
448 /*
449  * Test the key interface.
450  * Read the size of a key.
451  */
452 START_TEST(test_GetDataSize)
453 {
454    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
455    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
456    X_TEST_REPORT_REFERENCE("NONE");
457    X_TEST_REPORT_DESCRIPTION("Test of get data size");
458    X_TEST_REPORT_TYPE(GOOD);
459
460    int size = 0, ret = 0;
461
462    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
463
464    ret = pclInitLibrary(gTheAppId, shutdownReg);
465    x_fail_unless(ret <= 1, "Failed to init PCL");
466 #if 1
467    /**
468     * Logical DB ID: 0xFF with user 3 and seat 2
469     *       ==> local USER value (user 3, seat 2)
470     */
471    size = pclKeyGetSize(0xFF, "status/open_document", 3, 2);
472    x_fail_unless(size == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Invalid size");
473
474
475    /**
476     * Logical DB ID: 0x84 with user 2 and seat 1
477     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
478     */
479    size = pclKeyGetSize(0x84, "links/last_link", 2, 1);
480    x_fail_unless(size == strlen("CACHE_ /last_exit/queens"), "Invalid size");
481 #endif
482    pclDeinitLibrary();
483 }
484 END_TEST
485
486
487 /*
488  * Delete a key using the key value interface.
489  * First read a from a key, the delte the key
490  * and then try to read again. The Last read must fail.
491  */
492 START_TEST(test_DeleteData)
493 {
494    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
495    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
496    X_TEST_REPORT_REFERENCE("NONE");
497    X_TEST_REPORT_DESCRIPTION("Test of delete data");
498    X_TEST_REPORT_TYPE(GOOD);
499
500    int rval = 0;
501    unsigned char buffer[READ_SIZE];
502    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
503
504    rval = pclInitLibrary(gTheAppId, shutdownReg);
505    x_fail_unless(rval <= 1, "Failed to init PCL");
506 #if 1
507    // read data from key
508    rval = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
509    x_fail_unless(rval != EPERS_NOKEY, "Read form key key_70 fails");
510
511    // delete key
512    rval = pclKeyDelete(0xFF, "key_70", 1, 2);
513    x_fail_unless(rval == 0, "Failed to delete key");
514
515    // after deleting the key, reading from key must fail now!
516    rval = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
517    x_fail_unless(rval == EPERS_NOKEY, "Read form key key_70 works, but should fail");
518
519
520
521    // read data from key
522    rval = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
523    x_fail_unless(rval != EPERS_NOKEY, "Read form key 70 fails");
524
525    // delete key
526    rval = pclKeyDelete(0xFF, "70", 1, 2);
527    x_fail_unless(rval == 0, "Failed to delete key");
528
529    // after deleting the key, reading from key must fail now!
530    rval = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
531    x_fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail");
532 #endif
533    pclDeinitLibrary();
534 }
535 END_TEST
536
537
538
539 /*
540  * Test the file interface:
541  * - open file
542  * - read / write
543  * - remove file
544  * - map file
545  * - get size
546  */
547 START_TEST(test_DataFile)
548 {
549    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
550    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
551    X_TEST_REPORT_REFERENCE("NONE");
552    X_TEST_REPORT_DESCRIPTION("Test of data file");
553    X_TEST_REPORT_TYPE(GOOD);
554
555    int fd = 0, i = 0, idx = 0;
556    int size = 0, ret = 0;
557    int writeSize = 16*1024;
558    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
559
560    unsigned char buffer[READ_SIZE] = {0};
561    const char* refBuffer = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media";
562    char* writeBuffer;
563    char* fileMap = NULL;
564
565    ret = pclInitLibrary(gTheAppId, shutdownReg);
566    x_fail_unless(ret <= 1, "Failed to init PCL");
567 #if 1
568    writeBuffer = malloc(writeSize);
569
570
571    // fill buffer a sequence
572    for(i = 0; i<(writeSize/8); i++)
573    {
574       writeBuffer[idx++] = 'A';
575       writeBuffer[idx++] = 'B';
576       writeBuffer[idx++] = 'C';
577       writeBuffer[idx++] = ' ';
578       writeBuffer[idx++] = 'D';
579       writeBuffer[idx++] = 'E';
580       writeBuffer[idx++] = 'F';
581       writeBuffer[idx++] = ' ';
582    }
583    // create file
584    fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",
585              O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
586    close(fd);
587
588    // open ------------------------------------------------------------
589    fd = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
590    x_fail_unless(fd != -1, "Could not open file ==> /media/mediaDB.db");
591
592    size = pclFileGetSize(fd);
593    x_fail_unless(size == 68, "Wrong file size");
594
595    size = pclFileReadData(fd, buffer, READ_SIZE);
596    x_fail_unless(strncmp((char*)buffer, refBuffer, strlen(refBuffer)) == 0, "Buffer not correctly read => media/mediaDB.db");
597    x_fail_unless(size == (strlen(refBuffer)+1), "Wrong size returned");      // strlen + 1 ==> inlcude cr/lf
598
599
600    ret = pclFileClose(fd);
601    x_fail_unless(ret == 0, "Failed to close file");
602
603    // open ------------------------------------------------------------
604    fd = pclFileOpen(0xFF, "media/mediaDBWrite.db", 1, 1);
605    x_fail_unless(fd != -1, "Could not open file ==> /media/mediaDBWrite.db");
606
607    size = pclFileWriteData(fd, writeBuffer, strlen(writeBuffer));
608    x_fail_unless(size == strlen(writeBuffer), "Failed to write data");
609
610    ret = pclFileClose(fd);
611    x_fail_unless(ret == 0, "Failed to close file");
612
613
614    // remove ----------------------------------------------------------
615    ret = pclFileRemove(0xFF, "media/mediaDBWrite.db", 1, 1);
616    x_fail_unless(ret == 0, "File can't be removed ==> /media/mediaDBWrite.db");
617
618    fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",O_RDWR);
619    x_fail_unless(fd == -1, "Failed to remove file, file still exists");
620    close(fd);
621
622
623    // map file --------------------------------------------------------
624    fd = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
625
626    size = pclFileGetSize(fd);
627    pclFileMapData(fileMap, size, 0, fd);
628    x_fail_unless(fileMap != MAP_FAILED, "Failed to map file");
629
630    ret = pclFileUnmapData(fileMap, size);
631    x_fail_unless(ret != -1, "Failed to unmap file");
632
633    // negative test
634    size = pclFileGetSize(1024);
635    x_fail_unless(ret == 0, "Got size, but should not");
636
637    ret = pclFileClose(fd);
638    x_fail_unless(ret == 0, "Failed to close file");
639
640    free(writeBuffer);
641 #endif
642    pclDeinitLibrary();
643 }
644 END_TEST
645
646
647
648
649
650 START_TEST(test_DataFileRecovery)
651 {
652    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
653    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
654    X_TEST_REPORT_REFERENCE("NONE");
655    X_TEST_REPORT_DESCRIPTION("Test of data file recovery");
656    X_TEST_REPORT_TYPE(GOOD);
657
658    int fd_RW = 0, fd_RO = 0;
659    int ret = 0;
660    char* wBuffer = "This is a buffer to write";
661    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
662
663    ret = pclInitLibrary(gTheAppId, shutdownReg);
664    x_fail_unless(ret <= 1, "Failed to init PCL");
665 #if 1
666
667    // test backup creation --------------------------------------------
668    fd_RO = pclFileOpen(0xFF, "media/mediaDB_ReadOnly.db", 1, 1);
669    x_fail_unless(fd_RO != -1, "Could not open file ==> /media/mediaDB_ReadOnly.db");
670
671    fd_RW = pclFileOpen(0xFF, "media/mediaDB_ReadWrite.db", 1, 1);
672    x_fail_unless(fd_RW != -1, "Could not open file ==> /media/mediaDB_ReadWrite.db");
673    pclFileWriteData(fd_RW, wBuffer, strlen(wBuffer));
674
675    ret = pclFileClose(fd_RW);
676    if(ret == -1)
677
678    ret = pclFileClose(fd_RO);
679    if(ret == -1)
680
681 #endif
682    pclDeinitLibrary();
683 }
684 END_TEST
685
686 /*
687  * The the handle function of the key and file interface.
688  */
689 START_TEST(test_DataHandle)
690 {
691    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
692    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
693    X_TEST_REPORT_REFERENCE("NONE");
694    X_TEST_REPORT_DESCRIPTION("Test of data handle");
695    X_TEST_REPORT_TYPE(GOOD);
696
697    int handle1 = 0, handle2 = 0, size = 0;
698    int handleArray[4] = {0};
699    int ret = 0;
700    unsigned char buffer[READ_SIZE] = {0};
701    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
702
703    ret = pclInitLibrary(gTheAppId, shutdownReg);
704    x_fail_unless(ret <= 1, "Failed to init PCL");
705 #if 1
706    // test file handles
707    handle1 = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
708    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db");
709
710    ret = pclFileClose(handle1);
711    x_fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db");
712
713    ret = pclFileClose(1024);
714    x_fail_unless(ret == EPERS_MAXHANDLE, "Could close file, but should not!!");
715
716    ret = pclFileClose(17);
717    x_fail_unless(ret == -1, "Could close file, but should not!!");
718
719    // test multiple handles
720    handleArray[0] = pclFileOpen(0xFF, "media/mediaDB_write_01.db", 1, 1);
721    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_01.db");
722
723    handleArray[1] = pclFileOpen(0xFF, "media/mediaDB_write_02.db", 1, 1);
724    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_02.db");
725
726    handleArray[2] = pclFileOpen(0xFF, "media/mediaDB_write_03.db", 1, 1);
727    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_03.db");
728
729    handleArray[3] = pclFileOpen(0xFF, "media/mediaDB_write_04.db", 1, 1);
730    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_04.db");
731
732    size = pclFileReadData(handleArray[0], buffer, READ_SIZE);
733    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_01.db",
734          strlen("/user/1/seat/1/media/mediaDB_write_01.db"))
735          == 0, "Buffer not correctly read => mediaDB_write_01.db");
736
737    size = pclFileReadData(handleArray[1], buffer, READ_SIZE);
738    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_02.db",
739          strlen("/user/1/seat/1/media/mediaDB_write_02.db"))
740          == 0, "Buffer not correctly read => mediaDB_write_02.db");
741
742    size = pclFileReadData(handleArray[2], buffer, READ_SIZE);
743    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_03.db",
744          strlen("/user/1/seat/1/media/mediaDB_write_03.db"))
745          == 0, "Buffer not correctly read => mediaDB_write_03.db");
746
747    size = pclFileReadData(handleArray[3], buffer, READ_SIZE);
748    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_04.db",
749          strlen("/user/1/seat/1/media/mediaDB_write_04.db"))
750          == 0, "Buffer not correctly read => mediaDB_write_04.db");
751
752    ret = pclKeyHandleClose(handleArray[0]);
753    x_fail_unless(ret != -1, "Failed to close handle idx \"0\"!!");
754
755    ret = pclKeyHandleClose(handleArray[1]);
756    x_fail_unless(ret != -1, "Failed to close handle idx \"1\"!!");
757
758    ret = pclKeyHandleClose(handleArray[2]);
759    x_fail_unless(ret != -1, "Failed to close handle idx \"2\"!!");
760
761    ret = pclKeyHandleClose(handleArray[3]);
762    x_fail_unless(ret != -1, "Failed to close handle idx \"3\"!!");
763
764    // test key handles
765    handle2 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
766    x_fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
767
768    ret = pclKeyHandleClose(handle2);
769    x_fail_unless(ret != -1, "Failed to close handle!!");
770
771    ret = pclKeyHandleClose(1024);
772    x_fail_unless(ret == EPERS_MAXHANDLE, "Max handle!!");
773 #endif
774    pclDeinitLibrary();
775 }
776 END_TEST
777
778
779
780 /*
781  * Extended key handle test.
782  * Test have been created after a bug in the key handle function occured.
783  */
784 START_TEST(test_DataHandleOpen)
785 {
786    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
787    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
788    X_TEST_REPORT_REFERENCE("NONE");
789    X_TEST_REPORT_DESCRIPTION("Test of data handle open");
790    X_TEST_REPORT_TYPE(GOOD);
791
792    int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0;
793    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
794
795    ret = pclInitLibrary(gTheAppId, shutdownReg);
796    x_fail_unless(ret <= 1, "Failed to init PCL");
797 #if 1
798    // open handles ----------------------------------------------------
799    hd1 = pclKeyHandleOpen(0xFF, "posHandle/last_position1", 0, 0);
800    x_fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1");
801
802    hd2 = pclKeyHandleOpen(0xFF, "posHandle/last_position2", 0, 0);
803    x_fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2");
804
805    hd3 = pclKeyHandleOpen(0xFF, "posHandle/last_position3", 0, 0);
806    x_fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3");
807
808    // close handles ---------------------------------------------------
809    ret = pclKeyHandleClose(hd1);
810    x_fail_unless(ret != -1, "Failed to close handle!!");
811
812    ret = pclKeyHandleClose(hd2);
813    x_fail_unless(ret != -1, "Failed to close handle!!");
814
815    ret = pclKeyHandleClose(hd3);
816    x_fail_unless(ret != -1, "Failed to close handle!!");
817
818    // open handles ----------------------------------------------------
819    hd4 = pclKeyHandleOpen(0xFF, "posHandle/last_position4", 0, 0);
820    x_fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4");
821
822    hd5 = pclKeyHandleOpen(0xFF, "posHandle/last_position5", 0, 0);
823    x_fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5");
824
825    hd6 = pclKeyHandleOpen(0xFF, "posHandle/last_position6", 0, 0);
826    x_fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6");
827
828    hd7 = pclKeyHandleOpen(0xFF, "posHandle/last_position7", 0, 0);
829    x_fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7");
830
831    hd8 = pclKeyHandleOpen(0xFF, "posHandle/last_position8", 0, 0);
832    x_fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8");
833
834    hd9 = pclKeyHandleOpen(0xFF, "posHandle/last_position9", 0, 0);
835    x_fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9");
836
837    // close handles ---------------------------------------------------
838    ret = pclKeyHandleClose(hd4);
839    x_fail_unless(ret != -1, "Failed to close handle!!");
840
841    ret = pclKeyHandleClose(hd5);
842    x_fail_unless(ret != -1, "Failed to close handle!!");
843
844    ret = pclKeyHandleClose(hd6);
845    x_fail_unless(ret != -1, "Failed to close handle!!");
846
847    ret = pclKeyHandleClose(hd7);
848    x_fail_unless(ret != -1, "Failed to close handle!!");
849
850    ret = pclKeyHandleClose(hd8);
851    x_fail_unless(ret != -1, "Failed to close handle!!");
852
853    ret = pclKeyHandleClose(hd9);
854    x_fail_unless(ret != -1, "Failed to close handle!!");
855 #endif
856    pclDeinitLibrary();
857 }
858 END_TEST
859
860
861
862 /**
863  * Test for  i n t e r n a l  structures.
864  * Test the cursor functions.
865  */
866 START_TEST(test_Cursor)
867 {
868    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
869    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
870    X_TEST_REPORT_REFERENCE("NONE");
871    X_TEST_REPORT_DESCRIPTION("Test of cursor");
872    X_TEST_REPORT_TYPE(GOOD);
873
874    int handle = -1, rval = 0, size = 0, handle1 = 0;
875    char bufferKeySrc[READ_SIZE]  = {0};
876    char bufferDataSrc[READ_SIZE] = {0};
877    char bufferKeyDst[READ_SIZE]  = {0};
878    char bufferDataDst[READ_SIZE] = {0};
879    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
880
881    rval = pclInitLibrary(gTheAppId, shutdownReg);
882    x_fail_unless(rval <= 1, "Failed to init PCL");
883 #if 1
884    // create cursor
885    handle = pers_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/cached.itz");
886    x_fail_unless(handle != -1, "Failed to create cursor!!");
887
888    // create cursor
889    handle1 = pers_db_cursor_create("/Data/mnt-wt/lt-persistence_client_library_test/wt.itz");
890    x_fail_unless(handle1 != -1, "Failed to create cursor!!");
891
892    do
893    {
894       memset(bufferKeySrc, 0, READ_SIZE);
895       memset(bufferDataSrc, 0, READ_SIZE);
896       memset(bufferKeyDst, 0, READ_SIZE);
897       memset(bufferDataDst, 0, READ_SIZE);
898
899       // get key
900       rval = pers_db_cursor_get_key(handle, bufferKeySrc, 256);
901       x_fail_unless(rval != -1, "Cursor failed to get key!!");
902       // get data
903       rval = pers_db_cursor_get_data(handle, bufferDataSrc, 256);
904
905       x_fail_unless(rval != -1, "Cursor failed to get data!!");
906       // get size
907       size = pers_db_cursor_get_data_size(handle);
908       x_fail_unless(size != -1, "Cursor failed to get size!!");
909       //printf("1. Key: %s | Data: %s » Size: %d \n", bufferKeySrc, bufferDataSrc, size);
910
911       // get key
912       rval = pers_db_cursor_get_key(handle1, bufferKeyDst, 256);
913       x_fail_unless(rval != -1, "Cursor failed to get key!!");
914       // get data
915       rval = pers_db_cursor_get_data(handle1, bufferDataDst, 256);
916       x_fail_unless(rval != -1, "Cursor failed to get data!!");
917       // get size
918       size = pers_db_cursor_get_data_size(handle1);
919       x_fail_unless(size != -1, "Cursor failed to get size!!");
920       //printf("  2. Key: %s | Data: %s » Size: %d \n", bufferKeyDst, bufferDataDst, size);
921    }
922    while( (pers_db_cursor_next(handle) == 0) && (pers_db_cursor_next(handle1) == 0) ); // next cursor
923
924    // destory cursor
925    rval = pers_db_cursor_destroy(handle);
926    x_fail_unless(rval != -1, "Failed to destroy cursor!!");
927
928    rval = pers_db_cursor_destroy(handle1);
929    x_fail_unless(rval != -1, "Failed to destroy cursor!!");
930 #endif
931    pclDeinitLibrary();
932 }
933 END_TEST
934
935
936
937 START_TEST(test_Plugin)
938 {
939    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
940    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
941    X_TEST_REPORT_REFERENCE("NONE");
942    X_TEST_REPORT_DESCRIPTION("Test of plugins");
943    X_TEST_REPORT_TYPE(GOOD);
944
945         int ret = 0;
946         unsigned char buffer[READ_SIZE]  = {0};
947
948         unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
949
950    ret = pclInitLibrary(gTheAppId, shutdownReg);
951    x_fail_unless(ret <= 1, "Failed to init PCL");
952 #if 1
953         ret = pclKeyReadData(0xFF, "language/country_code",           0, 0, buffer, READ_SIZE);
954         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
955    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: secure!",
956                strlen((char*)buffer)) == 0, "Buffer SECURE not correctly read");
957
958
959         ret = pclKeyReadData(0xFF, "language/country_code_early",     0, 0, buffer, READ_SIZE);
960         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
961         //printf("B U F F E R - early: %s\n", buffer);
962    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: early!",
963                strlen((char*)buffer)) == 0, "Buffer EARLY not correctly read");
964
965         ret = pclKeyReadData(0xFF, "language/country_code_emergency", 0, 0, buffer, READ_SIZE);
966         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
967         //printf("B U F F E R - emergency: %s\n", buffer);
968    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: emergency!",
969                strlen((char*)buffer)) == 0, "Buffer EMERGENCY not correctly read");
970
971         ret = pclKeyReadData(0xFF, "language/info",                   0, 0, buffer, READ_SIZE);
972         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
973         //printf("B U F F E R - hwinfo: %s\n", buffer);
974    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: hwinfo!",
975                strlen((char*)buffer)) == 0, "Buffer HWINFO not correctly read");
976
977    ret = pclKeyReadData(0xFF, "language/country_code_custom3",   0, 0, buffer, READ_SIZE);
978    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
979    //printf("B U F F E R - hwinfo: %s\n", buffer);
980    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: custom3!",
981                strlen((char*)buffer)) == 0, "Buffer CUSTOM 3 not correctly read");
982 #endif
983         pclDeinitLibrary();
984 }
985 END_TEST
986
987
988
989
990
991 START_TEST(test_ReadDefault)
992 {
993    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
994    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
995    X_TEST_REPORT_REFERENCE("NONE");
996    X_TEST_REPORT_DESCRIPTION("Test of read default");
997    X_TEST_REPORT_TYPE(GOOD);
998
999    int ret = 0;
1000    unsigned char buffer[READ_SIZE]  = {0};
1001
1002    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1003
1004    ret = pclInitLibrary(gTheAppId, shutdownReg);
1005    x_fail_unless(ret <= 1, "Failed to init PCL");
1006 #if 1
1007    ret = pclKeyReadData(0xFF, "statusHandle/default01", 3, 2, buffer, READ_SIZE);
1008    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
1009    //printf("B U F F E R: %s\n", buffer);
1010    x_fail_unless(strncmp((char*)buffer,"DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
1011
1012    ret = pclKeyReadData(0xFF, "statusHandle/default02", 3, 2, buffer, READ_SIZE);
1013    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
1014    //printf("B U F F E R: %s\n", buffer);
1015    x_fail_unless(strncmp((char*)buffer,"DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
1016 #endif
1017    pclDeinitLibrary();
1018 }
1019 END_TEST
1020
1021
1022
1023 START_TEST(test_ReadConfDefault)
1024 {
1025    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
1026    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
1027    X_TEST_REPORT_REFERENCE("NONE");
1028    X_TEST_REPORT_DESCRIPTION("Test of configurable default data");
1029    X_TEST_REPORT_TYPE(GOOD);
1030
1031    int ret = 0;
1032    unsigned char buffer[READ_SIZE]  = {0};
1033
1034    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1035
1036    ret = pclInitLibrary(gTheAppId, shutdownReg);
1037    x_fail_unless(ret <= 1, "Failed to init PCL");
1038 #if 1
1039    ret = pclKeyReadData(0xFF, "statusHandle/confdefault01",     3, 2, buffer, READ_SIZE);
1040    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
1041    x_fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
1042
1043    ret = pclKeyReadData(0xFF, "statusHandle/confdefault02",     3, 2, buffer, READ_SIZE);
1044    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
1045    x_fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
1046 #endif
1047    pclDeinitLibrary();
1048 }
1049 END_TEST
1050
1051
1052
1053 START_TEST(test_GetPath)
1054 {
1055    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
1056    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
1057    X_TEST_REPORT_REFERENCE("NONE");
1058    X_TEST_REPORT_DESCRIPTION("Test of get path");
1059    X_TEST_REPORT_TYPE(GOOD);
1060
1061    int ret = 0;
1062    char* path = NULL;
1063    const char* thePath = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB_create.db";
1064    unsigned int pathSize = 0;
1065
1066    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1067
1068    ret = pclInitLibrary(gTheAppId, shutdownReg);
1069    x_fail_unless(ret <= 1, "Failed to init PCL");
1070 #if 1
1071    ret = pclFileCreatePath(0xFF, "media/mediaDB_create.db", 1, 1, &path, &pathSize);
1072    x_fail_unless(strncmp((char*)path, thePath, strlen((char*)path)) == 0, "Path not correct");
1073    x_fail_unless(pathSize == strlen((char*)path), "Path size not correct");
1074
1075    pclFileReleasePath(ret);
1076 #endif
1077    pclDeinitLibrary();
1078 }
1079 END_TEST
1080
1081
1082
1083 START_TEST(test_InitDeinit)
1084 {
1085    int ret = 0, i = 0;
1086    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1087
1088    for(i=0; i< 3; i++)
1089    {
1090       // initialize and deinitialize 1. time
1091       ret = pclInitLibrary(gTheAppId, shutdownReg);
1092       pclDeinitLibrary();
1093
1094
1095       // initialize and deinitialize 2. time
1096       ret = pclInitLibrary(gTheAppId, shutdownReg);
1097       pclDeinitLibrary();
1098
1099
1100       // initialize and deinitialize 3. time
1101       ret = pclInitLibrary(gTheAppId, shutdownReg);
1102       pclDeinitLibrary();
1103    }
1104 }
1105 END_TEST
1106
1107
1108
1109 static Suite * persistencyClientLib_suite()
1110 {
1111    Suite * s  = suite_create("Persistency client library");
1112
1113    TCase * tc_persGetData = tcase_create("GetData");
1114    tcase_add_test(tc_persGetData, test_GetData);
1115
1116    TCase * tc_persSetData = tcase_create("SetData");
1117    tcase_add_test(tc_persSetData, test_SetData);
1118
1119    TCase * tc_persSetDataNoPRCT = tcase_create("SetDataNoPRCT");
1120    tcase_add_test(tc_persSetDataNoPRCT, test_SetDataNoPRCT);
1121
1122    TCase * tc_persGetDataSize = tcase_create("GetDataSize");
1123    tcase_add_test(tc_persGetDataSize, test_GetDataSize);
1124
1125    TCase * tc_persDeleteData = tcase_create("DeleteData");
1126    tcase_add_test(tc_persDeleteData, test_DeleteData);
1127
1128    TCase * tc_persGetDataHandle = tcase_create("GetDataHandle");
1129    tcase_add_test(tc_persGetDataHandle, test_GetDataHandle);
1130
1131    TCase * tc_persDataHandle = tcase_create("DataHandle");
1132    tcase_add_test(tc_persDataHandle, test_DataHandle);
1133
1134    TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen");
1135    tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen);
1136
1137    TCase * tc_persDataFile = tcase_create("DataFile");
1138    tcase_add_test(tc_persDataFile, test_DataFile);
1139
1140    TCase * tc_persDataFileRecovery = tcase_create("DataFileRecovery");
1141    tcase_add_test(tc_persDataFileRecovery, test_DataFileRecovery);
1142
1143    TCase * tc_Cursor = tcase_create("Cursor");
1144    tcase_add_test(tc_Cursor, test_Cursor);
1145
1146    TCase * tc_Plugin = tcase_create("Plugin");
1147    tcase_add_test(tc_Plugin, test_Plugin);
1148
1149    TCase * tc_ReadDefault = tcase_create("ReadDefault");
1150    tcase_add_test(tc_ReadDefault, test_ReadDefault);
1151
1152    TCase * tc_ReadConfDefault = tcase_create("ReadConfDefault");
1153    tcase_add_test(tc_ReadConfDefault, test_ReadConfDefault);
1154
1155    TCase * tc_GetPath = tcase_create("GetPath");
1156    tcase_add_test(tc_GetPath, test_GetPath);
1157
1158    TCase * tc_InitDeinit = tcase_create("InitDeinit");
1159    tcase_add_test(tc_InitDeinit, test_InitDeinit);
1160
1161    suite_add_tcase(s, tc_persSetData);
1162    suite_add_tcase(s, tc_persGetData);
1163    suite_add_tcase(s, tc_persSetDataNoPRCT);
1164    suite_add_tcase(s, tc_persGetDataSize);
1165    suite_add_tcase(s, tc_persDeleteData);
1166    suite_add_tcase(s, tc_persGetDataHandle);
1167    suite_add_tcase(s, tc_persDataHandle);
1168    suite_add_tcase(s, tc_persDataHandleOpen);
1169    suite_add_tcase(s, tc_persDataFile);
1170    suite_add_tcase(s, tc_persDataFileRecovery);
1171    suite_add_tcase(s, tc_Cursor);
1172    suite_add_tcase(s, tc_ReadDefault);
1173    suite_add_tcase(s, tc_ReadConfDefault);
1174    suite_add_tcase(s, tc_GetPath);
1175    suite_add_tcase(s, tc_InitDeinit);
1176    //suite_add_tcase(s, tc_Plugin); // activate only if the plugins are available
1177    return s;
1178 }
1179
1180
1181 int main(int argc, char *argv[])
1182 {
1183    int nr_failed = 0,
1184           nr_run = 0,
1185             fail = 0,
1186                i = 0;
1187
1188    TestResult** tResult;
1189
1190    // assign application name
1191    strncpy(gTheAppId, "lt-persistence_client_library_test", MaxAppNameLen);
1192    gTheAppId[MaxAppNameLen-1] = '\0';
1193
1194    /// debug log and trace (DLT) setup
1195    DLT_REGISTER_APP("test","tests the persistence client library");
1196
1197 #if 1
1198    Suite * s = persistencyClientLib_suite();
1199    SRunner * sr = srunner_create(s);
1200    srunner_set_xml(sr, "/tmp/persistenceClientLibraryTest.xml");
1201    srunner_set_log(sr, "/tmp/persistenceClientLibraryTest.log");
1202    srunner_run_all(sr, /*CK_NORMAL*/ CK_VERBOSE);
1203
1204    nr_failed = srunner_ntests_failed(sr);
1205    nr_run = srunner_ntests_run(sr);
1206
1207    tResult = srunner_results(sr);
1208    for(i = 0; i< nr_run; i++)
1209    {
1210       fail = tr_rtype(tResult[i]);  // get status of each test
1211       //printf("[%d] Fail: %d \n", i, fail);
1212    }
1213
1214    srunner_free(sr);
1215 #endif
1216
1217    // unregister debug log and trace
1218    DLT_UNREGISTER_APP();
1219
1220    dlt_free();
1221
1222    return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;
1223
1224 }
1225