gwenhywfar 5.12.0
testlib.c
Go to the documentation of this file.
1
2
3#include <gwenhywfar/buffer.h>
4#include <gwenhywfar/base64.h>
5#include <gwenhywfar/debug.h>
6#include <gwenhywfar/padd.h>
7#include <gwenhywfar/cgui.h>
8#include <gwenhywfar/directory.h>
9#include <gwenhywfar/list.h>
10#include <gwenhywfar/pathmanager.h>
11#include <gwenhywfar/gwendate.h>
12#include <gwenhywfar/syncio.h>
13#include <gwenhywfar/json.h>
14#include <gwenhywfar/json_read.h>
15#include <gwenhywfar/json_dump.h>
16#include <errno.h>
17#include "gwenhywfar.h"
18
19
20
21int check1()
22{
23 const char *testString="01234567890123456789";
24 int rv;
25 GWEN_BUFFER *buf1;
26 GWEN_BUFFER *buf2;
27 const char *p1, *p2;
28 int i;
29 int len;
30
31 fprintf(stderr, "Check 1 ...");
32
33 buf1=GWEN_Buffer_new(0, 256, 0, 1);
34 rv=GWEN_Base64_Encode((const unsigned char *)testString,
35 strlen(testString),
36 buf1, 0);
37 if (rv) {
38 fprintf(stderr, "FAILED: Could not encode.\n");
39 return 2;
40 }
41
42 buf2=GWEN_Buffer_new(0, 256, 0, 1);
43 rv=GWEN_Base64_Decode((const unsigned char *)GWEN_Buffer_GetStart(buf1), 0,
44 buf2);
45 if (rv) {
46 fprintf(stderr, "FAILED: Could not decode.\n");
47 return 2;
48 }
49
50 p1=testString;
51 len=strlen(testString);
52 p2=GWEN_Buffer_GetStart(buf2);
53 if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
54 fprintf(stderr, "Data differs in size\n");
55 return 3;
56 }
57 rv=0;
58 for (i=0; i<len; i++) {
59 if (p1[i]!=p2[i]) {
60 fprintf(stderr, "Buffer1:\n%s\n", testString);
61 fprintf(stderr, "Buffer2:\n");
62 GWEN_Buffer_Dump(buf2, 2);
63
64 fprintf(stderr, "Differ at %d (%04x)\n", i, i);
65 rv=-1;
66 }
67 }
68
69 if (rv) {
70 fprintf(stderr, "Data differs in content\n");
71 return 3;
72 }
73
74 fprintf(stderr, "PASSED.\n");
75
76 return 0;
77}
78
79
80
81int test_gui(GWEN_UNUSED int test_with_interaction)
82{
83#if 0
84 char buffer[50];
85 int rv;
87
88 /* Set the static GUI object */
89 assert(gui);
90 GWEN_Gui_SetGui(gui);
92
93 rv = GWEN_Gui_ShowBox(0,
94 "This is a ShowBox test title",
95 "This is a ShowBox test.",
96 0);
97 printf("GWEN_Gui_ShowBox: rv=%d\n", rv);
99 printf("GWEN_Gui_HideBox called.\n\n");
100
101 if (test_with_interaction) {
102 rv = GWEN_Gui_InputBox(0,
103 "This is a InputBox test title",
104 "Just enter something.",
105 buffer,
106 1, 40,
107 0);
108 printf("GWEN_Gui_InputBox: rv=%d, result=\"%s\"\n\n",
109 rv, buffer);
110
111 rv = GWEN_Gui_MessageBox(0,
112 "Third test title, this time MessageBox",
113 "Just press the first or second button.",
114 "First button.", "Second button", NULL,
115 0);
116 printf("GWEN_Gui_MessageBox: rv=%d; button=%s\n", rv,
117 (rv == 1 ? "first" : (rv == 2 ? "second" : "unknown")));
118 }
119
120 GWEN_Gui_free(gui);
121#endif
122 return 0;
123}
124
125
126
127#ifndef MAX_PATH
128# define MAX_PATH 200
129#endif
131{
132 char tmpdir[MAX_PATH];
133 GWEN_DIRECTORY *dir;
134 int rv;
135
137 printf("GWEN_Directory_GetTmpDirectory returns \"%s\" as tmp directory\n",
138 tmpdir);
139
140 dir = GWEN_Directory_new();
141 rv = GWEN_Directory_Open(dir, tmpdir);
142 if (rv) {
143 /* error */
144 printf("Error on GWEN_Directory_Open(\"%s\"): errno=%d: %s\n",
145 tmpdir, errno, strerror(errno));
146 }
147 else {
148 rv = GWEN_Directory_Close(dir);
149 }
151 return rv;
152}
153
154#define ASSERT(expr) if (!(expr)) \
155 { printf("FAILED assertion in " __FILE__ ": %d: " #expr "\n", \
156 __LINE__); return -1; }
158{
159 const char *e1 = "one", *e2 = "two", *e3 = "three";
160 GWEN_LIST *list;
161 GWEN_LIST_ITERATOR *iter;
162
163 list = GWEN_List_new();
164 ASSERT(GWEN_List_GetSize(list) == 0);
165 GWEN_List_PushBack(list, (void *) e2);
166 ASSERT(GWEN_List_GetSize(list) == 1);
167 GWEN_List_PushBack(list, (void *) e3);
168 ASSERT(GWEN_List_GetSize(list) == 2);
169 GWEN_List_PushFront(list, (void *) e1);
170 ASSERT(GWEN_List_GetSize(list) == 3);
171 ASSERT(GWEN_List_GetFront(list) == e1);
172 ASSERT(GWEN_List_GetBack(list) == e3);
173
174 GWEN_List_Remove(list, e2);
175 ASSERT(GWEN_List_GetSize(list) == 2);
176 ASSERT(GWEN_List_GetFront(list) == e1);
177 ASSERT(GWEN_List_GetBack(list) == e3);
178
179 GWEN_List_PopBack(list);
180 ASSERT(GWEN_List_GetSize(list) == 1);
181 ASSERT(GWEN_List_GetFront(list) == e1);
182 ASSERT(GWEN_List_GetBack(list) == e1);
183
184 GWEN_List_PushBack(list, (void *) e2);
185 ASSERT(GWEN_List_GetSize(list) == 2);
186 ASSERT(GWEN_List_GetFront(list) == e1);
187 ASSERT(GWEN_List_GetBack(list) == e2);
188
189 iter = GWEN_List_First(list);
190 ASSERT(GWEN_ListIterator_Data(iter) == e1);
191 ASSERT(GWEN_ListIterator_Next(iter) == e2);
192 ASSERT(GWEN_ListIterator_Data(iter) == e2);
193
195 GWEN_List_Erase(list, iter);
196 ASSERT(GWEN_List_GetSize(list) == 1);
197 ASSERT(GWEN_List_GetFront(list) == e2);
198 ASSERT(GWEN_List_GetBack(list) == e2);
199
200 GWEN_List_Clear(list);
201 ASSERT(GWEN_List_GetSize(list) == 0);
202
203 GWEN_List_free(list);
205 printf("check_list: All tests passed.\n");
206 return 0;
207}
208
210{
211 const char *e1 = "one", *e2 = "two", *e3 = "three";
212 GWEN_CONSTLIST *list;
214
215 list = GWEN_ConstList_new();
216 ASSERT(GWEN_ConstList_GetSize(list) == 0);
217 GWEN_ConstList_PushBack(list, e2);
218 ASSERT(GWEN_ConstList_GetSize(list) == 1);
219 GWEN_ConstList_PushBack(list, e3);
220 ASSERT(GWEN_ConstList_GetSize(list) == 2);
221 GWEN_ConstList_PushFront(list, e1);
222 ASSERT(GWEN_ConstList_GetSize(list) == 3);
223 ASSERT(GWEN_ConstList_GetFront(list) == e1);
224 ASSERT(GWEN_ConstList_GetBack(list) == e3);
225
227 ASSERT(GWEN_ConstList_GetSize(list) == 2);
228 ASSERT(GWEN_ConstList_GetFront(list) == e1);
229 ASSERT(GWEN_ConstList_GetBack(list) == e2);
230
231 GWEN_ConstList_PushBack(list, e3);
232 ASSERT(GWEN_ConstList_GetSize(list) == 3);
233 ASSERT(GWEN_ConstList_GetFront(list) == e1);
234 ASSERT(GWEN_ConstList_GetBack(list) == e3);
235
236 iter = GWEN_ConstList_First(list);
240
242
244 ASSERT(GWEN_ConstList_GetSize(list) == 0);
245
248 printf("check_constlist: All tests passed.\n");
249 return 0;
250}
251
252void *printfunc(const char *s, void *u)
253{
254 const char *pathname = u;
255 printf("Path %s contains: %s\n", pathname, s);
256 return 0;
257}
259{
260 const char *paths[] = { GWEN_PM_SYSCONFDIR
264 , 0
265 };
266 const char **p = paths;
267 for (; *p != 0; ++p) {
268 const char *pathname = *p;
269 GWEN_STRINGLIST *sl =
271 printf("Path %s has %d elements.\n", pathname, GWEN_StringList_Count(sl));
272 GWEN_StringList_ForEach(sl, printfunc, (void *)pathname);
273 }
274 return 0;
275}
276
277
278
280{
281 const char *testString="01234567890123456789";
282 int rv;
283 GWEN_BUFFER *buf1;
284 GWEN_BUFFER *buf2;
285 const char *p1, *p2;
286 int i;
287 int len;
288
289 fprintf(stderr, "Check 2 ...");
290
291 buf1=GWEN_Buffer_new(0, 256, 0, 1);
292 GWEN_Buffer_AppendString(buf1, testString);
293 rv=GWEN_Padd_PaddWithIso9796_2(buf1, 256);
294 if (rv) {
295 fprintf(stderr, "FAILED: Could not padd.\n");
296 return 2;
297 }
298
299 buf2=GWEN_Buffer_new(0, 256, 0, 1);
300 GWEN_Buffer_AppendBuffer(buf2, buf1);
302 if (rv) {
303 fprintf(stderr, "FAILED: Could not unpadd.\n");
304 return 2;
305 }
306
307 p1=testString;
308 len=strlen(testString);
309 p2=GWEN_Buffer_GetStart(buf2);
310 if (GWEN_Buffer_GetUsedBytes(buf2)!=len) {
311 fprintf(stderr, "Data differs in size\n");
312 return 3;
313 }
314 rv=0;
315 for (i=0; i<len; i++) {
316 if (p1[i]!=p2[i]) {
317 fprintf(stderr, "Buffer1:\n%s\n", testString);
318 fprintf(stderr, "Buffer2:\n");
319 GWEN_Buffer_Dump(buf2, 2);
320
321 fprintf(stderr, "Differ at %d (%04x)\n", i, i);
322 rv=-1;
323 }
324 }
325
326 if (rv) {
327 fprintf(stderr, "Data differs in content\n");
328 return 3;
329 }
330
331 fprintf(stderr, "PASSED.\n");
332
333 return 0;
334}
335
336
338{
339 GWEN_DATE *dt1;
340 GWEN_DATE *dt2;
341 time_t tt;
342
344 assert(dt1);
345 tt=GWEN_Date_toLocalTime(dt1);
346
348 if (GWEN_Date_Compare(dt1, dt2)!=0) {
349 fprintf(stderr, "Error: Date doesn't match: dt1: %s dt2: %s\n",
351 return 3;
352 }
353 else {
354 fprintf(stderr, "Date is okay (%s)\n", GWEN_Date_GetString(dt2));
355 }
356
357 return 0;
358}
359
360
361
362int test_json(const char *fname, const char *sSearch)
363{
364 int rv;
365 GWEN_BUFFER *buf;
366 GWEN_JSON_ELEM *je;
367 GWEN_JSON_ELEM *jeSearch;
368
369 buf=GWEN_Buffer_new(0, 256, 0, 1);
370 rv=GWEN_SyncIo_Helper_ReadFile(fname, buf);
371 if (rv<0) {
372 fprintf(stderr, "Error reading JSON file (%d).\n", rv);
373 return 3;
374 }
375
377 if (je==NULL) {
378 fprintf(stderr, "Error parsing JSON file.\n");
379 return 3;
380 }
381 fprintf(stdout, "File successfully read.\n");
382
385 fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(buf));
386
387 jeSearch=GWEN_JsonElement_GetElementByPath(je, sSearch, 0);
388 if (jeSearch) {
389 fprintf(stdout, "Found element %s:\n", sSearch);
391 GWEN_JsonElement_DumpToBuffer(jeSearch, 2, buf);
392 fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(buf));
393 }
394
395 GWEN_Buffer_free(buf);
396 return 0;
397}
398
399
400
401int main(int argc, char **argv)
402{
403 int rv;
404 const char *cmd;
405
406 if (argc>1)
407 cmd=argv[1];
408 else
409 cmd="check";
410
411 if (strcasecmp(cmd, "check")==0) {
412 rv=check1() ||
413 check2() ||
414 test_gui(0) ||
415 check_directory() ||
416 check_list() ||
418 || print_paths()
419 ;
420 }
421 else if (strcasecmp(cmd, "gui")==0) {
422 rv=test_gui(1);
423 }
424 else if (strcasecmp(cmd, "date")==0) {
425 rv=test_date();
426 }
427 else if (strcasecmp(cmd, "json")==0) {
428 if (argc<4) {
429 fprintf(stderr, "args: FILENAME SEARCHPATH.\n");
430 return 1;
431 }
432 rv=test_json(argv[2], argv[3]);
433 }
434 else {
435 fprintf(stderr, "Unknown command \"%s\"\n", cmd);
436 return 1;
437 }
438 return rv;
439}
440
441
int GWEN_Base64_Encode(const unsigned char *src, unsigned int size, GWEN_BUFFER *dst, unsigned int maxLineLength)
Definition base64.c:44
int GWEN_Base64_Decode(const unsigned char *src, unsigned int size, GWEN_BUFFER *dst)
Definition base64.c:133
#define NULL
Definition binreloc.c:300
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition buffer.c:42
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
Definition buffer.c:653
void GWEN_Buffer_Dump(GWEN_BUFFER *bf, unsigned int insert)
Definition buffer.c:585
int GWEN_Buffer_AppendBuffer(GWEN_BUFFER *bf, GWEN_BUFFER *sf)
Definition buffer.c:506
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition buffer.c:89
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition buffer.c:992
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition buffer.c:277
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition buffer.c:235
GWEN_GUI * GWEN_Gui_CGui_new(void)
Definition cgui.c:77
struct GWEN_DIRECTORY GWEN_DIRECTORY
Definition directory.h:41
GWENHYWFAR_API int GWEN_Directory_Open(GWEN_DIRECTORY *d, const char *n)
GWENHYWFAR_API GWEN_DIRECTORY * GWEN_Directory_new(void)
GWENHYWFAR_API void GWEN_Directory_free(GWEN_DIRECTORY *d)
GWENHYWFAR_API int GWEN_Directory_Close(GWEN_DIRECTORY *d)
GWENHYWFAR_API int GWEN_Directory_GetTmpDirectory(char *buffer, unsigned int size)
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition buffer.h:38
void GWEN_Gui_SetGui(GWEN_GUI *gui)
Definition gui.c:170
void GWEN_Gui_free(GWEN_GUI *gui)
Definition gui.c:127
void GWEN_Gui_AddFlags(GWEN_GUI *gui, uint32_t fl)
Definition gui.c:211
GWENHYWFAR_API int GWEN_Gui_InputBox(uint32_t flags, const char *title, const char *text, char *buffer, int minLen, int maxLen, uint32_t guiid)
GWENHYWFAR_API GWEN_DEPRECATED uint32_t GWEN_Gui_ShowBox(uint32_t flags, const char *title, const char *text, uint32_t guiid)
GWENHYWFAR_API int GWEN_Gui_MessageBox(uint32_t flags, const char *title, const char *text, const char *b1, const char *b2, const char *b3, uint32_t guiid)
#define GWEN_GUI_FLAGS_NONINTERACTIVE
Definition gui.h:992
GWENHYWFAR_API GWEN_DEPRECATED void GWEN_Gui_HideBox(uint32_t id)
struct GWEN_GUI GWEN_GUI
Definition gui.h:176
const char * GWEN_Date_GetString(const GWEN_DATE *gd)
Definition gwendate.c:425
int GWEN_Date_Compare(const GWEN_DATE *gd1, const GWEN_DATE *gd0)
Definition gwendate.c:433
GWEN_DATE * GWEN_Date_CurrentDate(void)
Definition gwendate.c:245
time_t GWEN_Date_toLocalTime(const GWEN_DATE *gd)
Definition gwendate.c:202
GWEN_DATE * GWEN_Date_fromLocalTime(time_t t)
Definition gwendate.c:185
struct GWEN_DATE GWEN_DATE
Definition gwendate.h:34
#define GWEN_PM_SYSCONFDIR
Definition gwenhywfar.h:46
#define GWEN_PM_PLUGINDIR
Definition gwenhywfar.h:53
#define GWEN_PM_LIBNAME
Definition gwenhywfar.h:42
#define GWEN_PM_LOCALEDIR
Definition gwenhywfar.h:49
#define GWEN_PM_DATADIR
Definition gwenhywfar.h:56
#define GWEN_UNUSED
GWEN_JSON_ELEM * GWEN_JsonElement_GetElementByPath(GWEN_JSON_ELEM *je, const char *path, uint32_t flags)
Definition json.c:147
struct GWEN_JSON_ELEM GWEN_JSON_ELEM
Definition json.h:33
void GWEN_JsonElement_DumpToBuffer(const GWEN_JSON_ELEM *jeRoot, int indent, GWEN_BUFFER *buf)
Definition json_dump.c:42
GWEN_JSON_ELEM * GWEN_JsonElement_fromString(const char *s)
Definition json_read.c:53
struct GWEN_LIST GWEN_CONSTLIST
Doubly-linked list with const objects.
Definition list.h:64
struct GWEN_LIST GWEN_LIST
Doubly-linked list.
Definition list.h:55
struct GWEN_LIST_ITERATOR GWEN_LIST_ITERATOR
Definition list.h:72
struct GWEN_LIST_ITERATOR GWEN_CONSTLIST_ITERATOR
Definition list.h:76
int GWEN_Padd_PaddWithIso9796_2(GWEN_BUFFER *buf, int dstSize)
Definition padd.c:148
int GWEN_Padd_UnpaddWithIso9796_2(GWEN_BUFFER *buf)
Definition padd.c:196
GWEN_STRINGLIST * GWEN_PathManager_GetPaths(const char *destLib, const char *pathName)
const void * GWEN_ConstListIterator_Data(GWEN_CONSTLIST_ITERATOR *li)
void GWEN_ConstList_PushFront(GWEN_CONSTLIST *l, const void *p)
void GWEN_ConstList_Clear(GWEN_CONSTLIST *l)
void GWEN_ConstList_free(GWEN_CONSTLIST *l)
GWEN_LIST * GWEN_List_new(void)
const void * GWEN_ConstListIterator_Next(GWEN_CONSTLIST_ITERATOR *li)
void GWEN_ConstListIterator_free(GWEN_CONSTLIST_ITERATOR *li)
GWEN_CONSTLIST * GWEN_ConstList_new(void)
const void * GWEN_ConstListIterator_Previous(GWEN_CONSTLIST_ITERATOR *li)
GWEN_LIST_ITERATOR * GWEN_List_First(const GWEN_LIST *l)
const void * GWEN_ConstList_GetFront(const GWEN_CONSTLIST *l)
void GWEN_List_Clear(GWEN_LIST *l)
void GWEN_ListIterator_free(GWEN_LIST_ITERATOR *li)
GWEN_CONSTLIST_ITERATOR * GWEN_ConstList_First(const GWEN_CONSTLIST *l)
void * GWEN_List_GetFront(const GWEN_LIST *l)
void * GWEN_List_GetBack(const GWEN_LIST *l)
void GWEN_List_free(GWEN_LIST *l)
void * GWEN_ListIterator_Next(GWEN_LIST_ITERATOR *li)
void GWEN_ConstList_PopBack(GWEN_CONSTLIST *l)
void GWEN_List_Erase(GWEN_LIST *l, GWEN_LIST_ITERATOR *it)
void * GWEN_ListIterator_Previous(GWEN_LIST_ITERATOR *li)
void GWEN_List_PushFront(GWEN_LIST *l, void *p)
void GWEN_List_Remove(GWEN_LIST *l, const void *p)
void * GWEN_ListIterator_Data(GWEN_LIST_ITERATOR *li)
const void * GWEN_ConstList_GetBack(const GWEN_CONSTLIST *l)
unsigned int GWEN_List_GetSize(const GWEN_LIST *l)
unsigned int GWEN_ConstList_GetSize(const GWEN_CONSTLIST *l)
void GWEN_List_PopBack(GWEN_LIST *l)
void GWEN_ConstList_PushBack(GWEN_CONSTLIST *l, const void *p)
void GWEN_List_PushBack(GWEN_LIST *l, void *p)
unsigned int GWEN_StringList_Count(const GWEN_STRINGLIST *sl)
Definition stringlist.c:427
void * GWEN_StringList_ForEach(const GWEN_STRINGLIST *l, void *(*func)(const char *s, void *u), void *user_data)
Definition stringlist.c:551
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition stringlist.h:56
int GWEN_SyncIo_Helper_ReadFile(const char *fName, GWEN_BUFFER *dbuf)
Definition syncio.c:524
int print_paths()
Definition testlib.c:258
#define ASSERT(expr)
Definition testlib.c:154
int check_constlist()
Definition testlib.c:209
int test_gui(GWEN_UNUSED int test_with_interaction)
Definition testlib.c:81
int main(int argc, char **argv)
Definition testlib.c:401
int check2()
Definition testlib.c:279
int check1()
Definition testlib.c:21
int test_date()
Definition testlib.c:337
int test_json(const char *fname, const char *sSearch)
Definition testlib.c:362
#define MAX_PATH
Definition testlib.c:128
int check_list()
Definition testlib.c:157
int check_directory()
Definition testlib.c:130
void * printfunc(const char *s, void *u)
Definition testlib.c:252