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