test.c 800 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <pb_decode.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "test_helpers.h"
  6. #include "test.pb.h"
  7. bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
  8. {
  9. FILE *file = (FILE*)stream->state;
  10. size_t len = fread(buf, 1, count, file);
  11. if (len == count)
  12. {
  13. return true;
  14. }
  15. else
  16. {
  17. stream->bytes_left = 0;
  18. return false;
  19. }
  20. }
  21. int main()
  22. {
  23. pb_istream_t stream = {&stream_callback, NULL, SIZE_MAX};
  24. MyMessage msg = MyMessage_init_default;
  25. bool status;
  26. stream.state = stdin;
  27. SET_BINARY_MODE(stdin);
  28. set_max_alloc_bytes(512);
  29. status = pb_decode(&stream, MyMessage_fields, &msg);
  30. assert(!status);
  31. assert(strcmp(stream.errmsg, "realloc failed") == 0);
  32. return 0;
  33. }