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