LCOV - code coverage report
Current view: top level - regression/issue_342 - test_extensions.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 24 100.0 %
Date: 2023-02-14 20:10:26 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include <stdio.h>
       2             : #include <stdlib.h>
       3             : #include <string.h>
       4             : #include <pb_encode.h>
       5             : #include <pb_decode.h>
       6             : #include "extensions.pb.h"
       7             : #include "unittests.h"
       8             : 
       9           1 : static bool write_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
      10             : {
      11           2 :     return pb_encode_tag_for_field(stream, field) &&
      12           1 :            pb_encode_string(stream, (const void*)"abc", 3);
      13             : }
      14             : 
      15           1 : int main(int argc, char **argv)
      16             : {
      17           1 :     int status = 0;
      18             :     uint8_t buffer[64];
      19           1 :     pb_size_t msglen = 0;
      20             :     
      21             :     {
      22           1 :         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
      23           1 :         pb_callback_t callback_def = {{0}};
      24           1 :         pb_extension_t ext = {0};
      25           1 :         BaseMessage msg = {0};
      26             : 
      27           1 :         callback_def.funcs.encode = &write_string;
      28           1 :         ext.type = &string_extension;
      29           1 :         ext.dest = &callback_def;
      30           1 :         msg.extensions = &ext;
      31             :         
      32           1 :         TEST(pb_encode(&stream, BaseMessage_fields, &msg));
      33             :         
      34           1 :         msglen = stream.bytes_written;
      35           1 :         TEST(msglen > 3);
      36             :     }
      37             :     
      38             :     {
      39           1 :         pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
      40           1 :         pb_extension_t ext = {0};
      41           1 :         BaseMessage msg = {0};
      42             :         
      43           1 :         ext.type = &string_extension;
      44             :         /* Note: ext.dest remains null to trigger bug #342 */
      45           1 :         msg.extensions = &ext;
      46             :     
      47           1 :         TEST(pb_decode(&stream, BaseMessage_fields, &msg));
      48             :     }
      49             :     
      50           1 :     return status;
      51             : }
      52             : 

Generated by: LCOV version 1.14