Move TRP messaging to tr_msg.c. Fix old bug.
[trust_router.git] / trp / msgtst.c
1 /* Testing trp message encoding / decoding */
2
3 /* compiles with: gcc -o msgtst -I../include msgtst.c trp_msg.c $(pkg-config --cflags --libs glib-2.0) ../common/tr_debug.c  ../common/tr_name.c ../common/tr_msg.c -ltalloc -ljansson */
4
5 #include <stdio.h>
6 #include <talloc.h>
7
8 #include <trust_router/trp.h>
9 #include <tr_msg.h>
10 #include <tr_debug.h>
11
12 #define MAX_MSG_LEN 8192
13
14 int main(int argc, const char *argv[])
15 {
16   TALLOC_CTX *main_ctx=talloc_new(NULL);
17   FILE *f;
18   char *buf;
19   size_t buflen;
20   TR_MSG *msg;
21   
22   if (argc != 2) {
23     printf("Usage: %s <input file>\n\n", argv[0]);
24     exit(-1);
25   }
26
27   buf=malloc(MAX_MSG_LEN);
28   if (!buf) {
29     printf("Allocation error.\n\n");
30     exit(-1);
31   }
32
33   f=fopen(argv[1], "r");
34   if (!f) {
35     printf("Error opening %s for reading.\n\n", argv[1]);
36     exit(-1);
37   }
38
39   printf("Reading from %s...\n", argv[1]);
40
41   buflen=fread(buf, sizeof(char), MAX_MSG_LEN, f);
42   if (buflen==0) {
43     printf("File empty.\n\n");
44     exit(0);
45   }
46
47   if (buflen>=MAX_MSG_LEN)
48     printf("Warning: file may exceed maximum message length (%d bytes).\n", MAX_MSG_LEN);
49
50   msg=tr_msg_decode(buf, buflen);
51
52 /*  if (rc==TRP_SUCCESS)
53     trp_msg_print(msg);*/
54
55   printf("\nEncoding...\n");
56
57   printf("Result: \n%s\n\n", tr_msg_encode(msg));
58
59   talloc_report_full(main_ctx, stdout);
60
61   return 0;
62 }