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