Browse code

webkernel_test: webkernel basic test

Dario Rodriguez authored on 21/06/2014 09:42:38
Showing 1 changed files
... ...
@@ -29,6 +29,8 @@ char *test1(test_action action);
29 29
 char *socklib_connect(test_action action);
30 30
 char *socklib_sselect(test_action action);
31 31
 char *sbuf_memory(test_action action);
32
+char *webkernel_basic(test_action action);
33
+
32 34
 
33 35
 struct {
34 36
         char *(*test)(/* test_action action */);
... ...
@@ -36,6 +38,7 @@ struct {
36 38
            {socklib_connect},
37 39
            {socklib_sselect},
38 40
            {sbuf_memory},
41
+           {webkernel_basic},
39 42
           };
40 43
 
41 44
 int
... ...
@@ -75,6 +78,7 @@ main(int argc, char *argv[])
75 78
         return((totalfail!=0)?1:0);
76 79
 }
77 80
 
81
+/* test1 */
78 82
 char *
79 83
 test1(test_action action)
80 84
 {
... ...
@@ -86,6 +90,7 @@ test1(test_action action)
86 90
         return("ok");
87 91
 }
88 92
 
93
+/* socklib_connect */
89 94
 char *
90 95
 socklib_connect(test_action action)
91 96
 {
... ...
@@ -128,6 +133,7 @@ socklib_connect(test_action action)
128 133
         return(STRING_OK);
129 134
 }
130 135
 
136
+/* socklib_sselect */
131 137
 char *
132 138
 test_socketsinit(int *server, int *client, char **host, long *hostsize, int *port)
133 139
 {
... ...
@@ -275,6 +281,7 @@ socklib_sselect(test_action action)
275 281
         return(STRING_OK);
276 282
 }
277 283
 
284
+/* sbuf_memory */
278 285
 char *
279 286
 sbuf_memory(test_action action)
280 287
 {
... ...
@@ -341,3 +348,132 @@ sbuf_memory(test_action action)
341 348
         return(STRING_OK);
342 349
 }
343 350
 
351
+/* webkernel_basic */
352
+wk_action
353
+webkernel_basichttp(wk *web, int connid, wk_uri *uri, void *userptr)
354
+{
355
+
356
+        *((int *)userptr)=0;
357
+        char reply_ok[]={"Ok"};
358
+        if(strcmp(uri->path,"/test.cgi")==0) {
359
+                *((int *)userptr)=1;
360
+                wk_serve_buffer_as_file(web,connid,reply_ok,sizeof(reply_ok)-1,mime_getdefault("test.txt",NULL));
361
+        }
362
+        return(wkact_finished);
363
+}
364
+
365
+void
366
+webkernel_basicclose(int *client,sselect **ssel, char **host, wk **web)
367
+{
368
+        if(*client!=-1)
369
+                close(*client),*client=-1;
370
+        if(*host!=NULL)
371
+                free(*host),*host=NULL;
372
+        if(*web!=NULL)
373
+                wk_free(*web),*web=NULL;
374
+        if(*ssel!=NULL)
375
+                sselect_free(*ssel),*ssel=NULL;
376
+}
377
+
378
+char *
379
+webkernel_basic(test_action action)
380
+{
381
+        wk *web;
382
+        int port;
383
+        int off;
384
+        int check;
385
+        sselect *ssel;
386
+        int client;
387
+        char *host;
388
+        long hostsize;
389
+        int timeout;
390
+        int fds[2];
391
+        char buf;
392
+        char test_request[]={"\
393
+GET /test.cgi HTTP/1.0\r\n\
394
+Host: localhost\r\n\
395
+Connection: keep-alive\r\n\
396
+\r\n\
397
+"};
398
+        if(action==test_name)
399
+                return("webkernel_basic");
400
+        else if(action==test_description)
401
+                return("webkernel basic usage");
402
+        client=-1;
403
+        ssel=NULL;
404
+        host=NULL;
405
+        web=NULL;
406
+        timeout=100;
407
+        check=0;
408
+        if((ssel=sselect_init())==NULL)
409
+                return(STRING_FAIL ": couln't alloc a sselect structure");
410
+        for(port=19747,off=0,check=0;(web=wk_init(port,ssel,NULL,webkernel_basichttp,NULL,&check))==NULL;off++,port++)
411
+                ;
412
+        if(web==NULL) {
413
+                webkernel_basicclose(&client,&ssel,&host,&web);
414
+                return(STRING_FAIL ": couln't find a free port to init webkernel");
415
+        }
416
+        if((host=ipv4_genip("localhost",&hostsize))==NULL) {
417
+                webkernel_basicclose(&client,&ssel,&host,&web);
418
+                return(STRING_FAIL ": couldn't resove localhost");
419
+        }
420
+        if((client=ipv4_preconnect(host,hostsize,port))==-1) {
421
+                webkernel_basicclose(&client,&ssel,&host,&web);
422
+                return(STRING_FAIL ": couldn't connect to server\n");
423
+        }
424
+        if(ipv4_connect(client,timeout)==-1) {
425
+                webkernel_basicclose(&client,&ssel,&host,&web);
426
+                return(STRING_FAIL ": timeout on connect\n");
427
+        }
428
+        ipv4_postconnect(client);
429
+        /* accept the connection */
430
+        if(sselect_wait(ssel,timeout)!=1) {
431
+                webkernel_basicclose(&client,&ssel,&host,&web);
432
+                return(STRING_FAIL ": timeout on sselect_wait\n");
433
+        }
434
+        wk_service(web);
435
+        if(sselect_getread(ssel,fds,sizeof(fds)/sizeof(fds[0]))==1) {
436
+                webkernel_basicclose(&client,&ssel,&host,&web);
437
+                return(STRING_FAIL ": wk_service hasn't serviced the server connection\n");
438
+        }
439
+        /* client does request into socket */
440
+        write(client,test_request,sizeof(test_request)-1);
441
+        /* socket-> callback_http -> reply_in_wk */
442
+        if(sselect_wait(ssel,timeout)!=1) {
443
+                webkernel_basicclose(&client,&ssel,&host,&web);
444
+                return(STRING_FAIL ": timeout on sselect_wait (after client request)\n");
445
+        }
446
+        wk_service(web);
447
+        if(check!=1) {
448
+                webkernel_basicclose(&client,&ssel,&host,&web);
449
+                return(STRING_FAIL ": http callback was not called\n");
450
+        }
451
+        /* reply_in_wk -> socket */
452
+        if(sselect_wait(ssel,timeout)!=1) {
453
+                webkernel_basicclose(&client,&ssel,&host,&web);
454
+                return(STRING_FAIL ": timeout on sselect_wait (after client request)\n");
455
+        }
456
+        wk_service(web);
457
+        /* client read availability */
458
+        sselect_addread(ssel,client,NULL);
459
+        if(sselect_wait(ssel,timeout)!=1) {
460
+                webkernel_basicclose(&client,&ssel,&host,&web);
461
+                return(STRING_FAIL ": timeout on sselect_wait (to read client reply)\n");
462
+        }
463
+        wk_service(web);
464
+        if(sselect_getread(ssel,fds,sizeof(fds)/sizeof(fds[0]))!=1 || *fds!=client) {
465
+                webkernel_basicclose(&client,&ssel,&host,&web);
466
+                return(STRING_FAIL ": client read not detected\n");
467
+        }
468
+        /* read data */
469
+        if(read(client,buf,2)!=2 || memcmp(buf,"Ok",2)!=0) {
470
+                webkernel_basicclose(&client,&ssel,&host,&web);
471
+                return(STRING_FAIL ": corrupted data in read on client\n");
472
+        }
473
+        close(client),client=-1;
474
+        wk_free(web),web=NULL;
475
+        sselect_free(ssel),ssel=NULL;
476
+        free(host),host=NULL;
477
+        return(STRING_OK);
478
+}
479
+