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