Expanding the tests to better cover decoding from memory buffer.

git-svn-id: https://svn.kapsi.fi/jpa/nanopb@1090 e3a754e5-d11d-0410-8d38-ebb782a927b9
This commit is contained in:
Petteri Aimonen
2011-12-30 08:57:27 +00:00
parent 1506450b11
commit a3f5e4d81f
4 changed files with 104 additions and 27 deletions

View File

@@ -57,33 +57,16 @@ bool print_person(pb_istream_t *stream)
return true;
}
/* This binds the pb_istream_t to stdin */
bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
{
FILE *file = (FILE*)stream->state;
bool status;
if (buf == NULL)
{
/* Skipping data */
while (count-- && fgetc(file) != EOF);
return count == 0;
}
status = (fread(buf, 1, count, file) == count);
if (feof(file))
stream->bytes_left = 0;
return status;
}
int main()
{
/* Maximum size is specified to prevent infinite length messages from
* hanging this in the fuzz test.
*/
pb_istream_t stream = {&callback, stdin, 10000};
/* Read the data into buffer */
uint8_t buffer[512];
size_t count = fread(buffer, 1, sizeof(buffer), stdin);
/* Construct a pb_istream_t for reading from the buffer */
pb_istream_t stream = pb_istream_from_buffer(buffer, count);
/* Decode and print out the stuff */
if (!print_person(&stream))
{
printf("Parsing failed.\n");