Make all the tests ANSI C compatible.
This commit is contained in:
@@ -70,15 +70,15 @@ if 'gcc' in env['CC']:
|
|||||||
# GNU Compiler Collection
|
# GNU Compiler Collection
|
||||||
|
|
||||||
# Debug info, warnings as errors
|
# Debug info, warnings as errors
|
||||||
env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror --coverage -fstack-protector-all')
|
env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror --coverage -fstack-protector-all')
|
||||||
env.Append(LINKFLAGS = '--coverage')
|
env.Append(LINKFLAGS = '--coverage')
|
||||||
|
|
||||||
# More strict checks on the nanopb core
|
# More strict checks on the nanopb core
|
||||||
env.Append(CORECFLAGS = '-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion')
|
env.Append(CORECFLAGS = '-Wextra -Wcast-qual -Wlogical-op -Wconversion')
|
||||||
elif 'clang' in env['CC']:
|
elif 'clang' in env['CC']:
|
||||||
# CLang
|
# CLang
|
||||||
env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror')
|
env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror')
|
||||||
env.Append(CORECFLAGS = '-pedantic -Wextra -Wcast-qual -Wconversion')
|
env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
|
||||||
elif 'cl' in env['CC']:
|
elif 'cl' in env['CC']:
|
||||||
# Microsoft Visual C++
|
# Microsoft Visual C++
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ elif 'cl' in env['CC']:
|
|||||||
env.Append(LINKFLAGS = '/DEBUG')
|
env.Append(LINKFLAGS = '/DEBUG')
|
||||||
|
|
||||||
# More strict checks on the nanopb core
|
# More strict checks on the nanopb core
|
||||||
env.Append(CORECFLAGS = '/W4 /Za')
|
env.Append(CORECFLAGS = '/W4')
|
||||||
|
|
||||||
# PB_RETURN_ERROR triggers C4127 because of while(0)
|
# PB_RETURN_ERROR triggers C4127 because of while(0)
|
||||||
env.Append(CFLAGS = '/wd4127')
|
env.Append(CFLAGS = '/wd4127')
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include "alltypes_legacy.h"
|
#include "alltypes_legacy.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -114,18 +115,21 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
alltypes.end = 1099;
|
alltypes.end = 1099;
|
||||||
|
|
||||||
uint8_t buffer[1024];
|
{
|
||||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
uint8_t buffer[1024];
|
||||||
|
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
/* Now encode it and check if we succeeded. */
|
/* Now encode it and check if we succeeded. */
|
||||||
if (pb_encode(&stream, AllTypes_fields, &alltypes))
|
if (pb_encode(&stream, AllTypes_fields, &alltypes))
|
||||||
{
|
{
|
||||||
fwrite(buffer, 1, stream.bytes_written, stdout);
|
SET_BINARY_MODE(stdout);
|
||||||
return 0; /* Success */
|
fwrite(buffer, 1, stream.bytes_written, stdout);
|
||||||
}
|
return 0; /* Success */
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
fprintf(stderr, "Encoding failed!\n");
|
{
|
||||||
return 1; /* Failure */
|
fprintf(stderr, "Encoding failed!\n");
|
||||||
|
return 1; /* Failure */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include "person.pb.h"
|
#include "person.pb.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
/* This function is called once from main(), it handles
|
/* This function is called once from main(), it handles
|
||||||
the decoding and printing.
|
the decoding and printing.
|
||||||
@@ -69,10 +70,10 @@ bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
/* Maximum size is specified to prevent infinite length messages from
|
pb_istream_t stream = {&callback, NULL, SIZE_MAX};
|
||||||
* hanging this in the fuzz test.
|
stream.state = stdin;
|
||||||
*/
|
SET_BINARY_MODE(stdin);
|
||||||
pb_istream_t stream = {&callback, stdin, 10000};
|
|
||||||
if (!print_person(&stream))
|
if (!print_person(&stream))
|
||||||
{
|
{
|
||||||
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
|
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include "person.pb.h"
|
#include "person.pb.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
/* This binds the pb_ostream_t into the stdout stream */
|
/* This binds the pb_ostream_t into the stdout stream */
|
||||||
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
|
||||||
@@ -22,7 +23,9 @@ int main()
|
|||||||
}};
|
}};
|
||||||
|
|
||||||
/* Prepare the stream, output goes directly to stdout */
|
/* Prepare the stream, output goes directly to stdout */
|
||||||
pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
|
pb_ostream_t stream = {&streamcallback, NULL, SIZE_MAX, 0};
|
||||||
|
stream.state = stdout;
|
||||||
|
SET_BINARY_MODE(stdout);
|
||||||
|
|
||||||
/* Now encode it and check if we succeeded. */
|
/* Now encode it and check if we succeeded. */
|
||||||
if (pb_encode(&stream, Person_fields, &person))
|
if (pb_encode(&stream, Person_fields, &person))
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include "callbacks.pb.h"
|
#include "callbacks.pb.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
|
bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
|
||||||
{
|
{
|
||||||
@@ -50,21 +51,24 @@ bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
|
|||||||
if (!pb_decode_fixed64(stream, &value))
|
if (!pb_decode_fixed64(stream, &value))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
printf((char*)*arg, (long long)value);
|
printf((char*)*arg, (long)value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
uint8_t buffer[1024];
|
uint8_t buffer[1024];
|
||||||
size_t length = fread(buffer, 1, 1024, stdin);
|
size_t length;
|
||||||
pb_istream_t stream = pb_istream_from_buffer(buffer, length);
|
pb_istream_t stream;
|
||||||
|
|
||||||
/* Note: empty initializer list initializes the struct with all-0.
|
/* Note: empty initializer list initializes the struct with all-0.
|
||||||
* This is recommended so that unused callbacks are set to NULL instead
|
* This is recommended so that unused callbacks are set to NULL instead
|
||||||
* of crashing at runtime.
|
* of crashing at runtime.
|
||||||
*/
|
*/
|
||||||
TestMessage testmessage = {};
|
TestMessage testmessage = {{{NULL}}};
|
||||||
|
|
||||||
|
SET_BINARY_MODE(stdin);
|
||||||
|
length = fread(buffer, 1, 1024, stdin);
|
||||||
|
stream = pb_istream_from_buffer(buffer, length);
|
||||||
|
|
||||||
testmessage.submsg.stringvalue.funcs.decode = &print_string;
|
testmessage.submsg.stringvalue.funcs.decode = &print_string;
|
||||||
testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
|
testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
|
||||||
@@ -73,7 +77,7 @@ int main()
|
|||||||
testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
|
testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
|
||||||
testmessage.submsg.fixed32value.arg = " fixed32value: %ld\n";
|
testmessage.submsg.fixed32value.arg = " fixed32value: %ld\n";
|
||||||
testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
|
testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
|
||||||
testmessage.submsg.fixed64value.arg = " fixed64value: %lld\n}\n";
|
testmessage.submsg.fixed64value.arg = " fixed64value: %ld\n}\n";
|
||||||
|
|
||||||
testmessage.stringvalue.funcs.decode = &print_string;
|
testmessage.stringvalue.funcs.decode = &print_string;
|
||||||
testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
|
testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include "callbacks.pb.h"
|
#include "callbacks.pb.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
|
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
|
||||||
{
|
{
|
||||||
@@ -25,19 +26,21 @@ bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, void * const *a
|
|||||||
|
|
||||||
bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
|
bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
|
||||||
{
|
{
|
||||||
|
uint32_t value = 42;
|
||||||
|
|
||||||
if (!pb_encode_tag_for_field(stream, field))
|
if (!pb_encode_tag_for_field(stream, field))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint32_t value = 42;
|
|
||||||
return pb_encode_fixed32(stream, &value);
|
return pb_encode_fixed32(stream, &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
|
bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
|
||||||
{
|
{
|
||||||
|
uint64_t value = 42;
|
||||||
|
|
||||||
if (!pb_encode_tag_for_field(stream, field))
|
if (!pb_encode_tag_for_field(stream, field))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint64_t value = 42;
|
|
||||||
return pb_encode_fixed64(stream, &value);
|
return pb_encode_fixed64(stream, &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,8 +63,10 @@ bool encode_repeatedstring(pb_ostream_t *stream, const pb_field_t *field, void *
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
uint8_t buffer[1024];
|
uint8_t buffer[1024];
|
||||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, 1024);
|
pb_ostream_t stream;
|
||||||
TestMessage testmessage = {};
|
TestMessage testmessage = {{{NULL}}};
|
||||||
|
|
||||||
|
stream = pb_ostream_from_buffer(buffer, 1024);
|
||||||
|
|
||||||
testmessage.stringvalue.funcs.encode = &encode_string;
|
testmessage.stringvalue.funcs.encode = &encode_string;
|
||||||
testmessage.int32value.funcs.encode = &encode_int32;
|
testmessage.int32value.funcs.encode = &encode_int32;
|
||||||
@@ -79,6 +84,7 @@ int main()
|
|||||||
if (!pb_encode(&stream, TestMessage_fields, &testmessage))
|
if (!pb_encode(&stream, TestMessage_fields, &testmessage))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
SET_BINARY_MODE(stdout);
|
||||||
if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
|
if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include "alltypes.pb.h"
|
#include "alltypes.pb.h"
|
||||||
#include "extensions.pb.h"
|
#include "extensions.pb.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
#define TEST(x) if (!(x)) { \
|
#define TEST(x) if (!(x)) { \
|
||||||
printf("Test " #x " failed.\n"); \
|
printf("Test " #x " failed.\n"); \
|
||||||
@@ -15,25 +16,39 @@
|
|||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
uint8_t buffer[1024];
|
uint8_t buffer[1024];
|
||||||
size_t count = fread(buffer, 1, sizeof(buffer), stdin);
|
size_t count;
|
||||||
pb_istream_t stream = pb_istream_from_buffer(buffer, count);
|
pb_istream_t stream;
|
||||||
|
|
||||||
AllTypes alltypes = {};
|
|
||||||
|
|
||||||
|
AllTypes alltypes = {0};
|
||||||
int32_t extensionfield1;
|
int32_t extensionfield1;
|
||||||
pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
|
pb_extension_t ext1;
|
||||||
|
ExtensionMessage extensionfield2;
|
||||||
|
pb_extension_t ext2;
|
||||||
|
|
||||||
|
/* Read the message data */
|
||||||
|
SET_BINARY_MODE(stdin);
|
||||||
|
count = fread(buffer, 1, sizeof(buffer), stdin);
|
||||||
|
stream = pb_istream_from_buffer(buffer, count);
|
||||||
|
|
||||||
|
/* Add the extensions */
|
||||||
alltypes.extensions = &ext1;
|
alltypes.extensions = &ext1;
|
||||||
|
|
||||||
ExtensionMessage extensionfield2 = {};
|
ext1.type = &AllTypes_extensionfield1;
|
||||||
pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
|
ext1.dest = &extensionfield1;
|
||||||
ext1.next = &ext2;
|
ext1.next = &ext2;
|
||||||
|
|
||||||
|
ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
|
||||||
|
ext2.dest = &extensionfield2;
|
||||||
|
ext2.next = NULL;
|
||||||
|
|
||||||
|
/* Decode the message */
|
||||||
if (!pb_decode(&stream, AllTypes_fields, &alltypes))
|
if (!pb_decode(&stream, AllTypes_fields, &alltypes))
|
||||||
{
|
{
|
||||||
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
|
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Check that the extensions decoded properly */
|
||||||
TEST(extensionfield1 == 12345)
|
TEST(extensionfield1 == 12345)
|
||||||
TEST(strcmp(extensionfield2.test1, "test") == 0)
|
TEST(strcmp(extensionfield2.test1, "test") == 0)
|
||||||
TEST(extensionfield2.test2 == 54321)
|
TEST(extensionfield2.test2 == 54321)
|
||||||
|
|||||||
@@ -7,25 +7,37 @@
|
|||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
#include "alltypes.pb.h"
|
#include "alltypes.pb.h"
|
||||||
#include "extensions.pb.h"
|
#include "extensions.pb.h"
|
||||||
|
#include "test_helpers.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
AllTypes alltypes = {};
|
uint8_t buffer[1024];
|
||||||
|
pb_ostream_t stream;
|
||||||
|
|
||||||
|
AllTypes alltypes = {0};
|
||||||
int32_t extensionfield1 = 12345;
|
int32_t extensionfield1 = 12345;
|
||||||
pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
|
pb_extension_t ext1;
|
||||||
|
ExtensionMessage extensionfield2 = {"test", 54321};
|
||||||
|
pb_extension_t ext2;
|
||||||
|
|
||||||
|
/* Set up the extensions */
|
||||||
alltypes.extensions = &ext1;
|
alltypes.extensions = &ext1;
|
||||||
|
|
||||||
ExtensionMessage extensionfield2 = {"test", 54321};
|
ext1.type = &AllTypes_extensionfield1;
|
||||||
pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
|
ext1.dest = &extensionfield1;
|
||||||
ext1.next = &ext2;
|
ext1.next = &ext2;
|
||||||
|
|
||||||
uint8_t buffer[1024];
|
ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
|
||||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
ext2.dest = &extensionfield2;
|
||||||
|
ext2.next = NULL;
|
||||||
|
|
||||||
/* Now encode it and check if we succeeded. */
|
/* Set up the output stream */
|
||||||
|
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
/* Now encode the message and check if we succeeded. */
|
||||||
if (pb_encode(&stream, AllTypes_fields, &alltypes))
|
if (pb_encode(&stream, AllTypes_fields, &alltypes))
|
||||||
{
|
{
|
||||||
|
SET_BINARY_MODE(stdout);
|
||||||
fwrite(buffer, 1, stream.bytes_written, stdout);
|
fwrite(buffer, 1, stream.bytes_written, stdout);
|
||||||
return 0; /* Success */
|
return 0; /* Success */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,13 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
uint8_t buffer[512] = {};
|
uint8_t buffer[512];
|
||||||
|
|
||||||
/* Create a message with one missing field */
|
/* Create a message with one missing field */
|
||||||
{
|
{
|
||||||
MissingField msg = {};
|
MissingField msg = {0};
|
||||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
if (!pb_encode(&stream, MissingField_fields, &msg))
|
if (!pb_encode(&stream, MissingField_fields, &msg))
|
||||||
{
|
{
|
||||||
printf("Encode failed.\n");
|
printf("Encode failed.\n");
|
||||||
@@ -22,7 +23,7 @@ int main()
|
|||||||
|
|
||||||
/* Test that it decodes properly if we don't require that field */
|
/* Test that it decodes properly if we don't require that field */
|
||||||
{
|
{
|
||||||
MissingField msg = {};
|
MissingField msg = {0};
|
||||||
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
|
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
if (!pb_decode(&stream, MissingField_fields, &msg))
|
if (!pb_decode(&stream, MissingField_fields, &msg))
|
||||||
@@ -34,7 +35,7 @@ int main()
|
|||||||
|
|
||||||
/* Test that it does *not* decode properly if we require the field */
|
/* Test that it does *not* decode properly if we require the field */
|
||||||
{
|
{
|
||||||
AllFields msg = {};
|
AllFields msg = {0};
|
||||||
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
|
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
|
||||||
|
|
||||||
if (pb_decode(&stream, AllFields_fields, &msg))
|
if (pb_decode(&stream, AllFields_fields, &msg))
|
||||||
|
|||||||
Reference in New Issue
Block a user