Line data Source code
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 : 9 1 : int main() 10 : { 11 1 : 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 1 : count = fread(buffer, 1, sizeof(buffer), stdin); 20 1 : stream = pb_istream_from_buffer(buffer, count); 21 : 22 : /* Decode the base message */ 23 1 : if (!pb_decode(&stream, BaseMessage_fields, &msg)) 24 : { 25 0 : printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); 26 0 : return 1; 27 : } 28 : 29 1 : assert(msg.start == 1234); 30 1 : assert(msg.end == 5678); 31 : 32 : /* Decode the Any message if we know the type */ 33 1 : if (strcmp(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration") == 0) 34 : { 35 1 : google_protobuf_Duration duration = google_protobuf_Duration_init_zero; 36 1 : stream = pb_istream_from_buffer(msg.details.value.bytes, msg.details.value.size); 37 1 : status = pb_decode(&stream, google_protobuf_Duration_fields, &duration); 38 1 : assert(status); 39 1 : assert(duration.seconds == 99999); 40 1 : assert(duration.nanos == 100); 41 1 : return 0; 42 : } 43 : else 44 : { 45 0 : fprintf(stderr, "Unknown Any type\n"); 46 0 : return 2; 47 : } 48 : } 49 :