]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/lib/fwp/tests/fwp_msgtest/fwp_msgtest.c
Rename lib/core to lib/fwp. Clean-ups
[frescor/fwp.git] / fwp / lib / fwp / tests / fwp_msgtest / fwp_msgtest.c
1 /**
2  * \file fwp_msgtest.c
3  *
4  * This is a test application for msg handling that
5  * - allocates two msgb
6  * - serializes two contracts into msgb
7  * - put both msgb to message queue
8  * - revert previous steps 
9  * - compare contracts 
10  */
11 #include "fwp_msgq.h"
12 #include "fwp_msgb.h"
13
14 #include <semaphore.h>
15 #include <stdio.h>
16
17 int main(int argc, char** argv)
18 {
19         struct fwp_msgq msgq;
20         struct fwp_msgb *msgb;
21         int val_w1 = 1;
22         int val_w2 = 2;
23         int val_r1, val_r2;
24         int nparams;
25
26         nparams = argc;
27         fwp_msgq_init(&msgq);   
28         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
29         
30         /* prepare and enqueue the first message buffer */
31         msgb = fwp_msgb_alloc(sizeof(val_w1));
32         if (!msgb) {
33                 printf("Could not allocate msgb for message A\n");
34                 return -1;
35         }else 
36                 printf("Message buffer for message A allocated\n");
37
38         memcpy(msgb->data, &val_w1, sizeof(val_w1)); 
39         printf("Enqueue message A. ");
40         if (!fwp_msgq_enqueue(&msgq, msgb))
41                 printf("OK.\n");
42         else {
43                 printf("Failed! \n");
44                 return -1;
45         }
46         
47         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
48         /* prepare and enqueue the second message buffer */
49         msgb = fwp_msgb_alloc(sizeof(val_w2));
50         if (!msgb) {
51                 printf("Could not allocate msgb for message B\n");
52                 return -1;
53         }else 
54                 printf("Message buffer for message B allocated\n");
55
56         memcpy(msgb->data, &val_w2, sizeof(val_w2)); 
57         printf("Enqueue message B. ");
58         if (!fwp_msgq_enqueue(&msgq, msgb))
59                 printf("OK.\n");
60         else {
61                 printf("Failed! \n");
62                 return -1;
63         }
64         
65         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
66         
67         /* dequeue the first message and compare with original contract*/
68         printf("Dequeue message A. ");
69         msgb = fwp_msgq_dequeue(&msgq);
70         if (!msgb) {
71                 printf("Failed! \n");
72                 return -1;
73         }else
74                 printf("OK.\n");
75
76         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
77         
78         memcpy(&val_r1, msgb->data, sizeof(val_w1)); 
79         fwp_msgb_free(msgb);
80         
81         if (val_w1 != val_r1) {
82                 printf("Message A: W/R values do not match.\n");
83                 return -1;
84         }
85         printf("Message A: W/R values matches.\n");
86         
87
88         /* dequeue the second message and compare with original contract*/
89         printf("Dequeue message B. ");
90         msgb = fwp_msgq_dequeue(&msgq);
91         if (!msgb) {
92                 printf("Failed! \n");
93                 return -1;
94         }else
95                 printf("OK.\n");
96
97         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
98         
99         memcpy(&val_r2, msgb->data, sizeof(val_w2)); 
100         fwp_msgb_free(msgb);
101         
102         if (val_w2 != val_r2) {
103                 printf("Message B: W/R values do not match.\n");
104                 return -1;
105         }
106         printf("Message B: W/R values matches.\n");
107
108         msgb = fwp_msgq_dequeue(&msgq);
109
110         if (msgb) {
111                 printf("Message should be empty but is is not\n");
112                 return -1;
113         }
114                 
115         printf("%s PASSED!\n", argv[0]);
116         return 0;
117 }