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