Updated client library with version 0.1.5 (rev. 1665), for changes see change log
[profile/ivi/persistence-client-library.git] / test / persistence_client_library_test.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
4  * Company         XS Embedded GmbH
5  *****************************************************************************/
6 /******************************************************************************
7    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:
13
14    The above copyright notice and this permission notice shall be included 
15    in all copies or substantial portions of the Software.
16
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 ******************************************************************************/
25  /**
26  * @file           persistence_client_library_test.c
27  * @ingroup        Persistence client library test
28  * @author         Ingo Huerner
29  * @brief          Test of persistence client library
30  * @see            
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <unistd.h>     /* exit */
38 #include <check.h>
39 #include <time.h>
40
41 #include "../include/persistence_client_library_key.h"
42 #include "../include/persistence_client_library_file.h"
43
44
45 #define BUF_SIZE     64
46 #define NUM_OF_FILES 3
47 #define READ_SIZE    1024
48
49 char* dayOfWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
50
51
52
53 START_TEST (test_persGetData)
54 {
55    int ret = 0;
56    unsigned char* buffer = NULL;
57    char sysTimeBuffer[256];
58    buffer = malloc(READ_SIZE);
59
60    struct tm *locTime;
61    time_t t = time(0);
62   
63    locTime = localtime(&t);
64
65    snprintf(sysTimeBuffer, 256, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900),
66                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
67    printf("\n\n");
68
69    printf("<-----------------------------------------------\n");
70    memset(buffer, 0, READ_SIZE);
71    ret = key_read_data(0xFF, "/language/country_code",         0, 0, buffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/Node/pos/last position"
72    printf("T E S T  Data - country_code: %s | size: %d \n", buffer, ret);
73    fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle", strlen((char*)buffer)) == 0, "Buffer not correctly read");
74
75    printf("----------------------------------------------->\n\n");
76
77
78    printf("<-----------------------------------------------\n");
79    memset(buffer, 0, READ_SIZE);
80    ret = key_read_data(0xFF, "/pos/last_position",         0, 0, buffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/Node/pos/last position"
81    fail_unless(strncmp((char*)buffer, "+48° 10' 38.95\", +8° 44' 39.06\"", ret-1) == 0, "Buffer not correctly read");
82    printf("T E S T  Data - last_position: %s \n", buffer);
83    printf("----------------------------------------------->\n\n");
84
85
86    printf("<-----------------------------------------------\n");
87    memset(buffer, 0, READ_SIZE);
88    ret = key_read_data(0,    "/language/current_language", 3, 0, buffer, READ_SIZE);   // "/Data/mnt-wt/Shared/Public/wt.dconf"        => "/User/3/language/current_language"
89    fail_unless(strncmp((char*)buffer, "S H A R E D   D A T A  => not implemented yet", strlen((char*)buffer)) == 0, "Buffer not correctly read");
90    printf("T E S T  Data - current_language: %s \n", buffer);
91    printf("----------------------------------------------->\n\n");
92
93
94    printf("<-----------------------------------------------\n");
95    memset(buffer, 0, READ_SIZE);
96    ret = key_read_data(0xFF, "/status/open_document",      3, 2, buffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/User/3/Seat/2/status/open_document"
97    printf("T E S T  Data - last link: %s \n", buffer);
98    fail_unless(strncmp((char*)buffer, "/var/opt/user_manual_climateControl.pdf", ret-1) == 0, "Buffer not correctly read");
99    printf("T E S T  Data - open_document: %s \n", buffer);
100    printf("----------------------------------------------->\n\n");
101
102
103    printf("<-----------------------------------------------\n");
104    memset(buffer, 0, READ_SIZE);
105    ret = key_read_data(0x20, "/address/home_address",      4, 0, buffer, READ_SIZE);   // "/Data/mnt-c/Shared/Group/20/cached.dconf"   => "/User/4/address/home_address"
106    fail_unless(strncmp((char*)buffer, "S H A R E D   D A T A  => not implemented yet", strlen((char*)buffer)) == 0, "Buffer not correctly read");
107    printf("T E S T  Data - home_address: %s \n", buffer);
108    printf("----------------------------------------------->\n\n");
109
110
111    printf("<-----------------------------------------------\n");
112    memset(buffer, 0, READ_SIZE);
113    ret = key_read_data(0xFF, "/pos/last satellites",       0, 0, buffer, READ_SIZE);   // "/Data/mnt-wt/Appl-1/wt.gvdb"                => "/Node/pos/last satellites"
114    fail_unless(strncmp((char*)buffer, "17", ret-1) == 0, "Buffer not correctly read");
115    printf("T E S T  Data - last satellites: %s \n", buffer);
116    printf("----------------------------------------------->\n\n");
117
118
119    printf("<-----------------------------------------------\n");
120    memset(buffer, 0, READ_SIZE);
121    ret = key_read_data(0x84, "/links/last link",           2, 0, buffer, READ_SIZE);   // "/Data/mnt-wt/Appl-2/wt.gvdb"                => "/84/User/2/links/last link"
122    fail_unless(strncmp((char*)buffer, "/last_exit/brooklyn", ret-1) == 0, "Buffer not correctly read");
123    printf("T E S T  Data - last link: %s \n", buffer);
124    printf("----------------------------------------------->\n\n");
125
126
127    printf("<-----------------------------------------------\n");
128    memset(buffer, 0, READ_SIZE);
129    ret = key_read_data(0x84, "/links/last link",           2, 1, buffer, READ_SIZE);   // "/Data/mnt-wt/Appl-2/wt.gvdb"                => "/84/User/2/links/last link"
130    fail_unless(strncmp((char*)buffer, "/last_exit/queens", ret-1) == 0, "Buffer not correctly read");
131    printf("T E S T  Data - last link: %s \n", buffer);
132    printf("----------------------------------------------->\n\n");
133
134    printf("<-----------------------------------------------\n");
135    memset(buffer, 0, READ_SIZE);
136    //ret = key_write_data(0xFF, "/status/open_document",          1, 2, (unsigned char*)sysTimeBuffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/User/3/Seat/2/status/open_document"
137    printf("T E S T  write data /status/open_document: %s \n", sysTimeBuffer);
138    printf("----------------------------------------------->\n\n");
139
140
141    printf("<-----------------------------------------------\n");
142    memset(buffer, 0, READ_SIZE);
143    ret = key_read_data(0xFF, "/status/open_document",           1, 2, buffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/User/3/Seat/2/status/open_document"
144    printf("T E S T  Data /status/open_document: %s \n", buffer);
145    fail_unless(strncmp((char*)buffer, "empty", strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
146
147    printf("----------------------------------------------->\n\n");
148
149
150    free(buffer);
151 }
152 END_TEST
153
154
155
156 START_TEST (test_persGetDataHandle)
157 {
158    int ret = 0, handlePos = 0, handleLang = 0, handleStatus = 0, handleHome = 0;
159    unsigned char* buffer = NULL;
160    char sysTimeBuffer[128];
161    buffer = malloc(READ_SIZE);
162
163    struct tm *locTime;
164    time_t t = time(0);
165   
166    locTime = localtime(&t);
167
168    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), 
169                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
170    printf("\n\n");
171
172    memset(buffer, 0, READ_SIZE);
173    handlePos = key_handle_open(0xFF, "/posHandle/last_position", 0, 0);
174    ret = key_handle_read_data(handlePos, buffer, READ_SIZE);
175    fail_unless(strncmp((char*)buffer, "", ret-1) == 0, "Buffer not correctly read");
176    printf("T E S T  handle: %d | Data: %s \n\n", handlePos, buffer);
177
178    memset(buffer, 0, READ_SIZE);
179    handleLang = key_handle_open(0, "/languageHandle/current_language", 3, 0);
180    ret = key_handle_read_data(handleLang, buffer, READ_SIZE);
181    printf("T E S T  handle: %d | Data: %s \n\n", handleLang, buffer);
182
183    memset(buffer, 0, READ_SIZE);
184    handleStatus = key_handle_open(0xFF, "/statusHandle/open_documen", 3, 2);
185    ret = key_handle_read_data(handleStatus, buffer, READ_SIZE);
186    printf("T E S T  handle: %d | Data: %s \n\n", handleStatus, buffer);
187
188    // close handle
189    key_handle_close(handleLang);
190
191    memset(buffer, 0, READ_SIZE);
192    handleHome = key_handle_open(0x20, "/address/home_address", 4, 0);
193    ret = key_handle_read_data(handleHome, buffer, READ_SIZE);
194    printf("T E S T  handle: %d | Data: %s \n\n", handleHome, buffer);
195
196    free(buffer);
197 }
198 END_TEST
199
200
201
202 START_TEST(test_persSetData)
203 {
204    unsigned char* buffer = NULL;
205    buffer = malloc(READ_SIZE);                            
206    memset(buffer, 0, READ_SIZE);
207
208
209    free(buffer);
210 }
211 END_TEST
212
213
214
215 START_TEST(test_persSetSharedData)
216 {
217    int ret = 0;
218    unsigned char* buffer = NULL;
219    buffer = malloc(READ_SIZE);                            
220
221
222    memset(buffer, 0, READ_SIZE);
223    ret = key_write_data(0x20, "/address/home_address",      4, 0, buffer, READ_SIZE);
224    fail_unless(strncmp((char*)buffer, "", ret-1) == 0, "Buffer not correctly read");
225    printf("T E S T  Data: %s \n\n", buffer);
226
227    free(buffer);
228 }
229 END_TEST
230
231
232
233 START_TEST(test_persGetData_file)
234 {
235    int ret = 0;
236    unsigned char* buffer = NULL;
237    buffer = malloc(READ_SIZE);
238
239
240    memset(buffer, 0, READ_SIZE);
241
242    ret = file_open(0xFF, "/media/mediaDB.db", 1, 1);
243    fail_unless(ret != -1, "Could not open file");
244    printf("T E S T  Data: %s \n\n", buffer);
245
246    free(buffer);
247 }
248 END_TEST
249
250
251
252
253 static Suite * persistencyClientLib_suite()
254 {
255    Suite * s  = suite_create("Persistency client library");
256
257    TCase * tc_persGetData = tcase_create("persGetData");
258    tcase_add_test(tc_persGetData, test_persGetData);
259
260    TCase * tc_persGetDataHandle = tcase_create("persGetDataHandle");
261    tcase_add_test(tc_persGetDataHandle, test_persGetDataHandle);
262
263    TCase * tc_persSetData = tcase_create("persSetData");
264    tcase_add_test(tc_persSetData, test_persSetData);
265
266    TCase * tc_persSetSharedData = tcase_create("persSetSharedData");
267    tcase_add_test(tc_persSetSharedData, test_persSetSharedData);
268
269    TCase * tc_persGetData_file = tcase_create("persGetData_file");
270    tcase_add_test(tc_persGetData_file, test_persGetData_file);
271
272    suite_add_tcase(s, tc_persGetData);
273    /*suite_add_tcase(s, tc_persGetDataHandle);
274    suite_add_tcase(s, tc_persSetData);
275    suite_add_tcase(s, tc_persSetSharedData);
276    suite_add_tcase(s, tc_persGetData_file);
277 */
278    return s;
279 }
280
281
282
283
284 int main(int argc, char *argv[])
285 {
286    int nr_failed;
287
288    Suite * s = persistencyClientLib_suite();
289    SRunner * sr = srunner_create(s);
290    srunner_run_all(sr, CK_NORMAL);
291    nr_failed = srunner_ntests_failed(sr);
292    srunner_free(sr);
293
294    return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;
295
296 }
297