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