1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
23 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ******************************************************************************/
26 * @file persistence_client_library_test.c
27 * @ingroup Persistence client library test
28 * @author Ingo Huerner
29 * @brief Test of persistence client library
37 #include <unistd.h> /* exit */
43 #include "../include/persistence_client_library_key.h"
44 #include "../include/persistence_client_library_file.h"
45 #include "../include/persistence_client_library_error_def.h"
47 // internal header, should normally not included in any application
48 // only for testing the cursor functionality
49 #include "../src/persistence_client_library_data_access.h"
53 #define NUM_OF_FILES 3
54 #define READ_SIZE 1024
57 char* dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
61 START_TEST (test_GetData)
64 unsigned char buffer[READ_SIZE];
66 memset(buffer, 0, READ_SIZE);
67 ret = key_read_data(0xFF, "language/country_code", 0, 0, buffer, READ_SIZE);
68 fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle",
69 strlen((char*)buffer)) == 0, "Buffer not correctly read");
70 fail_unless(ret = strlen("Custom plugin -> plugin_get_data_handle"));
72 memset(buffer, 0, READ_SIZE);
73 ret = key_read_data(0xFF, "pos/last_position", 0, 0, buffer, READ_SIZE);
74 fail_unless(strncmp((char*)buffer, "CACHE_ +48° 10' 38.95\", +8° 44' 39.06\"",
75 strlen((char*)buffer)) == 0, "Buffer not correctly read");
76 fail_unless(ret = strlen("CACHE_ +48° 10' 38.95\", +8° 44' 39.06\""));
78 memset(buffer, 0, READ_SIZE);
79 ret = key_read_data(0, "language/current_language", 3, 0, buffer, READ_SIZE);
80 printf("Buffer: %s \n", buffer);
81 fail_unless(strncmp((char*)buffer, "CACHE_ Kisuaheli", strlen((char*)buffer)) == 0, "Buffer not correctly read");
83 memset(buffer, 0, READ_SIZE);
84 ret = key_read_data(0xFF, "status/open_document", 3, 2, buffer, READ_SIZE);
85 fail_unless(strncmp((char*)buffer, "WT_ /var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0, "Buffer not correctly read");
87 memset(buffer, 0, READ_SIZE);
88 ret = key_read_data(0x20, "address/home_address", 4, 0, buffer, READ_SIZE);
89 fail_unless(strncmp((char*)buffer, "WT_ 55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0, "Buffer not correctly read");
91 memset(buffer, 0, READ_SIZE);
92 ret = key_read_data(0xFF, "pos/last_satellites", 0, 0, buffer, READ_SIZE);
93 fail_unless(strncmp((char*)buffer, "WT_ 17", strlen((char*)buffer)) == 0, "Buffer not correctly read");
95 memset(buffer, 0, READ_SIZE);
96 ret = key_read_data(0x84, "links/last_link", 2, 0, buffer, READ_SIZE);
97 fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/brooklyn", strlen((char*)buffer)) == 0, "Buffer not correctly read");
99 memset(buffer, 0, READ_SIZE);
100 ret = key_read_data(0x84, "links/last_link", 2, 1, buffer, READ_SIZE);
101 fail_unless(strncmp((char*)buffer, "CACHE_ /last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read");
107 START_TEST (test_GetDataHandle)
109 int ret = 0, handle = 0, handle2 = 0, handle3 = 0, handle4 = 0, size = 0;
110 unsigned char buffer[READ_SIZE];
114 char sysTimeBuffer[128];
115 memset(buffer, 0, READ_SIZE);
117 locTime = localtime(&t);
119 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),
120 locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
121 // open handle ---------------------------------------------------
122 handle = key_handle_open(0xFF, "posHandle/last_position", 0, 0);
123 fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last position");
125 ret = key_handle_read_data(handle, buffer, READ_SIZE);
126 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");
128 size = key_handle_get_size(handle);
129 fail_unless(size = strlen("WT_ H A N D L E: +48° 10' 38.95\", +8° 44' 39.06\""));
132 // open handle ---------------------------------------------------
133 handle2 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2);
134 fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
136 size = key_handle_write_data(handle2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
137 fail_unless(size = strlen(sysTimeBuffer));
139 ret = key_handle_close(handle2);
142 // open handle ---------------------------------------------------
143 memset(buffer, 0, READ_SIZE);
144 handle4 = key_handle_open(0xFF, "language/country_code", 0, 0);
145 fail_unless(handle4 >= 0, "Failed to open handle /language/country_code");
147 ret = key_handle_read_data(handle4, buffer, READ_SIZE);
148 fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle", -1) == 0, "Buffer not correctly read");
150 size = key_handle_get_size(handle4);
151 fail_unless(size = strlen("Custom plugin -> plugin_get_data_handle"));
153 ret = key_handle_write_data(handle4, (unsigned char*)"Only dummy implementation behind custom library", READ_SIZE);
156 // open handle ---------------------------------------------------
157 handle3 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2);
158 fail_unless(handle3 >= 0, "Failed to open handle /statusHandle/open_document");
160 ret = key_handle_read_data(handle3, buffer, READ_SIZE);
161 fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
163 size = key_handle_get_size(handle3);
164 fail_unless(size = strlen(sysTimeBuffer));
167 ret = key_handle_close(handle);
168 ret = key_handle_close(handle3);
169 ret = key_handle_close(handle4);
177 START_TEST(test_SetData)
180 unsigned char buffer[READ_SIZE];
181 char write1[READ_SIZE];
182 char write2[READ_SIZE];
183 char sysTimeBuffer[256];
188 locTime = localtime(&t);
189 memset(buffer, 0, READ_SIZE);
190 memset(write1, 0, READ_SIZE);
191 memset(write2, 0, READ_SIZE);
194 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),
195 locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
196 ret = key_write_data(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer));
197 fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size");
199 snprintf(write1, 128, "%s %s", "/70", sysTimeBuffer);
200 ret = key_write_data(0xFF, "70", 1, 2, (unsigned char*)write1, strlen(write1));
201 fail_unless(ret == strlen(write1), "Wrong write size");
203 snprintf(write2, 128, "%s %s", "/key_70", sysTimeBuffer);
204 ret = key_write_data(0xFF, "key_70", 1, 2, (unsigned char*)write2, strlen(write2));
205 fail_unless(ret == strlen(write2), "Wrong write size");
207 // read data again and and verify datat has been written correctly
208 memset(buffer, 0, READ_SIZE);
209 ret = key_read_data(0xFF, "69", 1, 2, buffer, READ_SIZE);
210 fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
211 fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size");
213 memset(buffer, 0, READ_SIZE);
214 ret = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE);
215 fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
216 fail_unless(ret == strlen(write1), "Wrong read size");
218 memset(buffer, 0, READ_SIZE);
219 ret = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
220 fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
221 fail_unless(ret == strlen(write2), "Wrong read size");
228 START_TEST(test_GetDataSize)
232 size = key_get_size(0xFF, "status/open_document", 3, 2);
233 fail_unless(size == strlen("WT_ /var/opt/user_manual_climateControl.pdf"), "Invalid size");
235 size = key_get_size(0x84, "links/last_link", 2, 1);
236 fail_unless(size == strlen("CACHE_ /last_exit/queens"), "Invalid size");
242 START_TEST(test_DeleteData)
245 unsigned char buffer[READ_SIZE];
248 rval = key_delete(0xFF, "key_70", 1, 2);
249 fail_unless(rval == 0, "Failed to delete key");
250 // reading from key must fail now
251 rval = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
252 fail_unless(rval == EPERS_NOKEY, "Read form key key_70 works, but should fail");
255 rval = key_delete(0xFF, "70", 1, 2);
256 fail_unless(rval == 0, "Failed to delete key");
257 rval = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE);
258 fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail");
264 START_TEST(test_DataFile)
266 int fd = 0, i = 0, idx = 0;
267 int size = 0, ret = 0;
268 int writeSize = 16*1024;
269 unsigned char buffer[READ_SIZE];
270 const char* refBuffer = "/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media";
272 char* fileMap = NULL;
273 writeBuffer = malloc(writeSize);
276 // fill buffer a sequence
277 for(i = 0; i<(writeSize/8); i++)
279 writeBuffer[idx++] = 'A';
280 writeBuffer[idx++] = 'B';
281 writeBuffer[idx++] = 'C';
282 writeBuffer[idx++] = ' ';
283 writeBuffer[idx++] = 'D';
284 writeBuffer[idx++] = 'E';
285 writeBuffer[idx++] = 'F';
286 writeBuffer[idx++] = ' ';
288 memset(buffer, 0, READ_SIZE);
291 fd = open("/Data/mnt-wt/lt-persistence_client_library_test/user/1/seat/1/media/mediaDBWrite.db",
292 O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
295 // open ----------------------------------------------------------
296 fd = file_open(0xFF, "media/mediaDB.db", 1, 1);
297 fail_unless(fd != -1, "Could not open file ==> /media/mediaDB.db");
299 size = file_get_size(fd);
300 fail_unless(size == 68, "Wrong file size");
302 size = file_read_data(fd, buffer, READ_SIZE);
303 fail_unless(strncmp((char*)buffer, refBuffer, strlen(refBuffer)) == 0, "Buffer not correctly read");
304 fail_unless(size == (strlen(refBuffer)+1), "Wrong size returned"); // strlen + 1 ==> inlcude cr/lf
306 ret = file_close(fd);
307 fail_unless(ret == 0, "Failed to close file");
310 // open ----------------------------------------------------------
311 fd = file_open(0xFF, "media/mediaDBWrite.db", 1, 1);
312 fail_unless(fd != -1, "Could not open file ==> /media/mediaDBWrite.db");
314 size = file_write_data(fd, writeBuffer, strlen(writeBuffer));
315 fail_unless(size == strlen(writeBuffer), "Failed to write data");
317 ret = file_close(fd);
318 fail_unless(ret == 0, "Failed to close file");
321 // remove ----------------------------------------------------------
322 ret = file_remove(0xFF, "media/mediaDBWrite.db", 1, 1);
323 fail_unless(ret == 0, "File can't be removed ==> /media/mediaDBWrite.db");
325 fd = file_open(0xFF, "media/mediaDBWrite.db", 1, 1);
326 fail_unless(fd == -1, "File can be opend, but should not ==> /media/mediaDBWrite.db");
329 // map file ------------------------------------------------------
330 fd = file_open(0xFF, "media/mediaDB.db", 1, 1);
332 size = file_get_size(fd);
333 file_map_data(fileMap, size, 0, fd);
334 fail_unless(fileMap != MAP_FAILED, "Failed to map file");
336 ret = file_unmap_data(fileMap, size);
337 fail_unless(ret != -1, "Failed to unmap file");
340 size = file_get_size(1024);
341 fail_unless(ret == 0, "Got size, but should not");
350 START_TEST(test_DataHandle)
352 int handle1 = 0, handle2 = 0;
356 handle1 = file_open(0xFF, "media/mediaDB.db", 1, 1);
357 fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db");
359 ret = file_close(handle1);
360 fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db");
362 ret = file_close(1024);
363 fail_unless(ret == -1, "Could close file, but should not!!");
365 ret = file_close(17);
366 fail_unless(ret == -1, "Could close file, but should not!!");
370 handle2 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2);
371 fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document");
373 ret = key_handle_close(handle2);
374 fail_unless(ret != -1, "Failed to close handle!!");
376 ret = key_handle_close(1024);
377 fail_unless(ret == -1, "Could close, but should not!!");
383 START_TEST(test_DataHandleOpen)
385 int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0;
387 // open handles ----------------------------------------------------
388 hd1 = key_handle_open(0xFF, "posHandle/last_position1", 0, 0);
389 fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1");
391 hd2 = key_handle_open(0xFF, "posHandle/last_position2", 0, 0);
392 fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2");
394 hd3 = key_handle_open(0xFF, "posHandle/last_position3", 0, 0);
395 fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3");
397 // close handles ---------------------------------------------------
398 ret = key_handle_close(hd1);
399 fail_unless(ret != -1, "Failed to close handle!!");
401 ret = key_handle_close(hd2);
402 fail_unless(ret != -1, "Failed to close handle!!");
404 ret = key_handle_close(hd3);
405 fail_unless(ret != -1, "Failed to close handle!!");
407 // open handles ----------------------------------------------------
408 hd4 = key_handle_open(0xFF, "posHandle/last_position4", 0, 0);
409 fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4");
411 hd5 = key_handle_open(0xFF, "posHandle/last_position5", 0, 0);
412 fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5");
414 hd6 = key_handle_open(0xFF, "posHandle/last_position6", 0, 0);
415 fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6");
417 hd7 = key_handle_open(0xFF, "posHandle/last_position7", 0, 0);
418 fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7");
420 hd8 = key_handle_open(0xFF, "posHandle/last_position8", 0, 0);
421 fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8");
423 hd9 = key_handle_open(0xFF, "posHandle/last_position9", 0, 0);
424 fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9");
426 // close handles ---------------------------------------------------
427 ret = key_handle_close(hd4);
428 fail_unless(ret != -1, "Failed to close handle!!");
430 ret = key_handle_close(hd5);
431 fail_unless(ret != -1, "Failed to close handle!!");
433 ret = key_handle_close(hd6);
434 fail_unless(ret != -1, "Failed to close handle!!");
436 ret = key_handle_close(hd7);
437 fail_unless(ret != -1, "Failed to close handle!!");
439 ret = key_handle_close(hd8);
440 fail_unless(ret != -1, "Failed to close handle!!");
442 ret = key_handle_close(hd9);
443 fail_unless(ret != -1, "Failed to close handle!!");
449 START_TEST(test_Cursor)
451 int handle = -1, rval = 0, size = 0, handle1 = 0;
452 char bufferKeySrc[READ_SIZE];
453 char bufferDataSrc[READ_SIZE];
454 char bufferKeyDst[READ_SIZE];
455 char bufferDataDst[READ_SIZE];
457 memset(bufferKeySrc, 0, READ_SIZE);
458 memset(bufferDataSrc, 0, READ_SIZE);
460 memset(bufferKeyDst, 0, READ_SIZE);
461 memset(bufferDataDst, 0, READ_SIZE);
464 handle = persistence_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/cached.itz",
465 PersistenceStorage_local, PersistencePolicy_wc);
466 printf("Handle : %d \n", handle);
467 fail_unless(handle != -1, "Failed to create cursor!!");
470 handle1 = persistence_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/wt.itz",
471 PersistenceStorage_local, PersistencePolicy_wt);
472 printf("Handle1 : %d \n", handle1);
473 fail_unless(handle1 != -1, "Failed to create cursor!!");
477 memset(bufferKeySrc, 0, READ_SIZE);
478 memset(bufferDataSrc, 0, READ_SIZE);
479 memset(bufferKeyDst, 0, READ_SIZE);
480 memset(bufferDataDst, 0, READ_SIZE);
483 rval = persistence_db_cursor_get_key(handle, bufferKeySrc, 128);
484 fail_unless(rval != -1, "Cursor failed to get key!!");
486 rval = persistence_db_cursor_get_data(handle, bufferDataSrc, 128);
487 fail_unless(rval != -1, "Cursor failed to get data!!");
489 size = persistence_db_cursor_get_data_size(handle);
490 fail_unless(size != -1, "Cursor failed to get size!!");
491 //printf("1. Key: %s | Data: %s » Size: %d \n", bufferKeySrc, bufferDataSrc, size);
494 rval = persistence_db_cursor_get_key(handle1, bufferKeyDst, 128);
495 fail_unless(rval != -1, "Cursor failed to get key!!");
497 rval = persistence_db_cursor_get_data(handle1, bufferDataDst, 128);
498 fail_unless(rval != -1, "Cursor failed to get data!!");
500 size = persistence_db_cursor_get_data_size(handle1);
501 fail_unless(size != -1, "Cursor failed to get size!!");
502 //printf(" 2. Key: %s | Data: %s » Size: %d \n", bufferKeyDst, bufferDataDst, size);
504 while( (persistence_db_cursor_next(handle) == 0) && (persistence_db_cursor_next(handle1) == 0) ); // next cursor
507 rval = persistence_db_cursor_destroy(handle);
508 fail_unless(rval != -1, "Failed to destroy cursor!!");
510 rval = persistence_db_cursor_destroy(handle1);
511 fail_unless(rval != -1, "Failed to destroy cursor!!");
517 static Suite * persistencyClientLib_suite()
519 Suite * s = suite_create("Persistency client library");
521 TCase * tc_persGetData = tcase_create("GetData");
522 tcase_add_test(tc_persGetData, test_GetData);
524 TCase * tc_persSetData = tcase_create("SetData");
525 tcase_add_test(tc_persSetData, test_SetData);
527 TCase * tc_persGetDataSize = tcase_create("GetDataSize");
528 tcase_add_test(tc_persGetDataSize, test_GetDataSize);
530 TCase * tc_persDeleteData = tcase_create("DeleteData");
531 tcase_add_test(tc_persDeleteData, test_DeleteData);
533 TCase * tc_persGetDataHandle = tcase_create("GetDataHandle");
534 tcase_add_test(tc_persGetDataHandle, test_GetDataHandle);
536 TCase * tc_persDataHandle = tcase_create("DataHandle");
537 tcase_add_test(tc_persDataHandle, test_DataHandle);
539 TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen");
540 tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen);
542 TCase * tc_persDataFile = tcase_create("DataFile");
543 tcase_add_test(tc_persDataFile, test_DataFile);
545 TCase * tc_Cursor = tcase_create("Cursor");
546 tcase_add_test(tc_Cursor, test_Cursor);
548 suite_add_tcase(s, tc_persGetData);
549 suite_add_tcase(s, tc_persSetData);
550 suite_add_tcase(s, tc_persGetDataSize);
551 suite_add_tcase(s, tc_persDeleteData);
552 suite_add_tcase(s, tc_persGetDataHandle);
553 suite_add_tcase(s, tc_persDataHandle);
554 suite_add_tcase(s, tc_persDataHandleOpen);
555 suite_add_tcase(s, tc_persDataFile);
556 suite_add_tcase(s, tc_Cursor);
564 int main(int argc, char *argv[])
568 Suite * s = persistencyClientLib_suite();
569 SRunner * sr = srunner_create(s);
570 srunner_run_all(sr, CK_VERBOSE);
571 nr_failed = srunner_ntests_failed(sr);
574 return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;