Line data Source code
1 : /* Checks that missing required fields are detected properly */ 2 : 3 : #include <stdio.h> 4 : #include <pb_encode.h> 5 : #include <pb_decode.h> 6 : #include "missing_fields.pb.h" 7 : 8 1 : int main() 9 : { 10 : uint8_t buffer[512]; 11 : size_t size; 12 : 13 : /* Create a message with one missing field */ 14 : { 15 1 : MissingField msg = {0}; 16 1 : pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 17 : 18 1 : if (!pb_encode(&stream, MissingField_fields, &msg)) 19 : { 20 0 : printf("Encode failed.\n"); 21 0 : return 1; 22 : } 23 : 24 1 : size = stream.bytes_written; 25 : } 26 : 27 : /* Test that it decodes properly if we don't require that field */ 28 : { 29 1 : MissingField msg = {0}; 30 1 : pb_istream_t stream = pb_istream_from_buffer(buffer, size); 31 : 32 1 : if (!pb_decode(&stream, MissingField_fields, &msg)) 33 : { 34 0 : printf("Decode failed: %s\n", PB_GET_ERROR(&stream)); 35 0 : return 2; 36 : } 37 : } 38 : 39 : /* Test that it does *not* decode properly if we require the field */ 40 : { 41 1 : AllFields msg = {0}; 42 1 : pb_istream_t stream = pb_istream_from_buffer(buffer, size); 43 : 44 1 : if (pb_decode(&stream, AllFields_fields, &msg)) 45 : { 46 0 : printf("Decode didn't detect missing field.\n"); 47 0 : return 3; 48 : } 49 : } 50 : 51 1 : return 0; /* All ok */ 52 : } 53 :