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