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