Corrected tests
[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    /*
96    ret = pclKeyReadData(0xFF, "language/country_code",         0, 0, buffer, READ_SIZE);
97    x_fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data: secure!",
98                strlen((char*)buffer)) == 0, "Buffer not correctly read");
99    x_fail_unless(ret = strlen("Custom plugin -> plugin_get_data_handle"));
100
101    memset(buffer, 0, READ_SIZE);
102    */
103
104
105    /**
106     * Logical DB ID: 0 with user 3 and seat 0
107     *       ==> public shared user value (user 3, seat 0)
108     */
109    ret = pclKeyReadData(0,    "language/current_language", 3, 0, buffer, READ_SIZE);
110    x_fail_unless(strncmp((char*)buffer, "CACHE_ Kisuaheli", strlen((char*)buffer)) == 0, "Buffer not correctly read");
111
112    memset(buffer, 0, READ_SIZE);
113
114    /**
115     * Logical DB ID: 0xFF with user 3 and seat 2
116     *       ==> local USER value (user 3, seat 2)
117     */
118    ret = pclKeyReadData(0xFF, "status/open_document",      3, 2, buffer, READ_SIZE);
119    x_fail_unless(strncmp((char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0, "Buffer not correctly read");
120
121    memset(buffer, 0, READ_SIZE);
122
123    /**
124     * Logical DB ID: 0x20 with user 4 and seat 0
125     *       ==> shared user value accessible by a group (user 4 and seat 0)
126     */
127    ret = pclKeyReadData(0x20, "address/home_address",      4, 0, buffer, READ_SIZE);
128    x_fail_unless(strncmp((char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0, "Buffer not correctly read");
129
130    memset(buffer, 0, READ_SIZE);
131
132    /**
133     * Logical DB ID: 0xFF with user 0 and seat 0
134     *       ==> local value accessible by ALL USERS (user 0, seat 0)
135     */
136    ret = pclKeyReadData(0xFF, "pos/last_satellites",       0, 0, buffer, READ_SIZE);
137    x_fail_unless(strncmp((char*)buffer, "WT_ 17", strlen((char*)buffer)) == 0, "Buffer not correctly read");
138
139    memset(buffer, 0, READ_SIZE);
140
141    /**
142     * Logical DB ID: 0x84 with user 4 and seat 0
143     *       ==> shared user value accessible by A GROUP (user 4 and seat 0)
144     */
145    ret = pclKeyReadData(0x84, "links/last_link",           2, 0, buffer, READ_SIZE);
146    x_fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/brooklyn", strlen((char*)buffer)) == 0, "Buffer not correctly read");
147
148    memset(buffer, 0, READ_SIZE);
149
150    /**
151     * Logical DB ID: 0x84 with user 2 and seat 1
152     *       ==> local merge value
153     */
154    ret = pclKeyReadData(0x84, "links/last_link",           2, 1, buffer, READ_SIZE);
155    x_fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read");
156 #endif
157    pclDeinitLibrary();
158 }
159 END_TEST
160
161
162
163 /**
164  * Test the key value  h a n d l e  interface using different logicalDB id's, users and seats
165  * Each resource below has an entry in the resource configuration table where
166  * the storage location (cached or write through) and type (e.g. custom) has bee configured.
167  */
168 START_TEST (test_GetDataHandle)
169 {
170    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
171    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
172    X_TEST_REPORT_REFERENCE("NONE");
173    X_TEST_REPORT_DESCRIPTION("Test of get data handle");
174    X_TEST_REPORT_TYPE(GOOD);
175
176    int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0;
177    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
178
179    unsigned char buffer[READ_SIZE] = {0};
180    struct tm *locTime;
181
182    char sysTimeBuffer[128];
183
184    ret = pclInitLibrary(gTheAppId, shutdownReg);
185    x_fail_unless(ret <= 1, "Failed to init PCL");
186 #if 1
187    time_t t = time(0);
188
189    locTime = localtime(&t);
190
191    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),
192                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
193
194
195    // open handle ---------------------------------------------------
196    /**
197     * Logical DB ID: 0xFF with user 0 and seat 0
198     *       ==> local value accessible by ALL USERS (user 0, seat 0)
199     */
200    handle = pclKeyHandleOpen(0xFF, "posHandle/last_position", 0, 0);
201    x_fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");
202
203    ret = pclKeyHandleReadData(handle, buffer, READ_SIZE);
204    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");
205
206    size = pclKeyHandleGetSize(handle);
207    x_fail_unless(size == strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
208    // ---------------------------------------------------------------------------------------------
209
210
211    // open handle ---------------------------------------------------
212    /**
213     * Logical DB ID: 0xFF with user 3 and seat 2
214     *       ==> local USER value (user 3, seat 2)
215     */
216    handle2 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
217    x_fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
218
219    size = pclKeyHandleWriteData(handle2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
220    printf("pclKeyHandleWriteData: Soll: %d | ist:%d\n", strlen(sysTimeBuffer), size);
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    ret = pclKeyWriteData(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
314    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
315 #if 1
316    snprintf(write1, 128, "%s %s", "/70",  sysTimeBuffer);
317    /**
318     * Logical DB ID: 0xFF with user 1 and seat 2
319     *       ==> local USER value (user 1, seat 2)
320     * Resource ID: 70
321     */
322    ret = pclKeyWriteData(0xFF, "70", 1, 2, (unsigned char*)write1, strlen(write1));
323    x_fail_unless(ret == strlen(write1), "Wrong write size");
324
325    snprintf(write2, 128, "%s %s", "/key_70",  sysTimeBuffer);
326    /**
327     * Logical DB ID: 0xFF with user 1 and seat 2
328     *       ==> local USER value (user 1, seat 2)
329     * Resource ID: key_70
330     */
331    ret = pclKeyWriteData(0xFF, "key_70", 1, 2, (unsigned char*)write2, strlen(write2));
332    x_fail_unless(ret == strlen(write2), "Wrong write size");
333
334
335    /*******************************************************************************************************************************************/
336    /* used for changed notification testing */
337    /*******************************************************************************************************************************************/
338    /**
339     * Logical DB ID: 0x84 with user 2 and seat 1
340     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
341     *
342     *       ==> used for shared testing
343     */
344    //printf("Write data to trigger change notification\n");
345    ret = pclKeyWriteData(0x84, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
346    x_fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
347
348    /**
349     * Logical DB ID: 0x84 with user 2 and seat 1
350     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
351     *
352     *       ==> used for shared testing
353     */
354    //printf("Write data to trigger change notification\n");
355    ret = pclKeyWriteData(0x84, "links/last_link3",  3, 2, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
356    x_fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
357
358    /**
359     * Logical DB ID: 0x84 with user 2 and seat 1
360     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
361     *
362     *       ==> used for shared testing
363     */
364    //printf("Write data to trigger change notification\n");
365    ret = pclKeyWriteData(0x84, "links/last_link4",  4, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
366    x_fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
367    /*******************************************************************************************************************************************/
368    /*******************************************************************************************************************************************/
369
370
371    /*
372     * now read the data written in the previous steps to the keys
373     * and verify data has been written correctly.
374     */
375    memset(buffer, 0, READ_SIZE);
376
377    ret = pclKeyReadData(0xFF, "69", 1, 2, buffer, READ_SIZE);
378    x_fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
379    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
380
381    memset(buffer, 0, READ_SIZE);
382
383    ret = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
384    x_fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
385    x_fail_unless(ret == strlen(write1), "Wrong read size");
386
387    memset(buffer, 0, READ_SIZE);
388
389    ret = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
390    x_fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
391    x_fail_unless(ret == strlen(write2), "Wrong read size");
392 #endif
393 #endif
394    pclDeinitLibrary();
395 }
396 END_TEST
397
398
399
400 /**
401  * Write data to a key using the key interface.
402  * The key is not in the persistence resource table.
403  * The key sill then be stored to the location local and cached.
404  */
405 START_TEST(test_SetDataNoPRCT)
406 {
407    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
408    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
409    X_TEST_REPORT_REFERENCE("NONE");
410    X_TEST_REPORT_DESCRIPTION("Test of set data no PRCT");
411    X_TEST_REPORT_TYPE(GOOD);
412
413    int ret = 0;
414    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
415    unsigned char buffer[READ_SIZE] = {0};
416    struct tm *locTime;
417
418    ret = pclInitLibrary(gTheAppId, shutdownReg);
419    x_fail_unless(ret <= 1, "Failed to init PCL");
420 #if 1
421    time_t t = time(0);
422
423    char sysTimeBuffer[128];
424
425    locTime = localtime(&t);
426
427    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),
428                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
429
430    /**
431     * Logical DB ID: 0xFF with user 1 and seat 2
432     *       ==> local USER value (user 1, seat 2)
433     */
434    ret = pclKeyWriteData(0xFF, "NoPRCT", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
435    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
436    //printf("Write Buffer : %s\n", sysTimeBuffer);
437
438    // read data again and and verify datat has been written correctly
439    memset(buffer, 0, READ_SIZE);
440
441    ret = pclKeyReadData(0xFF, "NoPRCT", 1, 2, buffer, READ_SIZE);
442    x_fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
443    x_fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
444    //printf("read buffer  : %s\n", buffer);
445 #endif
446    pclDeinitLibrary();
447 }
448 END_TEST
449
450
451
452 /*
453  * Test the key interface.
454  * Read the size of a key.
455  */
456 START_TEST(test_GetDataSize)
457 {
458    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
459    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
460    X_TEST_REPORT_REFERENCE("NONE");
461    X_TEST_REPORT_DESCRIPTION("Test of get data size");
462    X_TEST_REPORT_TYPE(GOOD);
463
464    int size = 0, ret = 0;
465
466    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
467
468    ret = pclInitLibrary(gTheAppId, shutdownReg);
469    x_fail_unless(ret <= 1, "Failed to init PCL");
470 #if 1
471    /**
472     * Logical DB ID: 0xFF with user 3 and seat 2
473     *       ==> local USER value (user 3, seat 2)
474     */
475    size = pclKeyGetSize(0xFF, "status/open_document", 3, 2);
476    x_fail_unless(size == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Invalid size");
477
478
479    /**
480     * Logical DB ID: 0x84 with user 2 and seat 1
481     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
482     */
483    size = pclKeyGetSize(0x84, "links/last_link", 2, 1);
484    printf("=>=>=>=> soll: %d | ist: %d\n", strlen("CACHE_ /last_exit/queens"), size);
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    ret = pclFileClose(fd);
605    x_fail_unless(ret == 0, "Failed to close file");
606
607    // open ------------------------------------------------------------
608    fd = pclFileOpen(0xFF, "media/mediaDBWrite.db", 1, 1);
609    x_fail_unless(fd != -1, "Could not open file ==> /media/mediaDBWrite.db");
610
611    size = pclFileWriteData(fd, writeBuffer, strlen(writeBuffer));
612    x_fail_unless(size == strlen(writeBuffer), "Failed to write data");
613    ret = pclFileClose(fd);
614    x_fail_unless(ret == 0, "Failed to close file");
615
616    // remove ----------------------------------------------------------
617    ret = pclFileRemove(0xFF, "media/mediaDBWrite.db", 1, 1);
618    x_fail_unless(ret == 0, "File can't be removed ==> /media/mediaDBWrite.db");
619
620    fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",O_RDWR);
621    x_fail_unless(fd == -1, "Failed to remove file, file still exists");
622    close(fd);
623
624    // map file --------------------------------------------------------
625
626    fd = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
627
628    size = pclFileGetSize(fd);
629    pclFileMapData(fileMap, size, 0, fd);
630    x_fail_unless(fileMap != MAP_FAILED, "Failed to map file");
631
632    ret = pclFileUnmapData(fileMap, size);
633    x_fail_unless(ret != -1, "Failed to unmap file");
634
635    // negative test
636    size = pclFileGetSize(1024);
637    x_fail_unless(ret == 0, "Got size, but should not");
638
639    ret = pclFileClose(fd);
640    x_fail_unless(ret == 0, "Failed to close file");
641
642    free(writeBuffer);
643 #endif
644    pclDeinitLibrary();
645 }
646 END_TEST
647
648
649
650
651
652 START_TEST(test_DataFileRecovery)
653 {
654    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
655    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
656    X_TEST_REPORT_REFERENCE("NONE");
657    X_TEST_REPORT_DESCRIPTION("Test of data file recovery");
658    X_TEST_REPORT_TYPE(GOOD);
659
660    int fd_RW = 0, fd_RO = 0;
661    int ret = 0;
662    char* wBuffer = "This is a buffer to write";
663    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
664
665    ret = pclInitLibrary(gTheAppId, shutdownReg);
666    x_fail_unless(ret <= 1, "Failed to init PCL");
667 #if 1
668
669    // test backup creation --------------------------------------------
670    fd_RO = pclFileOpen(0xFF, "media/mediaDB_ReadOnly.db", 1, 1);
671    x_fail_unless(fd_RO != -1, "Could not open file ==> /media/mediaDB_ReadOnly.db");
672
673    fd_RW = pclFileOpen(0xFF, "media/mediaDB_ReadWrite.db", 1, 1);
674    x_fail_unless(fd_RW != -1, "Could not open file ==> /media/mediaDB_ReadWrite.db");
675    pclFileWriteData(fd_RW, wBuffer, strlen(wBuffer));
676
677    ret = pclFileClose(fd_RW);
678    if(ret == -1)
679
680    ret = pclFileClose(fd_RO);
681    if(ret == -1)
682
683 #endif
684    pclDeinitLibrary();
685 }
686 END_TEST
687
688 /*
689  * The the handle function of the key and file interface.
690  */
691 START_TEST(test_DataHandle)
692 {
693    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
694    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
695    X_TEST_REPORT_REFERENCE("NONE");
696    X_TEST_REPORT_DESCRIPTION("Test of data handle");
697    X_TEST_REPORT_TYPE(GOOD);
698
699    int handle1 = 0, handle2 = 0;
700    int handleArray[4] = {0};
701    int ret = 0;
702    unsigned char buffer[READ_SIZE] = {0};
703    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
704
705    ret = pclInitLibrary(gTheAppId, shutdownReg);
706    x_fail_unless(ret <= 1, "Failed to init PCL");
707 #if 1
708    // test file handles
709    handle1 = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
710    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db");
711
712    ret = pclFileClose(handle1);
713    x_fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db");
714
715    ret = pclFileClose(1024);
716    x_fail_unless(ret == EPERS_MAXHANDLE, "Could close file, but should not!!");
717
718    ret = pclFileClose(17);
719    x_fail_unless(ret == -1, "Could close file, but should not!!");
720
721    // test multiple handles
722    handleArray[0] = pclFileOpen(0xFF, "media/mediaDB_write_01.db", 1, 1);
723    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_01.db");
724
725    handleArray[1] = pclFileOpen(0xFF, "media/mediaDB_write_02.db", 1, 1);
726    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_02.db");
727
728    handleArray[2] = pclFileOpen(0xFF, "media/mediaDB_write_03.db", 1, 1);
729    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_03.db");
730
731    handleArray[3] = pclFileOpen(0xFF, "media/mediaDB_write_04.db", 1, 1);
732    x_fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB_write_04.db");
733
734    (void)pclFileReadData(handleArray[0], buffer, READ_SIZE);
735    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_01.db",
736          strlen("/user/1/seat/1/media/mediaDB_write_01.db"))
737          == 0, "Buffer not correctly read => mediaDB_write_01.db");
738
739    (void)pclFileReadData(handleArray[1], buffer, READ_SIZE);
740    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_02.db",
741          strlen("/user/1/seat/1/media/mediaDB_write_02.db"))
742          == 0, "Buffer not correctly read => mediaDB_write_02.db");
743
744    (void)pclFileReadData(handleArray[2], buffer, READ_SIZE);
745    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_03.db",
746          strlen("/user/1/seat/1/media/mediaDB_write_03.db"))
747          == 0, "Buffer not correctly read => mediaDB_write_03.db");
748
749    (void)pclFileReadData(handleArray[3], buffer, READ_SIZE);
750    x_fail_unless(strncmp((char*)buffer, "/user/1/seat/1/media/mediaDB_write_04.db",
751          strlen("/user/1/seat/1/media/mediaDB_write_04.db"))
752          == 0, "Buffer not correctly read => mediaDB_write_04.db");
753
754    ret = pclKeyHandleClose(handleArray[0]);
755    x_fail_unless(ret != -1, "Failed to close handle idx \"0\"!!");
756
757    ret = pclKeyHandleClose(handleArray[1]);
758    x_fail_unless(ret != -1, "Failed to close handle idx \"1\"!!");
759
760    ret = pclKeyHandleClose(handleArray[2]);
761    x_fail_unless(ret != -1, "Failed to close handle idx \"2\"!!");
762
763    ret = pclKeyHandleClose(handleArray[3]);
764    x_fail_unless(ret != -1, "Failed to close handle idx \"3\"!!");
765
766    // test key handles
767    handle2 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
768    x_fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
769
770    ret = pclKeyHandleClose(handle2);
771    x_fail_unless(ret != -1, "Failed to close handle!!");
772
773    ret = pclKeyHandleClose(1024);
774    x_fail_unless(ret == EPERS_MAXHANDLE, "Max handle!!");
775 #endif
776    pclDeinitLibrary();
777 }
778 END_TEST
779
780
781
782 /*
783  * Extended key handle test.
784  * Test have been created after a bug in the key handle function occured.
785  */
786 START_TEST(test_DataHandleOpen)
787 {
788    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
789    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
790    X_TEST_REPORT_REFERENCE("NONE");
791    X_TEST_REPORT_DESCRIPTION("Test of data handle open");
792    X_TEST_REPORT_TYPE(GOOD);
793
794    int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0;
795    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
796
797    ret = pclInitLibrary(gTheAppId, shutdownReg);
798    x_fail_unless(ret <= 1, "Failed to init PCL");
799 #if 1
800    // open handles ----------------------------------------------------
801    hd1 = pclKeyHandleOpen(0xFF, "posHandle/last_position1", 0, 0);
802    x_fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1");
803
804    hd2 = pclKeyHandleOpen(0xFF, "posHandle/last_position2", 0, 0);
805    x_fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2");
806
807    hd3 = pclKeyHandleOpen(0xFF, "posHandle/last_position3", 0, 0);
808    x_fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3");
809
810    // close handles ---------------------------------------------------
811    ret = pclKeyHandleClose(hd1);
812    x_fail_unless(ret != -1, "Failed to close handle!!");
813
814    ret = pclKeyHandleClose(hd2);
815    x_fail_unless(ret != -1, "Failed to close handle!!");
816
817    ret = pclKeyHandleClose(hd3);
818    x_fail_unless(ret != -1, "Failed to close handle!!");
819
820    // open handles ----------------------------------------------------
821    hd4 = pclKeyHandleOpen(0xFF, "posHandle/last_position4", 0, 0);
822    x_fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4");
823
824    hd5 = pclKeyHandleOpen(0xFF, "posHandle/last_position5", 0, 0);
825    x_fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5");
826
827    hd6 = pclKeyHandleOpen(0xFF, "posHandle/last_position6", 0, 0);
828    x_fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6");
829
830    hd7 = pclKeyHandleOpen(0xFF, "posHandle/last_position7", 0, 0);
831    x_fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7");
832
833    hd8 = pclKeyHandleOpen(0xFF, "posHandle/last_position8", 0, 0);
834    x_fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8");
835
836    hd9 = pclKeyHandleOpen(0xFF, "posHandle/last_position9", 0, 0);
837    x_fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9");
838
839    // close handles ---------------------------------------------------
840    ret = pclKeyHandleClose(hd4);
841    x_fail_unless(ret != -1, "Failed to close handle!!");
842
843    ret = pclKeyHandleClose(hd5);
844    x_fail_unless(ret != -1, "Failed to close handle!!");
845
846    ret = pclKeyHandleClose(hd6);
847    x_fail_unless(ret != -1, "Failed to close handle!!");
848
849    ret = pclKeyHandleClose(hd7);
850    x_fail_unless(ret != -1, "Failed to close handle!!");
851
852    ret = pclKeyHandleClose(hd8);
853    x_fail_unless(ret != -1, "Failed to close handle!!");
854
855    ret = pclKeyHandleClose(hd9);
856    x_fail_unless(ret != -1, "Failed to close handle!!");
857 #endif
858    pclDeinitLibrary();
859 }
860 END_TEST
861
862
863
864 START_TEST(test_Plugin)
865 {
866    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
867    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
868    X_TEST_REPORT_REFERENCE("NONE");
869    X_TEST_REPORT_DESCRIPTION("Test of plugins");
870    X_TEST_REPORT_TYPE(GOOD);
871
872         int ret = 0;
873         unsigned char buffer[READ_SIZE]  = {0};
874
875         unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
876
877    ret = pclInitLibrary(gTheAppId, shutdownReg);
878    x_fail_unless(ret <= 1, "Failed to init PCL");
879 #if 1
880         ret = pclKeyReadData(0xFF, "language/country_code",           0, 0, buffer, READ_SIZE);
881         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
882    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: secure!",
883                strlen((char*)buffer)) == 0, "Buffer SECURE not correctly read");
884
885
886         ret = pclKeyReadData(0xFF, "language/country_code_early",     0, 0, buffer, READ_SIZE);
887         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
888         //printf("B U F F E R - early: %s\n", buffer);
889    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: early!",
890                strlen((char*)buffer)) == 0, "Buffer EARLY not correctly read");
891
892         ret = pclKeyReadData(0xFF, "language/country_code_emergency", 0, 0, buffer, READ_SIZE);
893         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
894         //printf("B U F F E R - emergency: %s\n", buffer);
895    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: emergency!",
896                strlen((char*)buffer)) == 0, "Buffer EMERGENCY not correctly read");
897
898         ret = pclKeyReadData(0xFF, "language/info",                   0, 0, buffer, READ_SIZE);
899         x_fail_unless(ret != EPERS_NOT_INITIALIZED);
900         //printf("B U F F E R - hwinfo: %s\n", buffer);
901    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: hwinfo!",
902                strlen((char*)buffer)) == 0, "Buffer HWINFO not correctly read");
903
904    ret = pclKeyReadData(0xFF, "language/country_code_custom3",   0, 0, buffer, READ_SIZE);
905    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
906    //printf("B U F F E R - hwinfo: %s\n", buffer);
907    x_fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: custom3!",
908                strlen((char*)buffer)) == 0, "Buffer CUSTOM 3 not correctly read");
909 #endif
910         pclDeinitLibrary();
911 }
912 END_TEST
913
914
915
916
917
918 START_TEST(test_ReadDefault)
919 {
920    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
921    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
922    X_TEST_REPORT_REFERENCE("NONE");
923    X_TEST_REPORT_DESCRIPTION("Test of read default");
924    X_TEST_REPORT_TYPE(GOOD);
925
926    int ret = 0;
927    unsigned char buffer[READ_SIZE]  = {0};
928
929    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
930
931    ret = pclInitLibrary(gTheAppId, shutdownReg);
932    x_fail_unless(ret <= 1, "Failed to init PCL");
933 #if 1
934    ret = pclKeyReadData(0xFF, "statusHandle/default01", 3, 2, buffer, READ_SIZE);
935    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
936    //printf("B U F F E R: %s\n", buffer);
937    x_fail_unless(strncmp((char*)buffer,"DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
938
939    ret = pclKeyReadData(0xFF, "statusHandle/default02", 3, 2, buffer, READ_SIZE);
940    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
941    //printf("B U F F E R: %s\n", buffer);
942    x_fail_unless(strncmp((char*)buffer,"DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
943 #endif
944    pclDeinitLibrary();
945 }
946 END_TEST
947
948
949
950 START_TEST(test_ReadConfDefault)
951 {
952    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
953    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
954    X_TEST_REPORT_REFERENCE("NONE");
955    X_TEST_REPORT_DESCRIPTION("Test of configurable default data");
956    X_TEST_REPORT_TYPE(GOOD);
957
958    int ret = 0;
959    unsigned char buffer[READ_SIZE]  = {0};
960
961    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
962
963    ret = pclInitLibrary(gTheAppId, shutdownReg);
964    x_fail_unless(ret <= 1, "Failed to init PCL");
965 #if 1
966    ret = pclKeyReadData(0xFF, "statusHandle/confdefault01",     3, 2, buffer, READ_SIZE);
967    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
968    x_fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
969
970    ret = pclKeyReadData(0xFF, "statusHandle/confdefault02",     3, 2, buffer, READ_SIZE);
971    x_fail_unless(ret != EPERS_NOT_INITIALIZED);
972    x_fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
973 #endif
974    pclDeinitLibrary();
975 }
976 END_TEST
977
978
979
980 START_TEST(test_GetPath)
981 {
982    X_TEST_REPORT_TEST_NAME("persistence_client_library_test");
983    X_TEST_REPORT_COMP_NAME("libpersistence_client_library");
984    X_TEST_REPORT_REFERENCE("NONE");
985    X_TEST_REPORT_DESCRIPTION("Test of get path");
986    X_TEST_REPORT_TYPE(GOOD);
987
988    int ret = 0;
989    char* path = NULL;
990    const char* thePath = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB_create.db";
991    unsigned int pathSize = 0;
992
993    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
994
995    ret = pclInitLibrary(gTheAppId, shutdownReg);
996    x_fail_unless(ret <= 1, "Failed to init PCL");
997 #if 1
998    ret = pclFileCreatePath(0xFF, "media/mediaDB_create.db", 1, 1, &path, &pathSize);
999
1000    x_fail_unless(strncmp((char*)path, thePath, strlen((char*)path)) == 0, "Path not correct");
1001    x_fail_unless(pathSize == strlen((char*)path), "Path size not correct");
1002
1003    pclFileReleasePath(ret);
1004 #endif
1005    pclDeinitLibrary();
1006 }
1007 END_TEST
1008
1009
1010
1011 START_TEST(test_InitDeinit)
1012 {
1013    int i = 0;
1014    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1015
1016    for(i=0; i< 3; i++)
1017    {
1018       // initialize and deinitialize 1. time
1019       (void)pclInitLibrary(gTheAppId, shutdownReg);
1020       pclDeinitLibrary();
1021
1022
1023       // initialize and deinitialize 2. time
1024       (void)pclInitLibrary(gTheAppId, shutdownReg);
1025       pclDeinitLibrary();
1026
1027
1028       // initialize and deinitialize 3. time
1029       (void)pclInitLibrary(gTheAppId, shutdownReg);
1030       pclDeinitLibrary();
1031    }
1032 }
1033 END_TEST
1034
1035
1036
1037 START_TEST(test_NegHandle)
1038 {
1039    int handle = -1, ret = 0;;
1040    int negativeHandle = -17;
1041    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1042
1043    unsigned char buffer[128] = {0};
1044
1045    (void)pclInitLibrary(gTheAppId, shutdownReg);
1046
1047    handle = pclKeyHandleOpen(0xFF, "posHandle/last_position", 0, 0);
1048    x_fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");
1049
1050
1051
1052    ret = pclKeyHandleReadData(negativeHandle, buffer, READ_SIZE);
1053    x_fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleReadData => negative handle not detected");
1054
1055    ret = pclKeyHandleClose(negativeHandle);
1056    x_fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleClose => negative handle not detected");
1057
1058    ret = pclKeyHandleGetSize(negativeHandle);
1059    x_fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleGetSize => negative handle not detected");
1060
1061    ret = pclKeyHandleReadData(negativeHandle, buffer, 128);
1062    x_fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleReadData => negative handle not detected");
1063
1064    ret = pclKeyHandleRegisterNotifyOnChange(negativeHandle, &myChangeCallback);
1065    x_fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleRegisterNotifyOnChange => negative handle not detected");
1066
1067    ret = pclKeyHandleWriteData(negativeHandle, (unsigned char*)"Whatever", strlen("Whatever"));
1068    x_fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleWriteData => negative handle not detected");
1069
1070
1071    // close handle
1072    ret = pclKeyHandleClose(handle);
1073
1074    pclDeinitLibrary();
1075 }
1076 END_TEST
1077
1078
1079
1080
1081
1082 START_TEST(test_FileOpenCreate)
1083 {
1084    int handle = -1, ret = 0;
1085    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
1086    char buffer[128] = {0};
1087    char* writeBuffer = "test_FileOpenCreate: write some data to the file!";
1088
1089    (void)pclInitLibrary(gTheAppId, shutdownReg);
1090
1091    // remove file
1092    remove("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db");
1093
1094    handle = pclFileOpen(0xFF, "media/mediaDBWrite.db", 1, 1);
1095    x_fail_unless(handle != -1, "Could not open file ==> /media/mediaDBWrite.db");
1096
1097
1098    ret = pclFileWriteData(handle, writeBuffer, strlen(writeBuffer));
1099    x_fail_unless(ret == strlen(writeBuffer), "pclKeyHandleWriteData => error writing data");
1100
1101    /*
1102    ret = pclFileSeek(handle, 0, SEEK_SET);
1103    x_fail_unless(ret <= 0, "pclFileSeek => failed to position fd");
1104
1105
1106    ret = pclFileReadData(handle, buffer, 128);
1107    x_fail_unless(ret == strlen(writeBuffer), "pclKeyHandleReadData => error read data");
1108    x_fail_unless(strncmp(buffer, writeBuffer, strlen(writeBuffer)) == 0, "pclKeyHandleReadData => Buffer not correctly read");
1109
1110    ret = pclFileClose(handle);
1111    x_fail_unless(ret <= 0, "pclKeyHandleClose => failed to close");
1112         */
1113
1114
1115    // remove file
1116    remove("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db");
1117
1118    pclDeinitLibrary();
1119 }
1120 END_TEST
1121
1122
1123
1124 static Suite * persistencyClientLib_suite()
1125 {
1126    Suite * s  = suite_create("Persistency client library");
1127
1128    TCase * tc_persGetData = tcase_create("GetData");
1129    tcase_add_test(tc_persGetData, test_GetData);
1130
1131    TCase * tc_persSetData = tcase_create("SetData");
1132    tcase_add_test(tc_persSetData, test_SetData);
1133
1134    TCase * tc_persSetDataNoPRCT = tcase_create("SetDataNoPRCT");
1135    tcase_add_test(tc_persSetDataNoPRCT, test_SetDataNoPRCT);
1136
1137    TCase * tc_persGetDataSize = tcase_create("GetDataSize");
1138    tcase_add_test(tc_persGetDataSize, test_GetDataSize);
1139
1140    TCase * tc_persDeleteData = tcase_create("DeleteData");
1141    tcase_add_test(tc_persDeleteData, test_DeleteData);
1142
1143    TCase * tc_persGetDataHandle = tcase_create("GetDataHandle");
1144    tcase_add_test(tc_persGetDataHandle, test_GetDataHandle);
1145
1146    TCase * tc_persDataHandle = tcase_create("DataHandle");
1147    tcase_add_test(tc_persDataHandle, test_DataHandle);
1148
1149    TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen");
1150    tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen);
1151
1152    TCase * tc_persDataFile = tcase_create("DataFile");
1153    tcase_add_test(tc_persDataFile, test_DataFile);
1154
1155    TCase * tc_persDataFileRecovery = tcase_create("DataFileRecovery");
1156    tcase_add_test(tc_persDataFileRecovery, test_DataFileRecovery);
1157
1158    TCase * tc_Plugin = tcase_create("Plugin");
1159    tcase_add_test(tc_Plugin, test_Plugin);
1160
1161    TCase * tc_ReadDefault = tcase_create("ReadDefault");
1162    tcase_add_test(tc_ReadDefault, test_ReadDefault);
1163
1164    TCase * tc_ReadConfDefault = tcase_create("ReadConfDefault");
1165    tcase_add_test(tc_ReadConfDefault, test_ReadConfDefault);
1166
1167    TCase * tc_GetPath = tcase_create("GetPath");
1168    tcase_add_test(tc_GetPath, test_GetPath);
1169
1170    TCase * tc_InitDeinit = tcase_create("InitDeinit");
1171    tcase_add_test(tc_InitDeinit, test_InitDeinit);
1172
1173    TCase * tc_NegHandle = tcase_create("NegHandle");
1174    tcase_add_test(tc_NegHandle, test_NegHandle);
1175
1176    TCase * tc_FileOpenCreate = tcase_create("FileOpenCreate");
1177    tcase_add_test(tc_FileOpenCreate, test_FileOpenCreate);
1178
1179    suite_add_tcase(s, tc_persSetData);
1180    suite_add_tcase(s, tc_persGetData);
1181    suite_add_tcase(s, tc_persSetDataNoPRCT);
1182    suite_add_tcase(s, tc_persGetDataSize);
1183    suite_add_tcase(s, tc_persDeleteData);
1184    suite_add_tcase(s, tc_persGetDataHandle);
1185    suite_add_tcase(s, tc_persDataHandle);
1186    suite_add_tcase(s, tc_persDataHandleOpen);
1187    suite_add_tcase(s, tc_persDataFile);
1188    suite_add_tcase(s, tc_persDataFileRecovery);
1189    suite_add_tcase(s, tc_ReadDefault);
1190    suite_add_tcase(s, tc_ReadConfDefault);
1191    suite_add_tcase(s, tc_GetPath);
1192    suite_add_tcase(s, tc_InitDeinit);
1193    suite_add_tcase(s, tc_NegHandle);
1194    suite_add_tcase(s, tc_FileOpenCreate);
1195
1196    //suite_add_tcase(s, tc_Plugin); // activate only if the plugins are available
1197
1198    return s;
1199 }
1200
1201
1202 int main(int argc, char *argv[])
1203 {
1204    int nr_failed = 0,
1205           nr_run = 0,
1206                i = 0;
1207    //int fail = 0;
1208
1209    TestResult** tResult;
1210
1211    // assign application name
1212    strncpy(gTheAppId, "lt-persistence_client_library_test", MaxAppNameLen);
1213    gTheAppId[MaxAppNameLen-1] = '\0';
1214
1215    /// debug log and trace (DLT) setup
1216    DLT_REGISTER_APP("PCLt","tests the persistence client library");
1217
1218 #if 1
1219    Suite * s = persistencyClientLib_suite();
1220    SRunner * sr = srunner_create(s);
1221    srunner_set_xml(sr, "/tmp/persistenceClientLibraryTest.xml");
1222    srunner_set_log(sr, "/tmp/persistenceClientLibraryTest.log");
1223    srunner_run_all(sr, /*CK_NORMAL*/ CK_VERBOSE);
1224
1225    nr_failed = srunner_ntests_failed(sr);
1226    nr_run = srunner_ntests_run(sr);
1227
1228    tResult = srunner_results(sr);
1229    for(i = 0; i< nr_run; i++)
1230    {
1231       (void)tr_rtype(tResult[i]);  // get status of each test
1232       //fail = tr_rtype(tResult[i]);  // get status of each test
1233       //printf("[%d] Fail: %d \n", i, fail);
1234    }
1235
1236    srunner_free(sr);
1237 #endif
1238
1239    // unregister debug log and trace
1240    DLT_UNREGISTER_APP();
1241
1242    dlt_free();
1243
1244    return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;
1245
1246 }
1247