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