1 #include <assert.h>
2 #include <pb_decode.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include "test_helpers.h"
6 #include "anytest.pb.h"
7 #include "google/protobuf/duration.pb.h"
8 
main()9 int main()
10 {
11     BaseMessage msg = BaseMessage_init_zero;
12     uint8_t buffer[256];
13     pb_istream_t stream;
14     size_t count;
15     bool status;
16 
17     /* Read the data into buffer */
18     SET_BINARY_MODE(stdin);
19     count = fread(buffer, 1, sizeof(buffer), stdin);
20     stream = pb_istream_from_buffer(buffer, count);
21 
22     /* Decode the base message */
23     if (!pb_decode(&stream, BaseMessage_fields, &msg))
24     {
25         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
26         return 1;
27     }
28 
29     assert(msg.start == 1234);
30     assert(msg.end == 5678);
31 
32     /* Decode the Any message if we know the type */
33     if (strcmp(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration") == 0)
34     {
35         google_protobuf_Duration duration = google_protobuf_Duration_init_zero;
36         stream = pb_istream_from_buffer(msg.details.value.bytes, msg.details.value.size);
37         status = pb_decode(&stream, google_protobuf_Duration_fields, &duration);
38         assert(status);
39         assert(duration.seconds == 99999);
40         assert(duration.nanos == 100);
41         return 0;
42     }
43     else
44     {
45         fprintf(stderr, "Unknown Any type\n");
46         return 2;
47     }
48 }
49 
50