summaryrefslogtreecommitdiff
path: root/test/persistence_client_library_test.c
blob: 1d62aff90b17abdb481cf26b9f56d6cd7c5e4e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
/******************************************************************************
 * Project         Persistence
 * (c) copyright   2012
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_test.c
 * @author         Ingo Huerner
 * @brief          Test of persistence client library
 * @see            
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>     /* exit */
#include <time.h>
#include <fcntl.h>

#include <dlt.h>
#include <dlt_common.h>
#include <pthread.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sendfile.h>

#include <dbus/dbus.h>

#include <check.h>
#include <signal.h>

#include "../include/persistence_client_library_file.h"
#include "../include/persistence_client_library_key.h"
#include "../include/persistence_client_library.h"
#include "../include/persistence_client_library_error_def.h"

//#define SKIP_MULTITHREADED_TESTS 1

#define BUF_SIZE        64
#define NUM_OF_FILES    3
#define READ_SIZE       1024
#define MaxAppNameLen   256

#define NUM_THREADS     20
#define NUM_OF_READS    100
#define NUM_OF_WRITES   100
#define NAME_LEN     24

typedef struct s_threadData
{
   char threadName[NAME_LEN];
   int index;
   int fd1;
   int fd2;
} t_threadData;

static pthread_barrier_t gBarrierOne, gBarrierTwo;

/// application id
char gTheAppId[MaxAppNameLen] = {0};

// definition of weekday
char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char* gWriteBackupTestData  = "This is the content of the file /Data/mnt-c/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB_ReadWrite.db";
char* gWriteRecoveryTestData = "This is the data to recover: /Data/mnt-c/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB_DataRecovery.db";
char* gRecovChecksum = "608a3b5d";  // generated with http://www.tools4noobs.com/online_php_functions/crc32/

extern const char* gWriteBuffer;
extern const char* gWriteBuffer2;


/// debug log and trace (DLT) setup
DLT_DECLARE_CONTEXT(gPcltDLTContext);


// function prototype
void run_concurrency_test();
int check_environment();


void data_setup(void)
{
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1);

   (void)pclInitLibrary(gTheAppId, shutdownReg);
}


void data_setup_browser(void)
{
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   (void)pclInitLibrary("browser", shutdownReg);
}


void data_setup_norct(void)
{
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;

   (void)pclInitLibrary("norct", shutdownReg);
}


void data_teardown(void)
{
   pclDeinitLibrary();
}


int myChangeCallback(pclNotification_s * notifyStruct)
{
   printf(" ==> * - * myChangeCallback * - *\n");
   (void)notifyStruct;
   return 1;
}



/**
 * Test the key value interface using different logicalDB id's, users and seats.
 * Each resource below has an entry in the resource configuration table where the
 * storage location (cached or write through) and type (e.g. custom) has been configured.
 */
START_TEST(test_GetData)
{
   int ret = 0;
   unsigned char buffer[READ_SIZE] = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_GetData"));

   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 0 and seat 0
    *       ==> local value accessible by all users (user 0, seat 0)
    */
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "pos/last_position",         1, 1, buffer, READ_SIZE);
   //printf("----test_GetData => pos/last_position: \"%s\" => ret: %d \nReference: %s => size: %d\n", buffer, ret, "CACHE_ +48 10' 38.95, +8 44' 39.06", strlen("CACHE_ +48 10' 38.95, +8 44' 39.06"));
   ck_assert_str_eq( (char*)buffer, "CACHE_ +48 10' 38.95, +8 44' 39.06");
   ck_assert_int_eq( ret,  (int)strlen("CACHE_ +48 10' 38.95, +8 44' 39.06") );
   memset(buffer, 0, READ_SIZE);

   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 3 and seat 2
    *       ==> local USER value (user 3, seat 2)
    */
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "status/open_document",      3, 2, buffer, READ_SIZE);
   //printf("----test_GetData => status/open_document \"%s\" => ret: %d \n", buffer, ret);
   ck_assert_str_eq( (char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf");
   ck_assert_int_eq(ret, (int)strlen("WT_ /var/opt/user_manual_climateControl.pdf"));
   memset(buffer, 0, READ_SIZE);

   /**
    * Logical DB ID: 0x20 with user 4 and seat 0
    *       ==> shared user value accessible by a group (user 4 and seat 0)
    */
   ret = pclKeyReadData(0x20, "address/home_address",      4, 0, buffer, READ_SIZE);
   //printf("----test_GetData => address/home_address \"%s\" => ret: %d \n", buffer, ret);
   ck_assert_str_eq( (char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31");
   ck_assert_int_eq(ret, (int)strlen("WT_ 55327 Heimatstadt, Wohnstrasse 31"));
   memset(buffer, 0, READ_SIZE);

   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 0 and seat 0
    *       ==> local value accessible by ALL USERS (user 0, seat 0)
    */
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "pos/last_satellites",       0, 0, buffer, READ_SIZE);
   //printf("----test_GetData => pos/last_satellites \"%s\" => ret: %d \n", buffer, ret);
   ck_assert_str_eq( (char*)buffer, "WT_ 17");
   ck_assert_int_eq(ret, (int)strlen("WT_ 17"));
   memset(buffer, 0, READ_SIZE);

   /**
    * Logical DB ID: 0x20 with user 4 and seat 0
    *       ==> shared user value accessible by A GROUP (user 4 and seat 0)
    */
   ret = pclKeyReadData(0x20, "links/last_link",           2, 0, buffer, READ_SIZE);
   //printf("----test_GetData => links/last_link \"%s\" => ret: %d \n", buffer, ret);
   ck_assert_str_eq( (char*)buffer, "CACHE_ /last_exit/queens");
   ck_assert_int_eq(ret, (int)strlen("CACHE_ /last_exit/queens"));
   memset(buffer, 0, READ_SIZE);
}
END_TEST


/**
 * Test the key value  h a n d l e  interface using different logicalDB id's, users and seats
 * Each resource below has an entry in the resource configuration table where
 * the storage location (cached or write through) and type (e.g. custom) has bee configured.
 */
START_TEST (test_GetDataHandle)
{
   int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0;

   unsigned char buffer[READ_SIZE] = {0};
   struct tm *locTime;

   char sysTimeBuffer[128];

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_GetDataHandle"));

   time_t t = time(0);

   locTime = localtime(&t);

   snprintf(sysTimeBuffer, 128, "TimeAndData: \"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon+1, (locTime->tm_year+1900),
                                                                  locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
   // open handle ---------------------------------------------------
   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 0 and seat 0
    *       ==> local value accessible by ALL USERS (user 0, seat 0)
    */
   handle = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position", 0, 0);
   ck_assert_int_gt(handle, 0);

   ret = pclKeyHandleReadData(handle, buffer, READ_SIZE);
   //printf("pclKeyHandleReadData: \nsoll: %s \nist : %s => ret: %d | strlen: %d\n", "WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\"", buffer, ret, strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
   fail_unless(strncmp((char*)buffer, "WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\"", (size_t)ret) == 0, "Buffer not correctly read => 1");

   size = pclKeyHandleGetSize(handle);
   //printf("pclKeyHandleGetSize => size: %d\n", size);
   ck_assert_int_eq(size, (int)strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
   // ---------------------------------------------------------------------------------------------

   // open handle ---------------------------------------------------
   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 3 and seat 2
    *       ==> local USER value (user 3, seat 2)
    */
   handle2 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "statusHandle/open_document", 3, 2);
   fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");

   size = pclKeyHandleWriteData(handle2, (unsigned char*)sysTimeBuffer, (int)strlen(sysTimeBuffer));
   fail_unless(size == (int)strlen(sysTimeBuffer));
   // close
   ret = pclKeyHandleClose(handle2);
   // ---------------------------------------------------------------------------------------------

   // open handle ---------------------------------------------------
   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 3 and seat 2
    *       ==> local USER value (user 3, seat 2)
    */
   handle3 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "statusHandle/open_document", 3, 2);
   fail_unless(handle3 >= 0, "Failed to open handle /statusHandle/open_document");

   ret = pclKeyHandleReadData(handle3, buffer, READ_SIZE);
   fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read => 3");

   size = pclKeyHandleGetSize(handle3);
   fail_unless(size = (int)strlen(sysTimeBuffer));
   // ---------------------------------------------------------------------------------------------

   // close handle
   ret = pclKeyHandleClose(handle);
   ret = pclKeyHandleClose(handle3);
   ret = pclKeyHandleClose(handle4);
}
END_TEST


/*
 * Write data to a key using the key interface.
 * First write data to different keys and after
 * read the data for verification.
 */
START_TEST(test_SetData)
{
   int ret = 0;
   unsigned char buffer[READ_SIZE]  = {0};

   char write1[READ_SIZE] = {0};
   char write2[READ_SIZE] = {0};
   char sysTimeBuffer[256];

   struct tm *locTime;

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_SetData"));

   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 3 and seat 2
    *       ==> local USER value (user 3, seat 2)
    */
   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "status/open_document",      3, 2, (unsigned char*)"WT_ /var/opt/user_manual_climateControl.pdf", strlen("WT_ /var/opt/user_manual_climateControl.pdf"));
   fail_unless(ret == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Wrong write size");


   ret = pclKeyWriteData(0x84, "links/last_link",      2, 1, (unsigned char*)"CACHE_ /last_exit/queens", strlen("CACHE_ /last_exit/queens"));
   fail_unless(ret == strlen("CACHE_ /last_exit/queens"), "Wrong write size");

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "posHandle/last_position", 0, 0, (unsigned char*)"WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\"", strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
   fail_unless(ret == strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""), "Wrong write size");

   time_t t = time(0);
   locTime = localtime(&t);

   // write data
   snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon+1, (locTime->tm_year+1900),
                                                                      locTime->tm_hour, locTime->tm_min, locTime->tm_sec);

   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 1 and seat 2
    *       ==> local USER value (user 1, seat 2)
    * Resource ID: 69
    */
   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "69", 1, 2, (unsigned char*)sysTimeBuffer, (int)strlen(sysTimeBuffer));
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Wrong write size");

   snprintf(write1, 128, "%s %s", "/70",  sysTimeBuffer);
   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 1 and seat 2
    *       ==> local USER value (user 1, seat 2)
    * Resource ID: 70
    */
   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "70", 1, 2, (unsigned char*)write1, (int)strlen(write1));
   fail_unless(ret == (int)strlen(write1), "Wrong write size");

   snprintf(write2, 128, "%s %s", "/key_70",  sysTimeBuffer);
   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 1 and seat 2
    *       ==> local USER value (user 1, seat 2)
    * Resource ID: key_70
    */
   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "key_70", 1, 2, (unsigned char*)write2, (int)strlen(write2));
   fail_unless(ret == (int)strlen(write2), "Wrong write size");


   /*******************************************************************************************************************************************/
   /* used for changed notification testing */
   /*******************************************************************************************************************************************/
#if 0
   /**
    * Logical DB ID: 0x84 with user 2 and seat 1
    *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
    *
    *       ==> used for shared testing
    */
   //printf("Write data to trigger change notification\n");
   ret = pclKeyWriteData(0x20, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
   printf("Ist: %d - Soll: %d\n", ret, (int)strlen("Test notify shared data"));
   fail_unless(ret == (int)strlen("Test notify shared data"), "Wrong write size");

   /**
    * Logical DB ID: 0x84 with user 2 and seat 1
    *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
    *
    *       ==> used for shared testing
    */
   //printf("Write data to trigger change notification\n");
   ret = pclKeyWriteData(0x20, "links/last_link3",  3, 2, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
   fail_unless(ret == (int)strlen("Test notify shared data"), "Wrong write size");

   /**
    * Logical DB ID: 0x84 with user 2 and seat 1
    *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
    *
    *       ==> used for shared testing
    */
   //printf("Write data to trigger change notification\n");
   ret = pclKeyWriteData(0x20, "links/last_link4",  4, 1, (unsigned char*)"Test notify shared data", strlen("Test notify shared data"));
   fail_unless(ret == strlen("Test notify shared data"), "Wrong write size");
#endif
   /*******************************************************************************************************************************************/
   /*******************************************************************************************************************************************/


   /*
    * now read the data written in the previous steps to the keys
    * and verify data has been written correctly.
    */
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "69", 1, 2, buffer, READ_SIZE);
   fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Wrong read size");

   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "70", 1, 2, buffer, READ_SIZE);
   fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
   fail_unless(ret == (int)strlen(write1), "Wrong read size");

   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "key_70", 1, 2, buffer, READ_SIZE);
   fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
   fail_unless(ret == (int)strlen(write2), "Wrong read size");

}
END_TEST



/**
 * Write data to a key using the key interface.
 * The key is not in the persistence resource table.
 * The key sill then be stored to the location local and cached.
 */
START_TEST(test_SetDataNoPRCT)
{
   int ret = 0;
   unsigned char buffer[READ_SIZE] = {0};
   struct tm *locTime;

   time_t t = time(0);

   char sysTimeBuffer[128];

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_SetDataNoPRCT"));

   locTime = localtime(&t);

   snprintf(sysTimeBuffer, 128, "TimeAndData: \"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon+1, (locTime->tm_year+1900),
                                                                  locTime->tm_hour, locTime->tm_min, locTime->tm_sec);

   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 1 and seat 2
    *       ==> local USER value (user 1, seat 2)
    */
   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "NoPRCT", 1, 2, (unsigned char*)sysTimeBuffer, (int)strlen(sysTimeBuffer));
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Wrong write size");
   //printf("Write Buffer : %s\n", sysTimeBuffer);

   // read data again and and verify datat has been written correctly
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "NoPRCT", 1, 2, buffer, READ_SIZE);
   fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Wrong read size");
   //printf("read buffer  : %s\n", buffer);
}
END_TEST



/*
 * Test the key interface.
 * Read the size of a key.
 */
START_TEST(test_GetDataSize)
{
   int size = 0;

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_GetDataSize"));
   /**
    * Logical DB ID: PCL_LDBID_LOCAL with user 3 and seat 2
    *       ==> local USER value (user 3, seat 2)
    */
   size = pclKeyGetSize(PCL_LDBID_LOCAL, "status/open_document", 3, 2);
   fail_unless(size == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Invalid size");

   /**
    * Logical DB ID: 0x84 with user 2 and seat 1
    *       ==> shared user value accessible by A GROUP (user 2 and seat 1)
    */
   size = pclKeyGetSize(0x84, "links/last_link", 2, 1);
   fail_unless(size == strlen("CACHE_ /last_exit/queens"), "Invalid size");
}
END_TEST


/*
 * Delete a key using the key value interface.
 * First read a from a key, the delte the key
 * and then try to read again. The Last read must fail.
 */
START_TEST(test_DeleteData)
{
   int rval = 0;
   unsigned char buffer[READ_SIZE];

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_DeleteData"));

   // read data from key
   rval = pclKeyReadData(PCL_LDBID_LOCAL, "key_70", 1, 2, buffer, READ_SIZE);
   fail_unless(rval != EPERS_NOKEY, "Read form key key_70 fails");

   // delete key
   rval = pclKeyDelete(PCL_LDBID_LOCAL, "key_70", 1, 2);
   fail_unless(rval >= 0, "Failed to delete key");

   // after deleting the key, reading from key must fail now!
   rval = pclKeyReadData(PCL_LDBID_LOCAL, "key_70", 1, 2, buffer, READ_SIZE);
   fail_unless(rval == EPERS_NOKEY, "Read form key key_70 works, but should fail");



   // read data from key
   rval = pclKeyReadData(PCL_LDBID_LOCAL, "70", 1, 2, buffer, READ_SIZE);
   fail_unless(rval != EPERS_NOKEY, "Read form key 70 fails");

   // delete key
   rval = pclKeyDelete(PCL_LDBID_LOCAL, "70", 1, 2);
   fail_unless(rval >= 0, "Failed to delete key");

   // after deleting the key, reading from key must fail now!
   rval = pclKeyReadData(PCL_LDBID_LOCAL, "70", 1, 2, buffer, READ_SIZE);
   fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail");
}
END_TEST



void data_setupBackup(void)
{
   int handle = -1;
   const char* path = "/Data/mnt-c/lt-persistence_client_library_test/user/1/seat/1/media/mediaDB_ReadWrite.db";

   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   (void)pclInitLibrary(gTheAppId, shutdownReg);

   handle = open(path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
   if(write(handle, gWriteBackupTestData, strlen(gWriteBackupTestData)) == -1)
   {
      printf("setup test: failed to write test data: %s\n", path);
   }
}







/*
 * Extended key handle test.
 * Test have been created after a bug in the key handle function occured.
 */
START_TEST(test_DataHandleOpen)
{
   int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0;

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_DataHandleOpen"));

   // open handles ----------------------------------------------------
   hd1 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position1", 0, 0);
   fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1");

   hd2 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position2", 0, 0);
   fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2");

   hd3 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position3", 0, 0);
   fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3");

   // close handles ---------------------------------------------------
   ret = pclKeyHandleClose(hd1);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd2);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd3);
   fail_unless(ret != -1, "Failed to close handle!!");

   // open handles ----------------------------------------------------
   hd4 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position4", 0, 0);
   fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4");

   hd5 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position5", 0, 0);
   fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5");

   hd6 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position6", 0, 0);
   fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6");

   hd7 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position7", 0, 0);
   fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7");

   hd8 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position8", 0, 0);
   fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8");

   hd9 = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position9", 0, 0);
   fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9");

   // close handles ---------------------------------------------------
   ret = pclKeyHandleClose(hd4);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd5);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd6);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd7);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd8);
   fail_unless(ret != -1, "Failed to close handle!!");

   ret = pclKeyHandleClose(hd9);
   fail_unless(ret != -1, "Failed to close handle!!");
}
END_TEST



START_TEST(test_Plugin)
{
   int ret = 0;
   unsigned char buffer[READ_SIZE]  = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_Plugin"));

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "secured",           0, 0, buffer, READ_SIZE);
   //printf("B U F F E R - secure: \"%s\" => ist: %d | soll: %d\n", buffer, ret, strlen("Custom plugin -> plugin_get_data: secure!"));
   fail_unless(ret == strlen("Custom plugin -> plugin_get_data: secure!") );
   fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: secure!",
                 strlen((char*)buffer)) == 0, "Buffer SECURE not correctly read");
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "early",     0, 0, buffer, READ_SIZE);
   //printf("B U F F E R - early: \"%s\" => ist: %d | soll: %d\n", buffer, ret, strlen("Custom plugin -> plugin_get_data: early!"));
   fail_unless(ret == strlen("Custom plugin -> plugin_get_data: early!"));
   fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: early!",
               strlen((char*)buffer)) == 0, "Buffer EARLY not correctly read");
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "emergency", 0, 0, buffer, READ_SIZE);
   //printf("B U F F E R - emergency: \"%s\" => ist: %d | soll: %d\n", buffer, ret, strlen("Custom plugin -> plugin_get_data: emergency!"));
   fail_unless(ret == strlen("Custom plugin -> plugin_get_data: emergency!"));
   fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: emergency!",
               strlen((char*)buffer)) == 0, "Buffer EMERGENCY not correctly read");
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "hwinfo",   0, 0, buffer, READ_SIZE);
   //printf("B U F F E R - hwinfo: \"%s\" => ist: %d | soll: %d\n", buffer, ret, strlen("Custom plugin -> plugin_get_data: hwinfo!"));
   fail_unless(ret != EPERS_NOT_INITIALIZED);
   fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: hwinfo!",
               strlen((char*)buffer)) == 0, "Buffer HWINFO not correctly read");
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "custom2",   0, 0, buffer, READ_SIZE);
   //printf("B U F F E R - custom2: \"%s\" => ist: %d | soll: %d\n", buffer, ret, strlen("Custom plugin -> plugin_get_data: custom2!"));
   fail_unless(ret == strlen("Custom plugin -> plugin_get_data: custom2!"));
   fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: custom2!",
               strlen((char*)buffer)) == 0, "Buffer CUSTOM 2 not correctly read");
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "custom3",   0, 0, buffer, READ_SIZE);
   //printf("B U F F E R - custom3: \"%s\" => ist: %d | soll: %d\n", buffer, ret, strlen("Custom plugin -> plugin_get_data: custom3!"));
   fail_unless(ret == strlen("Custom plugin -> plugin_get_data: custom3!"));
   fail_unless(strncmp((char*)buffer,"Custom plugin -> plugin_get_data: custom3!",
                 strlen((char*)buffer)) == 0, "Buffer CUSTOM 3 not correctly read");
   memset(buffer, 0, READ_SIZE);

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "custom3",   0, 0, (unsigned char*)"This is a message to write", READ_SIZE);
   fail_unless(ret == 321456, "Failed to write custom data");  // plugin should return 321456


   ret = pclKeyGetSize(PCL_LDBID_LOCAL, "custom3",   0, 0);
   fail_unless(ret == 44332211, "Failed query custom data size"); // plugin should return 44332211


   ret = pclKeyDelete(PCL_LDBID_LOCAL, "custom3",   0, 0);
   fail_unless(ret == 13579, "Failed query custom data size"); // plugin should return 13579
}
END_TEST





START_TEST(test_ReadDefault)
{
   int ret = 0;
   unsigned char buffer[READ_SIZE]  = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_ReadDefault"));

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "statusHandle/default01", 3, 2, buffer, READ_SIZE);
   //printf(" --- test_ReadConfDefault => statusHandle/default01: %s => retIst: %d retSoll: %d\n", buffer, ret, strlen("DEFAULT_01!"));
   fail_unless(ret == strlen("DEFAULT_01!"));
   fail_unless(strncmp((char*)buffer,"DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "statusHandle/default02", 3, 2, buffer, READ_SIZE);
   //printf(" --- test_ReadConfDefault => statusHandle/default02: %s => retIst: %d retSoll: %d\n", buffer, ret, strlen("DEFAULT_02!"));
   fail_unless(ret == strlen("DEFAULT_02!"));
   fail_unless(strncmp((char*)buffer,"DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");

   ret = pclKeyGetSize(PCL_LDBID_LOCAL, "statusHandle/default02", 3, 2);
   //printf("IST: %d - SOLL: %d\n", ret, (int)strlen("DEFAULT_02!"));
   fail_unless(ret == strlen("DEFAULT_01!"), "Invalid size");

   ret = pclKeyGetSize(PCL_LDBID_LOCAL, "statusHandle/default01", 3, 2);
   //printf("IST: %d - SOLL: %d\n", ret, (int)strlen("DEFAULT_01!"));
   fail_unless(ret == strlen("DEFAULT_01!"), "Invalid size");
}
END_TEST



START_TEST(test_ReadConfDefault)
{
   int ret = 0;
   unsigned char buffer[READ_SIZE]  = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_ReadConfDefault"));

#if 1
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "statusHandle/confdefault01",     3, 2, buffer, READ_SIZE);
   //printf(" --- test_ReadConfDefault => statusHandle/confdefault01: %s => retIst: %d retSoll: %d\n", buffer, ret, strlen("CONF_DEFAULT_01!"));
   fail_unless(ret == strlen("CONF_DEFAULT_01!"));
   fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_01!", strlen((char*)buffer)) == 0, "Buffer not correctly read");

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "statusHandle/confdefault02",     3, 2, buffer, READ_SIZE);
   //printf(" --- test_ReadConfDefault => statusHandle/confdefault02: %s => retIst: %d retSoll: %d\n", buffer, ret, strlen("CONF_DEFAULT_02!"));
   fail_unless(ret == strlen("CONF_DEFAULT_02!"));
   fail_unless(strncmp((char*)buffer,"CONF_DEFAULT_02!", strlen((char*)buffer)) == 0, "Buffer not correctly read");

   ret = pclKeyGetSize(PCL_LDBID_LOCAL, "statusHandle/confdefault02", 3, 2);
   fail_unless(ret == strlen("CONF_DEFAULT_02!"), "Invalid size");

#endif
}
END_TEST



START_TEST(test_WriteConfDefault)
{
   int ret = 0;
   unsigned char writeBuffer[]  = "This is a test string";
   unsigned char writeBuffer2[]  = "And this is a test string which is different form previous test string";
   unsigned char readBuffer[READ_SIZE]  = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_WriteConfDefault"));

   // -- key-value interface ---
   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "statusHandle/writeconfdefault01", PCL_USER_DEFAULTDATA, 0, writeBuffer, (int)strlen((char*)writeBuffer));
   fail_unless(ret == (int)strlen((char*)writeBuffer), "Write Conf default data: write size does not match");
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "statusHandle/writeconfdefault01",  3, 2, readBuffer, READ_SIZE);
   fail_unless(ret == (int)strlen((char*)writeBuffer), "Write Conf default data: read size does not match");
   fail_unless(strncmp((char*)readBuffer, (char*)writeBuffer, strlen((char*)readBuffer)) == 0, "Buffer not correctly read");
   //printf(" --- test_ReadConfDefault => statusHandle/writeconfdefault01: \"%s\" => \"%s\" \n    retIst: %d retSoll: %d\n", readBuffer, writeBuffer, ret, strlen((char*)writeBuffer));


   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "statusHandle/writeconfdefault01", PCL_USER_DEFAULTDATA, 0, writeBuffer2, (int)strlen((char*)writeBuffer2));
   fail_unless(ret == (int)strlen((char*)writeBuffer2), "Write Conf default data 2: write size does not match");
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "statusHandle/writeconfdefault01",  3, 2, readBuffer, READ_SIZE);
   fail_unless(strncmp((char*)readBuffer, (char*)writeBuffer2, strlen((char*)readBuffer)) == 0, "Buffer2 not correctly read");
   //printf(" --- test_ReadConfDefault => statusHandle/writeconfdefault01: \"%s\" => \"%s\" \n    retIst: %d retSoll: %d\n", readBuffer, writeBuffer2, ret, strlen((char*)writeBuffer2));

}
END_TEST




START_TEST(test_InitDeinit)
{
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;

   int i = 0, rval = -1, handle = 0;


   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_InitDeinit"));


   for(i=0; i<5; i++)
   {
      // initialize and deinitialize 1. time
      (void)pclInitLibrary(gTheAppId, shutdownReg);
      pclDeinitLibrary();


      // initialize and deinitialize 2. time
      (void)pclInitLibrary(gTheAppId, shutdownReg);
      pclDeinitLibrary();


      // initialize and deinitialize 3. time
      (void)pclInitLibrary(gTheAppId, shutdownReg);
      pclDeinitLibrary();
   }


   // test multiple init/deinit
   pclInitLibrary(gTheAppId, shutdownReg);
   pclInitLibrary(gTheAppId, shutdownReg);

   pclDeinitLibrary();
   pclDeinitLibrary();
   pclDeinitLibrary();

   // test lifecycle set
   pclInitLibrary(gTheAppId, shutdownReg);
   rval = pclLifecycleSet(PCL_SHUTDOWN);
   fail_unless(rval == EPERS_SHUTDOWN_NO_PERMIT, "Lifecycle set allowed, but should not");
   pclDeinitLibrary();


   pclInitLibrary(gTheAppId, PCL_SHUTDOWN_TYPE_NONE);

   handle = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position", 0, 0);
   //printf("pclKeyHandleOpen: %d\n", handle);
   fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");
   (void)pclKeyHandleClose(handle);

   rval = pclLifecycleSet(PCL_SHUTDOWN);
   fail_unless(rval != EPERS_SHUTDOWN_NO_PERMIT, "Lifecycle set NOT allowed, but should");


   rval = pclLifecycleSet(PCL_SHUTDOWN_CANCEL);
   rval = pclLifecycleSet(PCL_SHUTDOWN_CANCEL);
   rval = pclLifecycleSet(PCL_SHUTDOWN_CANCEL);
   rval = pclLifecycleSet(PCL_SHUTDOWN_CANCEL);
   rval = pclLifecycleSet(PCL_SHUTDOWN_CANCEL);
   rval = pclLifecycleSet(PCL_SHUTDOWN_CANCEL);

   pclDeinitLibrary();

   pclInitLibrary("NodeStateManager", PCL_SHUTDOWN_TYPE_NONE);

   pclDeinitLibrary();

   fail_unless(pclInitLibrary("", shutdownReg) == EPERS_COMMON);
   fail_unless(pclInitLibrary(NULL, shutdownReg) == EPERS_COMMON);
   fail_unless(pclInitLibrary(gWriteBuffer2, shutdownReg) == EPERS_COMMON);

}
END_TEST



START_TEST(test_NegHandle)
{
   int handle = -1, ret = 0;
   int negativeHandle = -17;
   unsigned char buffer[128] = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_NegHandle"));

   handle = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position", 0, 0);
   fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");

   ret = pclKeyHandleReadData(negativeHandle, buffer, READ_SIZE);
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleReadData => negative handle not detected");

   ret = pclKeyHandleClose(negativeHandle);
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleClose => negative handle not detected");

   ret = pclKeyHandleGetSize(negativeHandle);
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleGetSize => negative handle not detected");

   ret = pclKeyHandleReadData(negativeHandle, buffer, 128);
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleReadData => negative handle not detected");

   ret = pclKeyHandleRegisterNotifyOnChange(negativeHandle, &myChangeCallback);
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleRegisterNotifyOnChange => negative handle not detected");

   ret = pclKeyHandleUnRegisterNotifyOnChange(negativeHandle, &myChangeCallback);
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleUnRegisterNotifyOnChange => negative handle not detected");

   ret = pclKeyHandleWriteData(negativeHandle, (unsigned char*)"Whatever", strlen("Whatever"));
   fail_unless(ret == EPERS_MAXHANDLE, "pclKeyHandleWriteData => negative handle not detected");


   // close handle
   ret = pclKeyHandleClose(handle);
}
END_TEST



START_TEST(test_utf8_string)
{
   int ret = 0, size = 0;
   const char* utf8StringBuffer = "String °^° Ñ text";
   unsigned char buffer[128] = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_utf8_string"));

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "utf8String", 3, 2, buffer, READ_SIZE);
   fail_unless(ret == (int)strlen(utf8StringBuffer), "Wrong read size");
   fail_unless(strncmp((char*)buffer, utf8StringBuffer, (size_t)ret-1) == 0, "Buffer not correctly read => 1");

   size = pclKeyGetSize(PCL_LDBID_LOCAL, "utf8String", 3, 2);
   fail_unless(size == (int)strlen(utf8StringBuffer), "Invalid size");
}
END_TEST



START_TEST(test_Notifications)
{
   int ret = 0;

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_Notifications"));

   ret = pclKeyRegisterNotifyOnChange(0x20, "address/home_address", 1, 1, myChangeCallback);
   fail_unless(ret == 0, "Failed to register");

   ret = pclKeyUnRegisterNotifyOnChange(0x20, "address/home_address", 1, 1, myChangeCallback);
   fail_unless(ret == 0, "Failed to register");

   ret = pclKeyUnRegisterNotifyOnChange(PCL_LDBID_PUBLIC, "aSharedResource", 1, 1, myChangeCallback);
   fail_unless(ret == 0, "Failed to register");

   ret = pclKeyUnRegisterNotifyOnChange(PCL_LDBID_LOCAL, "status/open_document", 1, 1, myChangeCallback);
   fail_unless(ret == EPERS_NOTIFY_NOT_ALLOWED, "Possible to register, but should not - is local variable");

   ret = pclKeyUnRegisterNotifyOnChange(0x20, "notInRCT", 1, 1, myChangeCallback);
   fail_unless(ret == EPERS_NOKEYDATA, "Possible to register, but should not - not in rct");
}
END_TEST


#if USE_APPCHECK
START_TEST(test_ValidApplication)
{
   int ret = 0;
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   unsigned char buffer[128] = {0};

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_ValidApplication"));

   ret = pclInitLibrary("InvalidAppID", shutdownReg);

   ret = pclKeyGetSize(PCL_LDBID_LOCAL, "JustTesting", 1, 1);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyGetSize => invalid application ID not detected");

   ret =  pclKeyDelete(PCL_LDBID_LOCAL, "JustTesting", 1, 1);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyDelete => invalid application ID not detected");

   ret =  pclKeyHandleClose(1);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyHandleClose => invalid application ID not detected");

   ret =  pclKeyHandleGetSize(1);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyHandleGetSize => invalid application ID not detected");

   ret =  pclKeyHandleOpen(PCL_LDBID_LOCAL, "JustTesting", 1, 1);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyHandleOpen => invalid application ID not detected");

   ret =  pclKeyHandleReadData(1, buffer, 128);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyHandleReadData => invalid application ID not detected");

   ret =  pclKeyHandleWriteData(1, (unsigned char*)"Test", strlen("Test"));
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyHandleWriteData => invalid application ID not detected");

   ret =  pclKeyReadData(PCL_LDBID_LOCAL, "JustTesting", 1, 1, buffer, 128);
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyReadData => invalid application ID not detected");

   ret =  pclKeyWriteData(PCL_LDBID_LOCAL, "JustTesting", 1, 1, (unsigned char*)"Test", strlen("Test"));
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "pclKeyWriteData => invalid application ID not detected");


   pclDeinitLibrary();
}
END_TEST
#endif


START_TEST(test_PAS_DbusInterface)
{
   // let the administration servis generate a message to the PCL
   if(system("/usr/local/bin/persadmin_tool export /tmp/myBackup 0") == -1)
   {
      printf("Failed to execute command -> admin service!!\n");
   }
}
END_TEST



START_TEST(test_LC_DbusInterface)
{

// send the following dbus command
//
   printf("\n\n*******************************************************\n");
   printf("Past and execute NOW the following command to a console: \"dbus-send --system --print-reply --dest=org.genivi.NodeStateManager /org/genivi/NodeStateManager/LifecycleControl org.genivi.NodeStateManager.LifecycleControl.SetNodeState int32:6\"\n");
   printf("*******************************************************\n\n");

#if 0
#if 0
   const char* theDbusCommand =
   "dbus-send --system --print-reply \
   --dest=org.genivi.NodeStateManager \
   /org/genivi/NodeStateManager/LifecycleControl \
   \"org.genivi.NodeStateManager.LifecycleControl.SetNodeState\" \
   int32:6";

   // notify the NSM to shutdown the system
   if(system(theDbusCommand) == -1)
   {
      printf("Failed to execute command -> NSM!!\n");
   }
#else
   int nodeState = 6;   // shutdown state
   DBusConnection* conn = NULL;
   DBusError err;

   dbus_error_init(&err);
   conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);

   DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",        // destination
                                                       "/org/genivi/NodeStateManager/LifecycleControl",           // path
                                                       "org.genivi.NodeStateManager.LifecycleControl",        // interface
                                                       "SetNodeState");                      // method

   dbus_message_append_args(message, DBUS_TYPE_INT32, &nodeState, DBUS_TYPE_INVALID);

   printf("*************************** ==> Send message and block\n");
   if(!dbus_connection_send_with_reply_and_block(conn, message, 5000, &err))
   {
         printf("connection send: - Access denied: %s\n", err.message);
   }
   dbus_connection_flush(conn);
   dbus_message_unref(message);
   printf("*************************** <== \n");

   dbus_connection_close(conn);
   dbus_connection_unref(conn);
#endif
#else

   sleep(6);

#endif
}
END_TEST


START_TEST(test_SharedAccess)
{
   int ret = 0;
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   unsigned char buffer[256] = {0};
   char sysTimeBuffer[256];
   struct tm *locTime;
   time_t t = time(0);

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_SharedAccess"));

   locTime = localtime(&t);

   // write data
   snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon+1, (locTime->tm_year+1900),
                                                                      locTime->tm_hour, locTime->tm_min, locTime->tm_sec);

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   ret = pclKeyWriteData(PCL_LDBID_PUBLIC, "aSharedResource", 1, 1, (unsigned char*)sysTimeBuffer, (int)strlen(sysTimeBuffer));
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Failed to write shared data ");

   ret = pclKeyReadData(PCL_LDBID_PUBLIC, "aSharedResource", 1, 1, buffer, 256);
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Failed to read shared data ");

   pclDeinitLibrary();

   // ----------------------------------------------

   (void)pclInitLibrary("node-health-monitor", shutdownReg);   // now use a different app id, which is not able to write this resource

   ret = pclKeyWriteData(PCL_LDBID_PUBLIC, "aSharedResource", 1, 1, (unsigned char*)"This is a test Buffer", (int)strlen("This is a test Buffer"));
   fail_unless(ret == EPERS_NOT_RESP_APP, "Able to write shared data, but should not!!");

   ret = pclKeyReadData(PCL_LDBID_PUBLIC, "aSharedResource", 1, 1, buffer, 256);
   fail_unless(ret == (int)strlen(sysTimeBuffer), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen((char*)sysTimeBuffer)) == 0, "Buffer not correctly read");

   pclDeinitLibrary();
}
END_TEST



START_TEST(test_VO722)
{
   int ret = 0;
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;

   unsigned char buffer[256] = {0};
   char* writeBuffer[] = {"VO722 - TestString One",
                          "VO722 - TestString Two -",
                          "VO722 - TestString Three -",};

   char* writeBuffer2[] = {"2 - VO722 - Test - String One",
                           "2 - VO722 - Test - String Two -",
                           "2 - VO722 - Test - String Three -", };

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_VO722"));

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 1, 2, (unsigned char*)writeBuffer[0], (int)strlen(writeBuffer[0]));
   fail_unless(ret == (int)strlen(writeBuffer[0]), "Wrong write size");

   memset(buffer, 0, 256);
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 1, 2, (unsigned char*)buffer, 256);
   //printf("****** 1.1. read AEVOO722 => buffer: \"%s\"\n", buffer);
   fail_unless(ret == (int)strlen(writeBuffer[0]), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, writeBuffer[0], strlen((char*)writeBuffer[0])) == 0, "Buffer not correctly read - 1.1");

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 1, 2, (unsigned char*)writeBuffer[1], (int)strlen(writeBuffer[1]));
   fail_unless(ret == (int)strlen(writeBuffer[1]), "Wrong write size");

   memset(buffer, 0, 256);
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 1, 2, (unsigned char*)buffer, 256);
   //printf("****** 1.2. read AEVOO722 => buffer: \"%s\"\n", buffer);
   fail_unless(ret == (int)strlen(writeBuffer[1]), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, writeBuffer[1], strlen((char*)writeBuffer[1])) == 0, "Buffer not correctly read - 1.2");

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 1, 2, (unsigned char*)writeBuffer[2], (int)strlen(writeBuffer[2]));
   fail_unless(ret == (int)strlen(writeBuffer[2]), "Wrong write size");

   pclDeinitLibrary();
   sleep(1);


   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   memset(buffer, 0, 256);
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 1, 2, (unsigned char*)buffer, 256);
   //printf("****** 1.3. read AEVOO722 => buffer: \"%s\"\n\n", buffer);
   fail_unless(ret == (int)strlen(writeBuffer[2]), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, writeBuffer[2], strlen((char*)writeBuffer[2])) == 0, "Buffer not correctly read - 1.3");

   pclDeinitLibrary();

//------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 0, 0, (unsigned char*)writeBuffer2[0], (int)strlen(writeBuffer2[0]));
   fail_unless(ret == (int)strlen(writeBuffer2[0]), "Wrong write size");

   memset(buffer, 0, 256);
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 0, 0, (unsigned char*)buffer, 256);
   //printf("****** 2.1. read AEVOO722 => buffer: \"%s\"\n", buffer);
   fail_unless(ret == (int)strlen(writeBuffer2[0]), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, writeBuffer2[0], strlen((char*)writeBuffer2[0])) == 0, "Buffer not correctly read - 2.1");


   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 0, 0, (unsigned char*)writeBuffer2[1], (int)strlen(writeBuffer2[1]));
   fail_unless(ret == (int)strlen(writeBuffer2[1]), "Wrong write size");

   memset(buffer, 0, 256);
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 0, 0, (unsigned char*)buffer, 256);
   //printf("****** 2.2. read AEVOO722 => buffer: \"%s\"\n", buffer);
   fail_unless(ret == (int)strlen(writeBuffer2[1]), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, writeBuffer2[1], strlen((char*)writeBuffer2[1])) == 0, "Buffer not correctly read - 2.2");

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 0, 0, (unsigned char*)writeBuffer2[2], (int)strlen(writeBuffer2[2]));
   fail_unless(ret == (int)strlen(writeBuffer2[2]), "Wrong write size");

   pclDeinitLibrary();


   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   memset(buffer, 0, 256);
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "ContactListViewSortOrder", 0, 0, (unsigned char*)buffer, 256);
   //printf("****** 2.3. read AEVOO722 => buffer: \"%s\"\n\n", buffer);
   fail_unless(ret == (int)strlen(writeBuffer2[2]), "Failed to read shared data ");
   fail_unless(strncmp((char*)buffer, writeBuffer2[2], strlen((char*)writeBuffer2[2])) == 0, "Buffer not correctly read - 2.3");

   pclDeinitLibrary();
}
END_TEST



START_TEST(test_NoRct)
{
   int ret = 0;
   const char writeBuffer[] = "This is a test string";

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_NoRct"));

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "someResourceId", 0, 0, (unsigned char*)writeBuffer, (int)strlen(writeBuffer));

#if USE_APPCHECK
   fail_unless(ret == EPERS_SHUTDOWN_NO_TRUSTED, "Shutdown is trusted, but should not");
#else
   fail_unless(ret == EPERS_NOPRCTABLE, "RCT available, but should not");
#endif

}
END_TEST



START_TEST(test_InvalidPluginfConf)
{
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_InvalidPluginfConf"));

   // change to an invalid plugin configuration file using environment variable
   setenv(envVariable, "/tmp/whatever/pclCustomLibConfigFile.cfg", 1);

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   pclDeinitLibrary();


   // change to an empty plugin configuration file using environment variable
   setenv(envVariable, "/etc/pclCustomLibConfigFileEmpty.cfg", 1);

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   pclDeinitLibrary();

   (void)unsetenv(envVariable);
}
END_TEST


START_TEST(test_SharedData)
{
   int ret = 0;
   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_SharedData"));

   ret = pclKeyWriteData(0x20, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data___1111", strlen("Test notify shared data___1111"));
   fail_unless(ret == (int)strlen("Test notify shared data___1111"), "Wrong write size");

   sleep(1);

   ret = pclKeyWriteData(0x20, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data___2211", strlen("Test notify shared data___2211"));
   fail_unless(ret == (int)strlen("Test notify shared data___2211"), "Wrong write size");

   sleep(1);

   ret = pclKeyWriteData(0x20, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data___3311", strlen("Test notify shared data___3311"));
   fail_unless(ret == (int)strlen("Test notify shared data___3311"), "Wrong write size");

   sleep(1);

   ret = pclKeyWriteData(0x20, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data___4411", strlen("Test notify shared data___4411"));
   fail_unless(ret == (int)strlen("Test notify shared data___4411"), "Wrong write size");

   sleep(1);

   ret = pclKeyWriteData(0x20, "links/last_link2",  2, 1, (unsigned char*)"Test notify shared data___5511", strlen("Test notify shared data___5511"));
   fail_unless(ret == (int)strlen("Test notify shared data___5511"), "Wrong write size");
}
END_TEST



void* readThread(void* userData)
{
   int ret = 0, i = 0, handleOne = 0, handleTwo = 0, handleThree = 0;
   unsigned char buffer[READ_SIZE] = {0};
   char threadName[64] = {0};
   char* uData = NULL;
   uData = (char*)userData;

   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   memset(threadName, 0, 64-1);
   memcpy(threadName, uData, 64-1);
   threadName[64-1] = '\0';

   setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1);

   (void)pclInitLibrary(gTheAppId, shutdownReg);


   pthread_barrier_wait(&gBarrierOne);
   usleep(5000);

   handleOne   = pclKeyHandleOpen(PCL_LDBID_LOCAL, "pos/last_satellites", 1, 2);
   handleTwo   = pclKeyHandleOpen(PCL_LDBID_LOCAL, "pos/last_satellites", 2, 3);
   handleThree = pclKeyHandleOpen(PCL_LDBID_LOCAL, "pos/last_satellites", 3, 4);

   ret = pclKeyHandleWriteData(handleOne, (unsigned char*)"pos/last_satellites_1_2_data", (int)strlen("pos/last_satellites_1_2_data"));
   fail_unless(ret == (int)strlen("pos/last_satellites_1_2_data"));

   ret = pclKeyHandleWriteData(handleTwo, (unsigned char*)"pos/last_satellites_2_3_data_23", (int)strlen("pos/last_satellites_2_3_data_23"));
   fail_unless(ret == (int)strlen("pos/last_satellites_2_3_data_23"));

   ret = pclKeyHandleWriteData(handleThree, (unsigned char*)"pos/last_satellites_3_4_data_34_34", (int)strlen("pos/last_satellites_3_4_data_34_34"));
   fail_unless(ret == (int)strlen("pos/last_satellites_3_4_data_34_34"));

   for(i=0; i<NUM_OF_READS; i++)
   {
      /**
       * Logical DB ID: PCL_LDBID_LOCAL with user 0 and seat 0
       *       ==> local value accessible by all users (user 0, seat 0)
       */
      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(PCL_LDBID_LOCAL, "pos/last_position",         1, 1, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "CACHE_ +48 10' 38.95, +8 44' 39.06",
                    strlen((char*)buffer)) == 0, "Buffer not correctly read - pos/last_position");
      fail_unless(ret == strlen("CACHE_ +48 10' 38.95, +8 44' 39.06"));
      usleep(3000);

      memset(buffer, 0, READ_SIZE);
      ret = pclKeyHandleReadData(handleOne, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "pos/last_satellites_1_2_data",
                    strlen((char*)buffer)) == 0, "Buffer not correctly read - pos/last_satellites_1_2_data");
      fail_unless(ret == strlen("pos/last_satellites_1_2_data"));
      usleep(3000);

      /**
       * Logical DB ID: PCL_LDBID_LOCAL with user 3 and seat 2
       *       ==> local USER value (user 3, seat 2)
       */
      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(PCL_LDBID_LOCAL, "status/open_document",      3, 2, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0,
                    "Buffer not correctly read - status/open_document");
      fail_unless(ret == strlen("WT_ /var/opt/user_manual_climateControl.pdf"));
      usleep(2000);

#if 1
      /**
       * Logical DB ID: 0x20 with user 4 and seat 0
       *       ==> shared user value accessible by a group (user 4 and seat 0)
       */
      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(0x20, "address/home_address",      4, 0, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0,
                    "Buffer not correctly read - address/home_address");
      fail_unless(ret == strlen("WT_ 55327 Heimatstadt, Wohnstrasse 31"));
      usleep(5000);
#endif

      memset(buffer, 0, READ_SIZE);
      ret = pclKeyHandleReadData(handleTwo, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "pos/last_satellites_2_3_data_23",
                    strlen((char*)buffer)) == 0, "Buffer not correctly read - pos/last_satellites_2_3_data_23");
      fail_unless(ret == strlen("pos/last_satellites_2_3_data_23"));
      usleep(3000);


      /**
       * Logical DB ID: PCL_LDBID_LOCAL with user 0 and seat 0
       *       ==> local value accessible by ALL USERS (user 0, seat 0)
       */
      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(PCL_LDBID_LOCAL, "pos/last_satellites",       0, 0, buffer, READ_SIZE);

      fail_unless(strncmp((char*)buffer, "WT_ 17", strlen((char*)buffer)) == 0,
                    "Buffer not correctly read - pos/last_satellites");
      fail_unless(ret == strlen("WT_ 17"));
      usleep(2000);

#if 1
      /**
       * Logical DB ID: 0x20 with user 4 and seat 0
       *       ==> shared user value accessible by A GROUP (user 4 and seat 0)
       */
      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(0x20, "links/last_link",           2, 0, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0,
                    "Buffer not correctly read - links/last_link");
      fail_unless(ret == strlen("CACHE_ /last_exit/queens"));
      usleep(3000);
#endif

      memset(buffer, 0, READ_SIZE);
      ret = pclKeyHandleReadData(handleThree, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, "pos/last_satellites_3_4_data_34_34",
                    strlen((char*)buffer)) == 0, "Buffer not correctly read - pos/last_satellites_3_4_data_34_34");
      fail_unless(ret == strlen("pos/last_satellites_3_4_data_34_34"));
      usleep(3000);
   }

   (void)pclKeyHandleClose(handleOne);
   (void)pclKeyHandleClose(handleTwo);
   (void)pclKeyHandleClose(handleThree);


   pclDeinitLibrary();

   pthread_exit(0);
}



START_TEST(test_MultiThreadedRead)
{
   pthread_t gReadthreads[NUM_THREADS];
   int i=0;
   char threadName[NUM_THREADS][NAME_LEN];

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_MultiThreadedRead"));

   if(pthread_barrier_init(&gBarrierOne, NULL, NUM_THREADS) == 0)
   {
      for(i=0; i<NUM_THREADS; i++)
      {
         memset(threadName[i], 0, NAME_LEN);
         sprintf(threadName[i], "R-Thread -%3d-", i);
         threadName[i][NAME_LEN-1] = '\0';

         if(pthread_create(&gReadthreads[i], NULL, readThread, threadName[i]) != -1)
         {
            (void)pthread_setname_np(gReadthreads[i], threadName[i]);
         }
      }

      for(i=0; i<NUM_THREADS; i++)
      {
         if(pthread_join(gReadthreads[i], NULL) != 0)    // wait until thread has ended
            printf("pthread_join - FAILED [%d]\n", i);
      }

      if(pthread_barrier_destroy(&gBarrierOne) != 0)
         printf("Failed to destroy barrier\n");
   }
   else
   {
      printf("Failed to init barrier\n");
   }
}
END_TEST



void* writeThread(void* userData)
{
   int ret = 0, i = 0;
   unsigned char buffer[READ_SIZE] = {0};
   char sysTimeBuffer[128];
   char* staticString = "A quick movement of the enemy will jeopardize six gunboats";
   char payload[NAME_LEN] = {0};
   struct tm *locTime;
   struct timespec curTime;
   t_threadData* threadData = (t_threadData*)userData;

   memset(payload, 0, NAME_LEN);
   strncpy(payload, threadData->threadName, NAME_LEN);
   payload[NAME_LEN-1] = '\0'; // string end termination

   pthread_barrier_wait(&gBarrierTwo);
   usleep(5000);

   for(i=0; i<NUM_OF_WRITES; i++)
   {
      time_t t = time(0);
      locTime = localtime(&t);
      clock_gettime(CLOCK_MONOTONIC, &curTime);
      memset(sysTimeBuffer, 0, 128);
      snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d::%.4d:%.8ld Uhr\"", dayOfWeek[locTime->tm_wday], (int)locTime->tm_mday, (int)locTime->tm_mon+1, (int)(locTime->tm_year+1900),
                                                                        (int)locTime->tm_hour, (int)locTime->tm_min, (int)locTime->tm_sec, (int)((int)curTime.tv_nsec / 1.0e6), curTime.tv_nsec );

      ret = pclKeyWriteData((unsigned int)PCL_LDBID_LOCAL, "69", (unsigned int)threadData->index, 2, (unsigned char*)payload, (int)strlen(payload));
      fail_unless(ret == (int)strlen(payload), "Wrong write size");
      usleep(2000);

      ret = pclKeyWriteData(PCL_LDBID_LOCAL, payload, 1, (unsigned int)threadData->index, (unsigned char*)sysTimeBuffer, (int)strlen(sysTimeBuffer));
      fail_unless(ret == (int)strlen(sysTimeBuffer), "Wrong write size");
      usleep(3000);

      ret = pclKeyWriteData(PCL_LDBID_LOCAL, "70", 1, 2, (unsigned char*)staticString, (int)strlen(staticString));
      fail_unless(ret == (int)strlen(staticString), "Wrong write size");
      usleep(3000);

      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(PCL_LDBID_LOCAL, "69",      (unsigned int)threadData->index, 2, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, payload, strlen(payload)) == 0, "2: Buffer not correctly read");
      fail_unless(ret == (int)strlen(payload), "Wrong read size");
      usleep(5000);

      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(PCL_LDBID_LOCAL, payload,  1, (unsigned int)threadData->index, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "1: Buffer not correctly read");
      fail_unless(ret == (int)strlen(sysTimeBuffer), "Wrong read size");
      usleep(2000);

      memset(buffer, 0, READ_SIZE);
      ret = pclKeyReadData(PCL_LDBID_LOCAL, "70",      1, 2, buffer, READ_SIZE);
      fail_unless(strncmp((char*)buffer, staticString, strlen(staticString)) == 0, "3: Buffer not correctly read");
      fail_unless(ret == (int)strlen(staticString), "Wrong read size");
      usleep(3000);
   }

   pthread_exit(0);
}



START_TEST(test_MultiThreadedWrite)
{
   int i=0;
   pthread_t gWritethreads[NUM_THREADS];
   t_threadData threadData[NUM_THREADS];

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_MultiThreadedWrite"));

   if(pthread_barrier_init(&gBarrierTwo, NULL, NUM_THREADS) == 0)
   {
      for(i=0; i<NUM_THREADS; i++)
      {
         memset(threadData[i].threadName, 0, NAME_LEN);
         sprintf(threadData[i].threadName, "-%3d-W-Key-%3d-", i, i);
         threadData[i].threadName[NAME_LEN-1] = '\0';
         threadData[i].index = i;

         if(pthread_create(&gWritethreads[i], NULL, writeThread, &(threadData[i])) != -1)
         {
            (void)pthread_setname_np(gWritethreads[i], threadData[i].threadName);
         }
      }

      for(i=0; i<NUM_THREADS; i++)
      {
         if(pthread_join(gWritethreads[i], NULL) != 0)    // wait until thread has ended
            printf("pthread_join - FAILED [%d]\n", i);
      }

      if(pthread_barrier_destroy(&gBarrierTwo) != 0)
         printf("Failed to destroy barrier\n");
   }
   else
   {
      printf("Failed to init barrier\n");
   }
}
END_TEST



START_TEST(test_NoPluginFunc)
{
   unsigned char buffer[READ_SIZE] = {0};
   int ret = 0, handle;
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_NoPluginFunc"));

   // change to an wrong plugin configuration file using environment variable
   setenv(envVariable, "/etc/pclCustomLibConfigFileWrongDefault.cfg", 1);

   sleep(2);

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // use the app id, the resource is registered for

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "status/open_document", 3, 2, buffer, READ_SIZE);
   ck_assert_int_eq(ret, EPERS_COMMON);

   ret = pclKeyGetSize(PCL_LDBID_LOCAL, "status/open_document", 3, 2);
   ck_assert_int_eq(ret, EPERS_COMMON);

   handle = pclKeyHandleOpen(PCL_LDBID_LOCAL, "posHandle/last_position", 0, 0);
   ck_assert_int_gt(handle, 0);

   ret = pclKeyHandleReadData(handle, buffer, READ_SIZE);
   ck_assert_int_eq(ret, EPERS_COMMON);

   ret = pclKeyHandleClose(handle);
   ck_assert_int_eq(ret, 1);

   pclDeinitLibrary();

   (void)unsetenv(envVariable);
}
END_TEST



int createSem(const char* semName)
{
   int ret = -1;
   sem_t * sem;
   if(semName != NULL)
   {
      sem = sem_open(semName, O_CREAT, 0644, 0);
      if(sem != SEM_FAILED)
      {
         ret = 0;
      }
   }
   return ret;
}

START_TEST(test_RemoveSem)
{
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   int i=0;
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_RemoveSem"));

   // change to an wrong plugin configuration file using environment variable
   setenv(envVariable, "/etc/pclCustomLibConfigFileWrongDefault.cfg", 1);
   static const char* semFiles[] = {   "_Data_mnt_c_lt_persistence_client_library_test_cached_itz-sem",
                                       "_Data_mnt_c_lt_persistence_client_library_test_configurable_default_data_itz-sem",
                                       "_Data_mnt_c_lt_persistence_client_library_test_default_data_itz-sem",
                                       "_Data_mnt_wt_lt_persistence_client_library_test_resource_table_cfg_itz-sem",
                                       "_Data_mnt_wt_lt_persistence_client_library_test_wt_itz-sem",
                                        NULL };

   while(semFiles[i] != NULL)
   {
      fail_unless(createSem(semFiles[i++]) >= 0);
   }

   (void)pclInitLibrary(gTheAppId, shutdownReg);   // after calling init, files created above must be deleted

   i=0;
   while(semFiles[i] != NULL)
   {
      fail_unless(access(semFiles[i++], F_OK) == -1);
   }

   pclDeinitLibrary();

   (void)unsetenv(envVariable);

}
END_TEST




START_TEST(test_AccessRights)
{
   int i=0, ret = -1, fd = -1;
   char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";
   char write2[128] = { 0 };
   char key[128] = { 0 };

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("PCL_TEST test_RemoveSem"));

   // change to an wrong plugin configuration file using environment variable
   setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1);

   (void)pclInitLibrary("helloworldpcl", PCL_SHUTDOWN_TYPE_NONE);   // after calling init, files created above must be deleted

   //write to cache
   for(i=0; i< 300; i++)
   {
      memset(key, 0, 128);
      memset(write2, 0, sizeof((const char*)write2));
      snprintf(key, 128, "Key_in_loop_%d_%d",i,i*i);
      snprintf(write2, 128, "DATA-%d-%d",i,i*i );

      ret = pclKeyWriteData(PCL_LDBID_LOCAL, (const char*)key, 1000, 1, (unsigned char*)write2, (int)strlen((const char*)write2));
      fail_unless(ret == strlen((const char*)write2) , "Wrong write size while inserting key: %d", ret);
   }



   fd = pclFileOpen(PCL_LDBID_LOCAL, "data/rctFileWriteThrough", 1000, 1);
   fail_unless(fd != -1, "Could not open file ==> data/rctFileWriteThrough");

   ret = pclFileWriteData(fd, "Some file data_rctFileWriteThrough", (int)strlen("Some file data_rctFileWriteThrough"));
   fail_unless(ret == (int)strlen("Some file data_rctFileWriteThrough"), "Wrong file size  ==> rctFileWriteThrough: %d", ret);

   ret = pclFileClose(fd);
   fail_unless(ret == 0, "Failed to close file: data/rctFileWriteThrough");



   fd = -1;
   fd = pclFileOpen(PCL_LDBID_LOCAL, "data/rctFileCached", 1000, 1);
   fail_unless(fd != -1, "Could not open file ==> data/rctFileCached");

   ret = pclFileWriteData(fd, "Some file data_rctFileCached", (int)strlen("Some file data_rctFileCached"));
   fail_unless(ret == (int)strlen("Some file data_rctFileCached"), "Wrong file size  ==> rctFileCached: %d", ret);

   ret = pclFileClose(fd);
   fail_unless(ret == 0, "Failed to close file: data/rctFileCached");




   fd = -1;
   fd = pclFileOpen(PCL_LDBID_LOCAL, "data/file1.txt", 1000, 1);
   fail_unless(fd != -1, "Could not open file ==> data/file1");

   ret = pclFileWriteData(fd, "Some file data_file1", (int)strlen("Some file data_file1"));
   fail_unless(ret == (int)strlen("Some file data_file1"), "Wrong file size  ==> file1: %d", ret);

   ret = pclFileClose(fd);
   fail_unless(ret == 0, "Failed to close file: data/file1");



   ret = pclLifecycleSet(PCL_SHUTDOWN);
   fail_unless(ret != EPERS_SHUTDOWN_NO_PERMIT, "Lifecycle set NOT allowed, but should");

   pclDeinitLibrary();

   (void)unsetenv(envVariable);

}
END_TEST





void* pasInstallThread(void* userData)
{
   // install data
   if(system("persadmin_tool install " LOCALSTATEDIR "/PAS_data.tar.gz") == -1)
   {
      printf("#### Failed to install data\n");
   }
   printf("#### Installation of data succeeded\n");

   pthread_exit(0);
}



START_TEST(test_PclInitPasNotAllowed)
{
   pthread_t installThread;
   int rval = -1;
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1);

   if(pthread_create(&installThread, NULL, pasInstallThread, NULL) == -1)
   {
      printf("#### Failed to create install thread\n");
   }
   else
   {
      sleep(1);
      rval = pclInitLibrary(gTheAppId, shutdownReg);
      //printf("#### 1 pclInit: %d\n\n", rval);
      fail_unless(rval == EPERS_NO_REG_TO_PAS, "Should be NOT allowed to register");
      pclDeinitLibrary();


      rval = pclInitLibrary(gTheAppId, shutdownReg);
      //printf("#### 2 pclInit: %d\n\n", rval);
      fail_unless(rval == EPERS_NO_REG_TO_PAS, "Should be NOT allowed to register");
      pclDeinitLibrary();

      rval = pclInitLibrary(gTheAppId, shutdownReg);
      //printf("#### 3 pclInit: %d\n\n", rval);
      fail_unless(rval == EPERS_NO_REG_TO_PAS, "Should be NOT allowed to register");
      pclDeinitLibrary();
   }

   //printf("#### wait for install thread to end\n");
   if(pthread_join(installThread, NULL) != 0)    // wait until thread has ended
      printf("#### pthread_join - FAILED\n");


   // printf("#### Install thread ended\n");
   rval = pclInitLibrary(gTheAppId, shutdownReg);
   printf("#### 5 pclInit: %d\n\n", rval);
   fail_unless(rval == 1, "Should be allowed to register");
   pclDeinitLibrary();

   (void)unsetenv(envVariable);
}
END_TEST



static Suite* persistenceClientLib_suite_multi()
{
   const char* testSuiteName = "\n\nPersistence Client Library (Key-API) - Multi";

   Suite * s  = suite_create(testSuiteName);

#ifdef SKIP_MULTITHREADED_TESTS
   printf("INFO: Skipping testcase MultiThreadedRead  (%p)\n", test_MultiThreadedRead);
   printf("INFO: Skipping testcase MultiThreadedWrite (%p)\n", test_MultiThreadedWrite);
#else
   TCase * tc_MultiThreadedRead = tcase_create("MultiThreadedRead");
   tcase_add_test(tc_MultiThreadedRead, test_MultiThreadedRead);
   tcase_set_timeout(tc_MultiThreadedRead, 20);

   TCase * tc_MultiThreadedWrite = tcase_create("MultiThreadedWrite");
   tcase_add_test(tc_MultiThreadedWrite, test_MultiThreadedWrite);
   tcase_set_timeout(tc_MultiThreadedWrite, 20);
#endif

#ifndef SKIP_MULTITHREADED_TESTS
   suite_add_tcase(s, tc_MultiThreadedRead);
   tcase_add_checked_fixture(tc_MultiThreadedRead, data_setup, data_teardown);

   suite_add_tcase(s, tc_MultiThreadedWrite);
   tcase_add_checked_fixture(tc_MultiThreadedWrite, data_setup, data_teardown);

#endif

   return s;

}

static Suite * persistenceClientLib_suite()
{
   const char* testSuiteName = "\n\nPersistence Client Library (Key-API)";

   Suite * s  = suite_create(testSuiteName);

   TCase * tc_persGetData = tcase_create("GetData");
   tcase_add_test(tc_persGetData, test_GetData);
   tcase_set_timeout(tc_persGetData, 3);

   TCase * tc_persSetData = tcase_create("SetData");
   tcase_add_test(tc_persSetData, test_SetData);
   tcase_set_timeout(tc_persSetData, 3);

   TCase * tc_persSetDataNoPRCT = tcase_create("SetDataNoPRCT");
   tcase_add_test(tc_persSetDataNoPRCT, test_SetDataNoPRCT);
   tcase_set_timeout(tc_persSetDataNoPRCT, 3);

   TCase * tc_persGetDataSize = tcase_create("GetDataSize");
   tcase_add_test(tc_persGetDataSize, test_GetDataSize);
   tcase_set_timeout(tc_persGetDataSize, 3);

   TCase * tc_persDeleteData = tcase_create("DeleteData");
   tcase_add_test(tc_persDeleteData, test_DeleteData);
   tcase_set_timeout(tc_persDeleteData, 3);

   TCase * tc_persGetDataHandle = tcase_create("GetDataHandle");
   tcase_add_test(tc_persGetDataHandle, test_GetDataHandle);
   tcase_set_timeout(tc_persGetDataHandle, 3);

   TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen");
   tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen);
   tcase_set_timeout(tc_persDataHandleOpen, 3);

   TCase * tc_Plugin = tcase_create("Plugin");
   tcase_add_test(tc_Plugin, test_Plugin);
   tcase_set_timeout(tc_Plugin, 3);

   TCase * tc_ReadDefault = tcase_create("ReadDefault");
   tcase_add_test(tc_ReadDefault, test_ReadDefault);
   tcase_set_timeout(tc_ReadDefault, 3);

   TCase * tc_ReadConfDefault = tcase_create("ReadConfDefault");
   tcase_add_test(tc_ReadConfDefault, test_ReadConfDefault);
   tcase_set_timeout(tc_ReadConfDefault, 3);

   TCase * tc_WriteConfDefault = tcase_create("WriteConfDefault");
   tcase_add_test(tc_WriteConfDefault, test_WriteConfDefault);
   tcase_set_timeout(tc_WriteConfDefault, 3);

   TCase * tc_InitDeinit = tcase_create("InitDeinit");
   tcase_add_test(tc_InitDeinit, test_InitDeinit);
   tcase_set_timeout(tc_InitDeinit, 3);

   TCase * tc_NegHandle = tcase_create("NegHandle");
   tcase_add_test(tc_NegHandle, test_NegHandle);
   tcase_set_timeout(tc_NegHandle, 3);

   TCase * tc_utf8_string = tcase_create("UTF-8");
   tcase_add_test(tc_utf8_string, test_utf8_string);
   tcase_set_timeout(tc_utf8_string, 3);

   TCase * tc_Notifications = tcase_create("Notifications");
   tcase_add_test(tc_Notifications, test_Notifications);
   tcase_set_timeout(tc_Notifications, 3);

#if USE_APPCHECK
   TCase * tc_ValidApplication = tcase_create("ValidApplication");
   tcase_add_test(tc_ValidApplication, test_ValidApplication);
   tcase_set_timeout(tc_ValidApplication, 3);
#endif

   TCase * tc_PAS_DbusInterface = tcase_create("PAS_DbusInterface");
   tcase_add_test(tc_PAS_DbusInterface, test_PAS_DbusInterface);
   tcase_set_timeout(tc_PAS_DbusInterface, 3);

   TCase * tc_LC_DbusInterface = tcase_create("LC_DbusInterface");
   tcase_add_test(tc_LC_DbusInterface, test_LC_DbusInterface);
   tcase_set_timeout(tc_LC_DbusInterface, 3);

   TCase * tc_SharedAccess = tcase_create("SharedAccess");
   tcase_add_test(tc_SharedAccess, test_SharedAccess);
   tcase_set_timeout(tc_SharedAccess, 3);

   TCase * tc_VO722 = tcase_create("VO722");
   tcase_add_test(tc_VO722, test_VO722);
   tcase_set_timeout(tc_VO722, 5);

   TCase * tc_NoRct = tcase_create("NoRct");
   tcase_add_test(tc_NoRct, test_NoRct);
   tcase_set_timeout(tc_NoRct, 3);

   TCase * tc_NoPluginFunc = tcase_create("NoPluginFunc");
   tcase_add_test(tc_NoPluginFunc, test_NoPluginFunc);

   TCase * tc_InvalidPluginfConf = tcase_create("InvalidPluginfConf");
   tcase_add_test(tc_InvalidPluginfConf, test_InvalidPluginfConf);

   TCase * tc_SharedData = tcase_create("SharedData");
   tcase_add_test(tc_SharedData, test_SharedData);
   tcase_set_timeout(tc_SharedData, 10);

   TCase * tc_RemoveSem = tcase_create("RemoveSem");
   tcase_add_test(tc_RemoveSem, test_RemoveSem);
   tcase_set_timeout(tc_RemoveSem, 12);


   TCase * tc_AccessRights = tcase_create("AccessRights");
   tcase_add_test(tc_AccessRights, test_AccessRights);

   TCase * tc_PclInitPasNotAllowed = tcase_create("PclInitPasNotAllowed");
   tcase_add_test(tc_PclInitPasNotAllowed, test_PclInitPasNotAllowed);
   tcase_set_timeout(tc_PclInitPasNotAllowed, 20);

#if 1
   suite_add_tcase(s, tc_NoPluginFunc);

   suite_add_tcase(s, tc_persSetData);
   tcase_add_checked_fixture(tc_persSetData, data_setup, data_teardown);

   suite_add_tcase(s, tc_persGetData);
   tcase_add_checked_fixture(tc_persGetData, data_setup, data_teardown);

   suite_add_tcase(s, tc_persGetDataHandle);
   tcase_add_checked_fixture(tc_persGetDataHandle, data_setup, data_teardown);

   suite_add_tcase(s, tc_persSetDataNoPRCT);
   tcase_add_checked_fixture(tc_persSetDataNoPRCT, data_setup, data_teardown);

   suite_add_tcase(s, tc_persGetDataSize);
   tcase_add_checked_fixture(tc_persGetDataSize, data_setup, data_teardown);

   suite_add_tcase(s, tc_persDeleteData);
   tcase_add_checked_fixture(tc_persDeleteData, data_setup, data_teardown);

   suite_add_tcase(s, tc_persDataHandleOpen);
   tcase_add_checked_fixture(tc_persDataHandleOpen, data_setup, data_teardown);

   suite_add_tcase(s, tc_ReadDefault);
   tcase_add_checked_fixture(tc_ReadDefault, data_setup, data_teardown);

   suite_add_tcase(s, tc_ReadConfDefault);
   tcase_add_checked_fixture(tc_ReadConfDefault, data_setup, data_teardown);

   suite_add_tcase(s, tc_WriteConfDefault);
   tcase_add_checked_fixture(tc_WriteConfDefault, data_setup, data_teardown);

   suite_add_tcase(s, tc_NegHandle);
   tcase_add_checked_fixture(tc_NegHandle, data_setup, data_teardown);

   suite_add_tcase(s, tc_utf8_string);
   tcase_add_checked_fixture(tc_utf8_string, data_setup, data_teardown);

   suite_add_tcase(s, tc_Notifications);
   tcase_add_checked_fixture(tc_Notifications, data_setup, data_teardown);

   suite_add_tcase(s, tc_Plugin);
   tcase_add_checked_fixture(tc_Plugin, data_setup, data_teardown);

   suite_add_tcase(s, tc_SharedAccess);

   suite_add_tcase(s, tc_VO722);

   suite_add_tcase(s, tc_InvalidPluginfConf);

   suite_add_tcase(s, tc_NoRct);
   tcase_add_checked_fixture(tc_NoRct, data_setup_norct, data_teardown);

   suite_add_tcase(s, tc_InitDeinit);

   suite_add_tcase(s, tc_SharedData);
   tcase_add_checked_fixture(tc_SharedData, data_setup, data_teardown);

   suite_add_tcase(s, tc_RemoveSem);

   suite_add_tcase(s, tc_PclInitPasNotAllowed);    // NOTE: make sure this test is run as the last test

#else

   //suite_add_tcase(s, tc_AccessRights);
   suite_add_tcase(s, tc_PclInitPasNotAllowed);    // NOTE: make sure this test is run as the last test

#endif

#if USE_APPCHECK
   //suite_add_tcase(s, tc_ValidApplication);
#endif



#if 0
   suite_add_tcase(s, tc_PAS_DbusInterface);
   tcase_add_checked_fixture(tc_PAS_DbusInterface, data_setup, data_teardown);
   tcase_set_timeout(tc_PAS_DbusInterface, 10);

   suite_add_tcase(s, tc_LC_DbusInterface);
   tcase_add_checked_fixture(tc_LC_DbusInterface, data_setup, data_teardown);
   tcase_set_timeout(tc_LC_DbusInterface, 8);
#endif

   return s;
}





static void doFileCopy(const char* src, const char* dst)
{
   if(src != NULL && dst != NULL)
   {
      int srcFd = -1, dstFd = -1;

      srcFd = open(src, O_RDWR);
      if(srcFd != -1)
      {
         struct stat buf;
         memset(&buf, 0, sizeof(buf));

         if(fstat(srcFd, &buf) != -1)
         {
            dstFd = open(dst, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
            if((int)sendfile(dstFd, srcFd, 0, (size_t)buf.st_size) == -1)
            {
               printf("Failed to copy file: %s\n", strerror(errno));
            }
            close(dstFd);
         }
         close(srcFd);
      }
   }
}


START_TEST(test_CrashingApp)
{

   int ret = 0, rval = -1, i = 0;
   char key[128] = {0};
   char writeData[128] = {0};
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1);

   //Cleaning up
   remove("/dev/shm/_tmp_attachToExistingCacheFragment_db-cache");
   remove("/dev/shm/_tmp_attachToExistingCacheFragment_db-ht");
   remove("/dev/shm/_tmp_attachToExistingCacheFragment_db-shm-info");

   // backup original database
   doFileCopy("/Data/mnt-c/lt-persistence_client_library_test/cached.itz",
              "/Data/mnt-c/lt-persistence_client_library_test/cached_backup.itz");

   remove("/Data/mnt-c/lt-persistence_client_library_test/cached.itz");
   sync();

   //
   // populate database with some new data
   //
   pclInitLibrary(gTheAppId, PCL_SHUTDOWN_TYPE_NONE);

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "Key_A", 2, 2, (unsigned char*)"some_Key_A_data", (int)strlen("some_Key_A_data"));
   fail_unless(ret == strlen("some_Key_A_data"), "Wrong write size");

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "Key_BB", 2, 2, (unsigned char*)"some_Key_BB_data", (int)strlen("some_Key_BB_data"));
   fail_unless(ret == strlen("some_Key_BB_data"), "Wrong write size");

   ret = pclKeyWriteData(PCL_LDBID_LOCAL, "Key_CCC", 2, 2, (unsigned char*)"some_Key_CCC_data", (int)strlen("some_Key_CCC_data"));
   fail_unless(ret == strlen("some_Key_CCC_data"), "Wrong write size");

   // now write back data from cache to non volatile memory device
   rval = pclLifecycleSet(PCL_SHUTDOWN);
   fail_unless(rval == 0, "failed pclLifecycleSet: %d", rval);

   pclDeinitLibrary();

   //
   // write some data to cache, and after writing to cache let the test crash to make sure data
   // will not be written back to non volatile memory and the data remains only in cache
   //
   pclInitLibrary(gTheAppId, PCL_SHUTDOWN_TYPE_NONE);

   // write new keys to cache
   for(i=0; i< 400; i++)
   {
      memset(key, 0, 128);
      memset(writeData, 0, 128);
      snprintf(key, 128, "Key_in_loop_%d_%d_cache",i,i*i);
      snprintf(writeData, 128, "DATA-%d_cache",i);

      ret = pclKeyWriteData(PCL_LDBID_LOCAL, key, 3, 3, (unsigned char*)writeData, (int)strlen(writeData));
      fail_unless(ret == strlen(writeData), "Wrong write size");
   }

   // now cause a crash (SIGILL) is simulated ==> next test process (test_RestartedApp) tries to read data from cache and from database file
   raise(SIGILL);
}
END_TEST



START_TEST(test_RestartedApp)
{
   int ret = 0, rval = -1, i = 0;
   char key[128] = {0};
   char writeData[128] = {0};
   char readData[128] = {0};
   const char* envVariable = "PERS_CLIENT_LIB_CUSTOM_LOAD";

   setenv(envVariable, "/etc/pclCustomLibConfigFileTest.cfg", 1);

   //
   // check if all temporary files are available from presious crashed test
   //
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-cache", F_OK)    == 0);
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-ht", F_OK)       == 0);
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-shm-info", F_OK) == 0);


   pclInitLibrary(gTheAppId, PCL_SHUTDOWN_TYPE_NONE);

   //
   // try to read data from database file
   //
   ret = pclKeyReadData(PCL_LDBID_LOCAL, "Key_A", 2, 2, (unsigned char*)readData, 128);
   //printf("Key: - %s - | Data: - %s - \n", key, readData);
   fail_unless(strncmp((char*)readData, "some_Key_A_data", strlen("some_Key_A_data")) == 0, "Wrong data read - file 1 - %d", ret);
   fail_unless(ret == strlen("some_Key_A_data"), "Wrong size - file 1 - %d", ret);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "Key_BB", 2, 2, (unsigned char*)readData, 128);
   fail_unless(strncmp((char*)readData, "some_Key_BB_data", strlen("some_Key_BB_data")) == 0, "Wrong data read - file 2 - %d", ret);
   fail_unless(ret == strlen("some_Key_BB_data"), "Wrong size - file 2 - %d", ret);

   ret = pclKeyReadData(PCL_LDBID_LOCAL, "Key_CCC", 2, 2, (unsigned char*)readData, 128);
   fail_unless(strncmp((char*)readData, "some_Key_CCC_data", strlen("some_Key_CCC_data")) == 0, "Wrong data read - file 3 - %d", ret);
   fail_unless(ret == strlen("some_Key_CCC_data"), "Wrong size - file 2 - %d", ret);


   //
   // now try to read data from cache which has been written there by the previous test (test_CrashingApp)
   //
   for(i=0; i< 400; i++)
   {
      memset(key, 0, 128);
      memset(readData, 0, 128);
      memset(writeData, 0, 128);
      snprintf(key, 128, "Key_in_loop_%d_%d_cache",i,i*i);
      snprintf(writeData, 128, "DATA-%d_cache",i); // reference data that should be read

      ret = pclKeyReadData(PCL_LDBID_LOCAL, key, 3, 3, (unsigned char*)readData, 128);
      //printf("Key: - %s - | Data: - %s - \n", key, readData);
      fail_unless(strncmp((char*)readData, writeData, strlen(writeData)) == 0, "Wrong data read - cache - %d", i);
      fail_unless(ret == strlen(writeData), "Wrong size - cache - %d", i);
   }


   //
   // now add some new keys
   //
   for(i=0; i< 400; i++)
   {
      memset(key, 0, 128);
      memset(writeData, 0, 128);
      snprintf(key, 128, "new_Key_in_loop_%d_%d_cache",i,i*i);
      snprintf(writeData, 128, "new_DATA-%d_cache",i);

      ret = pclKeyWriteData(PCL_LDBID_LOCAL, key, 4, 4, (unsigned char*)writeData, (int)strlen(writeData));
      fail_unless(ret == strlen(writeData), "Wrong write size");
   }

   // now write back data from cache to non volatile memory device
   rval = pclLifecycleSet(PCL_SHUTDOWN);
   fail_unless(rval == 0, "failed pclLifecycleSet");

   pclDeinitLibrary();


   //
   // check if all temporary files were removed
   //
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-cache", F_OK)    == -1);
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-ht", F_OK)       == -1);
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-shm-info", F_OK) == -1);


   //
   //try to read all keys in database
   //
   pclInitLibrary(gTheAppId, PCL_SHUTDOWN_TYPE_NONE);

   //
   // read keys previously in cache
   //
   for(i=0; i< 400; i++)
   {
      memset(key, 0, 128);
      memset(readData, 0, 128);
      memset(writeData, 0, 128);
      snprintf(key, 128, "Key_in_loop_%d_%d_cache",i,i*i);
      snprintf(writeData, 128, "DATA-%d_cache",i); // reference data that should be read

      ret = pclKeyReadData(PCL_LDBID_LOCAL, key, 3, 3, (unsigned char*)readData, 128);
      //printf("Key: - %s - | Data: - %s - \n", key, readData);
      fail_unless(strncmp((char*)readData, writeData, strlen(writeData)) == 0, "Wrong data read - cache - %d", i);
      fail_unless(ret == strlen(writeData), "Wrong size - cache - %d", i);
   }


   //
   // read newly added keys
   //
   for(i=0; i< 400; i++)
   {
      memset(key, 0, 128);
      memset(readData, 0, 128);
      memset(writeData, 0, 128);
      snprintf(key, 128, "new_Key_in_loop_%d_%d_cache",i,i*i);
      snprintf(writeData, 128, "new_DATA-%d_cache",i); // reference data that should be read

      ret = pclKeyReadData(PCL_LDBID_LOCAL, key, 4, 4, (unsigned char*)readData, 128);
      //printf("Key: - %s - | Data: - %s - \n", key, readData);
      fail_unless(strncmp((char*)readData, writeData, strlen(writeData)) == 0, "Wrong data read - cache - %d", i);
      fail_unless(ret == strlen(writeData), "Wrong size - cache - %d", i);
   }


   rval = pclLifecycleSet(PCL_SHUTDOWN);
   fail_unless(rval == 0, "failed pclLifecycleSet");

   pclDeinitLibrary();


   //
   // check if all temporary files were removed
   //
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-cache", F_OK)    == -1);
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-ht", F_OK)       == -1);
   fail_unless(access("/dev/shm/_Data_mnt_c_lt_persistence_client_library_test_cached_itz-shm-info", F_OK) == -1);


   // recover original database
   doFileCopy("/Data/mnt-c/lt-persistence_client_library_test/cached_backup.itz",
              "/Data/mnt-c/lt-persistence_client_library_test/cached.itz");

   remove("/Data/mnt-c/lt-persistence_client_library_test/cached_backup.itz");

   sync();
}
END_TEST




static Suite * persistenceClientLib_suite_appcrash()
{
   const char* testSuiteName = "\n\nPersistence Client Library (App crash)";

   Suite * s  = suite_create(testSuiteName);

   TCase* tc_CrashingApp = tcase_create("CrashingApp");
   tcase_set_timeout(tc_CrashingApp, 25);
   tcase_add_test_raise_signal(tc_CrashingApp, test_CrashingApp, SIGILL);


   TCase* tc_RestartedApp = tcase_create("RestartedApp");
   tcase_add_test(tc_RestartedApp, test_RestartedApp);
   tcase_set_timeout(tc_RestartedApp, 25);


   suite_add_tcase(s, tc_CrashingApp);

   suite_add_tcase(s, tc_RestartedApp);

   return s;
}


int main(int argc, char *argv[])
{
   int nr_failed = 0, nr_failed2 = 0, nr_failed3 = 0;
   (void)argv;

   // assign application name
   strncpy(gTheAppId, "lt-persistence_client_library_test", MaxAppNameLen);
   gTheAppId[MaxAppNameLen-1] = '\0';

   /// debug log and trace (DLT) setup
   DLT_REGISTER_APP("PCLTk", "PCL test");

   DLT_REGISTER_CONTEXT(gPcltDLTContext, "PCLt", "Context for PCL testing");

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("Starting PCL test"));


   if(check_environment() != 1)
   {
      printf("\nERROR: Environemnt not setup correclty, won't run tests!\n");
      printf("         Please fix issues reported above.\n\n");
      return EXIT_FAILURE;
   }

   if(argc >= 2)
   {
      printf("Running concurrency tests\n");

      run_concurrency_test();
   }
   else
   {
      Suite * sPcl = persistenceClientLib_suite();
      Suite * sPclMulti = persistenceClientLib_suite_multi();
      Suite * sPclAppCrash = persistenceClientLib_suite_appcrash();

      SRunner * srPCL = srunner_create(sPcl);
      SRunner * srPCLMulti = srunner_create(sPclMulti);
      SRunner * srPCLAppCrash = srunner_create(sPclAppCrash);

      srunner_set_fork_status(srPCL, CK_FORK);
      srunner_set_xml(srPCL, "/tmp/persistenceClientLibraryTest.xml");
      srunner_set_log(srPCL, "/tmp/persistenceClientLibraryTest.log");

      srunner_set_fork_status(srPCLMulti, CK_NOFORK);
      srunner_set_xml(srPCLMulti, "/tmp/persistenceClientLibraryTestMulti.xml");
      srunner_set_log(srPCLMulti, "/tmp/persistenceClientLibraryTestMulti.log");

      srunner_set_fork_status(srPCLAppCrash, CK_FORK);
      srunner_set_xml(srPCLAppCrash, "/tmp/persistenceClientLibraryTestAppCrash.xml");
      srunner_set_log(srPCLAppCrash, "/tmp/persistenceClientLibraryTestAppCrash.log");

      srunner_run_all(srPCL,         CK_VERBOSE);
      srunner_run_all(srPCLMulti,    CK_VERBOSE);
      srunner_run_all(srPCLAppCrash, CK_VERBOSE);

      srunner_ntests_run(srPCL);
      srunner_ntests_run(srPCLMulti);
      srunner_ntests_run(srPCLAppCrash);

      nr_failed  = srunner_ntests_failed(srPCL);
      nr_failed2 = srunner_ntests_failed(srPCLMulti);
      nr_failed3 = srunner_ntests_failed(srPCLAppCrash);

      srunner_free(srPCL);
      srunner_free(srPCLMulti);
      srunner_free(srPCLAppCrash);
   }

   DLT_LOG(gPcltDLTContext, DLT_LOG_INFO, DLT_STRING("End of PCL test"));

   // unregister debug log and trace
   DLT_UNREGISTER_CONTEXT(gPcltDLTContext);
   DLT_UNREGISTER_APP();

   return (0==nr_failed && 0==nr_failed2 && 0==nr_failed3)?EXIT_SUCCESS:EXIT_FAILURE;

}


void do_pcl_concurrency_access(const char* applicationID, const char* resourceID, int operation)
{
   int ret = 0, i = 0;
   size_t bufferSize = 0;
   int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
   unsigned char* buffer = NULL;

   char* writeBuffer = "Pack my box with five dozen liquor jugs. - "
      "Jackdaws love my big sphinx of quartz. - "
      "The five boxing wizards jump quickly. - "
      "How vexingly quick daft zebras jump! - "
      "Bright vixens jump; dozy fowl quack - "
      "Sphinx of black quartz, judge my vow"
      "Voyez le brick géant que j’examine près du wha"
      "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
      "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
      "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
      "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß";

   bufferSize = strlen(writeBuffer);

   buffer = malloc(bufferSize);

   if(buffer != NULL)
   {
      (void)pclInitLibrary(applicationID, shutdownReg);

      for(i=0; i< 10000; i++)
      {
         memset(buffer, 0, bufferSize);

         //printf("[%d] - i: %d \n", operation, i);
         if(operation == 0 )
         {
            ret = pclKeyWriteData(0x20, resourceID,  2, 1,(unsigned char* )writeBuffer, (int)bufferSize);
            if(ret < 0)
               printf("Failed to write data: %d\n", ret);

            ret = pclKeyReadData(0x20, resourceID,  2, 1, buffer, (int)bufferSize);
            if(ret < 0)
            {
               printf("Failed to read data: %d\n", ret);
            }
            else
            {
               if(strncmp((char*)buffer, writeBuffer, (size_t)ret) != 0)
                  printf("Wrong buffer\n");
            }
         }
         else if(operation == 1)
         {
            ret = pclKeyReadData(0x20, resourceID, 2, 1, buffer, (int)bufferSize);
            if(ret < 0)
            {
               printf("Failed to read data: %d\n", ret);
            }
            else
            {
               if(strncmp((char*)buffer, writeBuffer, (size_t)ret) != 0)
                  printf("Wrong buffer\n");
            }
         }
         else
         {
            printf("invalid operation - end!! \n");
            break;
         }

         if(operation == 0)
            usleep(1500);
         else
            usleep(1000);
      }

      free(buffer);

      pclDeinitLibrary();
   }

}


void run_concurrency_test()
{
   const char* appId_one = "lt-persistence_client_library_test";
   const char* appId_two = "concurrency_test";

   int pid = fork();

   if (pid == 0)
   { /*child*/
      printf("Started child process with PID: [%d] \n", pid);

      do_pcl_concurrency_access(appId_one, "links/last_link2", 0); //write

      printf("CHILD exits! \n");

      _exit(EXIT_SUCCESS);
   }
   else if (pid > 0)
   { /*parent*/
      printf("Started father process with PID: [%d] \n", pid);

      do_pcl_concurrency_access(appId_two, "links/last_link2", 1); //read

      printf("PARENT exits! \n");

      _exit(EXIT_SUCCESS);
   }
}



const char* gWriteBuffer =   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste""Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste";

const char* gWriteBuffer2 =   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste""Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - "
   "How vexingly quick daft zebras jump! - "
   "Bright vixens jump; dozy fowl quack - "
   "Sphinx of black quartz, judge my vow"
   "Voyez le brick géant que j’examine près du wha"
   "Zornig und gequält rügen jeweils Pontifex und Volk die maßlose bischöfliche Hybris"
   "Xaver schreibt für Wikipedia zum Spaß quälend lang über Yoga, Soja und Öko"
   "Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark"
   "Fix, Schwyz!“ quäkt Jürgen blöd vom Paß"
   "Welch fieser Katzentyp quält da süße Vögel bloß zum Jux"
   "Die heiße Zypernsonne quälte Max und Victoria ja böse auf dem Weg bis zur Küste"
   "Pack my box with five dozen liquor jugs. - "
   "Jackdaws love my big sphinx of quartz. - "
   "The five boxing wizards jump quickly. - ";