836f31390de83ea51c971aa8653096be7a4da64a
[profile/ivi/persistence-client-library.git] / test / persistence_client_library_test.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
4  * Company         XS Embedded GmbH
5  *****************************************************************************/
6 /******************************************************************************
7  * This Source Code Form is subject to the terms of the
8  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
9  * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
11  /**
12  * @file           persistence_client_library_test.c
13  * @ingroup        Persistence client library test
14  * @author         Ingo Huerner
15  * @brief          Test of persistence client library
16  * @see            
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>     /* exit */
24 #include <check.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <sys/mman.h>
28
29 #include <dlt/dlt.h>
30 #include <dlt/dlt_common.h>
31
32 #include "../include/persistence_client_library_file.h"
33 #include "../include/persistence_client_library_key.h"
34 #include "../include/persistence_client_library.h"
35
36
37 // protected header, should be used only be persistence components
38 // included here for testing purpose
39 #include "../include_protected/persistence_client_library_db_access.h"
40
41
42 #define BUF_SIZE     64
43 #define NUM_OF_FILES 3
44 #define READ_SIZE    1024
45
46 /// application id
47 char gTheAppId[MaxAppNameLen] = {0};
48
49 // definition of weekday
50 char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
51
52
53
54 /**
55  * Test the key value interface using different logicalDB id's, users and seats.
56  * Each resource below has an entry in the resource configuration table where the
57  * storage location (cached or write through) and type (e.g. custom) has been configured.
58  */
59 START_TEST (test_GetData)
60 {
61    int ret = 0;
62    unsigned int shutdownReg = (PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL);
63
64    unsigned char buffer[READ_SIZE] = {0};
65
66    ret = pclInitLibrary(gTheAppId, shutdownReg);
67    fail_unless(ret <= 1, "Failed to init PCL");
68
69    /**
70     * Logical DB ID: 0xFF with user 0 and seat 0
71     *       ==> local value accessible by all users (user 0, seat 0)
72     */
73    ret = pclKeyReadData(0xFF, "pos/last_position",         0, 0, buffer, READ_SIZE);
74    fail_unless(strncmp((char*)buffer, "CACHE_ +48° 10' 38.95\", +8° 44' 39.06\"",
75                strlen((char*)buffer)) == 0, "Buffer not correctly read");
76    fail_unless(ret = strlen("CACHE_ +48° 10' 38.95\", +8° 44' 39.06\""));
77
78    memset(buffer, 0, READ_SIZE);
79
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, "language/country_code",         0, 0, buffer, READ_SIZE);
85    fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data: secure!",
86                strlen((char*)buffer)) == 0, "Buffer not correctly read");
87    fail_unless(ret = strlen("Custom plugin -> plugin_get_data_handle"));
88
89    memset(buffer, 0, READ_SIZE);
90
91
92    /**
93     * Logical DB ID: 0 with user 3 and seat 0
94     *       ==> public shared user value (user 3, seat 0)
95     */
96    ret = pclKeyReadData(0,    "language/current_language", 3, 0, buffer, READ_SIZE);
97    fail_unless(strncmp((char*)buffer, "CACHE_ Kisuaheli", strlen((char*)buffer)) == 0, "Buffer not correctly read");
98
99    memset(buffer, 0, READ_SIZE);
100
101    /**
102     * Logical DB ID: 0xFF with user 3 and seat 2
103     *       ==> local USER value (user 3, seat 2)
104     */
105    ret = pclKeyReadData(0xFF, "status/open_document",      3, 2, buffer, READ_SIZE);
106    fail_unless(strncmp((char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0, "Buffer not correctly read");
107
108    memset(buffer, 0, READ_SIZE);
109
110    /**
111     * Logical DB ID: 0x20 with user 4 and seat 0
112     *       ==> shared user value accessible by a group (user 4 and seat 0)
113     */
114    ret = pclKeyReadData(0x20, "address/home_address",      4, 0, buffer, READ_SIZE);
115    fail_unless(strncmp((char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0, "Buffer not correctly read");
116
117    memset(buffer, 0, READ_SIZE);
118
119    /**
120     * Logical DB ID: 0xFF with user 0 and seat 0
121     *       ==> local value accessible by ALL USERS (user 0, seat 0)
122     */
123    ret = pclKeyReadData(0xFF, "pos/last_satellites",       0, 0, buffer, READ_SIZE);
124    fail_unless(strncmp((char*)buffer, "WT_ 17", strlen((char*)buffer)) == 0, "Buffer not correctly read");
125
126    memset(buffer, 0, READ_SIZE);
127
128    /**
129     * Logical DB ID: 0x84 with user 4 and seat 0
130     *       ==> shared user value accessible by A GROUP (user 4 and seat 0)
131     */
132    ret = pclKeyReadData(0x84, "links/last_link",           2, 0, buffer, READ_SIZE);
133    fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/brooklyn", strlen((char*)buffer)) == 0, "Buffer not correctly read");
134
135    memset(buffer, 0, READ_SIZE);
136
137    /**
138     * Logical DB ID: 0x84 with user 2 and seat 1
139     *       ==> local merge value
140     */
141    ret = pclKeyReadData(0x84, "links/last_link",           2, 1, buffer, READ_SIZE);
142    fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read");
143
144    pclDeinitLibrary();
145 }
146 END_TEST
147
148
149
150 /**
151  * Test the key value  h a n d l e  interface using different logicalDB id's, users and seats
152  * Each resource below has an entry in the resource configuration table where
153  * the storage location (cached or write through) and type (e.g. custom) has bee configured.
154  */
155 START_TEST (test_GetDataHandle)
156 {
157    int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0;
158    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
159
160    unsigned char buffer[READ_SIZE] = {0};
161    struct tm *locTime;
162
163    char sysTimeBuffer[128];
164
165    ret = pclInitLibrary(gTheAppId, shutdownReg);
166    fail_unless(ret <= 1, "Failed to init PCL");
167
168    time_t t = time(0);
169
170    locTime = localtime(&t);
171
172    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),
173                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
174
175
176    // open handle ---------------------------------------------------
177    /**
178     * Logical DB ID: 0xFF with user 0 and seat 0
179     *       ==> local value accessible by ALL USERS (user 0, seat 0)
180     */
181    handle = pclKeyHandleOpen(0xFF, "posHandle/last_position", 0, 0);
182    fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");
183
184    ret = pclKeyHandleReadData(handle, buffer, READ_SIZE);
185    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");
186
187    size = pclKeyHandleGetSize(handle);
188    fail_unless(size = strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
189    // ---------------------------------------------------------------------------------------------
190
191
192    // open handle ---------------------------------------------------
193    /**
194     * Logical DB ID: 0xFF with user 3 and seat 2
195     *       ==> local USER value (user 3, seat 2)
196     */
197    handle2 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
198    fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
199
200    size = pclKeyHandleWriteData(handle2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
201    fail_unless(size = strlen(sysTimeBuffer));
202    // close
203    ret = pclKeyHandleClose(handle2);
204    // ---------------------------------------------------------------------------------------------
205
206
207    // open handle ---------------------------------------------------
208    /**
209     * Logical DB ID: 0xFF with user 0 and seat 0
210     *       ==> local value accessible by ALL USERS (user 0, seat 0)
211     */
212 #if 0 // plugin test case
213    memset(buffer, 0, READ_SIZE);
214    handle4 = pclKeyHandleOpen(0xFF, "language/country_code", 0, 0);
215    printf("H A N D L E: %d\n", handle4);
216    fail_unless(handle4 >= 0, "Failed to open handle /language/country_code");
217
218    ret = pclKeyHandleReadData(handle4, buffer, READ_SIZE);
219    fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle: secure!", -1) == 0, "Buffer not correctly read => 2");
220
221    size = pclKeyHandleGetSize(handle4);
222    fail_unless(size = strlen("Custom plugin -> plugin_get_data_handle"));
223
224    ret = pclKeyHandleWriteData(handle4, (unsigned char*)"Only dummy implementation behind custom library", READ_SIZE);
225 #endif
226    // ---------------------------------------------------------------------------------------------
227
228
229    // open handle ---------------------------------------------------
230    /**
231     * Logical DB ID: 0xFF with user 3 and seat 2
232     *       ==> local USER value (user 3, seat 2)
233     */
234    handle3 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
235    fail_unless(handle3 >= 0, "Failed to open handle /statusHandle/open_document");
236
237    ret = pclKeyHandleReadData(handle3, buffer, READ_SIZE);
238    fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read => 3");
239
240    size = pclKeyHandleGetSize(handle3);
241    fail_unless(size = strlen(sysTimeBuffer));
242    // ---------------------------------------------------------------------------------------------
243
244
245    // close handle
246    ret = pclKeyHandleClose(handle);
247    ret = pclKeyHandleClose(handle3);
248    ret = pclKeyHandleClose(handle4);
249
250    pclDeinitLibrary();
251 }
252 END_TEST
253
254
255
256 /*
257  * Write data to a key using the key interface.
258  * First write data to different keys and after
259  * read the data for verification.
260  */
261 START_TEST(test_SetData)
262 {
263    int ret = 0;
264    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
265    unsigned char buffer[READ_SIZE]  = {0};
266    char write1[READ_SIZE] = {0};
267    char write2[READ_SIZE] = {0};
268    char sysTimeBuffer[256];
269
270    struct tm *locTime;
271
272    ret = pclInitLibrary(gTheAppId, shutdownReg);
273    fail_unless(ret <= 1, "Failed to init PCL");
274
275    time_t t = time(0);
276
277    locTime = localtime(&t);
278
279    // write data
280    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),
281                                                                  locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
282
283    /**
284     * Logical DB ID: 0xFF with user 1 and seat 2
285     *       ==> local USER value (user 1, seat 2)
286     * Resource ID: 69
287     */
288    ret = pclKeyWriteData(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
289    fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
290
291    snprintf(write1, 128, "%s %s", "/70",  sysTimeBuffer);
292    /**
293     * Logical DB ID: 0xFF with user 1 and seat 2
294     *       ==> local USER value (user 1, seat 2)
295     * Resource ID: 70
296     */
297    ret = pclKeyWriteData(0xFF, "70", 1, 2, (unsigned char*)write1, strlen(write1));
298    fail_unless(ret == strlen(write1), "Wrong write size");
299
300    snprintf(write2, 128, "%s %s", "/key_70",  sysTimeBuffer);
301    /**
302     * Logical DB ID: 0xFF with user 1 and seat 2
303     *       ==> local USER value (user 1, seat 2)
304     * Resource ID: key_70
305     */
306    ret = pclKeyWriteData(0xFF, "key_70", 1, 2, (unsigned char*)write2, strlen(write2));
307    fail_unless(ret == strlen(write2), "Wrong write size");
308
309
310    /*******************************************************************************************************************************************/
311    /* used for changed notification testing */
312    /*******************************************************************************************************************************************/
313    /**
314     * Logical DB ID: 0x84 with user 2 and seat 1
315     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
316     *
317     *       ==> used for shared testing
318     */
319    //printf("Write data to trigger change notification\n");
320    ret = pclKeyWriteData(0x84, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
321
322    /**
323     * Logical DB ID: 0x84 with user 2 and seat 1
324     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
325     *
326     *       ==> used for shared testing
327     */
328    //printf("Write data to trigger change notification\n");
329    ret = pclKeyWriteData(0x84, "links/last_link3",  3, 2, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
330
331    /**
332     * Logical DB ID: 0x84 with user 2 and seat 1
333     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
334     *
335     *       ==> used for shared testing
336     */
337    //printf("Write data to trigger change notification\n");
338    ret = pclKeyWriteData(0x84, "links/last_link4",  4, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
339    /*******************************************************************************************************************************************/
340    /*******************************************************************************************************************************************/
341
342
343    /*
344     * now read the data written in the previous steps to the keys
345     * and verify data has been written correctly.
346     */
347    memset(buffer, 0, READ_SIZE);
348
349    ret = pclKeyReadData(0xFF, "69", 1, 2, buffer, READ_SIZE);
350    fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
351    fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
352
353    memset(buffer, 0, READ_SIZE);
354
355    ret = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
356    fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
357    fail_unless(ret == strlen(write1), "Wrong read size");
358
359    memset(buffer, 0, READ_SIZE);
360
361    ret = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
362    fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
363    fail_unless(ret == strlen(write2), "Wrong read size");
364
365    pclDeinitLibrary();
366 }
367 END_TEST
368
369
370
371 /**
372  * Write data to a key using the key interface.
373  * The key is not in the persistence resource table.
374  * The key sill then be stored to the location local and cached.
375  */
376 START_TEST(test_SetDataNoPRCT)
377 {
378    int ret = 0;
379    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
380    unsigned char buffer[READ_SIZE] = {0};
381    struct tm *locTime;
382
383    ret = pclInitLibrary(gTheAppId, shutdownReg);
384    fail_unless(ret <= 1, "Failed to init PCL");
385
386    time_t t = time(0);
387
388    char sysTimeBuffer[128];
389
390    locTime = localtime(&t);
391
392    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),
393                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
394
395    /**
396     * Logical DB ID: 0xFF with user 1 and seat 2
397     *       ==> local USER value (user 1, seat 2)
398     */
399    ret = pclKeyWriteData(0xFF, "NoPRCT", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
400    fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
401    //printf("Write Buffer : %s\n", sysTimeBuffer);
402
403    // read data again and and verify datat has been written correctly
404    memset(buffer, 0, READ_SIZE);
405
406    ret = pclKeyReadData(0xFF, "NoPRCT", 1, 2, buffer, READ_SIZE);
407    fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
408    fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
409    //printf("read buffer  : %s\n", buffer);
410
411    pclDeinitLibrary();
412 }
413 END_TEST
414
415
416
417 /*
418  * Test the key interface.
419  * Read the size of a key.
420  */
421 START_TEST(test_GetDataSize)
422 {
423    int size = 0, ret = 0;
424
425    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
426
427    ret = pclInitLibrary(gTheAppId, shutdownReg);
428    fail_unless(ret <= 1, "Failed to init PCL");
429
430    /**
431     * Logical DB ID: 0xFF with user 3 and seat 2
432     *       ==> local USER value (user 3, seat 2)
433     */
434    size = pclKeyGetSize(0xFF, "status/open_document", 3, 2);
435    fail_unless(size == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Invalid size");
436
437
438    /**
439     * Logical DB ID: 0x84 with user 2 and seat 1
440     *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
441     */
442    size = pclKeyGetSize(0x84, "links/last_link", 2, 1);
443    fail_unless(size == strlen("CACHE_ /last_exit/queens"), "Invalid size");
444
445    pclDeinitLibrary();
446 }
447 END_TEST
448
449
450 /*
451  * Delete a key using the key value interface.
452  * First read a from a key, the delte the key
453  * and then try to read again. The Last read must fail.
454  */
455 START_TEST(test_DeleteData)
456 {
457    int rval = 0;
458    unsigned char buffer[READ_SIZE];
459    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
460
461    rval = pclInitLibrary(gTheAppId, shutdownReg);
462    fail_unless(rval <= 1, "Failed to init PCL");
463
464    // read data from key
465    rval = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
466    fail_unless(rval != EPERS_NOKEY, "Read form key key_70 fails");
467
468    // delete key
469    rval = pclKeyDelete(0xFF, "key_70", 1, 2);
470    fail_unless(rval == 0, "Failed to delete key");
471
472    // after deleting the key, reading from key must fail now!
473    rval = pclKeyReadData(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
474    fail_unless(rval == EPERS_NOKEY, "Read form key key_70 works, but should fail");
475
476
477
478    // read data from key
479    rval = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
480    fail_unless(rval != EPERS_NOKEY, "Read form key 70 fails");
481
482    // delete key
483    rval = pclKeyDelete(0xFF, "70", 1, 2);
484    fail_unless(rval == 0, "Failed to delete key");
485
486    // after deleting the key, reading from key must fail now!
487    rval = pclKeyReadData(0xFF, "70", 1, 2, buffer, READ_SIZE);
488    fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail");
489
490    pclDeinitLibrary();
491 }
492 END_TEST
493
494
495
496 /*
497  * Test the file interface:
498  * - open file
499  * - read / write
500  * - remove file
501  * - map file
502  * - get size
503  */
504 START_TEST(test_DataFile)
505 {
506    int fd = 0, i = 0, idx = 0;
507    int size = 0, ret = 0;
508    int writeSize = 16*1024;
509    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
510
511    unsigned char buffer[READ_SIZE] = {0};
512    const char* refBuffer = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media";
513    char* writeBuffer;
514    char* fileMap = NULL;
515
516    ret = pclInitLibrary(gTheAppId, shutdownReg);
517    fail_unless(ret <= 1, "Failed to init PCL");
518
519    writeBuffer = malloc(writeSize);
520
521
522    // fill buffer a sequence
523    for(i = 0; i<(writeSize/8); i++)
524    {
525       writeBuffer[idx++] = 'A';
526       writeBuffer[idx++] = 'B';
527       writeBuffer[idx++] = 'C';
528       writeBuffer[idx++] = ' ';
529       writeBuffer[idx++] = 'D';
530       writeBuffer[idx++] = 'E';
531       writeBuffer[idx++] = 'F';
532       writeBuffer[idx++] = ' ';
533    }
534    // create file
535    fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",
536              O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
537    close(fd);
538
539    // open ------------------------------------------------------------
540    fd = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
541    fail_unless(fd != -1, "Could not open file ==> /media/mediaDB.db");
542
543    size = pclFileGetSize(fd);
544    fail_unless(size == 68, "Wrong file size");
545
546    size = pclFileReadData(fd, buffer, READ_SIZE);
547    fail_unless(strncmp((char*)buffer, refBuffer, strlen(refBuffer)) == 0, "Buffer not correctly read => media/mediaDB.db");
548    fail_unless(size == (strlen(refBuffer)+1), "Wrong size returned");      // strlen + 1 ==> inlcude cr/lf
549
550
551    ret = pclFileClose(fd);
552    fail_unless(ret == 0, "Failed to close file");
553
554    // open ------------------------------------------------------------
555    fd = pclFileOpen(0xFF, "media/mediaDBWrite.db", 1, 1);
556    fail_unless(fd != -1, "Could not open file ==> /media/mediaDBWrite.db");
557
558    size = pclFileWriteData(fd, writeBuffer, strlen(writeBuffer));
559    fail_unless(size == strlen(writeBuffer), "Failed to write data");
560
561    ret = pclFileClose(fd);
562    fail_unless(ret == 0, "Failed to close file");
563
564
565    // remove ----------------------------------------------------------
566    ret = pclFileRemove(0xFF, "media/mediaDBWrite.db", 1, 1);
567    fail_unless(ret == 0, "File can't be removed ==> /media/mediaDBWrite.db");
568
569    fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",O_RDWR);
570    fail_unless(fd == -1, "Failed to remove file, file still exists");
571    close(fd);
572
573
574    // map file --------------------------------------------------------
575    fd = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
576
577    size = pclFileGetSize(fd);
578    pclFileMapData(fileMap, size, 0, fd);
579    fail_unless(fileMap != MAP_FAILED, "Failed to map file");
580
581    ret = pclFileUnmapData(fileMap, size);
582    fail_unless(ret != -1, "Failed to unmap file");
583
584    // negative test
585    size = pclFileGetSize(1024);
586    fail_unless(ret == 0, "Got size, but should not");
587
588    ret = pclFileClose(fd);
589    fail_unless(ret == 0, "Failed to close file");
590
591    free(writeBuffer);
592
593    pclDeinitLibrary();
594 }
595 END_TEST
596
597
598
599
600
601 START_TEST(test_DataFileRecovery)
602 {
603    int fd_RW = 0, fd_RO = 0;
604    int ret = 0;
605    char* wBuffer = "This is a buffer to write";
606    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
607
608    ret = pclInitLibrary(gTheAppId, shutdownReg);
609    fail_unless(ret <= 1, "Failed to init PCL");
610
611    // test backup creation --------------------------------------------
612    fd_RO = pclFileOpen(0xFF, "media/mediaDB_ReadOnly.db", 1, 1);
613    fail_unless(fd_RO != -1, "Could not open file ==> /media/mediaDB_ReadOnly.db");
614
615    fd_RW = pclFileOpen(0xFF, "media/mediaDB_ReadWrite.db", 1, 1);
616    fail_unless(fd_RW != -1, "Could not open file ==> /media/mediaDB_ReadWrite.db");
617    pclFileWriteData(fd_RW, wBuffer, strlen(wBuffer));
618
619    ret = pclFileClose(fd_RW);
620    if(ret == -1)
621
622    ret = pclFileClose(fd_RO);
623    if(ret == -1)
624
625
626    pclDeinitLibrary();
627 }
628 END_TEST
629
630 /*
631  * The the handle function of the key and file interface.
632  */
633 START_TEST(test_DataHandle)
634 {
635    int handle1 = 0, handle2 = 0;
636    int ret = 0;
637    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
638
639    ret = pclInitLibrary(gTheAppId, shutdownReg);
640    fail_unless(ret <= 1, "Failed to init PCL");
641
642    // test file handles
643    handle1 = pclFileOpen(0xFF, "media/mediaDB.db", 1, 1);
644    fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db");
645
646    ret = pclFileClose(handle1);
647    fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db");
648
649    ret = pclFileClose(1024);
650    fail_unless(ret == EPERS_MAXHANDLE, "Could close file, but should not!!");
651
652
653    ret = pclFileClose(17);
654    fail_unless(ret == -1, "Could close file, but should not!!");
655
656
657
658    // test key handles
659    handle2 = pclKeyHandleOpen(0xFF, "statusHandle/open_document", 3, 2);
660    fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
661
662    ret = pclKeyHandleClose(handle2);
663    fail_unless(ret != -1, "Failed to close handle!!");
664
665    ret = pclKeyHandleClose(1024);
666    fail_unless(ret == EPERS_MAXHANDLE, "Max handle!!");
667
668    pclDeinitLibrary();
669 }
670 END_TEST
671
672
673
674 /*
675  * Extended key handle test.
676  * Test have been created after a bug in the key handle function occured.
677  */
678 START_TEST(test_DataHandleOpen)
679 {
680    int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0;
681    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
682
683    ret = pclInitLibrary(gTheAppId, shutdownReg);
684    fail_unless(ret <= 1, "Failed to init PCL");
685
686    // open handles ----------------------------------------------------
687    hd1 = pclKeyHandleOpen(0xFF, "posHandle/last_position1", 0, 0);
688    fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1");
689
690    hd2 = pclKeyHandleOpen(0xFF, "posHandle/last_position2", 0, 0);
691    fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2");
692
693    hd3 = pclKeyHandleOpen(0xFF, "posHandle/last_position3", 0, 0);
694    fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3");
695
696    // close handles ---------------------------------------------------
697    ret = pclKeyHandleClose(hd1);
698    fail_unless(ret != -1, "Failed to close handle!!");
699
700    ret = pclKeyHandleClose(hd2);
701    fail_unless(ret != -1, "Failed to close handle!!");
702
703    ret = pclKeyHandleClose(hd3);
704    fail_unless(ret != -1, "Failed to close handle!!");
705
706    // open handles ----------------------------------------------------
707    hd4 = pclKeyHandleOpen(0xFF, "posHandle/last_position4", 0, 0);
708    fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4");
709
710    hd5 = pclKeyHandleOpen(0xFF, "posHandle/last_position5", 0, 0);
711    fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5");
712
713    hd6 = pclKeyHandleOpen(0xFF, "posHandle/last_position6", 0, 0);
714    fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6");
715
716    hd7 = pclKeyHandleOpen(0xFF, "posHandle/last_position7", 0, 0);
717    fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7");
718
719    hd8 = pclKeyHandleOpen(0xFF, "posHandle/last_position8", 0, 0);
720    fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8");
721
722    hd9 = pclKeyHandleOpen(0xFF, "posHandle/last_position9", 0, 0);
723    fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9");
724
725    // close handles ---------------------------------------------------
726    ret = pclKeyHandleClose(hd4);
727    fail_unless(ret != -1, "Failed to close handle!!");
728
729    ret = pclKeyHandleClose(hd5);
730    fail_unless(ret != -1, "Failed to close handle!!");
731
732    ret = pclKeyHandleClose(hd6);
733    fail_unless(ret != -1, "Failed to close handle!!");
734
735    ret = pclKeyHandleClose(hd7);
736    fail_unless(ret != -1, "Failed to close handle!!");
737
738    ret = pclKeyHandleClose(hd8);
739    fail_unless(ret != -1, "Failed to close handle!!");
740
741    ret = pclKeyHandleClose(hd9);
742    fail_unless(ret != -1, "Failed to close handle!!");
743
744    pclDeinitLibrary();
745 }
746 END_TEST
747
748
749
750 /**
751  * Test for  i n t e r n a l  structures.
752  * Test the cursor functions.
753  */
754 START_TEST(test_Cursor)
755 {
756    int handle = -1, rval = 0, size = 0, handle1 = 0;
757    char bufferKeySrc[READ_SIZE]  = {0};
758    char bufferDataSrc[READ_SIZE] = {0};
759    char bufferKeyDst[READ_SIZE]  = {0};
760    char bufferDataDst[READ_SIZE] = {0};
761    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
762
763    rval = pclInitLibrary(gTheAppId, shutdownReg);
764    fail_unless(rval <= 1, "Failed to init PCL");
765
766    // create cursor
767    handle = pers_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/cached.itz");
768    fail_unless(handle != -1, "Failed to create cursor!!");
769
770    // create cursor
771    handle1 = pers_db_cursor_create("/Data/mnt-wt/lt-persistence_client_library_test/wt.itz");
772    fail_unless(handle1 != -1, "Failed to create cursor!!");
773
774    do
775    {
776       memset(bufferKeySrc, 0, READ_SIZE);
777       memset(bufferDataSrc, 0, READ_SIZE);
778       memset(bufferKeyDst, 0, READ_SIZE);
779       memset(bufferDataDst, 0, READ_SIZE);
780
781       // get key
782       rval = pers_db_cursor_get_key(handle, bufferKeySrc, 128);
783       fail_unless(rval != -1, "Cursor failed to get key!!");
784       // get data
785       rval = pers_db_cursor_get_data(handle, bufferDataSrc, 128);
786       fail_unless(rval != -1, "Cursor failed to get data!!");
787       // get size
788       size = pers_db_cursor_get_data_size(handle);
789       fail_unless(size != -1, "Cursor failed to get size!!");
790       //printf("1. Key: %s | Data: %s » Size: %d \n", bufferKeySrc, bufferDataSrc, size);
791
792       // get key
793       rval = pers_db_cursor_get_key(handle1, bufferKeyDst, 128);
794       fail_unless(rval != -1, "Cursor failed to get key!!");
795       // get data
796       rval = pers_db_cursor_get_data(handle1, bufferDataDst, 128);
797       fail_unless(rval != -1, "Cursor failed to get data!!");
798       // get size
799       size = pers_db_cursor_get_data_size(handle1);
800       fail_unless(size != -1, "Cursor failed to get size!!");
801       //printf("  2. Key: %s | Data: %s » Size: %d \n", bufferKeyDst, bufferDataDst, size);
802    }
803    while( (pers_db_cursor_next(handle) == 0) && (pers_db_cursor_next(handle1) == 0) ); // next cursor
804
805    // destory cursor
806    rval = pers_db_cursor_destroy(handle);
807    fail_unless(rval != -1, "Failed to destroy cursor!!");
808
809    rval = pers_db_cursor_destroy(handle1);
810    fail_unless(rval != -1, "Failed to destroy cursor!!");
811
812    pclDeinitLibrary();
813 }
814 END_TEST
815
816
817
818 START_TEST(test_Plugin)
819 {
820         int ret = 0;
821         unsigned char buffer[READ_SIZE]  = {0};
822
823         unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
824
825    ret = pclInitLibrary(gTheAppId, shutdownReg);
826    fail_unless(ret <= 1, "Failed to init PCL");
827
828         ret = pclKeyReadData(0xFF, "language/country_code",           0, 0, buffer, READ_SIZE);
829         fail_unless(ret != EPERS_NOT_INITIALIZED);
830    fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: secure!",
831                strlen((char*)buffer)) == 0, "Buffer SECURE not correctly read");
832
833
834         ret = pclKeyReadData(0xFF, "language/country_code_early",     0, 0, buffer, READ_SIZE);
835         fail_unless(ret != EPERS_NOT_INITIALIZED);
836         //printf("B U F F E R - early: %s\n", buffer);
837    fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: early!",
838                strlen((char*)buffer)) == 0, "Buffer EARLY not correctly read");
839
840         ret = pclKeyReadData(0xFF, "language/country_code_emergency", 0, 0, buffer, READ_SIZE);
841         fail_unless(ret != EPERS_NOT_INITIALIZED);
842         //printf("B U F F E R - emergency: %s\n", buffer);
843    fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: emergency!",
844                strlen((char*)buffer)) == 0, "Buffer EMERGENCY not correctly read");
845
846         ret = pclKeyReadData(0xFF, "language/info",                   0, 0, buffer, READ_SIZE);
847         fail_unless(ret != EPERS_NOT_INITIALIZED);
848         //printf("B U F F E R - hwinfo: %s\n", buffer);
849    fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: hwinfo!",
850                strlen((char*)buffer)) == 0, "Buffer HWINFO not correctly read");
851
852    ret = pclKeyReadData(0xFF, "language/country_code_custom3",   0, 0, buffer, READ_SIZE);
853    fail_unless(ret != EPERS_NOT_INITIALIZED);
854    //printf("B U F F E R - hwinfo: %s\n", buffer);
855    fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: custom3!",
856                strlen((char*)buffer)) == 0, "Buffer CUSTOM 3 not correctly read");
857
858         pclDeinitLibrary();
859 }
860 END_TEST
861
862
863
864
865
866 START_TEST(test_ReadDefault)
867 {
868    int ret = 0;
869    unsigned char buffer[READ_SIZE]  = {0};
870
871    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
872
873    ret = pclInitLibrary(gTheAppId, shutdownReg);
874    fail_unless(ret <= 1, "Failed to init PCL");
875
876    ret = pclKeyReadData(0xFF, "statusHandle/default01", 3, 2, buffer, READ_SIZE);
877    fail_unless(ret != EPERS_NOT_INITIALIZED);
878    //printf("B U F F E R: %s\n", buffer);
879    fail_unless(strncmp((char*)buffer,"DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
880
881    ret = pclKeyReadData(0xFF, "statusHandle/default02", 3, 2, buffer, READ_SIZE);
882    fail_unless(ret != EPERS_NOT_INITIALIZED);
883    //printf("B U F F E R: %s\n", buffer);
884    fail_unless(strncmp((char*)buffer,"DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
885
886    pclDeinitLibrary();
887 }
888 END_TEST
889
890
891
892 START_TEST(test_ReadConfDefault)
893 {
894    int ret = 0;
895    unsigned char buffer[READ_SIZE]  = {0};
896
897    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
898
899    ret = pclInitLibrary(gTheAppId, shutdownReg);
900    fail_unless(ret <= 1, "Failed to init PCL");
901
902    ret = pclKeyReadData(0xFF, "statusHandle/confdefault01",     3, 2, buffer, READ_SIZE);
903    fail_unless(ret != EPERS_NOT_INITIALIZED);
904    fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
905
906    ret = pclKeyReadData(0xFF, "statusHandle/confdefault02",     3, 2, buffer, READ_SIZE);
907    fail_unless(ret != EPERS_NOT_INITIALIZED);
908    fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");
909
910    pclDeinitLibrary();
911 }
912 END_TEST
913
914
915
916 START_TEST(test_GetPath)
917 {
918    int ret = 0;
919    char* path = NULL;
920    const char* thePath = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB.db";
921    unsigned int pathSize = 0;
922
923    unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
924
925    ret = pclInitLibrary(gTheAppId, shutdownReg);
926    fail_unless(ret <= 1, "Failed to init PCL");
927
928    ret = pclFileCreatePath(0xFF, "media/mediaDB.db", 1, 1, &path, &pathSize);
929    printf("PATH: %s \n", path);
930    fail_unless(strncmp((char*)path, thePath, strlen((char*)path)) == 0, "Path not correct");
931    fail_unless(pathSize == strlen((char*)path), "Path size not correct");
932
933    free(path);
934
935    pclDeinitLibrary();
936 }
937 END_TEST
938
939
940
941 static Suite * persistencyClientLib_suite()
942 {
943    Suite * s  = suite_create("Persistency client library");
944
945    TCase * tc_persGetData = tcase_create("GetData");
946    tcase_add_test(tc_persGetData, test_GetData);
947
948    TCase * tc_persSetData = tcase_create("SetData");
949    tcase_add_test(tc_persSetData, test_SetData);
950
951    TCase * tc_persSetDataNoPRCT = tcase_create("SetDataNoPRCT");
952    tcase_add_test(tc_persSetDataNoPRCT, test_SetDataNoPRCT);
953
954    TCase * tc_persGetDataSize = tcase_create("GetDataSize");
955    tcase_add_test(tc_persGetDataSize, test_GetDataSize);
956
957    TCase * tc_persDeleteData = tcase_create("DeleteData");
958    tcase_add_test(tc_persDeleteData, test_DeleteData);
959
960    TCase * tc_persGetDataHandle = tcase_create("GetDataHandle");
961    tcase_add_test(tc_persGetDataHandle, test_GetDataHandle);
962
963    TCase * tc_persDataHandle = tcase_create("DataHandle");
964    tcase_add_test(tc_persDataHandle, test_DataHandle);
965
966    TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen");
967    tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen);
968
969    TCase * tc_persDataFile = tcase_create("DataFile");
970    tcase_add_test(tc_persDataFile, test_DataFile);
971
972    TCase * tc_persDataFileRecovery = tcase_create("DataFileRecovery");
973    tcase_add_test(tc_persDataFileRecovery, test_DataFileRecovery);
974
975    TCase * tc_Cursor = tcase_create("Cursor");
976    tcase_add_test(tc_Cursor, test_Cursor);
977
978    TCase * tc_Plugin = tcase_create("Plugin");
979    tcase_add_test(tc_Plugin, test_Plugin);
980
981    TCase * tc_ReadDefault = tcase_create("ReadDefault");
982    tcase_add_test(tc_ReadDefault, test_ReadDefault);
983
984    TCase * tc_ReadConfDefault = tcase_create("ReadConfDefault");
985    tcase_add_test(tc_ReadConfDefault, test_ReadConfDefault);
986
987    TCase * tc_GetPath = tcase_create("GetPath");
988    tcase_add_test(tc_GetPath, test_GetPath);
989
990    suite_add_tcase(s, tc_persGetData);
991    suite_add_tcase(s, tc_persSetData);
992    suite_add_tcase(s, tc_persSetDataNoPRCT);
993    suite_add_tcase(s, tc_persGetDataSize);
994    suite_add_tcase(s, tc_persDeleteData);
995    suite_add_tcase(s, tc_persGetDataHandle);
996    suite_add_tcase(s, tc_persDataHandle);
997    suite_add_tcase(s, tc_persDataHandleOpen);
998    suite_add_tcase(s, tc_persDataFile);
999    suite_add_tcase(s, tc_persDataFileRecovery);
1000    suite_add_tcase(s, tc_Cursor);
1001    suite_add_tcase(s, tc_ReadDefault);
1002    suite_add_tcase(s, tc_ReadConfDefault);
1003    suite_add_tcase(s, tc_GetPath);
1004
1005    //suite_add_tcase(s, tc_Plugin); // activate only if the plugins are available
1006    return s;
1007 }
1008
1009
1010 int main(int argc, char *argv[])
1011 {
1012    int nr_failed = 0;
1013
1014    // assign application name
1015    strncpy(gTheAppId, "lt-persistence_client_library_test", MaxAppNameLen);
1016    gTheAppId[MaxAppNameLen-1] = '\0';
1017
1018    /// debug log and trace (DLT) setup
1019    DLT_REGISTER_APP("test","tests the persistence client library");
1020
1021 #if 1
1022    Suite * s = persistencyClientLib_suite();
1023    SRunner * sr = srunner_create(s);
1024    srunner_run_all(sr, CK_VERBOSE);
1025    nr_failed = srunner_ntests_failed(sr);
1026
1027    srunner_free(sr);
1028 #else
1029
1030 #endif
1031
1032    // unregister debug log and trace
1033    DLT_UNREGISTER_APP();
1034
1035    dlt_free();
1036
1037    return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;
1038
1039 }
1040