1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 * This Source Code Form is subject to the terms of the
8 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
9 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
12 * @file persistence_client_library_test.c
13 * @ingroup Persistence client library test
14 * @author Ingo Huerner
15 * @brief Test of persistence client library
23 #include <unistd.h> /* exit */
29 #include "../include/persistence_client_library_key.h"
30 #include "../include/persistence_client_library_file.h"
31 #include "../include/persistence_client_library_error_def.h"
33 // protected header, should be used only be persistence components
34 #include "../include_protected/persistence_client_library_db_access.h"
38 #define NUM_OF_FILES 3
39 #define READ_SIZE 1024
42 char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
46 START_TEST (test_GetData)
49 unsigned char buffer[READ_SIZE];
51 memset(buffer, 0, READ_SIZE);
52 ret = key_read_data(0xFF, "language/country_code", 0, 0, buffer, READ_SIZE);
53 fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle",
54 strlen((char*)buffer)) == 0, "Buffer not correctly read");
55 fail_unless(ret = strlen("Custom plugin -> plugin_get_data_handle"));
57 memset(buffer, 0, READ_SIZE);
58 ret = key_read_data(0xFF, "pos/last_position", 0, 0, buffer, READ_SIZE);
59 fail_unless(strncmp((char*)buffer, "CACHE_ +48° 10' 38.95\", +8° 44' 39.06\"",
60 strlen((char*)buffer)) == 0, "Buffer not correctly read");
61 fail_unless(ret = strlen("CACHE_ +48° 10' 38.95\", +8° 44' 39.06\""));
63 memset(buffer, 0, READ_SIZE);
64 ret = key_read_data(0, "language/current_language", 3, 0, buffer, READ_SIZE);
65 fail_unless(strncmp((char*)buffer, "CACHE_ Kisuaheli", strlen((char*)buffer)) == 0, "Buffer not correctly read");
67 memset(buffer, 0, READ_SIZE);
68 ret = key_read_data(0xFF, "status/open_document", 3, 2, buffer, READ_SIZE);
69 fail_unless(strncmp((char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0, "Buffer not correctly read");
71 memset(buffer, 0, READ_SIZE);
72 ret = key_read_data(0x20, "address/home_address", 4, 0, buffer, READ_SIZE);
73 fail_unless(strncmp((char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0, "Buffer not correctly read");
75 memset(buffer, 0, READ_SIZE);
76 ret = key_read_data(0xFF, "pos/last_satellites", 0, 0, buffer, READ_SIZE);
77 fail_unless(strncmp((char*)buffer, "WT_ 17", strlen((char*)buffer)) == 0, "Buffer not correctly read");
79 memset(buffer, 0, READ_SIZE);
80 ret = key_read_data(0x84, "links/last_link", 2, 0, buffer, READ_SIZE);
81 fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/brooklyn", strlen((char*)buffer)) == 0, "Buffer not correctly read");
83 memset(buffer, 0, READ_SIZE);
84 ret = key_read_data(0x84, "links/last_link", 2, 1, buffer, READ_SIZE);
85 fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read");
91 START_TEST (test_GetDataHandle)
93 int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0;
94 unsigned char buffer[READ_SIZE];
98 char sysTimeBuffer[128];
99 memset(buffer, 0, READ_SIZE);
101 locTime = localtime(&t);
103 snprintf(sysTimeBuffer, 128, "TimeAndData: \"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900),
104 locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
105 // open handle ---------------------------------------------------
106 handle = key_handle_open(0xFF, "posHandle/last_position", 0, 0);
107 fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last_position");
109 ret = key_handle_read_data(handle, buffer, READ_SIZE);
110 fail_unless(strncmp((char*)buffer, "WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\"", ret-1) == 0, "Buffer not correctly read");
112 size = key_handle_get_size(handle);
113 fail_unless(size = strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
116 // open handle ---------------------------------------------------
117 handle2 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2);
118 fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
120 size = key_handle_write_data(handle2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
121 fail_unless(size = strlen(sysTimeBuffer));
123 ret = key_handle_close(handle2);
126 // open handle ---------------------------------------------------
127 memset(buffer, 0, READ_SIZE);
128 handle4 = key_handle_open(0xFF, "language/country_code", 0, 0);
129 fail_unless(handle4 >= 0, "Failed to open handle /language/country_code");
131 ret = key_handle_read_data(handle4, buffer, READ_SIZE);
132 fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle", -1) == 0, "Buffer not correctly read");
134 size = key_handle_get_size(handle4);
135 fail_unless(size = strlen("Custom plugin -> plugin_get_data_handle"));
137 ret = key_handle_write_data(handle4, (unsigned char*)"Only dummy implementation behind custom library", READ_SIZE);
140 // open handle ---------------------------------------------------
141 handle3 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2);
142 fail_unless(handle3 >= 0, "Failed to open handle /statusHandle/open_document");
144 ret = key_handle_read_data(handle3, buffer, READ_SIZE);
145 fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
147 size = key_handle_get_size(handle3);
148 fail_unless(size = strlen(sysTimeBuffer));
151 ret = key_handle_close(handle);
152 ret = key_handle_close(handle3);
153 ret = key_handle_close(handle4);
161 START_TEST(test_SetData)
164 unsigned char buffer[READ_SIZE];
165 char write1[READ_SIZE];
166 char write2[READ_SIZE];
167 char sysTimeBuffer[256];
172 locTime = localtime(&t);
173 memset(buffer, 0, READ_SIZE);
174 memset(write1, 0, READ_SIZE);
175 memset(write2, 0, READ_SIZE);
178 snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900),
179 locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
181 ret = key_write_data(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
182 fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
184 snprintf(write1, 128, "%s %s", "/70", sysTimeBuffer);
185 ret = key_write_data(0xFF, "70", 1, 2, (unsigned char*)write1, strlen(write1));
186 fail_unless(ret == strlen(write1), "Wrong write size");
188 snprintf(write2, 128, "%s %s", "/key_70", sysTimeBuffer);
189 ret = key_write_data(0xFF, "key_70", 1, 2, (unsigned char*)write2, strlen(write2));
190 fail_unless(ret == strlen(write2), "Wrong write size");
192 // read data again and and verify datat has been written correctly
193 memset(buffer, 0, READ_SIZE);
194 ret = key_read_data(0xFF, "69", 1, 2, buffer, READ_SIZE);
195 fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
196 fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
198 memset(buffer, 0, READ_SIZE);
199 ret = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE);
200 fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
201 fail_unless(ret == strlen(write1), "Wrong read size");
203 memset(buffer, 0, READ_SIZE);
204 ret = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
205 fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
206 fail_unless(ret == strlen(write2), "Wrong read size");
213 START_TEST(test_GetDataSize)
217 size = key_get_size(0xFF, "status/open_document", 3, 2);
218 fail_unless(size == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Invalid size");
220 size = key_get_size(0x84, "links/last_link", 2, 1);
221 fail_unless(size == strlen("CACHE_ /last_exit/queens"), "Invalid size");
227 START_TEST(test_DeleteData)
230 unsigned char buffer[READ_SIZE];
233 rval = key_delete(0xFF, "key_70", 1, 2);
234 fail_unless(rval == 0, "Failed to delete key");
235 // reading from key must fail now
236 rval = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
237 fail_unless(rval == EPERS_NOKEY, "Read form key key_70 works, but should fail");
240 rval = key_delete(0xFF, "70", 1, 2);
241 fail_unless(rval == 0, "Failed to delete key");
242 rval = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE);
243 fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail");
249 START_TEST(test_DataFile)
251 int fd = 0, i = 0, idx = 0;
252 int size = 0, ret = 0;
253 int writeSize = 16*1024;
254 unsigned char buffer[READ_SIZE];
255 const char* refBuffer = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media";
257 char* fileMap = NULL;
258 writeBuffer = malloc(writeSize);
261 // fill buffer a sequence
262 for(i = 0; i<(writeSize/8); i++)
264 writeBuffer[idx++] = 'A';
265 writeBuffer[idx++] = 'B';
266 writeBuffer[idx++] = 'C';
267 writeBuffer[idx++] = ' ';
268 writeBuffer[idx++] = 'D';
269 writeBuffer[idx++] = 'E';
270 writeBuffer[idx++] = 'F';
271 writeBuffer[idx++] = ' ';
273 memset(buffer, 0, READ_SIZE);
276 fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",
277 O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
280 // open ----------------------------------------------------------
281 fd = file_open(0xFF, "media/mediaDB.db", 1, 1);
282 fail_unless(fd != -1, "Could not open file ==> /media/mediaDB.db");
284 size = file_get_size(fd);
285 fail_unless(size == 68, "Wrong file size");
287 size = file_read_data(fd, buffer, READ_SIZE);
288 fail_unless(strncmp((char*)buffer, refBuffer, strlen(refBuffer)) == 0, "Buffer not correctly read");
289 fail_unless(size == (strlen(refBuffer)+1), "Wrong size returned"); // strlen + 1 ==> inlcude cr/lf
291 ret = file_close(fd);
292 fail_unless(ret == 0, "Failed to close file");
295 // open ----------------------------------------------------------
296 fd = file_open(0xFF, "media/mediaDBWrite.db", 1, 1);
297 fail_unless(fd != -1, "Could not open file ==> /media/mediaDBWrite.db");
299 size = file_write_data(fd, writeBuffer, strlen(writeBuffer));
300 fail_unless(size == strlen(writeBuffer), "Failed to write data");
302 ret = file_close(fd);
303 fail_unless(ret == 0, "Failed to close file");
306 // remove ----------------------------------------------------------
307 ret = file_remove(0xFF, "media/mediaDBWrite.db", 1, 1);
308 fail_unless(ret == 0, "File can't be removed ==> /media/mediaDBWrite.db");
310 fd = file_open(0xFF, "media/mediaDBWrite.db", 1, 1);
311 fail_unless(fd == -1, "File can be opend, but should not ==> /media/mediaDBWrite.db");
314 // map file ------------------------------------------------------
315 fd = file_open(0xFF, "media/mediaDB.db", 1, 1);
317 size = file_get_size(fd);
318 file_map_data(fileMap, size, 0, fd);
319 fail_unless(fileMap != MAP_FAILED, "Failed to map file");
321 ret = file_unmap_data(fileMap, size);
322 fail_unless(ret != -1, "Failed to unmap file");
325 size = file_get_size(1024);
326 fail_unless(ret == 0, "Got size, but should not");
335 START_TEST(test_DataHandle)
337 int handle1 = 0, handle2 = 0;
341 handle1 = file_open(0xFF, "media/mediaDB.db", 1, 1);
342 fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db");
344 ret = file_close(handle1);
345 fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db");
347 ret = file_close(1024);
348 fail_unless(ret == -1, "Could close file, but should not!!");
350 ret = file_close(17);
351 fail_unless(ret == -1, "Could close file, but should not!!");
355 handle2 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2);
356 fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
358 ret = key_handle_close(handle2);
359 fail_unless(ret != -1, "Failed to close handle!!");
361 ret = key_handle_close(1024);
362 fail_unless(ret == -1, "Could close, but should not!!");
368 START_TEST(test_DataHandleOpen)
370 int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0;
372 // open handles ----------------------------------------------------
373 hd1 = key_handle_open(0xFF, "posHandle/last_position1", 0, 0);
374 fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1");
376 hd2 = key_handle_open(0xFF, "posHandle/last_position2", 0, 0);
377 fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2");
379 hd3 = key_handle_open(0xFF, "posHandle/last_position3", 0, 0);
380 fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3");
382 // close handles ---------------------------------------------------
383 ret = key_handle_close(hd1);
384 fail_unless(ret != -1, "Failed to close handle!!");
386 ret = key_handle_close(hd2);
387 fail_unless(ret != -1, "Failed to close handle!!");
389 ret = key_handle_close(hd3);
390 fail_unless(ret != -1, "Failed to close handle!!");
392 // open handles ----------------------------------------------------
393 hd4 = key_handle_open(0xFF, "posHandle/last_position4", 0, 0);
394 fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4");
396 hd5 = key_handle_open(0xFF, "posHandle/last_position5", 0, 0);
397 fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5");
399 hd6 = key_handle_open(0xFF, "posHandle/last_position6", 0, 0);
400 fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6");
402 hd7 = key_handle_open(0xFF, "posHandle/last_position7", 0, 0);
403 fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7");
405 hd8 = key_handle_open(0xFF, "posHandle/last_position8", 0, 0);
406 fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8");
408 hd9 = key_handle_open(0xFF, "posHandle/last_position9", 0, 0);
409 fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9");
411 // close handles ---------------------------------------------------
412 ret = key_handle_close(hd4);
413 fail_unless(ret != -1, "Failed to close handle!!");
415 ret = key_handle_close(hd5);
416 fail_unless(ret != -1, "Failed to close handle!!");
418 ret = key_handle_close(hd6);
419 fail_unless(ret != -1, "Failed to close handle!!");
421 ret = key_handle_close(hd7);
422 fail_unless(ret != -1, "Failed to close handle!!");
424 ret = key_handle_close(hd8);
425 fail_unless(ret != -1, "Failed to close handle!!");
427 ret = key_handle_close(hd9);
428 fail_unless(ret != -1, "Failed to close handle!!");
434 START_TEST(test_Cursor)
436 int handle = -1, rval = 0, size = 0, handle1 = 0;
437 char bufferKeySrc[READ_SIZE];
438 char bufferDataSrc[READ_SIZE];
439 char bufferKeyDst[READ_SIZE];
440 char bufferDataDst[READ_SIZE];
442 memset(bufferKeySrc, 0, READ_SIZE);
443 memset(bufferDataSrc, 0, READ_SIZE);
445 memset(bufferKeyDst, 0, READ_SIZE);
446 memset(bufferDataDst, 0, READ_SIZE);
449 handle = pers_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/cached.itz");
451 fail_unless(handle != -1, "Failed to create cursor!!");
454 handle1 = pers_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/wt.itz");
456 fail_unless(handle1 != -1, "Failed to create cursor!!");
460 memset(bufferKeySrc, 0, READ_SIZE);
461 memset(bufferDataSrc, 0, READ_SIZE);
462 memset(bufferKeyDst, 0, READ_SIZE);
463 memset(bufferDataDst, 0, READ_SIZE);
466 rval = pers_db_cursor_get_key(handle, bufferKeySrc, 128);
467 fail_unless(rval != -1, "Cursor failed to get key!!");
469 rval = pers_db_cursor_get_data(handle, bufferDataSrc, 128);
470 fail_unless(rval != -1, "Cursor failed to get data!!");
472 size = pers_db_cursor_get_data_size(handle);
473 fail_unless(size != -1, "Cursor failed to get size!!");
474 //printf("1. Key: %s | Data: %s » Size: %d \n", bufferKeySrc, bufferDataSrc, size);
477 rval = pers_db_cursor_get_key(handle1, bufferKeyDst, 128);
478 fail_unless(rval != -1, "Cursor failed to get key!!");
480 rval = pers_db_cursor_get_data(handle1, bufferDataDst, 128);
481 fail_unless(rval != -1, "Cursor failed to get data!!");
483 size = pers_db_cursor_get_data_size(handle1);
484 fail_unless(size != -1, "Cursor failed to get size!!");
485 //printf(" 2. Key: %s | Data: %s » Size: %d \n", bufferKeyDst, bufferDataDst, size);
487 while( (pers_db_cursor_next(handle) == 0) && (pers_db_cursor_next(handle1) == 0) ); // next cursor
490 rval = pers_db_cursor_destroy(handle);
491 fail_unless(rval != -1, "Failed to destroy cursor!!");
493 rval = pers_db_cursor_destroy(handle1);
494 fail_unless(rval != -1, "Failed to destroy cursor!!");
500 static Suite * persistencyClientLib_suite()
502 Suite * s = suite_create("Persistency client library");
504 TCase * tc_persGetData = tcase_create("GetData");
505 tcase_add_test(tc_persGetData, test_GetData);
507 TCase * tc_persSetData = tcase_create("SetData");
508 tcase_add_test(tc_persSetData, test_SetData);
510 TCase * tc_persGetDataSize = tcase_create("GetDataSize");
511 tcase_add_test(tc_persGetDataSize, test_GetDataSize);
513 TCase * tc_persDeleteData = tcase_create("DeleteData");
514 tcase_add_test(tc_persDeleteData, test_DeleteData);
516 TCase * tc_persGetDataHandle = tcase_create("GetDataHandle");
517 tcase_add_test(tc_persGetDataHandle, test_GetDataHandle);
519 TCase * tc_persDataHandle = tcase_create("DataHandle");
520 tcase_add_test(tc_persDataHandle, test_DataHandle);
522 TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen");
523 tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen);
525 TCase * tc_persDataFile = tcase_create("DataFile");
526 tcase_add_test(tc_persDataFile, test_DataFile);
528 TCase * tc_Cursor = tcase_create("Cursor");
529 tcase_add_test(tc_Cursor, test_Cursor);
531 suite_add_tcase(s, tc_persGetData);
532 suite_add_tcase(s, tc_persSetData);
533 suite_add_tcase(s, tc_persGetDataSize);
534 suite_add_tcase(s, tc_persDeleteData);
535 suite_add_tcase(s, tc_persGetDataHandle);
536 suite_add_tcase(s, tc_persDataHandle);
537 suite_add_tcase(s, tc_persDataHandleOpen);
538 suite_add_tcase(s, tc_persDataFile);
539 suite_add_tcase(s, tc_Cursor);
547 int main(int argc, char *argv[])
551 Suite * s = persistencyClientLib_suite();
552 SRunner * sr = srunner_create(s);
553 srunner_run_all(sr, CK_VERBOSE);
554 nr_failed = srunner_ntests_failed(sr);
557 return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;