Updated client library version 0.2.0 (rev 1709), for changes see ChangeLog)
[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[READ_SIZE];
57
58    memset(buffer, 0, READ_SIZE);
59    ret = key_read_data(0xFF, "/language/country_code",         0, 0, buffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/Node/pos/last position"
60    //printf("T E S T  Data - country_code: %s | size: %d \n", buffer, ret);
61    fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle", strlen((char*)buffer)) == 0, "Buffer not correctly read");
62
63    memset(buffer, 0, READ_SIZE);
64    ret = key_read_data(0xFF, "/pos/last_position",         0, 0, buffer, READ_SIZE);   // "/Data/mnt-c/Appl-1/cached.gvdb"             => "/Node/pos/last position"
65    fail_unless(strncmp((char*)buffer, "+48° 10' 38.95\", +8° 44' 39.06\"", ret-1) == 0, "Buffer not correctly read");
66    //printf("T E S T  Data - last_position: %s \n", buffer);
67
68    memset(buffer, 0, READ_SIZE);
69    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"
70    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");
71    //printf("T E S T  Data - current_language: %s \n", buffer);
72
73    memset(buffer, 0, READ_SIZE);
74    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"
75    printf("T E S T  Data - last link: %s \n", buffer);
76    fail_unless(strncmp((char*)buffer, "/var/opt/user_manual_climateControl.pdf", ret-1) == 0, "Buffer not correctly read");
77    //printf("T E S T  Data - open_document: %s \n", buffer);
78
79    memset(buffer, 0, READ_SIZE);
80    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"
81    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");
82    //printf("T E S T  Data - home_address: %s \n", buffer);
83
84    memset(buffer, 0, READ_SIZE);
85    ret = key_read_data(0xFF, "/pos/last satellites",       0, 0, buffer, READ_SIZE);   // "/Data/mnt-wt/Appl-1/wt.gvdb"                => "/Node/pos/last satellites"
86    fail_unless(strncmp((char*)buffer, "17", ret-1) == 0, "Buffer not correctly read");
87    //printf("T E S T  Data - last satellites: %s \n", buffer);
88
89    memset(buffer, 0, READ_SIZE);
90    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"
91    fail_unless(strncmp((char*)buffer, "/last_exit/brooklyn", ret-1) == 0, "Buffer not correctly read");
92    //printf("T E S T  Data - last link: %s \n", buffer);
93
94    memset(buffer, 0, READ_SIZE);
95    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"
96    fail_unless(strncmp((char*)buffer, "/last_exit/queens", ret-1) == 0, "Buffer not correctly read");
97    //printf("T E S T  Data - last link: %s \n", buffer);
98 }
99 END_TEST
100
101
102
103 START_TEST (test_persGetDataHandle)
104 {
105    int ret = 0, handlePos = 0, handleLang = 0, handleStatus = 0, handleHome = 0;
106    unsigned char* buffer = NULL;
107    char sysTimeBuffer[128];
108    buffer = malloc(READ_SIZE);
109
110    struct tm *locTime;
111    time_t t = time(0);
112   
113    locTime = localtime(&t);
114
115    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), 
116                                                                   locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
117
118    memset(buffer, 0, READ_SIZE);
119    handlePos = key_handle_open(0xFF, "/posHandle/last_position", 0, 0);
120    ret = key_handle_read_data(handlePos, buffer, READ_SIZE);
121    fail_unless(strncmp((char*)buffer, "", ret-1) == 0, "Buffer not correctly read");
122    printf("T E S T  handle: %d | Data: %s \n\n", handlePos, buffer);
123
124    memset(buffer, 0, READ_SIZE);
125    handleLang = key_handle_open(0, "/languageHandle/current_language", 3, 0);
126    ret = key_handle_read_data(handleLang, buffer, READ_SIZE);
127    printf("T E S T  handle: %d | Data: %s \n\n", handleLang, buffer);
128
129    memset(buffer, 0, READ_SIZE);
130    handleStatus = key_handle_open(0xFF, "/statusHandle/open_documen", 3, 2);
131    ret = key_handle_read_data(handleStatus, buffer, READ_SIZE);
132    printf("T E S T  handle: %d | Data: %s \n\n", handleStatus, buffer);
133
134    // close handle
135    key_handle_close(handleLang);
136
137    memset(buffer, 0, READ_SIZE);
138    handleHome = key_handle_open(0x20, "/address/home_address", 4, 0);
139    ret = key_handle_read_data(handleHome, buffer, READ_SIZE);
140    printf("T E S T  handle: %d | Data: %s \n\n", handleHome, buffer);
141
142    free(buffer);
143 }
144 END_TEST
145
146
147
148 START_TEST(test_persSetData)
149 {
150    int ret = 0;
151    unsigned char buffer[READ_SIZE];
152    char write1[READ_SIZE];
153    char write2[READ_SIZE];
154    char sysTimeBuffer[256];
155
156    struct tm *locTime;
157    time_t t = time(0);
158
159    locTime = localtime(&t);
160    memset(buffer, 0, READ_SIZE);
161    memset(write1, 0, READ_SIZE);
162    memset(write2, 0, READ_SIZE);
163
164    // write data
165    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),
166                                                                  locTime->tm_hour, locTime->tm_min, locTime->tm_sec);
167    ret = key_write_data(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, READ_SIZE);
168
169    snprintf(write1, 128, "%s %s", "70",  sysTimeBuffer);
170    ret = key_write_data(0xFF, "70", 1, 2, (unsigned char*)write1, READ_SIZE);
171
172    snprintf(write2, 128, "%s %s", "key_70",  sysTimeBuffer);
173    ret = key_write_data(0xFF, "key_70", 1, 2, (unsigned char*)write2, READ_SIZE);
174
175
176    // read data again and and verify datat has been written correctly
177    memset(buffer, 0, READ_SIZE);
178    ret = key_read_data(0xFF, "69", 1, 2, buffer, READ_SIZE);
179    //printf("T E S T  Data 69: %s | read: \"%s\" \n", sysTimeBuffer, buffer);
180    fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read");
181
182    memset(buffer, 0, READ_SIZE);
183    ret = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE);
184    //printf("T E S T  Data 70: %s | read: \"%s\" \n", write1, buffer);
185    fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read");
186
187    memset(buffer, 0, READ_SIZE);
188    ret = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE);
189    //printf("T E S T  Data key_70: %s | read: \"%s\" \n", write2, buffer);
190    fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read");
191 }
192 END_TEST
193
194
195
196 START_TEST(test_persSetSharedData)
197 {
198    int ret = 0;
199    unsigned char* buffer = NULL;
200    buffer = malloc(READ_SIZE);                            
201
202
203    memset(buffer, 0, READ_SIZE);
204    ret = key_write_data(0x20, "/address/home_address",      4, 0, buffer, READ_SIZE);
205    fail_unless(strncmp((char*)buffer, "", ret-1) == 0, "Buffer not correctly read");
206    printf("T E S T  Data: %s \n\n", buffer);
207
208    free(buffer);
209 }
210 END_TEST
211
212
213
214 START_TEST(test_persGetData_file)
215 {
216    int ret = 0;
217    unsigned char* buffer = NULL;
218    buffer = malloc(READ_SIZE);
219
220
221    memset(buffer, 0, READ_SIZE);
222
223    ret = file_open(0xFF, "/media/mediaDB.db", 1, 1);
224    fail_unless(ret != -1, "Could not open file");
225    printf("T E S T  Data: %s \n\n", buffer);
226
227    free(buffer);
228 }
229 END_TEST
230
231
232
233
234 static Suite * persistencyClientLib_suite()
235 {
236    Suite * s  = suite_create("Persistency client library");
237
238    TCase * tc_persGetData = tcase_create("persGetData");
239    tcase_add_test(tc_persGetData, test_persGetData);
240
241    TCase * tc_persGetDataHandle = tcase_create("persGetDataHandle");
242    tcase_add_test(tc_persGetDataHandle, test_persGetDataHandle);
243
244    TCase * tc_persSetData = tcase_create("persSetData");
245    tcase_add_test(tc_persSetData, test_persSetData);
246
247    TCase * tc_persSetSharedData = tcase_create("persSetSharedData");
248    tcase_add_test(tc_persSetSharedData, test_persSetSharedData);
249
250    TCase * tc_persGetData_file = tcase_create("persGetData_file");
251    tcase_add_test(tc_persGetData_file, test_persGetData_file);
252
253    suite_add_tcase(s, tc_persGetData);
254    suite_add_tcase(s, tc_persSetData);
255
256 //   suite_add_tcase(s, tc_persGetDataHandle);
257 //   suite_add_tcase(s, tc_persSetSharedData);
258 //   suite_add_tcase(s, tc_persGetData_file);
259
260    return s;
261 }
262
263
264
265
266 int main(int argc, char *argv[])
267 {
268    int nr_failed;
269
270    Suite * s = persistencyClientLib_suite();
271    SRunner * sr = srunner_create(s);
272    srunner_run_all(sr, CK_NORMAL);
273    nr_failed = srunner_ntests_failed(sr);
274    srunner_free(sr);
275
276    return (0==nr_failed)?EXIT_SUCCESS:EXIT_FAILURE;
277
278 }
279