Tests for callback fields
[apps/agl-service-can-low-level.git] / tests / test_decode1.c
1 /* A very simple decoding test case, using person.proto.
2  * Produces output compatible with protoc --decode.
3  */
4
5 #include <stdio.h>
6 #include <pb_decode.h>
7 #include "person.pb.h"
8
9 bool print_person(pb_istream_t *stream)
10 {
11     int i;
12     Person person;
13     
14     if (!pb_decode(stream, Person_fields, &person))
15         return false;
16     
17     printf("name: \"%s\"\n", person.name);
18     printf("id: %d\n", person.id);
19     
20     if (person.has_email)
21         printf("email: \"%s\"\n", person.email);
22     
23     for (i = 0; i < person.phone_count; i++)
24     {
25         Person_PhoneNumber *phone = &person.phone[i];
26         printf("phone {\n");
27         printf("  number: \"%s\"\n", phone->number);
28         
29         switch (phone->type)
30         {
31             case Person_PhoneType_WORK:
32                 printf("  type: WORK\n");
33                 break;
34             
35             case Person_PhoneType_HOME:
36                 printf("  type: HOME\n");
37                 break;
38             
39             case Person_PhoneType_MOBILE:
40                 printf("  type: MOBILE\n");
41                 break;
42         }
43         printf("}\n");
44     }
45     
46     return true;
47 }
48
49 bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
50 {
51     FILE *file = (FILE*)stream->state;
52     bool status;
53     
54     if (buf == NULL)
55     {
56         while (count-- && fgetc(file) != EOF);
57         return count == 0;
58     }
59     
60     status = (fread(buf, 1, count, file) == count);
61     
62     if (feof(file))
63         stream->bytes_left = 0;
64     
65     return status;
66 }
67
68 int main()
69 {
70     /* Maximum size is specified to prevent infinite length messages from
71      * hanging this in the fuzz test.
72      */
73     pb_istream_t stream = {&callback, stdin, 10000};
74     if (!print_person(&stream))
75         printf("Parsing failed.\n");
76     
77     return 0;
78 }